forked from genericptr/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSP.CodeAction.pas
274 lines (232 loc) · 8 KB
/
LSP.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
271
272
273
// 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 LSP.CodeAction;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
uses
{ RTL }
SysUtils, Classes,
{ Protocol }
LSP.Base, LSP.Basic, LSP.BaseTypes;
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 = Class
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'.
RefactorExtract = 'refactor.extract';
// Base kind for refactoring inline actions: 'refactor.inline'.
RefactorInline = 'refactor.inline';
// Base kind for refactoring rewrite actions: 'refactor.rewrite'.
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';
end;
{ TCodeAction }
TCodeAction = class(TCollectionItem)
private
fTitle: string;
fKind: String;
fDiagnostics: TDiagnosticItems;
fIsPreferred: boolean;
fEdit: TWorkspaceEdit;
fCommand: TCommand;
procedure SetCommand(AValue: TCommand);
procedure SetDiagnostics(AValue: TDiagnosticItems);
procedure SetEdit(AValue: TWorkspaceEdit);
Public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
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 write fKind;
// The diagnostics that this code action resolves.
property diagnostics: TDiagnosticItems read fDiagnostics write SetDiagnostics;
// 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 SetEdit;
// 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 SetCommand;
end;
TCodeActionItems = specialize TGenericCollection<TCodeAction>;
{ TCodeActionContext
Contains additional diagnostic information about the context in which
a code action is run. }
TCodeActionContext = class(TLSPStreamable)
private
fDiagnostics: TDiagnosticItems;
fOnly: TStrings;
procedure SetDiagnostics(AValue: TDiagnosticItems);
procedure SetOnly(AValue: TStrings);
public
Constructor Create; override;
Destructor Destroy; override;
Procedure Assign(Source : TPersistent); override;
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 SetDiagnostics;
// (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 SetOnly;
end;
{ TCodeActionParams }
TCodeActionParams = class(TLSPStreamable)
private
fTextDocument: TTextDocumentIdentifier;
fRange: TRange;
fContext: TCodeActionContext;
procedure SetContext(AValue: TCodeActionContext);
Public
constructor create; override;
destructor Destroy; override;
Procedure Assign(Source : TPersistent); override;
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 SetContext;
end;
implementation
{ TCodeAction }
procedure TCodeAction.SetCommand(AValue: TCommand);
begin
if fCommand=AValue then Exit;
fCommand.Assign(AValue);
end;
procedure TCodeAction.SetDiagnostics(AValue: TDiagnosticItems);
begin
if fDiagnostics=AValue then Exit;
fDiagnostics.Assign(AValue);
end;
procedure TCodeAction.SetEdit(AValue: TWorkspaceEdit);
begin
if fEdit=AValue then Exit;
fEdit.Assign(AValue);
end;
constructor TCodeAction.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
fDiagnostics:=TDiagnosticItems.Create;
fEdit:=TWorkspaceEdit.Create;
fCommand:=TCommand.Create;
end;
destructor TCodeAction.Destroy;
begin
FreeAndNil(fDiagnostics);
FreeAndNil(fEdit);
FreeAndNil(fCommand);
inherited Destroy;
end;
{ TCodeActionContext }
procedure TCodeActionContext.SetDiagnostics(AValue: TDiagnosticItems);
begin
if fDiagnostics=AValue then Exit;
fDiagnostics.Assign(AValue);
end;
procedure TCodeActionContext.SetOnly(AValue: TStrings);
begin
if fOnly=AValue then Exit;
fOnly.Assign(AValue);
end;
constructor TCodeActionContext.Create;
begin
inherited Create;
fDiagnostics:=TDiagnosticItems.Create;
fOnly:=TStringList.Create;
end;
destructor TCodeActionContext.Destroy;
begin
FreeAndNil(fDiagnostics);
FreeAndNil(fOnly);
inherited Destroy;
end;
procedure TCodeActionContext.Assign(Source: TPersistent);
var
src: TCodeActionContext absolute source;
begin
if Source is TCodeActionContext then
begin
Diagnostics.Assign(Src.diagnostics);
Only.Assign(Src.Only);
end
else
inherited Assign(Source);
end;
{ TCodeActionParams }
procedure TCodeActionParams.SetContext(AValue: TCodeActionContext);
begin
if fContext=AValue then Exit;
fContext:=AValue;
end;
constructor TCodeActionParams.create;
begin
inherited create;
fTextDocument:=TTextDocumentIdentifier.Create;
fRange:=TRange.Create;
fContext:=TCodeActionContext.Create;
end;
destructor TCodeActionParams.Destroy;
begin
FreeAndNil(fTextDocument);
FreeAndNil(fRange);
FreeAndNil(fContext);
inherited Destroy;
end;
procedure TCodeActionParams.Assign(Source: TPersistent);
var
Src: TCodeActionParams absolute Source;
begin
if Source is TCodeActionParams then
begin
TextDocument.Assign(Src.textDocument);
Range.Assign(Src.Range);
Context.Assign(Src.context);
end
else
inherited Assign(Source);
end;
end.