-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Completion.pas
More file actions
256 lines (219 loc) · 8.23 KB
/
PasLS.Completion.pas
File metadata and controls
256 lines (219 loc) · 8.23 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Pascal Language Server
// Copyright 2020 Arjan Adriaanse
// Copyright 2020 Ryan Joseph
// This file is part of Pascal Language Server.
// Pascal Language Server 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.
// Pascal Language Server 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 Pascal Language Server. If not, see
// <https://www.gnu.org/licenses/>.
unit PasLS.Completion;
{$mode objfpc}{$H+}
{$scopedenums on}
interface
uses
Classes, DateUtils,
CodeToolManager, CodeCache, IdentCompletionTool, BasicCodeTools, CodeTree,
LSP.Base, LSP.Basic, LSP.BaseTypes, LSP.Completion;
Type
{ TCompletion }
TCompletion = class(specialize TLSPRequest<TCompletionParams, TCompletionList>)
function KindForIdentifier(Identifier: TIdentifierListItem): TCompletionItemKind;
function Process(var Params: TCompletionParams): TCompletionList; override;
end;
implementation
uses
SysUtils, Contnrs,
PasLS.CodeUtils, PasLS.Diagnostics, PasLS.Settings;
{ TCompletion }
function TCompletion.KindForIdentifier(Identifier: TIdentifierListItem): TCompletionItemKind;
var
desc: TCodeTreeNodeDesc;
begin
// PredefinedIdentifiers no node ,use default desc
if Identifier.Node = nil then
desc := Identifier.DefaultDesc
else
desc := Identifier.Node.Desc;
// get completion item kind from identifier node
case desc of
ctnUnit,
ctnUseUnit,
ctnUseUnitClearName,
ctnUseUnitNamespace:
result := TCompletionItemKind.ModuleItem;
ctnClass,
ctnObject,
ctnObjCClass,
ctnObjCCategory,
ctnObjCProtocol,
ctnCPPClass,
ctnTypeHelper,
ctnRecordHelper:
result := TCompletionItemKind.ClassItem;
ctnRecordType:
result := TCompletionItemKind.StructItem;
ctnClassInterface,
ctnDispinterface:
result := TCompletionItemKind.InterfaceItem;
ctnTypeSection,
ctnVarSection,
ctnConstSection,
ctnResStrSection,
ctnLabelSection,
ctnPropertySection,
ctnUsesSection,
ctnRequiresSection,
ctnContainsSection,
ctnExportsSection:
result := TCompletionItemKind.FolderItem; {todo: not sure?}
ctnProcedure:
begin
if not ServerSettings.minimalisticCompletions and IsNodeObjectMember(Identifier.Node) then
result := TCompletionItemKind.MethodItem
else
result := TCompletionItemKind.FunctionItem;
end;
ctnTypeDefinition:
result := TCompletionItemKind.TypeParameterItem;
ctnGenericType,
ctnGenericParameter:
result := TCompletionItemKind.TypeParameterItem; {todo: generics of class/recrod??}
ctnProperty,
ctnGlobalProperty:
result := TCompletionItemKind.PropertyItem;
ctnVarDefinition:
begin
if not ServerSettings.minimalisticCompletions and IsNodeObjectMember(Identifier.Node) then
result := TCompletionItemKind.FieldItem
else
result := TCompletionItemKind.VariableItem;
end;
ctnConstDefinition:
result := TCompletionItemKind.ConstantItem;
ctnEnumerationType:
result := TCompletionItemKind.EnumItem;
ctnEnumIdentifier:
result := TCompletionItemKind.EnumMemberItem;
otherwise
result := TCompletionItemKind.KeywordItem;
end;
end;
function TCompletion.Process(var Params: TCompletionParams): TCompletionList;
var
Code: TCodeBuffer;
X, Y, PStart, PEnd, Count, I: Integer;
Line: String;
Completions: TCompletionItems;
Identifier: TIdentifierListItem;
Completion: TCompletionItem;
OverloadMap: TFPHashList;
IdentContext, IdentDetails: ShortString;
ObjectMember: boolean;
Kind: TCompletionItemKind;
begin with Params do
begin
Code := CodeToolBoss.FindFile(textDocument.LocalPath);
if Code=nil then
Code:=CodeToolBoss.LoadFile(textDocument.LocalPath,true,false);
if Code=nil then
begin
Result := TCompletionList.Create;
Result.isIncomplete:=false;
exit;
end;
X := position.character;
Y := position.line;
Line := Code.GetLine(Y);
GetIdentStartEndAtPosition(Line, X + 1, PStart, PEnd);
CodeToolBoss.IdentifierList.Prefix := Copy(Line, PStart, PEnd - PStart);
OverloadMap := TFPHashList.Create;
Result := TCompletionList.Create;
// Alias
Completions:=Result.items;
try
if CodeToolBoss.GatherIdentifiers(Code, X + 1, Y + 1) then
begin
Count := CodeToolBoss.IdentifierList.GetFilteredCount;
IdentDetails := '';
for I := 0 to Count - 1 do
begin
// make sure we don't exceed the maximum completions count
if (ServerSettings.maximumCompletions > -1) and (I >= ServerSettings.maximumCompletions) then
begin
Result.isIncomplete := true;
break;
end;
Identifier := CodeToolBoss.IdentifierList.FilteredItems[I];
if not ServerSettings.minimalisticCompletions then
IdentContext := IdentifierContext(Identifier, IdentDetails, ObjectMember);
if Identifier.IsProcNodeWithParams then
begin
// Ignore duplicate overloads
Completion := TCompletionItem(OverloadMap.Find(Identifier.Identifier));
if Completion <> nil then
continue;
Kind := KindForIdentifier(Identifier);
if (ServerSettings.ignoreTextCompletions) and (Kind = TCompletionItemKind.TextItem) then
continue;
Completion := Completions.Add;
Completion.&label := Identifier.Identifier;
Completion.kind := Kind;
if not ServerSettings.minimalisticCompletions then
begin
// TODO: in 3.17 implement labelDetails to show parameters
// then we can consider showing overloads in the list
Completion.detail := IdentDetails;
if ServerSettings.insertCompletionsAsSnippets then
begin
// TODO: use `ParseParamList(AsSnippet)` instead of $0
Completion.insertText := Identifier.Identifier+'($0)';
Completion.insertTextFormat := TInsertTextFormat.Snippet;
end
else
begin
Completion.insertText := Identifier.Identifier;
Completion.insertTextFormat := TInsertTextFormat.PlainText;
end;
end;
Completion.sortText := IntToStr(I);
OverloadMap.Add(Identifier.Identifier, Completion);
end
else
begin
Kind := KindForIdentifier(Identifier);
if (ServerSettings.ignoreTextCompletions) and (Kind = TCompletionItemKind.TextItem) then
continue;
Completion := Completions.Add;
Completion.&label := Identifier.Identifier;
if not ServerSettings.minimalisticCompletions then
Completion.detail := IdentDetails;
Completion.kind := Kind;
Completion.sortText := IntToStr(I);
end;
end;
end
else
begin
PublishCodeToolsError(Self.Transport,'');
Result.isIncomplete := true;
end;
except
on E: Exception do
begin
LogError('Completion', E);
Result.isIncomplete := true;
end;
end;
// todo: make this a verbosity option
// DoLog('got completions %d in %d ms and processed in %d ms', [Completions.Count,MilliSecondsBetween(Now, GatherTime),MilliSecondsBetween(Now, StartTime));
end;
FreeAndNil(OverloadMap);
end;
end.