-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Commands.pas
More file actions
306 lines (241 loc) · 7.01 KB
/
PasLS.Commands.pas
File metadata and controls
306 lines (241 loc) · 7.01 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
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
295
296
297
298
299
300
301
302
303
304
305
306
// Pascal Language Server
// Copyright 2022 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.Commands;
{$mode objfpc}{$H+}
{$codepage UTF8}
interface
uses
{ RTL }
SysUtils, Classes, Types, FPJSON,
{ LSP }
LSP.BaseTypes, LSP.Messages ;
Type
{ TCustomCommand }
TCustomCommand = class
private
FTransport: TMessageTransport;
Protected
function DoExecute(aArguments : TJSONArray) : TLSPStreamable; virtual; abstract;
Public
class Function CommandName : String; virtual;
class Procedure Register;
class procedure UnRegister;
Constructor Create(aTransport : TMessageTransport); virtual;
function Execute(aArguments : TJSONArray) : TLSPStreamable;
Property Transport : TMessageTransport Read FTransport;
end;
TCustomCommandClass = class of TCustomCommand;
{ TCommandDef }
TCommandDef = Class(TCollectionItem)
private
FClass: TCustomCommandClass;
function GetCommandName: String;
Public
property CommandClass : TCustomCommandClass Read FClass Write FClass;
Property CommandName : String Read GetCommandName;
end;
{ TCommandDefs }
TCommandDefs = Class(TCollection)
private
FCommandCount: Integer;
function GetDef(aIndex: Integer): TCommandDef;
procedure SetDef(aIndex: Integer; AValue: TCommandDef);
Public
function Add(aCommandClass : TCustomCommandClass) : TCommandDef;
function IndexOfCommandClass(aCommandClass : TCustomCommandClass) : Integer;
function IndexOfCommand(const aName : String) : Integer;
function FindCommand(const aName: String): TCommandDef;
function FindCommandClass(aCommandClass : TCustomCommandClass): TCommandDef;
function Remove(aCommandClass : TCustomCommandClass) : Boolean;
Property Commands[aIndex: Integer] : TCommandDef Read GetDef Write SetDef; default;
Property CommandCount : Integer Read FCommandCount;
end;
{ TCommandFactory }
TCommandFactory = class
private
class var _instance : TCommandFactory;
private
FList : TCommandDefs;
public
Class Constructor Init;
Class Destructor Done;
Constructor Create;
Destructor Destroy; override;
Procedure RegisterCommand(aCommand : TCustomCommandClass);
Procedure UnregisterCommand(aCommand : TCustomCommandClass);
Function FindCommand(const aName : String) : TCommandDef;
Function FindCommandClass(const aName : String) : TCustomCommandClass;
Procedure GetCommandList(L : TStrings);
Function GetCommandNames : TStringDynArray;
Class Property Instance : TCommandFactory Read _Instance;
end;
Function CommandFactory : TCommandFactory;
implementation
uses
CodeToolManager,
FindDeclarationTool,
{ Protocols }
LSP.Workspace, PasLS.Settings;
function CommandFactory: TCommandFactory;
begin
Result:=TCommandFactory.Instance;
end;
{ TCustomCommand }
class function TCustomCommand.CommandName: String;
begin
Result:=ClassName;
if Result[1]='T' then
Delete(Result,1,1);
end;
class procedure TCustomCommand.Register;
begin
TCommandFactory.Instance.RegisterCommand(Self);
end;
class procedure TCustomCommand.UnRegister;
begin
TCommandFactory.Instance.UnRegisterCommand(Self);
end;
constructor TCustomCommand.Create(aTransport: TMessageTransport);
begin
FTransport:=aTransport;
end;
function TCustomCommand.Execute(aArguments: TJSONArray): TLSPStreamable;
begin
// Maybe later we can add some logging etc. here.
Result:=DoExecute(aArguments);
end;
{ TCommandDef }
function TCommandDef.GetCommandName: String;
begin
Result:='';
if Assigned(CommandClass) then
Result:=CommandClass.CommandName;
end;
{ TCommandDefs }
function TCommandDefs.GetDef(aIndex: Integer): TCommandDef;
begin
Result:=Items[aIndex] as TCommandDef;
end;
procedure TCommandDefs.SetDef(aIndex: Integer; AValue: TCommandDef);
begin
Items[aIndex]:=aValue;
end;
function TCommandDefs.Add(aCommandClass: TCustomCommandClass): TCommandDef;
begin
if IndexOfCommand(aCommandClass.CommandName)<>-1 then
Raise EListError.CreateFmt('Duplicate command: %s',[aCommandClass.CommandName]);
Result:=(Inherited Add) as TCommandDef;
Result.CommandClass:=aCommandClass;
end;
function TCommandDefs.IndexOfCommandClass(aCommandClass: TCustomCommandClass
): Integer;
begin
Result:=Count-1;
While (Result>=0) and (GetDef(Result).CommandClass<>aCommandClass) do
Dec(Result);
end;
function TCommandDefs.IndexOfCommand(const aName: String): Integer;
begin
Result:=Count-1;
While (Result>=0) and Not SameText(GetDef(Result).CommandName,aName) do
Dec(Result);
end;
function TCommandDefs.FindCommand(const aName: String): TCommandDef;
var
Idx : Integer;
begin
Result:=nil;
Idx:=IndexOfCommand(aName);
If Idx<>-1 then
Result:=Commands[Idx];
end;
function TCommandDefs.FindCommandClass(aCommandClass: TCustomCommandClass
): TCommandDef;
var
Idx : Integer;
begin
Result:=nil;
Idx:=IndexOfCommandClass(aCommandClass);
If Idx<>-1 then
Result:=Commands[Idx];
end;
function TCommandDefs.Remove(aCommandClass: TCustomCommandClass): Boolean;
var
Def : TCommandDef;
begin
Def:=FindCommandClass(aCommandClass);
Result:=Def<>Nil;
if Result then
Def.Free;
end;
{ TCommandFactory }
class constructor TCommandFactory.Init;
begin
_Instance:=TCommandFactory.Create
end;
class destructor TCommandFactory.Done;
begin
FreeAndNil(_Instance);
end;
constructor TCommandFactory.Create;
begin
FList:=TCommandDefs.Create(TCommandDef);
end;
destructor TCommandFactory.Destroy;
begin
FreeAndNil(FList);
inherited Destroy;
end;
procedure TCommandFactory.RegisterCommand(aCommand: TCustomCommandClass);
begin
FList.Add(aCommand);
end;
procedure TCommandFactory.UnregisterCommand(aCommand: TCustomCommandClass);
begin
FList.Remove(aCommand);
end;
function TCommandFactory.FindCommand(const aName: String): TCommandDef;
begin
Result:=FList.FindCommand(aName);
end;
function TCommandFactory.FindCommandClass(const aName: String): TCustomCommandClass;
var
aDef : TCommandDef;
begin
Result:=nil;
aDef:=FindCommand(aName);
if Assigned(aDef) then
Result:=aDef.CommandClass;
end;
procedure TCommandFactory.GetCommandList(L: TStrings);
Var
i : Integer;
begin
For I:=0 to FList.Count-1 do
L.Add(FList[i].CommandName);
end;
function TCommandFactory.GetCommandNames: TStringDynArray;
var
L : TStrings;
begin
L:=TStringList.Create;
try
GetCommandList(L);
Result:=L.ToStringArray;
finally
L.Free;
end;
end;
end.