forked from genericptr/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodeAction.pas
271 lines (218 loc) · 8.45 KB
/
codeAction.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
// Pascal Language Server
// 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 codeAction;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
uses
Classes,SysUtils, URIParser,
lsp, basic;
type
{ TCodeActionKind }
{ The kind of a code action.
Kinds are a hierarchical list of identifiers separated by `.`, e.g. `"refactor.extract.function"`.
The set of kinds is open and client needs to announce the kinds it supports to the server during
initialization. }
TCodeActionKind = record
public const
// Empty kind.
Empty = '';
// Base kind for quickfix actions: 'quickfix'.
QuickFix = 'quickfix';
// Base kind for refactoring actions: 'refactor'.
Refactor = 'refactor';
// Base kind for refactoring extraction actions: 'refactor.extract'.
//
// Example extract actions:
//
// - Extract method
// - Extract function
// - Extract variable
// - Extract interface from class
// - ...
RefactorExtract = 'refactor.extract';
// Base kind for refactoring inline actions: 'refactor.inline'.
//
// Example inline actions:
//
// - Inline function
// - Inline variable
// - Inline constant
// - ...
RefactorInline = 'refactor.inline';
// Base kind for refactoring rewrite actions: 'refactor.rewrite'.
//
// Example rewrite actions:
//
// - Convert JavaScript function to class
// - Add or remove parameter
// - Encapsulate field
// - Make method static
// - Move method to base class
// - ...
RefactorRewrite = 'refactor.rewrite';
// Base kind for source actions: `source`.
// Source code actions apply to the entire file.
Source = 'source';
// Base kind for an organize imports source action: `source.organizeImports`.
SourceOrganizeImports = 'source.organizeImports';
private
value: string;
end;
TCodeActionTriggerKind=record
public const
//Code actions were explicitly requested by the user or by an extension.
Invoked = 1;
//Code actions were requested automatically.
//
// This typically happens when current selection in a file changes, but can
// also be triggered when file content changes.
Automatic =2;
private
value:Integer;
end;
{ TCodeAction }
TCodeAction = class(TCollectionItem)
private
fTitle: string;
fKind: TCodeActionKind;
fDiagnostics: TDiagnosticItems;
fIsPreferred: boolean;
fEdit: TWorkspaceEdit;
fCommand: TCommand;
published
// A short, human-readable, title for this code action.
property title: string read fTitle write fTitle;
// The kind of the code action.
// Used to filter code actions.
property kind: string read fKind.value write fKind.value;
// The diagnostics that this code action resolves.
property diagnostics: TDiagnosticItems read fDiagnostics write fDiagnostics;
// Marks this as a preferred action. Preferred actions are used by the `auto fix` command and can be targeted
// by keybindings.
//
// A quick fix should be marked preferred if it properly addresses the underlying error.
// A refactoring should be marked preferred if it is the most reasonable choice of actions to take.
//
// @since 3.15.0
property isPreferred: boolean read fIsPreferred write fIsPreferred;
// The workspace edit this code action performs.
property edit: TWorkspaceEdit read fEdit write fEdit;
// A command this code action executes. If a code action
// provides an edit and a command, first the edit is
// executed and then the command.
property command: TCommand read fCommand write fCommand;
end;
TCodeActionItems = specialize TGenericCollection<TCodeAction>;
{ TCodeActionContext }
{ Contains additional diagnostic information about the context in which
a code action is run. }
TCodeActionContext = class(TPersistent)
private
fDiagnostics: TDiagnosticItems;
fOnly: TStrings;
ftriggerKind:TCodeActionTriggerKind;
published
// An array of diagnostics known on the client side overlapping the range provided to the
// `textDocument/codeAction` request. They are provided so that the server knows which
// errors are currently presented to the user for the given range. There is no guarantee
// that these accurately reflect the error state of the resource. The primary parameter
// to compute code actions is the provided range.
property diagnostics: TDiagnosticItems read fDiagnostics write fDiagnostics;
// (OPTIONAL) Requested kind of actions to return.
// Actions not of this kind are filtered out by the client before being shown. So servers
// can omit computing them.
property only: TStrings read fOnly write fOnly;
//The reason why code actions were requested.
//@since 3.17.0
property triggerKind:Integer read ftriggerKind.value write ftriggerKind.value;
public
procedure AfterConstruction; override;
end;
{ TCodeActionParams }
TCodeActionParams = class(TPersistent)
private
fTextDocument: TTextDocumentIdentifier;
fRange: TRange;
fContext: TCodeActionContext;
published
// The document in which the command was invoked.
property textDocument: TTextDocumentIdentifier read fTextDocument write fTextDocument;
// The range for which the command was invoked.
property range: TRange read fRange write fRange;
// Context carrying additional information.
property context: TCodeActionContext read fContext write fContext;
end;
{ TCodeActionRequest }
TCodeActionRequest = class(specialize TLSPRequest<TCodeActionParams, TCodeActionItems>)
function Process(var Params: TCodeActionParams): TCodeActionItems; override;
end;
{ TCodeActionRequestEx }
TCodeActionRequestEx = class(specialize TLSPRequest<TCodeActionParams, TCommand>)
function Process(var Params: TCodeActionParams): TCommand; override;
end;
implementation
uses CodeToolManager;
{ TCodeActionContext }
procedure TCodeActionContext.AfterConstruction;
begin
inherited AfterConstruction;
Self.fOnly:=TStringList.Create;
self.diagnostics:=TDiagnosticItems.Create;
end;
{ TCodeActionRequestEx }
function TCodeActionRequestEx.Process(var Params: TCodeActionParams): TCommand;
begin
result:=TCommand.Create;
result.title:='';
result.command:='save';
end;
function TCodeActionRequest.Process(var Params: TCodeActionParams): TCodeActionItems;
var codeAction:TCodeAction;
begin with Params do
begin
if context.triggerKind=TCodeActionTriggerKind.Invoked then
begin
Result := TCodeActionItems.Create;
codeAction:=(Result.Add as TCodeAction);
codeAction.title:='Remove unused units';
codeAction.kind:=TCodeActionKind.Source;
codeAction.isPreferred:=false;
codeAction.command:=TCommand.Create;
codeAction.command.title:=TCommandKind.RemoveUnusedUnit;
codeAction.command.command:=TCommandKind.RemoveUnusedUnit;
codeAction.command.arguments.Add(params.textDocument.uri);
codeAction:=(Result.Add as TCodeAction);
codeAction.title:='Code completion';
codeAction.kind:=TCodeActionKind.Refactor;
codeAction.isPreferred:=false;
codeAction.command:=TCommand.Create;
codeAction.command.title:='CompleteCode';
codeAction.command.command:=TCommandKind.CompleteCode;
codeAction.command.arguments.Add(params.textDocument.uri);
codeAction.command.arguments.add(inttostr( range.start.line));
codeAction.command.arguments.add(inttostr( range.start.character));
codeAction.command.arguments.add(inttostr( range.&end.line));
codeAction.command.arguments.add(inttostr( range.&end.character));
end
else
begin
Result:=nil;
end;
end;
end;
initialization
LSPHandlerManager.RegisterHandler('textDocument/codeAction', TCodeActionRequest);
end.