11Imports System.IO
22Imports Microsoft.Nautilus.Text
33Imports Microsoft.SmallVisualBasic.Completion
4+ Imports Microsoft.SmallVisualBasic.Engine
5+ Imports Microsoft.SmallVisualBasic.Statements
46
57Namespace Microsoft.SmallVisualBasic.LanguageService
68 Public Module CompilerService
@@ -194,7 +196,6 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
194196
195197 ' format sub lines
196198 Dim lineStart = False , lineEnd = False , subLine = 0
197-
198199 Dim subLineOffset = 0
199200 Dim indentStack As New Stack( Of Integer )
200201 Dim firstSubLine = True
@@ -567,7 +568,7 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
567568 textBuffer As ITextBuffer,
568569 start As Integer ,
569570 [end] As Integer
570- )
571+ )
571572
572573 Dim snapshot = textBuffer.CurrentSnapshot
573574 Dim symbolTable = GetSymbolTable(textBuffer)
@@ -580,6 +581,7 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
580581
581582 ' The exact type/method name is stored in the comment field
582583 textEdit.Replace(line.Start + token.Column, token.EndColumn - token.Column, token.Comment)
584+ FixParans(token, line, textEdit, True )
583585 Next
584586
585587 ' fix local vars definitions
@@ -628,50 +630,68 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
628630
629631 If id.LCaseText = "me" Then
630632 If id.Text <> "Me" Then textEdit.Replace(line.Start + id.Column, id.EndColumn - id.Column, "Me" )
633+ FixParans(id, line, textEdit, True )
631634 Continue For
632635 End If
633636
634- ' fix local vars usage
635- If id.SymbolType = CompletionItemType.LocalVariable Then
636- Dim subName = Statements.SubroutineStatement.GetSubroutine(id)?.Name.LCaseText
637- Dim key = $ "{subName}.{id. LCaseText}"
638- Dim definition = symbolTable.LocalVariables( key ).Identifier
639- If id.Line <> definition.Line OrElse id.Column <> definition.Column Then
640- Dim name = definition.Text
641- If id.Text <> name Then textEdit.Replace(line.Start + id.Column, id.EndColumn - id.Column, name)
642- End If
643- Continue For
644- End If
637+ Select Case id.SymbolType
638+ Case CompletionItemType.LocalVariable
639+ ' fix local vars usage
640+ Dim subName = Statements.SubroutineStatement.GetSubroutine(id)?.Name. LCaseText
641+ Dim key = $ "{subName}.{id.LCaseText}"
642+ Dim definition = symbolTable.LocalVariables( key ).Identifier
643+ If id.Line <> definition.Line OrElse id.Column <> definition.Column Then
644+ Dim name = definition.Text
645+ If id.Text <> name Then textEdit.Replace(line.Start + id.Column, id.EndColumn - id.Column, name)
646+ End If
647+ FixParans(id, line, textEdit, True )
645648
646- ' fix global vars usage
647- If id.SymbolType = CompletionItemType.GlobalVariable Then
648- If FixToken(id, line.Start, symbolTable.GlobalVariables, textEdit) Then
649- Continue For
650- End If
649+ Case CompletionItemType.GlobalVariable
650+ ' fix global vars usage
651+ If FixToken(id, line.Start, symbolTable.GlobalVariables, textEdit) Then
652+ FixParans(id, line, textEdit, True )
653+ Continue For
654+ End If
651655
652- If controlNames IsNot Nothing Then
653- ' fix control names usage
654- Dim controlId = id.LCaseText
655- For Each controlName In controlNames
656- If controlName.ToLower() = controlId Then
657- If id.Text <> controlName Then textEdit.Replace(line.Start + id.Column, id.EndColumn - id.Column, controlName)
658- Continue For
659- End If
660- Next
661- End If
662- End If
656+ Dim fixed = False
657+ If controlNames IsNot Nothing Then
658+ ' fix control names usage
659+ Dim controlId = id.LCaseText
660+ For Each controlName In controlNames
661+ If controlName.ToLower() = controlId Then
662+ If id.Text <> controlName Then textEdit.Replace(line.Start + id.Column, id.EndColumn - id.Column, controlName)
663+ fixed = True
664+ FixParans(id, line, textEdit, True )
665+ Exit For
666+ End If
667+ Next
668+ End If
663669
664- ' fix event handlers and sub calls
665- If id.SymbolType = CompletionItemType.SubroutineName Then
666- If FixToken(id, line.Start, symbolTable.Subroutines, textEdit) Then
667- Continue For
668- End If
669- End If
670+ ' Add missing parans to subroutine calls
671+ If Not fixed AndAlso FixToken(id, line.Start, symbolTable.Subroutines, textEdit) Then
672+ FixParans(id, line, textEdit)
673+ End If
670674
671- ' fix goto labels
672- If id.SymbolType = CompletionItemType.Label Then
673- FixToken(id, line.Start, symbolTable.Labels, textEdit)
674- End If
675+ Case CompletionItemType.SubroutineName
676+ ' fix event handlers and sub call
677+ If TypeOf id.Parent Is SubroutineStatement Then
678+ FixParans(id, line, textEdit)
679+ ElseIf FixToken(id, line.Start, symbolTable.Subroutines, textEdit) Then
680+ Dim LeftSide = TryCast (id.Parent, AssignmentStatement)?.LeftValue
681+ If LeftSide IsNot Nothing Then
682+ If TryCast (LeftSide, Expressions.PropertyExpression)?.IsEvent Then
683+ FixParans(id, line, textEdit, True )
684+ End If
685+ End If
686+ Else
687+ FixParans(id, line, textEdit, True )
688+ End If
689+
690+ Case CompletionItemType.Label
691+ ' fix goto labels
692+ FixToken(id, line.Start, symbolTable.Labels, textEdit)
693+ FixParans(id, line, textEdit, True )
694+ End Select
675695 Next
676696
677697 For Each obj In symbolTable.AllDynamicProperties
@@ -692,7 +712,10 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
692712 Dim y = CompletionHelper.TrimData(type.Key)
693713 If x.Contains(y) Then
694714 Dim propDictionery2 = symbolTable.Dynamics(type.Key)
695- If FixToken(prop, line.Start, propDictionery2, textEdit) Then Exit For
715+ If FixToken(prop, line.Start, propDictionery2, textEdit) Then
716+ FixParans(prop, line, textEdit, True )
717+ Exit For
718+ End If
696719 End If
697720 Next
698721 End If
@@ -703,12 +726,33 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
703726 End Using
704727 End Sub
705728
729+ Private Sub FixParans(
730+ id As Token,
731+ line As ITextSnapshotLine,
732+ textEdit As ITextEdit,
733+ Optional RemoveParans As Boolean = False )
734+
735+ Dim tokens = LineScanner.GetTokens(line.GetText(), id.Line)
736+ Dim n = tokens.Count - 1
737+ For i = 0 To n
738+ If tokens(i).Column = id.Column Then
739+ If RemoveParans Then
740+ If i < n - 1 AndAlso tokens(i + 1 ).Type = TokenType.LeftParens AndAlso tokens(i + 2 ).Type = TokenType.RightParens Then
741+ textEdit.Replace(line.Start + id.EndColumn, tokens(i + 2 ).EndColumn - id.EndColumn, "" )
742+ End If
743+ ElseIf i = n OrElse tokens(i + 1 ).Type <> TokenType.LeftParens Then
744+ textEdit.Insert(line.Start + id.EndColumn, "()" )
745+ Return
746+ End If
747+ End If
748+ Next
749+ End Sub
750+
706751 Private Function FixToken(
707752 token As Token,
708753 lineStart As Integer ,
709754 dictionary As Dictionary( Of String , Token),
710- textEdit As ITextEdit
711- )
755+ textEdit As ITextEdit) As Boolean
712756
713757 Dim key = token.LCaseText
714758 If key .StartsWith( "__foreach__" ) Then Return True
@@ -718,8 +762,8 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
718762 If defenition.Line <> token.Line OrElse defenition.Column <> token.Column Then
719763 Dim name = defenition.Text
720764 If token.Text <> name Then textEdit.Replace(lineStart + token.Column, token.EndColumn - token.Column, name)
721- Return True
722765 End If
766+ Return True
723767 End If
724768
725769 Return False
@@ -730,6 +774,7 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
730774 Dim token = dictionary.Values(i)
731775 Dim name = token.Text
732776 Dim n = 0
777+
733778 If name.StartsWith( "_" ) Then
734779 If name.Length = 1 Then Continue For
735780 n = 1
@@ -923,10 +968,10 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
923968 TokenType.Question, TokenType.HashQuestion
924969 If notLastToken Then
925970 Select Case nextToken.Type
926- Case TokenType.Comma,
927- TokenType.LeftBracket, TokenType.RightBracket,
928- TokenType.LeftBrace, TokenType.RightBrace,
929- TokenType.LeftParens, TokenType.RightParens
971+ Case TokenType.Comma, TokenType.Dot,
972+ TokenType.LeftBracket, TokenType.RightBracket,
973+ TokenType.LeftBrace, TokenType.RightBrace,
974+ TokenType.LeftParens, TokenType.RightParens
930975 FixSpaces(textEdit, line, token, nextToken, 0 )
931976 Case Else
932977 FixSpaces(textEdit, line, token, nextToken, 1 )
0 commit comments