-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIntelliSense.vb
More file actions
179 lines (146 loc) · 5.9 KB
/
IntelliSense.vb
File metadata and controls
179 lines (146 loc) · 5.9 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
#Region "Microsoft.VisualBasic::07cbff7b9d0a2109db2a2ddf88b2378b, studio\R-terminal\IntelliSense.vb"
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Code Statistics:
' Total Lines: 125
' Code Lines: 102 (81.60%)
' Comment Lines: 2 (1.60%)
' - Xml Docs: 0.00%
'
' Blank Lines: 21 (16.80%)
' File Size: 4.50 KB
' Class IntelliSense
'
' Constructor: (+1 Overloads) Sub New
' Function: AllSymbols, AutoCompletion, GetFileNamesCompletion, packageFunctions
'
' /********************************************************************************/
#End Region
Imports Microsoft.VisualBasic.ApplicationServices.Terminal.LineEdit
Imports Microsoft.VisualBasic.Linq
Imports SMRUCC.Rsharp.Interpreter
Imports RInternal = SMRUCC.Rsharp.Runtime.Internal
Imports rLang = SMRUCC.Rsharp.Language.Syntax
Friend Class IntelliSense
ReadOnly R As RInterpreter
Sub New(R As RInterpreter)
Me.R = R
End Sub
Private Function AllSymbols() As IEnumerable(Of String)
Dim globalEnv = R.globalEnvir
Dim globalSymbols = globalEnv.EnumerateAllSymbols.JoinIterates(globalEnv.EnumerateAllFunctions)
' system internal hiddens
Dim internals = RInternal.invoke.getAllInternals
Dim symbols As IEnumerable(Of String) = globalSymbols _
.Select(Function(s) s.name) _
.JoinIterates(internals.Select(Function(s) s.name)) _
.Distinct
Return symbols
End Function
Private Function packageFunctions(package As String) As IEnumerable(Of String)
Dim globalEnv = R.globalEnvir
Dim namespace_loaded = globalEnv.attachedNamespace(package)
If namespace_loaded Is Nothing Then
Return {}
Else
Return namespace_loaded _
.EnumerateAllSymbols(enumerateParents:=False) _
.JoinIterates(namespace_loaded.EnumerateAllFunctions(enumerateParents:=False)) _
.Select(Function(s) s.name) _
.Distinct _
.ToArray
End If
End Function
Private Function GetFileNamesCompletion(s As String, path_str As String) As Completion
Dim ls As String()
If path_str.DirectoryExists Then
Dim is_dot As Boolean = path_str.IsPattern("\.+")
ls = path_str _
.EnumerateFiles _
.Select(Function(f)
Dim name As String = f.FileName
If is_dot Then
Return "/" & name
Else
Return name
End If
End Function) _
.ToArray
Else
Dim check_name As String = path_str.BaseName(allowEmpty:=True)
Dim pos As Integer = check_name.Length
ls = path_str _
.ParentPath _
.EnumerateFiles _
.Select(Function(f) f.FileName) _
.Where(Function(f) f.StartsWith(check_name, StringComparison.Ordinal)) _
.Select(Function(si) si.Substring(pos)) _
.ToArray
End If
Return New Completion(s, ls)
End Function
Public Function AutoCompletion(s As String, pos As Integer) As Completion
Dim prefix As String = Nothing
Dim ls As String()
If rLang.IsStringOpen(s, prefix) Then
Return GetFileNamesCompletion(s, prefix)
Else
prefix = s.Substring(0, pos)
End If
If prefix.StringEmpty Then
ls = AllSymbols.ToArray
ElseIf prefix.EndsWith("::") Then
' get package functions
Dim packageName = prefix.Trim(":"c, " "c).Split({" "c, "+"c, "-"c, "*"c, "/"c, "\"c, "?"c, "&"c}).LastOrDefault
If packageName.StringEmpty Then
ls = {}
Else
ls = packageFunctions(packageName).ToArray
End If
ElseIf rLang.TryRequirePackage(prefix, prefix) Then
Dim repo = R.globalEnvir.packages.packageRepository
Dim lib_loc As String = R.globalEnvir.options.lib_loc
pos = prefix.Length
ls = repo _
.enumerateClrBases _
.JoinIterates(repo.enumerateNugets(lib_loc)) _
.Where(Function(c) c.StartsWith(prefix, StringComparison.Ordinal)) _
.Select(Function(c) c.Substring(pos)) _
.ToArray
Return New Completion(prefix, ls)
Else
If rLang.EndWithIdentifier(prefix, prefix) Then
pos = prefix.Length
End If
ls = AllSymbols _
.Where(Function(c) c.StartsWith(prefix, StringComparison.Ordinal)) _
.Select(Function(c) c.Substring(pos)) _
.ToArray
End If
Return New Completion(prefix, ls)
End Function
End Class