-
Notifications
You must be signed in to change notification settings - Fork 16
/
LSP.BaseTypes.pas
301 lines (231 loc) · 6.02 KB
/
LSP.BaseTypes.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
295
296
297
298
299
300
// Copyright 2023 Michael Van Canneyt
// 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.BaseTypes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
Type
TAnyArray = array of Variant;
{ TOptional }
generic TOptional<T> = class
private
fHasValue: Boolean;
fValue: T;
function GetValue: T;
procedure SetValue(AValue: T);
public
property HasValue: Boolean read fHasValue;
property Value: T read GetValue write SetValue;
procedure Clear;
end;
{ TOptionalVariantBase }
TOptionalVariantBase = class(specialize TOptional<Variant>);
{ TOptionalVariant }
generic TOptionalVariant<T> = class(TOptionalVariantBase)
private
function GetValue: T;
procedure SetValue(AValue: T);
public
constructor Create; overload;
constructor Create(AValue: T); overload;
property Value: T read GetValue write SetValue;
end;
{ TOptionalObjectBase }
TOptionalObjectBase = class(specialize TOptional<TObject>)
public
function ValueClass: TClass; virtual; abstract;
end;
{ TOptionalObject }
generic TOptionalObject<T> = class(TOptionalObjectBase)
private
function GetValue: T;
procedure SetValue(AValue: T);
public
constructor Create;
constructor Create(AValue: T);
function ValueClass: TClass; override;
property Value: T read GetValue write SetValue;
end;
TOptionalBoolean = specialize TOptionalVariant<Boolean>;
TOptionalString = specialize TOptionalVariant<String>;
TOptionalInteger = specialize TOptionalVariant<Integer>;
TOptionalAny = specialize TOptionalVariant<Variant>; // any type except structures (objects or arrays)
TOptionalNumber = TOptionalInteger;
{ TGenericCollection }
generic TGenericCollection<T> = class(TCollection)
private
function GetI(Index : Integer): T;
procedure SetI(Index : Integer; AValue: T);
public
constructor Create;
Function Add : T; reintroduce;
Property Items[Index : Integer] : T Read GetI Write SetI;
end;
{ TLSPStreamable }
TLSPStreamable = class(TPersistent)
Public
// We need a virtual constructor
Constructor Create; virtual;
end;
TLSPStreamableClass = Class of TLSPStreamable;
{ LSPException }
LSPException = class(Exception)
public
function Code: Integer; virtual; abstract;
end;
EServerNotInitialized = class(LSPException)
public
function Code: Integer; override;
end;
EUnknownErrorCode = class(LSPException)
public
function Code: Integer; override;
end;
// Defined by the protocol.
ERequestCancelled = class(LSPException)
public
function Code: Integer; override;
end;
EContentModified = class(LSPException)
public
function Code: Integer; override;
end;
operator :=(right: Boolean): TOptionalAny;
operator :=(right: Integer): TOptionalAny;
operator :=(right: String): TOptionalAny;
operator :=(right: Boolean): TOptionalBoolean;
operator :=(right: Integer): TOptionalInteger;
operator :=(right: String): TOptionalString;
implementation
{ Utilities }
operator :=(right: Boolean): TOptionalAny;
begin
result := TOptionalAny.Create(right);
end;
operator :=(right: Integer): TOptionalAny;
begin
result := TOptionalAny.Create(right);
end;
operator :=(right: String): TOptionalAny;
begin
result := TOptionalAny.Create(right);
end;
operator :=(right: Boolean): TOptionalBoolean;
begin
result := TOptionalBoolean.Create(right);
end;
operator :=(right: Integer): TOptionalInteger;
begin
result := TOptionalInteger.Create(right);
end;
operator :=(right: String): TOptionalString;
begin
result := TOptionalString.Create(right);
end;
{ TOptional }
function TOptional.GetValue: T;
begin
if fHasValue then Result := fValue
else Exception.Create('no value');
end;
procedure TOptional.SetValue(AValue: T);
begin
fValue := AValue;
fHasValue := True;
end;
procedure TOptional.Clear;
begin
fHasValue := False;
end;
{ TOptionalVariant }
function TOptionalVariant.GetValue: T;
begin
Result := T(inherited Value);
end;
procedure TOptionalVariant.SetValue(AValue: T);
begin
inherited Value := AValue;
end;
constructor TOptionalVariant.Create;
begin
inherited Create;
end;
constructor TOptionalVariant.Create(AValue: T);
begin
Create;
SetValue(AValue);
end;
{ TOptionalObject }
function TOptionalObject.GetValue: T;
begin
Result := T(inherited Value);
end;
procedure TOptionalObject.SetValue(AValue: T);
begin
inherited Value := AValue;
end;
constructor TOptionalObject.Create;
begin
inherited Create;
end;
constructor TOptionalObject.Create(AValue: T);
begin
Create;
SetValue(AValue);
end;
function TOptionalObject.ValueClass: TClass;
begin
Result := T;
end;
{ TGenericCollection }
function TGenericCollection.GetI(Index : Integer): T;
begin
Result:=T(Inherited Items[Index]);
end;
procedure TGenericCollection.SetI(Index : Integer; AValue: T);
begin
Inherited Items[Index]:=aValue;
end;
constructor TGenericCollection.Create;
begin
inherited Create(T);
end;
function TGenericCollection.Add: T;
begin
Result:=T(Inherited add);
end;
{ TLSPStreamable }
constructor TLSPStreamable.Create;
begin
Inherited Create;
end;
{ LSPException }
function EServerNotInitialized.Code: Integer;
begin
result := -32002;
end;
function EUnknownErrorCode.Code: Integer;
begin
result := -32001;
end;
function ERequestCancelled.Code: Integer;
begin
result := -32800;
end;
function EContentModified.Code: Integer;
begin
result := -32801;
end;
end.