-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Symbols.pas
More file actions
2426 lines (2153 loc) · 74.6 KB
/
PasLS.Symbols.pas
File metadata and controls
2426 lines (2153 loc) · 74.6 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.Symbols;
{$mode objfpc}{$H+}
{define SYMBOL_DEBUG}
// Define USE_SQLITE to enable SQLite database support for workspace symbols.
// Comment out the following line to build without SQLite dependency.
{.$DEFINE USE_SQLITE}
interface
uses
{ RTL }
Classes, Contnrs, fpjson, fpjsonrpc,
{$IFDEF USE_SQLITE}
SQLite3,
{$ENDIF}
{ Code Tools }
CodeToolManager, CodeCache, CodeTree, LinkScanner,
{ Protocols }
LSP.Base, LSP.Basic, LSP.BaseTypes, LSP.DocumentSymbol,
{ Other }
PasLS.CodeUtils, LSP.Streaming, LSP.Messages;
type
{ TSymbol }
TSymbolFlag = (ForwardClassDefinition);
TSymbolFlags = set of TSymbolFlag;
{ Extra symbol container for storage }
TSymbol = class(TSymbolInformation)
private
function GetFullName: String;
public
RawJSON: String;
Flags: TSymbolFlags;
OverloadCount: integer;
function Path: String;
function IsGlobal: boolean;
property FullName: String read GetFullName;
constructor Create; overload;
end;
TSymbolItems = specialize TGenericCollection<TSymbol>;
{ TSymbolTableEntry }
TSymbolTableEntry = class
private
Key: ShortString;
Symbols: TSymbolItems;
Code: TCodeBuffer;
fRawJSON: String;
fUnloaded: Boolean;
function GetRawJSON: String; inline;
public
Modified: Boolean;
public
constructor Create(_Code: TCodeBuffer);
destructor Destroy; override;
procedure Clear;
procedure SerializeSymbols;
function AddSymbol(Name: String; Kind: TSymbolKind; FileName: String; Line, Column: Integer;EndLine,EndCol: Integer): TSymbol;
function RequestReload: boolean;
function Count: integer; inline;
property RawJSON: String read GetRawJSON;
property Unloaded: Boolean read fUnloaded write fUnloaded;
end;
{ TSymbolBuilder }
{ Dual-mode symbol builder supporting both SymbolInformation (legacy)
and DocumentSymbol (LSP 3.10+) output formats }
TSymbolMode = (
smFlat, // Output SymbolInformation[] with Class.Method naming (Lazarus style)
smHierarchical // Output DocumentSymbol[] with nested children (LSP 3.10+)
);
type
TSymbolBuilder = class
private
FMode: TSymbolMode;
FEntry: TSymbolTableEntry;
FTool: TCodeTool;
// For hierarchical mode: map className/enumName -> TDocumentSymbolEx
FClassMap: TFPHashObjectList;
FEnumMap: TFPHashObjectList;
FRootSymbols: TDocumentSymbolExItems;
// For tracking current hierarchy
FCurrentClass: TDocumentSymbolEx;
// Last added function/method (for nested function support)
FLastAddedFunction: TDocumentSymbolEx;
// For Interface/Implementation namespaces (hierarchical mode)
FInterfaceSymbol: TDocumentSymbolEx;
FImplementationSymbol: TDocumentSymbolEx;
FCurrentSectionSymbol: TDocumentSymbolEx;
function FindOrCreateClass(const AClassName: String; Node: TCodeTreeNode; IsImplementationContainer: Boolean = False): TDocumentSymbolEx;
function FindEnumByName(const AEnumName: String): TDocumentSymbolEx;
procedure SetNodeRange(Symbol: TDocumentSymbolEx; Node: TCodeTreeNode);
function GetCurrentContainer: TDocumentSymbolExItems;
function AddFlatSymbol(Node: TCodeTreeNode; const Name: String; Kind: TSymbolKind; const ContainerName: String = ''): TSymbol;
public
constructor Create(AEntry: TSymbolTableEntry; ATool: TCodeTool; AMode: TSymbolMode);
destructor Destroy; override;
// Section management (hierarchical mode)
procedure BeginInterfaceSection(Node: TCodeTreeNode);
procedure BeginImplementationSection(Node: TCodeTreeNode);
// Add symbols based on mode
function AddClass(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddMethod(Node: TCodeTreeNode; const AClassName, AMethodName: String): TSymbol;
function AddGlobalFunction(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddStruct(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddEnum(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddEnumMember(Node: TCodeTreeNode; const EnumName, MemberName: String): TSymbol;
function AddTypeAlias(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddConstant(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddVariable(Node: TCodeTreeNode; const Name: String): TSymbol;
function AddProperty(Node: TCodeTreeNode; const AClassName, APropertyName: String): TSymbol;
function AddField(Node: TCodeTreeNode; const AClassName, AFieldName: String): TSymbol;
function AddClassConstant(Node: TCodeTreeNode; const AClassName, AConstName: String): TSymbol;
// Add nested function as child of parent
function AddNestedFunction(Parent: TDocumentSymbolEx; Node: TCodeTreeNode; const Name, ParentPath: String): TDocumentSymbolEx;
// Serialization
procedure SerializeSymbols;
property Mode: TSymbolMode read FMode;
property CurrentClass: TDocumentSymbolEx read FCurrentClass write FCurrentClass;
property RootSymbols: TDocumentSymbolExItems read FRootSymbols;
property LastAddedFunction: TDocumentSymbolEx read FLastAddedFunction;
end;
{ TSymbolExtractor }
TSymbolExtractor = class
private
Code: TCodeBuffer;
Tool: TCodeTool;
Entry: TSymbolTableEntry;
Builder: TSymbolBuilder;
OverloadMap: TFPHashList;
RelatedFiles: TFPHashList;
IndentLevel: integer;
CodeSection: TCodeTreeNodeDesc;
function ShouldExcludeInterfaceDecl: Boolean;
function ShouldExcludeImplClass: Boolean;
private
procedure PrintNodeDebug(Node: TCodeTreeNode; Deep: boolean = false);
procedure AdjustEndPosition(Node: TCodeTreeNode; var EndPos: TCodeXYPosition);
function AddSymbol(Node: TCodeTreeNode; Kind: TSymbolKind): TSymbol; overload;
function AddSymbol(Node: TCodeTreeNode; Kind: TSymbolKind; Name: String; Container: String = ''): TSymbol; overload;
procedure ExtractCodeSection(Node: TCodeTreeNode);
function ExtractProcedure(ParentNode, Node: TCodeTreeNode):TSymbol;
procedure ProcessNestedFunctions(Node: TCodeTreeNode; ParentSymbol: TDocumentSymbolEx; const ParentPath: String);
procedure ExtractTypeDefinition(TypeDefNode, Node: TCodeTreeNode);
procedure ExtractObjCClassMethods(ClassNode, Node: TCodeTreeNode);
public
constructor Create(_Entry: TSymbolTableEntry; _Code: TCodeBuffer; _Tool: TCodeTool);
destructor Destroy; override;
end;
{$IFDEF USE_SQLITE}
{ TSQLiteDatabase }
TSQLiteDatabase = class
protected
Database: psqlite3;
function SingleQuery(Stat: String): boolean;
function Exec(Stat: String): boolean;
procedure LogError(errmsg: pansichar); virtual;
public
destructor Destroy; override;
end;
{ TSymbolDatabase }
TSymbolDatabase = class(TSQLiteDatabase)
private
FTransport: TMessageTransport;
protected
procedure DoLog(const Msg : String); overload;
procedure DoLog(const Fmt : String; const Args : Array of const); overload;
procedure LogError(errmsg: pansichar); override;
public
constructor Create(Path: String);
{ Symbols }
function FindAllSymbols(Path: String): TJSONSerializedArray;
function FindSymbols(Query: String): TJSONSerializedArray;
procedure ClearSymbols(Path: String);
procedure InsertSymbol(Symbol: TSymbol);
procedure InsertSymbols(Collection: TSymbolItems; StartIndex, EndIndex: Integer);
{ Files }
procedure TouchFile(Path: String);
function FileModified(Path: String): boolean;
procedure InsertFile(Path: String);
procedure RemoveFile(Path: String);
Property Transport : TMessageTransport Read FTransport Write FTransport;
end;
{$ENDIF}
{ TSymbolManager }
TSymbolManager = class
private
fTransport: TMessageTransport;
SymbolTable: TFPHashObjectList;
ErrorList: TStringList;
{$IFDEF USE_SQLITE}
fDatabase: TSymbolDatabase;
{$ENDIF}
fWorkspacePaths: TStringList;
function Load(Path: String): TCodeBuffer;
procedure AddError(Message: String);
function GetEntry(Code: TCodeBuffer): TSymbolTableEntry;
{$IFDEF USE_SQLITE}
function GetDatabase: TSymbolDatabase;
{$ENDIF}
procedure setTransport(AValue: TMessageTransport);
{$IFDEF USE_SQLITE}
property Database: TSymbolDatabase read GetDatabase;
{$ENDIF}
Protected
Procedure DoLog(const Msg : String); overload;
Procedure DoLog(const Fmt : String; const Args : Array of const); overload;
public
{ Constructors }
constructor Create;
destructor Destroy; override;
{ Searching }
function FindDocumentSymbols(Path: String): TJSONSerializedArray;
function FindWorkspaceSymbols(Query: String): TJSONSerializedArray;
function CollectSerializedSymbols(Query: String): TJSONSerializedArray;
{ Errors }
procedure ClearErrors;
{ Loading }
procedure Reload(Code: TCodeBuffer; Always: Boolean = false); overload;
procedure Reload(Path: String; Always: Boolean = false); overload;
procedure Scan(Path: String; SearchSubDirs: Boolean);
procedure FileModified(Code: TCodeBuffer);
{ File Management }
procedure RemoveFile(FileName: String);
procedure UnloadFile(FileName: String);
function IsFileUnloaded(FileName: String): Boolean;
{ Workspace Management }
property WorkspacePaths: TStringList read fWorkspacePaths;
function NormalizePath(const Path: String): String;
function IsFileInWorkspace(const FilePath: String): Boolean;
Property Transport : TMessageTransport Read fTransport Write setTransport;
end;
var
SymbolManager: TSymbolManager = nil;
// Client capabilities and configuration storage
var
ClientSupportsDocumentSymbol: Boolean = False;
function GetSymbolMode: TSymbolMode;
procedure SetClientCapabilities(SupportsDocumentSymbol: Boolean);
{ Adjusts EndPos for LSP Range specification (end position must be exclusive).
For section nodes, moves back one line to not include next section.
For other nodes, moves forward one character to make end exclusive. }
procedure AdjustEndPositionForLSP(Node: TCodeTreeNode; var EndPos: TCodeXYPosition);
implementation
uses
{ RTL }
SysUtils, FileUtil, DateUtils, fpjsonrtti,
{ Code Tools }
CodeAtom,
FindDeclarationTool, KeywordFuncLists,PascalParserTool,
{ Protocol }
PasLS.Settings;
function GetSymbolMode: TSymbolMode;
begin
// Priority 1: Explicit setting forces flat mode
if ServerSettings.flatSymbolMode then
Exit(smFlat);
// Priority 2: Auto mode based on client capability
if ClientSupportsDocumentSymbol and ServerSettings.documentSymbols then
Result := smHierarchical
else
Result := smFlat;
end;
procedure SetClientCapabilities(SupportsDocumentSymbol: Boolean);
begin
ClientSupportsDocumentSymbol := SupportsDocumentSymbol;
end;
procedure AdjustEndPositionForLSP(Node: TCodeTreeNode; var EndPos: TCodeXYPosition);
var
LineText: String;
begin
if Node.Desc in AllCodeSections then
begin
// For section nodes (interface/implementation), CodeTools EndPos points to
// the start of the NEXT section (or end of file). We need to move back
// one line so we don't include the next section's first line.
if EndPos.Y > 1 then
begin
Dec(EndPos.Y);
// Set X to end of the previous line (past last char for exclusive end)
if (EndPos.Code <> nil) and (EndPos.Y <= EndPos.Code.LineCount) then
EndPos.X := Length(EndPos.Code.GetLine(EndPos.Y - 1, false)) + 1
else
EndPos.X := 1;
end;
end
else
begin
// For non-section nodes, move EndPos one position forward to make it exclusive
if (EndPos.Code <> nil) and (EndPos.Y > 0) and (EndPos.Y <= EndPos.Code.LineCount) then
begin
LineText := EndPos.Code.GetLine(EndPos.Y - 1, false);
if EndPos.X <= Length(LineText) then
Inc(EndPos.X)
else
// EndPos.X already points past the last character on this line.
// Keep it on the same line - don't move to next line.
// This prevents single-line symbols from having range extend to next line.
EndPos.X := Length(LineText) + 1;
end;
end;
end;
function GetFileKey(Path: String): ShortString;
begin
result := ExtractFileName(Path);
end;
function IndentLevelString(level: integer): ShortString;
var
i: integer;
begin
result := '';
for i := 0 to level - 1 do
result += ' ';
end;
{ TSymbol }
function TSymbol.Path: String;
begin
Result:=Location.LocalPath;
end;
function TSymbol.IsGlobal: boolean;
begin
result := Assigned(containerName) and (containerName.Value <> '');
end;
function TSymbol.GetFullName: String;
begin
if Assigned(containerName) and (containerName.Value <> '') then
Result := containerName.Value+'.'+Name
else
Result := Name;
end;
constructor TSymbol.Create;
begin
// we need this dummy constructor for serializing
Create(nil);
end;
{ TSymbolBuilder }
constructor TSymbolBuilder.Create(AEntry: TSymbolTableEntry; ATool: TCodeTool; AMode: TSymbolMode);
begin
FEntry := AEntry;
FTool := ATool;
FMode := AMode;
FCurrentClass := nil;
if FMode = smHierarchical then
begin
FClassMap := TFPHashObjectList.Create(False); // Don't own objects - they're owned by FRootSymbols
FEnumMap := TFPHashObjectList.Create(False);
FRootSymbols := TDocumentSymbolExItems.Create;
end;
end;
destructor TSymbolBuilder.Destroy;
begin
if FMode = smHierarchical then
begin
FreeAndNil(FClassMap);
FreeAndNil(FEnumMap);
FreeAndNil(FRootSymbols);
end;
inherited;
end;
procedure TSymbolBuilder.SetNodeRange(Symbol: TDocumentSymbolEx; Node: TCodeTreeNode);
var
StartPos, EndPos, NamePos: TCodeXYPosition;
begin
if (FTool = nil) or (Symbol = nil) or (Node = nil) then
Exit;
FTool.CleanPosToCaret(Node.StartPos, StartPos);
FTool.CleanPosToCaret(Node.EndPos, EndPos);
// Use shared adjustment logic for LSP Range specification
AdjustEndPositionForLSP(Node, EndPos);
Symbol.range.SetRange(StartPos.Y - 1, StartPos.X - 1, EndPos.Y - 1, EndPos.X - 1);
// For procedure/function nodes, find the actual name position
// Node.StartPos points to keyword ("procedure"/"function"), but we need
// the name position for selectionRange to highlight correctly in editors
if Node.Desc = ctnProcedure then
begin
FTool.MoveCursorToProcName(Node, True); // True = skip className prefix
FTool.CleanPosToCaret(FTool.CurPos.StartPos, NamePos);
Symbol.selectionRange.SetRange(NamePos.Y - 1, NamePos.X - 1, NamePos.Y - 1, NamePos.X - 1 + Length(Symbol.name));
end
else
Symbol.selectionRange.SetRange(StartPos.Y - 1, StartPos.X - 1, StartPos.Y - 1, StartPos.X - 1 + Length(Symbol.name));
end;
function TSymbolBuilder.GetCurrentContainer: TDocumentSymbolExItems;
begin
// In hierarchical mode, return the current section's children if available
if (FMode = smHierarchical) and (FCurrentSectionSymbol <> nil) then
Result := TDocumentSymbolExItems(FCurrentSectionSymbol.children)
else
Result := FRootSymbols;
end;
function TSymbolBuilder.AddFlatSymbol(Node: TCodeTreeNode; const Name: String; Kind: TSymbolKind; const ContainerName: String = ''): TSymbol;
var
CodePos, EndPos: TCodeXYPosition;
begin
Result := nil;
if (FTool <> nil) and (Node <> nil) then
begin
FTool.CleanPosToCaret(Node.StartPos, CodePos);
FTool.CleanPosToCaret(Node.EndPos, EndPos);
// Adjust EndPos for LSP Range specification (end position must be exclusive)
AdjustEndPositionForLSP(Node, EndPos);
Result := FEntry.AddSymbol(Name, Kind,
CodePos.Code.FileName,
CodePos.Y, CodePos.X,
EndPos.Y, EndPos.X);
// Set containerName for LSP semantics in workspace/symbol
if (Result <> nil) and (ContainerName <> '') then
Result.containerName := TOptionalString.Create(ContainerName);
end;
end;
procedure TSymbolBuilder.BeginInterfaceSection(Node: TCodeTreeNode);
begin
if FMode <> smHierarchical then
Exit;
// Create interface namespace symbol
FInterfaceSymbol := TDocumentSymbolEx.Create(FRootSymbols);
FInterfaceSymbol.name := kSymbolName_Interface;
FInterfaceSymbol.kind := TSymbolKind._Namespace;
SetNodeRange(FInterfaceSymbol, Node);
FCurrentSectionSymbol := FInterfaceSymbol;
end;
procedure TSymbolBuilder.BeginImplementationSection(Node: TCodeTreeNode);
begin
if FMode <> smHierarchical then
Exit;
// Create implementation namespace symbol
FImplementationSymbol := TDocumentSymbolEx.Create(FRootSymbols);
FImplementationSymbol.name := kSymbolName_Implementation;
FImplementationSymbol.kind := TSymbolKind._Namespace;
SetNodeRange(FImplementationSymbol, Node);
FCurrentSectionSymbol := FImplementationSymbol;
end;
function TSymbolBuilder.FindOrCreateClass(const AClassName: String; Node: TCodeTreeNode; IsImplementationContainer: Boolean = False): TDocumentSymbolEx;
var
Container: TDocumentSymbolExItems;
Key: String;
begin
Result := nil;
if FMode <> smHierarchical then
Exit;
// F1 Scheme: Classes exist in both Interface and Implementation namespaces
// Use section-specific key to distinguish between interface declaration and implementation methods
// Note: Must check for nil first, otherwise nil = nil is True for program files
if (FInterfaceSymbol <> nil) and (FCurrentSectionSymbol = FInterfaceSymbol) then
Key := 'interface.' + AClassName
else if (FImplementationSymbol <> nil) and (FCurrentSectionSymbol = FImplementationSymbol) then
Key := 'implementation.' + AClassName
else
begin
// Program files: distinguish between declaration and implementation container
if IsImplementationContainer then
Key := AClassName + '.impl'
else
Key := AClassName;
end;
// Check if class already exists in current section
Result := TDocumentSymbolEx(FClassMap.Find(Key));
if Result = nil then
begin
// Create class in current section's namespace
Container := GetCurrentContainer;
Result := TDocumentSymbolEx.Create(Container);
Result.name := AClassName;
Result.kind := TSymbolKind._Class;
// Set ranges using the node
if Node <> nil then
SetNodeRange(Result, Node);
// Add reference to class map for lookup with section-specific key
FClassMap.Add(Key, Result);
end;
end;
function TSymbolBuilder.FindEnumByName(const AEnumName: String): TDocumentSymbolEx;
begin
Result := nil;
if FMode <> smHierarchical then
Exit;
Result := TDocumentSymbolEx(FEnumMap.Find(AEnumName));
end;
function TSymbolBuilder.AddClass(Node: TCodeTreeNode; const Name: String): TSymbol;
begin
case FMode of
smFlat:
begin
// Flat mode: add class to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Class);
end;
smHierarchical:
begin
// Hierarchical mode: Create class in current section's namespace
// - Interface section: class declaration
// - Implementation section: class with method implementations (rare)
FCurrentClass := FindOrCreateClass(Name, Node);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Class);
end;
end;
end;
function TSymbolBuilder.AddMethod(Node: TCodeTreeNode; const AClassName, AMethodName: String): TSymbol;
var
ClassSymbol: TDocumentSymbolEx;
MethodSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: Class.Method naming, no containerName (Lazarus style)
Result := AddFlatSymbol(Node, AClassName + '.' + AMethodName, TSymbolKind._Method);
end;
smHierarchical:
begin
// Hierarchical mode: Add method as child of class
ClassSymbol := FindOrCreateClass(AClassName, nil, True);
if ClassSymbol <> nil then
begin
MethodSymbol := TDocumentSymbolEx.Create(ClassSymbol.children);
MethodSymbol.name := AMethodName;
MethodSymbol.kind := TSymbolKind._Method;
SetNodeRange(MethodSymbol, Node);
FLastAddedFunction := MethodSymbol;
// Initialize or extend class range to include method
if (ClassSymbol.range.start.line = 0) and (ClassSymbol.range.&end.line = 0) then
begin
// First method - initialize class range
ClassSymbol.range.start.line := MethodSymbol.range.start.line;
ClassSymbol.range.start.character := MethodSymbol.range.start.character;
ClassSymbol.range.&end.line := MethodSymbol.range.&end.line;
ClassSymbol.range.&end.character := MethodSymbol.range.&end.character;
ClassSymbol.selectionRange := ClassSymbol.range;
end
else
begin
// Extend class range to include this method
if MethodSymbol.range.start.line < ClassSymbol.range.start.line then
begin
ClassSymbol.range.start.line := MethodSymbol.range.start.line;
ClassSymbol.range.start.character := MethodSymbol.range.start.character;
end;
if MethodSymbol.range.&end.line > ClassSymbol.range.&end.line then
begin
ClassSymbol.range.&end.line := MethodSymbol.range.&end.line;
ClassSymbol.range.&end.character := MethodSymbol.range.&end.character;
end;
end;
end;
// Add to flat symbol list for database/workspace symbol with LSP semantics
// name=MethodName, containerName=ClassName
Result := AddFlatSymbol(Node, AMethodName, TSymbolKind._Method, AClassName);
end;
end;
end;
function TSymbolBuilder.AddGlobalFunction(Node: TCodeTreeNode; const Name: String): TSymbol;
var
GlobalSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add function to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Function);
end;
smHierarchical:
begin
// Hierarchical mode: Add to current container (Interface or Implementation namespace)
GlobalSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
GlobalSymbol.name := Name;
GlobalSymbol.kind := TSymbolKind._Function;
SetNodeRange(GlobalSymbol, Node);
FLastAddedFunction := GlobalSymbol;
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Function);
end;
end;
end;
function TSymbolBuilder.AddStruct(Node: TCodeTreeNode; const Name: String): TSymbol;
var
StructSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add struct to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Struct);
end;
smHierarchical:
begin
// Hierarchical mode: Add struct to current container
StructSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
StructSymbol.name := Name;
StructSymbol.kind := TSymbolKind._Struct;
SetNodeRange(StructSymbol, Node);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Struct);
end;
end;
end;
function TSymbolBuilder.AddEnum(Node: TCodeTreeNode; const Name: String): TSymbol;
var
EnumSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add enum to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Enum);
end;
smHierarchical:
begin
// Hierarchical mode: Add enum to current container
EnumSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
EnumSymbol.name := Name;
EnumSymbol.kind := TSymbolKind._Enum;
SetNodeRange(EnumSymbol, Node);
// Register enum for later member lookup
FEnumMap.Add(Name, EnumSymbol);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Enum);
end;
end;
end;
function TSymbolBuilder.AddEnumMember(Node: TCodeTreeNode; const EnumName, MemberName: String): TSymbol;
var
EnumSymbol, MemberSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: EnumName.MemberName naming
Result := AddFlatSymbol(Node, EnumName + '.' + MemberName, TSymbolKind._EnumMember);
end;
smHierarchical:
begin
// Hierarchical mode: add member to enum's children
EnumSymbol := FindEnumByName(EnumName);
if EnumSymbol <> nil then
begin
MemberSymbol := TDocumentSymbolEx.Create(EnumSymbol.children);
MemberSymbol.name := MemberName;
MemberSymbol.kind := TSymbolKind._EnumMember;
SetNodeRange(MemberSymbol, Node);
end;
// Add to flat symbol list with container
Result := AddFlatSymbol(Node, MemberName, TSymbolKind._EnumMember, EnumName);
end;
end;
end;
function TSymbolBuilder.AddTypeAlias(Node: TCodeTreeNode; const Name: String): TSymbol;
var
TypeSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add type alias to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._TypeParameter);
end;
smHierarchical:
begin
// Hierarchical mode: Add type alias to current container
TypeSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
TypeSymbol.name := Name;
TypeSymbol.kind := TSymbolKind._TypeParameter;
SetNodeRange(TypeSymbol, Node);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._TypeParameter);
end;
end;
end;
function TSymbolBuilder.AddConstant(Node: TCodeTreeNode; const Name: String): TSymbol;
var
ConstSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add constant to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Constant);
end;
smHierarchical:
begin
// Hierarchical mode: Add constant to current container
ConstSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
ConstSymbol.name := Name;
ConstSymbol.kind := TSymbolKind._Constant;
SetNodeRange(ConstSymbol, Node);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Constant);
end;
end;
end;
function TSymbolBuilder.AddVariable(Node: TCodeTreeNode; const Name: String): TSymbol;
var
VarSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: add variable to Entry.Symbols
Result := AddFlatSymbol(Node, Name, TSymbolKind._Variable);
end;
smHierarchical:
begin
// Hierarchical mode: Add variable to current container
VarSymbol := TDocumentSymbolEx.Create(GetCurrentContainer);
VarSymbol.name := Name;
VarSymbol.kind := TSymbolKind._Variable;
SetNodeRange(VarSymbol, Node);
// Also add to flat symbol list for database/workspace symbol
Result := AddFlatSymbol(Node, Name, TSymbolKind._Variable);
end;
end;
end;
function TSymbolBuilder.AddProperty(Node: TCodeTreeNode; const AClassName, APropertyName: String): TSymbol;
var
ClassSymbol: TDocumentSymbolEx;
PropertySymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: Class.Property naming, no containerName (Lazarus style)
Result := AddFlatSymbol(Node, AClassName + '.' + APropertyName, TSymbolKind._Property);
end;
smHierarchical:
begin
// Hierarchical mode: add property to class's children
ClassSymbol := FindOrCreateClass(AClassName, Node);
if ClassSymbol <> nil then
begin
PropertySymbol := TDocumentSymbolEx.Create(ClassSymbol.children);
PropertySymbol.name := APropertyName;
PropertySymbol.kind := TSymbolKind._Property;
SetNodeRange(PropertySymbol, Node);
end;
// Add to flat symbol list with LSP semantics
Result := AddFlatSymbol(Node, APropertyName, TSymbolKind._Property, AClassName);
end;
end;
end;
function TSymbolBuilder.AddField(Node: TCodeTreeNode; const AClassName, AFieldName: String): TSymbol;
var
ClassSymbol: TDocumentSymbolEx;
FieldSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: Class.Field naming, no containerName (Lazarus style)
Result := AddFlatSymbol(Node, AClassName + '.' + AFieldName, TSymbolKind._Field);
end;
smHierarchical:
begin
// Hierarchical mode: add field to class's children
ClassSymbol := FindOrCreateClass(AClassName, Node);
if ClassSymbol <> nil then
begin
FieldSymbol := TDocumentSymbolEx.Create(ClassSymbol.children);
FieldSymbol.name := AFieldName;
FieldSymbol.kind := TSymbolKind._Field;
SetNodeRange(FieldSymbol, Node);
end;
// Add to flat symbol list with LSP semantics
Result := AddFlatSymbol(Node, AFieldName, TSymbolKind._Field, AClassName);
end;
end;
end;
function TSymbolBuilder.AddClassConstant(Node: TCodeTreeNode;
const AClassName, AConstName: String): TSymbol;
var
ClassSymbol: TDocumentSymbolEx;
ConstSymbol: TDocumentSymbolEx;
begin
case FMode of
smFlat:
begin
// Flat mode: Class.Const naming, no containerName (Lazarus style)
Result := AddFlatSymbol(Node, AClassName + '.' + AConstName, TSymbolKind._Constant);
end;
smHierarchical:
begin
// Hierarchical mode: add constant to class's children
ClassSymbol := FindOrCreateClass(AClassName, Node);
if ClassSymbol <> nil then
begin
ConstSymbol := TDocumentSymbolEx.Create(ClassSymbol.children);
ConstSymbol.name := AConstName;
ConstSymbol.kind := TSymbolKind._Constant;
SetNodeRange(ConstSymbol, Node);
end;
// Add to flat symbol list with LSP semantics
Result := AddFlatSymbol(Node, AConstName, TSymbolKind._Constant, AClassName);
end;
end;
end;
function TSymbolBuilder.AddNestedFunction(Parent: TDocumentSymbolEx; Node: TCodeTreeNode; const Name, ParentPath: String): TDocumentSymbolEx;
var
FullPath: String;
begin
Result := nil;
FullPath := ParentPath + '.' + Name;
case FMode of
smFlat:
begin
// Flat mode: full path name, no containerName (Lazarus style)
AddFlatSymbol(Node, FullPath, TSymbolKind._Function);
end;
smHierarchical:
begin
if Parent = nil then
Exit;
// Create nested function as child of parent
Result := TDocumentSymbolEx.Create(Parent.children);
Result.name := Name;
Result.kind := TSymbolKind._Function;
SetNodeRange(Result, Node);
// Add to flat symbol list with LSP semantics
// name=NestedName, containerName=ParentPath
AddFlatSymbol(Node, Name, TSymbolKind._Function, ParentPath);
end;
end;
end;
procedure TSymbolBuilder.SerializeSymbols;
const
BATCH_COUNT = 1000;
var
SerializedItems: TJSONArray;
i, Start, Next, Total: Integer;
Symbol: TSymbol;
begin
case FMode of
smFlat:
begin
// Use existing serialization for flat SymbolInformation[]
FEntry.SerializeSymbols;
end;
smHierarchical:
begin
// Serialize DocumentSymbol hierarchy for textDocument/documentSymbol
SerializedItems := specialize TLSPStreaming<TDocumentSymbolExItems>.ToJSON(FRootSymbols) as TJSONArray;
try
FEntry.fRawJSON := SerializedItems.AsJSON;
finally
SerializedItems.Free;
end;
// Serialize flat SymbolInformation[] for database insertion and workspace/symbol
SerializedItems := specialize TLSPStreaming<TSymbolItems>.ToJSON(FEntry.Symbols) as TJSONArray;
try
// Set RawJSON for each symbol (needed for database insertion)
for i := 0 to SerializedItems.Count - 1 do
begin
Symbol := FEntry.Symbols.Items[i];
Symbol.RawJSON := SerializedItems[i].AsJson;
end;
// Insert symbols into database if available
{$IFDEF USE_SQLITE}
if SymbolManager.Database <> nil then
begin
Next := 0;
Start := 0;
Total := SerializedItems.Count;
while Start < Total do
begin
Next := Start + BATCH_COUNT;
if Next >= Total then
Next := Total - 1;
SymbolManager.Database.InsertSymbols(FEntry.Symbols, Start, Next);
Start := Next + 1;
end;
end;
{$ENDIF}