-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Settings.pas
More file actions
480 lines (416 loc) · 16.5 KB
/
PasLS.Settings.pas
File metadata and controls
480 lines (416 loc) · 16.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// 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 PasLS.Settings;
{$mode objfpc}{$H+}
{$scopedenums on}
interface
uses
Classes, FGL, LSP.BaseTypes, LSP.General;
{ Global String Constants }
const
kSymbolName_Interface = 'interface';
kSymbolName_Implementation = 'implementation';
type
TOverloadPolicy = ( __UNUSED__,
Duplicates, // duplicate function names appear in the list
Ignore, // after the original definition ignore others
Suffix // add a suffix which denotes the overload count
);
{ Excludable symbol types for excludeSymbols array }
TExcludableSymbol = (
esSectionContainers, // interface/implementation section containers
esInterfaceMethodDecls, // method declarations from interface section
esImplClassDefs, // class definitions from implementation section
esFields, // class/record fields
esProperties, // class properties
esConstants, // constants (global and class constants)
esEnumMembers // enum members
);
TExcludableSymbols = set of TExcludableSymbol;
const
{ String names for JSON serialization }
ExcludableSymbolNames: array[TExcludableSymbol] of string = (
'sectionContainers',
'interfaceMethodDecls',
'implClassDefs',
'fields',
'properties',
'constants',
'enumMembers'
);
{ Helper functions for TExcludableSymbol }
function TryStrToExcludableSymbol(const S: string; out Symbol: TExcludableSymbol): Boolean;
function ExcludableSymbolToStr(Symbol: TExcludableSymbol): string;
type
TMacroMap = specialize TFPGMap<ShortString, String>;
{ TServerSettings }
TServerSettings = class(TInitializationOptions)
private
fBooleans: array[0..32] of Boolean;
fProgram: String;
{$IFDEF USE_SQLITE}
fSymbolDatabase: String;
{$ENDIF}
fFPCOptions: TStrings;
fExcludeWorkspaceFolders: TStrings;
fCodeToolsConfig: String;
fMaximumCompletions: Integer;
fOverloadPolicy: TOverloadPolicy;
fConfig: String;
fScanFilePatterns: TStrings;
fExcludeSymbols: TStrings;
fExcludedSymbolSet: TExcludableSymbols;
procedure SetFPCOptions(AValue: TStrings);
procedure SetExcludeWorkspaceFolders(AValue: TStrings);
procedure SetScanFilePatterns(AValue: TStrings);
procedure SetExcludeSymbols(AValue: TStrings);
published
// Path to the main program file for resolving references
// if not available the path of the current document will be used
property &program: String read fProgram write fProgram;
{$IFDEF USE_SQLITE}
// Path to SQLite3 database for symbols (requires USE_SQLITE compile switch)
property symbolDatabase: String read fSymbolDatabase write fSymbolDatabase;
{$ENDIF}
// FPC compiler options (passed to Code Tools)
property fpcOptions: TStrings read fFPCOptions write SetFPCOptions;
// Optional codetools.config file to load settings from
property codeToolsConfig: String read fCodeToolsConfig write fCodeToolsConfig;
// Maximum number of completion items to be returned
// if the threshold is reached then CompletionList.isIncomplete = true
property maximumCompletions: Integer read fMaximumCompletions write fMaximumCompletions;
// Policy which determines how overloaded document symbols are displayed
property overloadPolicy: TOverloadPolicy read fOverloadPolicy write fOverloadPolicy;
// procedure completions with parameters are inserted as snippets
property insertCompletionsAsSnippets: Boolean read fBooleans[0] write fBooleans[0];
// procedure completions with parameters (non-snippet) insert
// empty brackets (and insert as snippet)
property insertCompletionProcedureBrackets: Boolean read fBooleans[1] write fBooleans[1];
// workspaces folders will be added to unit paths (i.e. -Fu)
property includeWorkspaceFoldersAsUnitPaths: Boolean read fBooleans[2] write fBooleans[2];
// workspaces folders will be added to include paths (i.e. -Fi)
property includeWorkspaceFoldersAsIncludePaths: Boolean read fBooleans[3] write fBooleans[3];
// workspace folder paths to exclude from workspace path collection
property excludeWorkspaceFolders: TStrings read fExcludeWorkspaceFolders write SetExcludeWorkspaceFolders;
// syntax will be checked when file opens or saves
property checkSyntax: Boolean read fBooleans[4] write fBooleans[4];
// syntax errors will be published as diagnostics
property publishDiagnostics: Boolean read fBooleans[5] write fBooleans[5];
// enable workspace symbols
property workspaceSymbols: Boolean read fBooleans[6] write fBooleans[6];
// enable document symbols
property documentSymbols: Boolean read fBooleans[7] write fBooleans[7];
// completions contain a minimal amount of extra information
property minimalisticCompletions: Boolean read fBooleans[8] write fBooleans[8];
// syntax errors as shown in the UI with ‘window/showMessage’
property showSyntaxErrors: Boolean read fBooleans[9] write fBooleans[9];
// ignores completion items like "begin" and "var" which may interfer with IDE snippets
property ignoreTextCompletions: Boolean read fBooleans[10] write fBooleans[10];
// config file or directory to read settings from (will support multiple formats)
property config: String read fConfig write fConfig;
// File patterns for workspace scanning (array of glob patterns)
property scanFilePatterns: TStrings read fScanFilePatterns write SetScanFilePatterns;
// Check inactive regions
property checkInactiveRegions : Boolean read fBooleans[11] write fBooleans[11];
// Force flat symbol mode (SymbolInformation[]) instead of hierarchical
property flatSymbolMode: Boolean read fBooleans[12] write fBooleans[12];
// Array of symbol types to exclude from document symbols
property excludeSymbols: TStrings read fExcludeSymbols write SetExcludeSymbols;
public
constructor Create; override;
Destructor Destroy; override;
procedure ReplaceMacros(Macros: TMacroMap);
Procedure Assign(aSource : TPersistent); override;
function CanProvideWorkspaceSymbols: boolean;
function IsSymbolExcluded(Symbol: TExcludableSymbol): Boolean; inline;
class function GetPropertyDescription(const PropName: String): String;
end;
{ TConfigEnvironmentSettings }
TConfigEnvironmentSettings = class(TLSPStreamable)
Private
ffpcDir: string;
ffpcTarget: string;
ffpcTargetCPU: string;
flazarusDir: string;
fpp: string;
Public
Constructor Create; override;
Procedure Assign(aSource : TPersistent); override;
published
property fpcDir : string Read ffpcDir Write ffpcDir;
property pp : string read fpp write fpp;
property lazarusDir : string read flazarusDir write flazarusDir;
property fpcTarget : string read ffpcTarget write ffpcTarget;
property fpcTargetCPU : string read ffpcTargetCPU write ffpcTargetCPU;
end;
type
TClients = class
public const
SublimeTextLSP = 'Sublime Text LSP';
VSCode = 'vscode';
end;
Function ServerSettings: TServerSettings;
Function ClientInfo: TClientInfo;
Function EnvironmentSettings:TConfigEnvironmentSettings;
implementation
uses
SysUtils, lazUTF8;
var
_ServerSettings: TServerSettings;
_ClientInfo: TClientInfo;
_EnvironmentSettings:TConfigEnvironmentSettings;
{ TExcludableSymbol helper functions }
function TryStrToExcludableSymbol(const S: string; out Symbol: TExcludableSymbol): Boolean;
var
I: TExcludableSymbol;
begin
Result := False;
for I := Low(TExcludableSymbol) to High(TExcludableSymbol) do
if SameText(S, ExcludableSymbolNames[I]) then
begin
Symbol := I;
Exit(True);
end;
end;
function ExcludableSymbolToStr(Symbol: TExcludableSymbol): string;
begin
Result := ExcludableSymbolNames[Symbol];
end;
Function EnvironmentSettings:TConfigEnvironmentSettings;
begin
if _EnvironmentSettings=Nil then
_EnvironmentSettings:=TConfigEnvironmentSettings.Create;
Result:=_EnvironmentSettings;
end;
Function ServerSettings: TServerSettings;
begin
if _ServerSettings=Nil then
_ServerSettings:=TServerSettings.Create;
Result:=_ServerSettings;
end;
Function ClientInfo: TClientInfo;
begin
if _ClientInfo=Nil then
_ClientInfo:=TClientInfo.Create;
Result:=_ClientInfo;
end;
{ TServerSettings }
procedure TServerSettings.ReplaceMacros(Macros: TMacroMap);
function ReplaceMacro(const S: String): String;
var
I: Integer;
begin
{ supported multiple formats:
1) $macro
2) $MACRO
3) $(macro)
4) $(MACRO)
}
Result := S;
for I := 0 to Macros.Count - 1 do
begin
Result := StringReplace(Result, '$('+LowerCase(Macros.Keys[I])+')', Macros.Data[I], [rfReplaceAll]);
Result := StringReplace(Result, '$('+UpperCase(Macros.Keys[I])+')', Macros.Data[I], [rfReplaceAll]);
Result := StringReplace(Result, '$'+LowerCase(Macros.Keys[I]), Macros.Data[I], [rfReplaceAll]);
Result := StringReplace(Result, '$'+UpperCase(Macros.Keys[I]), Macros.Data[I], [rfReplaceAll]);
end;
end;
var
ExpandedOption: String;
I: integer;
begin
&program := ReplaceMacro(&program);
{$IFDEF USE_SQLITE}
symbolDatabase := ReplaceMacro(symbolDatabase);
{$ENDIF}
for I := 0 to fpcOptions.Count - 1 do
begin
ExpandedOption := ReplaceMacro(fpcOptions[I]);
fpcOptions.Delete(I);
fpcOptions.Insert(I, ExpandedOption);
end;
end;
procedure TServerSettings.Assign(aSource : TPersistent);
var
src : TServerSettings absolute aSource;
begin
if (aSource is TServerSettings) then
begin
fBooleans:=Src.FBooleans;
fProgram:=Src.fProgram;;
{$IFDEF USE_SQLITE}
SymbolDatabase:=Src.SymbolDatabase;
{$ENDIF}
FPCOptions:=Src.fpcOptions;
ExcludeWorkspaceFolders:=Src.ExcludeWorkspaceFolders;
CodeToolsConfig:=Src.CodeToolsConfig;
MaximumCompletions:=Src.MaximumCompletions;
OverloadPolicy:=Src.OverloadPolicy;
Config:=Src.Config;
ScanFilePatterns:=Src.ScanFilePatterns;
ExcludeSymbols:=Src.ExcludeSymbols;
end
else
inherited Assign(aSource);
end;
procedure TServerSettings.SetFPCOptions(AValue: TStrings);
begin
if fFPCOptions=AValue then Exit;
fFPCOptions.Assign(AValue);
end;
procedure TServerSettings.SetExcludeWorkspaceFolders(AValue: TStrings);
begin
if fExcludeWorkspaceFolders=AValue then Exit;
fExcludeWorkspaceFolders.Assign(AValue);
end;
procedure TServerSettings.SetScanFilePatterns(AValue: TStrings);
begin
if fScanFilePatterns = AValue then Exit;
fScanFilePatterns.Assign(AValue);
end;
procedure TServerSettings.SetExcludeSymbols(AValue: TStrings);
var
I: Integer;
Symbol: TExcludableSymbol;
begin
// Only copy if different object; always re-parse the set
if fExcludeSymbols <> AValue then
fExcludeSymbols.Assign(AValue);
// Parse strings into set for efficient lookup
fExcludedSymbolSet := [];
for I := 0 to fExcludeSymbols.Count - 1 do
begin
if TryStrToExcludableSymbol(fExcludeSymbols[I], Symbol) then
Include(fExcludedSymbolSet, Symbol);
// Silently ignore unrecognized values for forward compatibility
end;
end;
function TServerSettings.IsSymbolExcluded(Symbol: TExcludableSymbol): Boolean;
begin
Result := Symbol in fExcludedSymbolSet;
end;
function TServerSettings.CanProvideWorkspaceSymbols: boolean;
begin
result := workspaceSymbols;
end;
class function TServerSettings.GetPropertyDescription(const PropName: String): String;
begin
case PropName of
'program': Result := 'Path to the main program file for resolving references';
{$IFDEF USE_SQLITE}
'symbolDatabase': Result := 'Path to SQLite3 database for symbols (requires USE_SQLITE)';
{$ENDIF}
'fpcOptions': Result := 'FPC compiler options (passed to Code Tools)';
'codeToolsConfig': Result := 'Optional codetools.config file to load settings from';
'maximumCompletions': Result := 'Maximum number of completion items to be returned';
'overloadPolicy': Result := 'Policy which determines how overloaded document symbols are displayed';
'insertCompletionsAsSnippets': Result := 'Procedure completions with parameters are inserted as snippets';
'insertCompletionProcedureBrackets': Result := 'Procedure completions with parameters insert empty brackets';
'includeWorkspaceFoldersAsUnitPaths': Result := 'Workspace folders will be added to unit paths (i.e. -Fu)';
'includeWorkspaceFoldersAsIncludePaths': Result := 'Workspace folders will be added to include paths (i.e. -Fi)';
'excludeWorkspaceFolders': Result := 'Workspace folder paths to exclude from workspace path collection';
'checkSyntax': Result := 'Syntax will be checked when file opens or saves';
'publishDiagnostics': Result := 'Syntax errors will be published as diagnostics';
'workspaceSymbols': Result := 'Enable workspace symbols';
'documentSymbols': Result := 'Enable document symbols';
'minimalisticCompletions': Result := 'Completions contain a minimal amount of extra information';
'showSyntaxErrors': Result := 'Syntax errors as shown in the UI with ''window/showMessage''';
'ignoreTextCompletions': Result := 'Ignores completion items like "begin" and "var"';
'config': Result := 'Config file or directory to read settings from';
'scanFilePatterns': Result := 'File patterns for workspace scanning (array of glob patterns, e.g. ["*.pas", "*.pp"])';
'checkInactiveRegions': Result := 'Check inactive regions';
'flatSymbolMode': Result := 'Force flat symbol mode (SymbolInformation[]) instead of hierarchical';
'excludeSymbols': Result := 'Array of symbol types to exclude: sectionContainers, interfaceMethodDecls, implClassDefs, fields, properties, constants, enumMembers';
else
Result := '';
end;
end;
constructor TServerSettings.Create;
begin
inherited;
fFPCOptions := TStringList.Create;
fExcludeWorkspaceFolders := TStringList.Create;
fScanFilePatterns := TStringList.Create;
fExcludeSymbols := TStringList.Create;
fExcludedSymbolSet := [];
// default settings
{$IFDEF USE_SQLITE}
symbolDatabase := '';
{$ENDIF}
maximumCompletions := 200;
overloadPolicy := TOverloadPolicy.Suffix;
// default file patterns for workspace scanning
fScanFilePatterns.Add('*.pas');
fScanFilePatterns.Add('*.pp');
fScanFilePatterns.Add('*.p');
fScanFilePatterns.Add('*.inc');
fScanFilePatterns.Add('*.lpr');
fScanFilePatterns.Add('*.dpr');
// options
insertCompletionsAsSnippets := true;
includeWorkspaceFoldersAsUnitPaths := true;
includeWorkspaceFoldersAsIncludePaths := true;
documentSymbols := true;
ignoreTextCompletions := true;
workspaceSymbols := true;
minimalisticCompletions := false;
checkInactiveRegions := true;
// errors/diagnostics
checkSyntax := false;
publishDiagnostics := false;
showSyntaxErrors := false;
end;
destructor TServerSettings.Destroy;
begin
FreeAndNil(fFPCOptions);
FreeAndNil(fExcludeWorkspaceFolders);
FreeAndNil(fScanFilePatterns);
FreeAndNil(fExcludeSymbols);
inherited Destroy;
end;
{ TConfigEnvironmentSettings }
constructor TConfigEnvironmentSettings.Create;
procedure MaybeSet(aEnvVar : String; var aVar : String);
begin
if GetEnvironmentVariableUTF8(aEnvVar)<>'' then
aVar:=GetEnvironmentVariableUTF8(aEnvVar);
end;
begin
MaybeSet('PP',fpp);
MaybeSet('FPCDIR',ffpcDir);
MaybeSet('LAZARUSDIR',fLazarusDir);
MaybeSet('FPCTARGET',ffpcTarget);
MaybeSet('FPCTARGETCPU',ffpcTargetCPU);
end;
procedure TConfigEnvironmentSettings.Assign(aSource : TPersistent);
var
src : TConfigEnvironmentSettings absolute aSource;
begin
if aSource is TConfigEnvironmentSettings then
begin
fpcDir:=src.fpcDir;
fpcTarget:=src.fpcTarget;
fpcTargetCPU:=src.fpcTargetCPU;
lazarusDir:=src.lazarusDir;
pp:=src.pp;
end
else
inherited Assign(aSource);
end;
finalization
_ServerSettings.Free;
_ClientInfo.Free;
_EnvironmentSettings.Free;
end.