X Tutup
Skip to content

Commit d40f5e7

Browse files
committed
sVB 3.0.9.2
1 parent 72cfaca commit d40f5e7

File tree

28 files changed

+1263
-430
lines changed

28 files changed

+1263
-430
lines changed

LangServices/CompilerService.vb

Lines changed: 92 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Imports System.IO
22
Imports Microsoft.Nautilus.Text
33
Imports Microsoft.SmallVisualBasic.Completion
4+
Imports Microsoft.SmallVisualBasic.Engine
5+
Imports Microsoft.SmallVisualBasic.Statements
46

57
Namespace 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)

LangServices/CompletionKeyboardFilter.vb

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Imports System.ComponentModel.Composition
2+
Imports System.Windows.Shell
23
Imports Microsoft.Nautilus.Core.Undo
34
Imports Microsoft.Nautilus.Text.Editor
45
Imports Microsoft.Nautilus.Text.Operations
@@ -39,6 +40,10 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
3940

4041
Case Key.Return
4142
args.Handled = CommitConditionally(textView, completionSurface, If(Keyboard.Modifiers And ModifierKeys.Control > 0, "+nl", ""))
43+
Dim repWith = CType(completionSurface.CompletionListBox.SelectedItem, CompletionItemWrapper).ReplacementText
44+
If repWith.EndsWith("(") Then
45+
textView.Caret.MoveToPreviousCaretPosition()
46+
End If
4247

4348
Case Key.Space, Key.Tab
4449
args.Handled = CommitConditionally(textView, completionSurface, " ")
@@ -108,10 +113,30 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
108113
Case ">"
109114
args.Handled = CommitConditionally(textView, completionSurface, " > ", True)
110115

111-
Case "!", ")", "[", "]", "{", "}"
116+
Case "!", ")", "]", "}"
112117
CommitConditionally(textView, completionSurface)
113118

114-
Case ",", "("
119+
Case "{"
120+
If Not CommitConditionally(textView, completionSurface, "[]") Then
121+
Dim EditOps = EditorOperationsProvider.GetEditorOperations(textView)
122+
EditOps.InsertText("{}", UndoHistoryRegistry.GetHistory(textView.TextBuffer))
123+
End If
124+
textView.Caret.MoveToPreviousCaretPosition()
125+
args.Handled = True
126+
127+
Case "["
128+
If Not CommitConditionally(textView, completionSurface, "[]") Then
129+
Dim EditOps = EditorOperationsProvider.GetEditorOperations(textView)
130+
EditOps.InsertText("[]", UndoHistoryRegistry.GetHistory(textView.TextBuffer))
131+
End If
132+
textView.Caret.MoveToPreviousCaretPosition()
133+
args.Handled = True
134+
135+
Case "("
136+
args.Handled = CommitConditionally(textView, completionSurface, ")")
137+
textView.Caret.MoveToPreviousCaretPosition()
138+
139+
Case ","
115140
args.Handled = CommitConditionally(textView, completionSurface, args.Text & " ")
116141

117142
End Select
@@ -161,10 +186,25 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
161186
End If
162187

163188
If repWith.EndsWith("(") OrElse repWith.EndsWith(")") Then
164-
If extraText.EndsWith("( ") Then extraText = ""
165189
Dim txt = line.GetText().Substring(pos - line.Start)
166-
If LineScanner.GetFirstToken(txt, 0).Type = TokenType.LeftParens Then
190+
Dim nextToken = LineScanner.GetFirstToken(txt, 0)
191+
192+
If nextToken.Type = TokenType.LeftParens Then
167193
repWith = repWith.TrimEnd("("c, ")"c)
194+
ElseIf repWith.EndsWith("(") Then
195+
If nextToken.IsIllegal OrElse nextToken.ParseType = ParseType.Operator Then
196+
If extraText <> ")" Then extraText &= ")"
197+
ElseIf extraText = ")" Then
198+
extraText = ""
199+
End If
200+
ElseIf repWith.EndsWith(")") AndAlso extraText = ")" Then
201+
extraText = ""
202+
End If
203+
ElseIf extraText = ")" Then
204+
If item.ItemType = Completion.CompletionItemType.MethodName Or item.ItemType = Completion.CompletionItemType.SubroutineName Then
205+
extraText = "()"
206+
Else
207+
extraText = "("
168208
End If
169209
End If
170210

LangServices/CompletionProvider.vb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ Namespace Microsoft.SmallVisualBasic.LanguageService
182182
End If
183183

184184
If textView.TextSnapshot.GetText(replaceSpan) <> repWith Then
185-
editorOperations.ReplaceText(replaceSpan, repWith, undoHistory)
185+
If repWith.EndsWith("(") Then
186+
editorOperations.ReplaceText(replaceSpan, repWith & ")", undoHistory)
187+
textView.Caret.MoveToPreviousCaretPosition()
188+
Else
189+
editorOperations.ReplaceText(replaceSpan, repWith, undoHistory)
190+
End If
186191
End If
187192

188193
DismissAdornment(force:=True)

Samples/Animation Samples/Moving worm/Form1.sb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
' Develpoed by M. Hamdy
22
' Inspired bt Ardi Ardi
33
R = 50
4-
4+
Me.BackColor = Colors.DarkBlue
55
' Add five labels on the form and draw five circles inside them
66
For I = 1 To 5
77
_Label = Me.AddLabel("circle" & I, R * (I - 1), 0, R, R)

Samples/Drawing Samples/Right-Angled Triangles/Form1.sb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ Sub GetTriangles()
88
i2 = i * i
99
For j = 2 To i - 1
1010
j2 = j * j
11-
For k = i - 1 To 2 Step -1
12-
n = k * k + j2
13-
If n = i2 And Array.ContainsValue(sides[i], j) = False Then
14-
sides[i] = Array.AddItem(sides[i], k)
15-
ListBox1.AddItem(Text.Format("[1],[2],[3]", {j, k, i}))
16-
ContinueLoop -
17-
ElseIf n < i2 Then
18-
ExitLoop
19-
EndIf
20-
Next
11+
k = Math.SquareRoot(i2 - j2)
12+
If Math.Round(k) = k And Array.ContainsValue(sides[i], j) = False Then
13+
sides[i] = Array.AddItem(sides[i], k)
14+
ListBox1.AddItem(Text.Format("[1],[2],[3]", {j, k, i}))
15+
ContinueLoop
16+
EndIf
2117
Next
2218
Next
2319
EndSub
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
L = 4
2+
X1 = Turtle.X
3+
Turtle.Speed = 50
4+
' The turtle will draw a half circle to stop at the end point of its horizontal diameter
5+
' So we can calculate the diameter
6+
For I = 1 To 180
7+
Turtle.Move(L)
8+
Turtle.Turn(1)
9+
Next
10+
Diameter = Turtle.X - X1
11+
Circumference = L * 360
12+
Pi = Circumference / Diameter
13+
14+
GW.BrushColor = Colors.Blue
15+
GW.FontBold = True
16+
GW.FontSize = 14
17+
GW.DrawText(300, 300, "Diameter = " & Diameter)
18+
GW.DrawText(300, 320, "Circumference = " & Circumference)
19+
GW.DrawText(300, 340, "Calculated Pi = " & Pi)
20+
GW.DrawText(300, 360, " Math.Pi = " & Math.Pi)
21+
GW.DrawText(300, 380, "Difference = " & (Pi - Math.Pi))
22+
GW.DrawText(300, 400, "Accurecy = " & Math.Round2(Math.Pi / Pi * 100, 3) & "%")
23+
24+
25+
26+

0 commit comments

Comments
 (0)
X Tutup