-
Notifications
You must be signed in to change notification settings - Fork 22
/
completion.pas
295 lines (259 loc) · 10.5 KB
/
completion.pas
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Pascal Language Server
// Copyright 2020 Arjan Adriaanse
// 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 completion;
{$mode objfpc}{$H+}
interface
uses
Classes, URIParser, CodeToolManager, CodeCache, IdentCompletionTool, BasicCodeTools,
lsp, basic;
type
{ TCompletionTriggerKind }
// How a completion was triggered
TCompletionTriggerKind = (
// Completion was triggered by typing an identifier (24x7 code
// complete), manual invocation (e.g Ctrl+Space) or via API.
Invoked = 1,
// Completion was triggered by a trigger character specified by
// the `triggerCharacters` properties of the
// `CompletionRegistrationOptions`.
TriggerCharacter = 2,
// Completion was re-triggered as the current completion list is
// incomplete.
TriggerForIncompleteCompletions = 3);
{ TCompletionContext }
// Contains additional information about the context in which a
// completion request is triggered.
TCompletionContext = class(TPersistent)
private
fTriggerKind: TCompletionTriggerKind;
fTriggerCharacter: string;
published
// How the completion was triggered.
property triggerKind: TCompletionTriggerKind read fTriggerKind write fTriggerKind;
// The trigger character (a single character) that has trigger
// code complete. Is undefined if `triggerKind !==
// CompletionTriggerKind.TriggerCharacter`
property triggerCharacter: string read fTriggerCharacter write fTriggerCharacter;
end;
{ TCompletionParams }
TCompletionParams = class(TTextDocumentPositionParams)
private
fContext: TCompletionContext;
published
// The completion context. This is only available if the client
// specifies to send this using
// `ClientCapabilities.textDocument.completion.contextSupport ===
// true`
property context: TCompletionContext read fContext write fContext;
end;
{ TInsertTextFormat }
// Defines whether the insert text in a completion item should be
// interpreted as plain text or a snippet.
TInsertTextFormat = (
// The primary text to be inserted is treated as a plain string.
PlainText = 1,
// The primary text to be inserted is treated as a snippet.
//
// A snippet can define tab stops and placeholders with `$1`, `$2`
// and `${3:foo}`. `$0` defines the final tab stop, it defaults to
// the end of the snippet. Placeholders with equal identifiers are
// linked, that is typing in one will update others too.
Snippet = 2);
{ TCompletionItemTag }
// Completion item tags are extra annotations that tweak the
// rendering of a completion item.
//
// @since 3.15.0
TCompletionItemTag = (
// Render a completion as obsolete, usually using a strike-out.
Deprecated = 1);
TCompletionItemTags = set of TCompletionItemTag;
{ TCompletionItemKind }
// The kind of a completion entry.
TCompletionItemKind = (
TextItem = 1,
MethodItem = 2,
FunctionItem = 3,
ConstructorItem = 4,
FieldItem = 5,
VariableItem = 6,
ClassItem = 7,
InterfaceItem = 8,
ModuleItem = 9,
PropertyItem = 10,
UnitItem = 11,
ValueItem = 12,
EnumItem = 13,
KeywordItem = 14,
SnippetItem = 15,
ColorItem = 16,
FileItem = 17,
ReferenceItem = 18,
FolderItem = 19,
EnumMemberItem = 20,
ConstantItem = 21,
StructItem = 22,
EventItem = 23,
OperatorItem = 24,
TypeParameterItem = 25);
{ TCompletionItem }
TCompletionItem = class(TCollectionItem)
private
fLabel: string;
fKind: TCompletionItemKind;
fTags: TCompletionItemTags;
fDetail: string;
fDocumentation: TMarkupContent;
fPreselect: Boolean;
fSortText: string;
fFilterText: string;
fInsertText: string;
fInsertTextFormat: TInsertTextFormat;
fTextEdit: TTextEdit;
fAdditionalTextEdits: TTextEdits;
fCommitCharacters: TStrings;
published
// The label of this completion item. By default also the text
// that is inserted when selecting this completion.
property label_: string read fLabel write fLabel;
// The kind of this completion item. Based of the kind an icon is
// chosen by the editor. The standardized set of available values
// is defined in `CompletionItemKind`.
property kind: TCompletionItemKind read fKind write fKind;
// Tags for this completion item.
//
// @since 3.15.0
property tags: TCompletionItemTags read fTags write fTags;
// A human-readable string with additional information about this
// item, like type or symbol information.
property detail: string read fDetail write fDetail;
// A human-readable string that represents a doc-comment.
property documentation: TMarkupContent read fDocumentation write fDocumentation;
// Select this item when showing.
//
// *Note* that only one completion item can be selected and that
// the tool / client decides which item that is. The rule is that
// the *first* item of those that match best is selected.
property preselect: Boolean read fPreselect write fPreselect;
// A string that should be used when comparing this item
// with other items. When `falsy` the label is used.
property sortText: string read fSortText write fSortText;
// A string that should be used when filtering a set of
// completion items. When `falsy` the label is used.
property filterText: string read fFilterText write fFilterText;
// A string that should be inserted into a document when selecting
// this completion. When `falsy` the label is used.
//
// The `insertText` is subject to interpretation by the client
// side. Some tools might not take the string literally. For
// example VS Code when code complete is requested in this example
// `con<cursor position>` and a completion item with an
// `insertText` of `console` is provided it will only insert
// `sole`. Therefore it is recommended to use `textEdit` instead
// since it avoids additional client side interpretation.
property insertText: string read fInsertText write fInsertText;
// The format of the insert text. The format applies to both the
// `insertText` property and the `newText` property of a provided
// `textEdit`. If omitted defaults to
// `InsertTextFormat.PlainText`.
property insertTextFormat: TInsertTextFormat read fInsertTextFormat write fInsertTextFormat;
// An edit which is applied to a document when selecting this
// completion. When an edit is provided the value of `insertText`
// is ignored.
//
// *Note:* The range of the edit must be a single line range and
// it must contain the position at which completion has been
// requested.
property textEdit: TTextEdit read fTextEdit write fTextEdit;
// An optional array of additional text edits that are applied
// when selecting this completion. Edits must not overlap
// (including the same insert position) with the main edit nor
// with themselves.
//
// Additional text edits should be used to change text unrelated
// to the current cursor position (for example adding an import
// statement at the top of the file if the completion item will
// insert an unqualified type).
property additionalTextEdits: TTextEdits read fAdditionalTextEdits write fAdditionalTextEdits;
// An optional set of characters that when pressed while this
// completion is active will accept it first and then type that
// character. *Note* that all commit characters should have
// `length=1` and that superfluous characters will be ignored.
property commitCharacters: TStrings read fCommitCharacters write fCommitCharacters;
end;
TCompletionItems = specialize TGenericCollection<TCompletionItem>;
{ TCompletionList }
// Represents a collection of [completion items](#CompletionItem) to
// be presented in the editor.
TCompletionList = class(TPersistent)
private
fIsIncomplete: Boolean;
fItems: TCompletionItems;
published
// This list it not complete. Further typing should result in
// recomputing this list.
property isIncomplete: Boolean read fIsIncomplete write fIsIncomplete;
// The completion items.
property items: TCompletionItems read fItems write fItems;
end;
{ TCompletion }
TCompletion = class(specialize TLSPRequest<TCompletionParams, TCompletionList>)
function Process(var Params: TCompletionParams): TCompletionList; override;
end;
implementation
{ TCompletion }
function TCompletion.Process(var Params: TCompletionParams): TCompletionList;
var
URI: TURI;
Code: TCodeBuffer;
X, Y, PStart, PEnd, Count, I: Integer;
Line: string;
Completions: TCompletionItems;
Identifier: TIdentifierListItem;
Completion: TCompletionItem;
begin with Params do
begin
URI := ParseURI(textDocument.uri);
Code := CodeToolBoss.FindFile(URI.Path + URI.Document);
X := position.character;
Y := position.line;
Line := Code.GetLine(Y);
GetIdentStartEndAtPosition(Line, X + 1, PStart, PEnd);
CodeToolBoss.IdentifierList.Prefix := Copy(Line, PStart, PEnd - PStart);
Completions := TCompletionItems.Create;
if CodeToolBoss.GatherIdentifiers(Code,X + 1,Y + 1) then
begin
Count := CodeToolBoss.IdentifierList.GetFilteredCount;
for I := 0 to Count - 1 do
begin
Identifier := CodeToolBoss.IdentifierList.FilteredItems[I];
Completion := TCompletionItem(Completions.Add);
Completion.insertText := Identifier.Identifier;
Completion.detail:=Identifier.Node.DescAsString;
Completion.insertTextFormat := TInsertTextFormat.PlainText;
end;
end else begin
if CodeToolBoss.ErrorMessage<>'' then
writeln(stderr, 'Parse error: ',CodeToolBoss.ErrorMessage)
else
writeln(stderr, 'Error: no context');
end;
Result := TCompletionList.Create;
Result.items := Completions;
end;
end;
initialization
LSPHandlerManager.RegisterHandler('textDocument/completion', TCompletion);
end.