forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteSample2.vb
More file actions
180 lines (155 loc) · 7.16 KB
/
Copy pathAutocompleteSample2.vb
File metadata and controls
180 lines (155 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
Imports System.Text.RegularExpressions
Public Class AutocompleteSample2
Private popupMenu As AutocompleteMenu
Private keywords As String() = {"abstract", "as", "base", "bool", "break", "byte", _
"case", "catch", "char", "checked", "class", "const", _
"continue", "decimal", "default", "delegate", "do", "double", _
"else", "enum", "event", "explicit", "extern", "false", _
"finally", "fixed", "float", "for", "foreach", "goto", _
"if", "implicit", "in", "int", "interface", "internal", _
"is", "lock", "long", "namespace", "new", "null", _
"object", "operator", "out", "override", "params", "private", _
"protected", "public", "readonly", "ref", "return", "sbyte", _
"sealed", "short", "sizeof", "stackalloc", "static", "string", _
"struct", "switch", "this", "throw", "true", "try", _
"typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", _
"using", "virtual", "void", "volatile", "while", "add", _
"alias", "ascending", "descending", "dynamic", "from", "get", _
"global", "group", "into", "join", "let", "orderby", _
"partial", "remove", "select", "set", "value", "var", _
"where", "yield"}
Private methods As String() = {"Equals()", "GetHashCode()", "GetType()", "ToString()"}
Private snippets As String() = {"if(^)" & vbLf & "{" & vbLf & ";" & vbLf & "}", "if(^)" & vbLf & "{" & vbLf & ";" & vbLf & "}" & vbLf & "else" & vbLf & "{" & vbLf & ";" & vbLf & "}", "for(^;;)" & vbLf & "{" & vbLf & ";" & vbLf & "}", "while(^)" & vbLf & "{" & vbLf & ";" & vbLf & "}", "do${" & vbLf & "^;" & vbLf & "}while();", "switch(^)" & vbLf & "{" & vbLf & "case : break;" & vbLf & "}"}
Private declarationSnippets As String() = {"public class ^" & vbLf & "{" & vbLf & "}", "private class ^" & vbLf & "{" & vbLf & "}", "internal class ^" & vbLf & "{" & vbLf & "}", "public struct ^" & vbLf & "{" & vbLf & ";" & vbLf & "}", "private struct ^" & vbLf & "{" & vbLf & ";" & vbLf & "}", "internal struct ^" & vbLf & "{" & vbLf & ";" & vbLf & "}", _
"public void ^()" & vbLf & "{" & vbLf & ";" & vbLf & "}", "private void ^()" & vbLf & "{" & vbLf & ";" & vbLf & "}", "internal void ^()" & vbLf & "{" & vbLf & ";" & vbLf & "}", "protected void ^()" & vbLf & "{" & vbLf & ";" & vbLf & "}", "public ^{ get; set; }", "private ^{ get; set; }", _
"internal ^{ get; set; }", "protected ^{ get; set; }"}
Public Sub New()
InitializeComponent()
'create autocomplete popup menu
popupMenu = New AutocompleteMenu(fctb)
popupMenu.Items.ImageList = imageList1
popupMenu.SearchPattern = "[\w\.:=!<>]"
'
BuildAutocompleteMenu()
End Sub
Private Sub BuildAutocompleteMenu()
Dim items As New List(Of AutocompleteItem)()
For Each item As String In snippets
items.Add(New SnippetAutocompleteItem(item) With {.ImageIndex = 1})
Next
For Each item As String In declarationSnippets
items.Add(New DeclarationSnippet(item) With {.ImageIndex = 0})
Next
For Each item As String In methods
items.Add(New MethodAutocompleteItem(item) With {.ImageIndex = 2})
Next
For Each item As String In keywords
items.Add(New AutocompleteItem(item))
Next
items.Add(New InsertSpaceSnippet())
items.Add(New InsertSpaceSnippet("^(\w+)([=<>!:]+)(\w+)$"))
items.Add(New InsertEnterSnippet())
'set as autocomplete source
popupMenu.Items.SetAutocompleteItems(items)
End Sub
''' <summary>
''' This item appears when any part of snippet text is typed
''' </summary>
Private Class DeclarationSnippet
Inherits SnippetAutocompleteItem
Public Sub New(ByVal snippet As String)
MyBase.New(snippet)
End Sub
Public Overrides Function Compare(ByVal fragmentText As String) As CompareResult
Dim pattern = Regex.Escape(fragmentText)
If Regex.IsMatch(Text, "\b" & pattern, RegexOptions.IgnoreCase) Then
Return CompareResult.Visible
End If
Return CompareResult.Hidden
End Function
End Class
''' <summary>
''' Divides numbers and words: "123AND456" -> "123 AND 456"
''' Or "i=2" -> "i = 2"
''' </summary>
Private Class InsertSpaceSnippet
Inherits AutocompleteItem
Private pattern As String
Public Sub New(ByVal pattern As String)
MyBase.New("")
Me.pattern = pattern
End Sub
Public Sub New()
Me.New("^(\d+)([a-zA-Z_]+)(\d*)$")
End Sub
Public Overrides Function Compare(ByVal fragmentText As String) As CompareResult
If Regex.IsMatch(fragmentText, pattern) Then
Text = InsertSpaces(fragmentText)
If Text <> fragmentText Then
Return CompareResult.Visible
End If
End If
Return CompareResult.Hidden
End Function
Public Function InsertSpaces(ByVal fragment As String) As String
Dim m = Regex.Match(fragment, pattern)
If m Is Nothing Then
Return fragment
End If
If m.Groups(1).Value = "" AndAlso m.Groups(3).Value = "" Then
Return fragment
End If
Return (m.Groups(1).Value & " " & m.Groups(2).Value & " " & m.Groups(3).Value).Trim()
End Function
Public Overrides Property ToolTipTitle() As String
Get
Return Text
End Get
Set(ByVal value As String)
End Set
End Property
End Class
''' <summary>
''' Inerts line break after '}'
''' </summary>
Private Class InsertEnterSnippet
Inherits AutocompleteItem
Private enterPlace As Place = Place.Empty
Public Sub New()
MyBase.New("[Line break]")
End Sub
Public Overrides Function Compare(ByVal fragmentText As String) As CompareResult
Dim r = Parent.Fragment.Clone()
While r.Start.iChar > 0
If r.CharBeforeStart = "}"c Then
enterPlace = r.Start
Return CompareResult.Visible
End If
r.GoLeftThroughFolded()
End While
Return CompareResult.Hidden
End Function
Public Overrides Function GetTextForReplace() As String
'extend range
Dim r As Range = Parent.Fragment
Dim [end] As Place = r.[End]
r.Start = enterPlace
r.[End] = r.[End]
'insert line break
Return Environment.NewLine + r.Text
End Function
Public Overrides Sub OnSelected(ByVal popupMenu As AutocompleteMenu, ByVal e As SelectedEventArgs)
MyBase.OnSelected(popupMenu, e)
If Parent.Fragment.tb.AutoIndent Then
Parent.Fragment.tb.DoAutoIndent()
End If
End Sub
Public Overrides Property ToolTipTitle() As String
Get
Return "Insert line break after '}'"
End Get
Set(ByVal value As String)
End Set
End Property
End Class
End Class