forked from coolchyni/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodetoolsutil.pas
332 lines (290 loc) · 9.29 KB
/
codetoolsutil.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
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
unit CodeToolsUtil;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, FileProcs, LazUtils, LazUtilities,
// Codetools
AVL_Tree, ExprEval,DefineTemplates,CodeToolManager,CodeCache,LinkScanner,sourcelog,
BasicCodeTools,
//pasls
inactiveRegions;
type
TCodeToolsDefinesNodeValues = class
public
Node: TDefineTemplate;
ValueParsed: boolean;
ParsedValue: string;
ExpressionCalculated: boolean;
ExpressionResult: string;
Execute: boolean;
end;
{ TCodeToolsUtil }
TCodeToolsUtil = class
private
//FDefineTree: TDefineTree;
FNodeValues: TAvlTree;
fReport: TStringList;
procedure DefineTreeCalculate({%H-}DefineTree: TDefineTree; Node: TDefineTemplate;
ValueParsed: boolean; const ParsedValue: string;
ExpressionCalculated: boolean; const ExpressionResult: string;
Execute: boolean);
protected
public
constructor Create;
destructor Destroy;override;
procedure AddCustomMacro;
procedure WriteDefinesDebugReport;
procedure WriteUnitDirectives(Code: TCodeBuffer);
procedure WriteUnitInfo(code:TCodeBuffer);
procedure CheckInactiveRegion(Code:TCodeBuffer;uri:String);
published
end;
function CompareNodeValues(Data1, Data2: Pointer): Integer;
function CompareNodeAndNodeValues(Node, NodeValues: Pointer): Integer;
var
CodeUtilBoss:TCodeToolsUtil;
implementation
function CompareNodeValues(Data1, Data2: Pointer): Integer;
begin
Result:=LazUtilities. ComparePointers(TCodeToolsDefinesNodeValues(Data1).Node,
TCodeToolsDefinesNodeValues(Data2).Node);
end;
function CompareNodeAndNodeValues(Node, NodeValues: Pointer): Integer;
begin
Result:=LazUtilities.ComparePointers(TCodeToolsDefinesNodeValues(Node),
TCodeToolsDefinesNodeValues(NodeValues).Node);
end;
{ TCodeToolsUtil }
procedure TCodeToolsUtil.DefineTreeCalculate(DefineTree: TDefineTree;
Node: TDefineTemplate; ValueParsed: boolean; const ParsedValue: string;
ExpressionCalculated: boolean; const ExpressionResult: string;
Execute: boolean);
var
NewNodeValues: TCodeToolsDefinesNodeValues;
begin
NewNodeValues:=TCodeToolsDefinesNodeValues.Create;
NewNodeValues.Node:=Node;
NewNodeValues.ValueParsed:=ValueParsed;
NewNodeValues.ParsedValue:=ParsedValue;
NewNodeValues.ExpressionCalculated:=ExpressionCalculated;
NewNodeValues.ExpressionResult:=ExpressionResult;
NewNodeValues.Execute:=Execute;
if FNodeValues=nil then
FNodeValues:=TAvlTree.Create(@CompareNodeValues);
FNodeValues.Add(NewNodeValues);
end;
constructor TCodeToolsUtil.Create;
begin
inherited Create;
end;
destructor TCodeToolsUtil.Destroy;
begin
inherited Destroy;
end;
procedure TCodeToolsUtil.AddCustomMacro;
begin
end;
procedure TCodeToolsUtil.WriteDefinesDebugReport;
// let the codetools calculate the defines for the directory
procedure AddNodeReport(Prefix: string; DefTempl: TDefineTemplate);
var
AVLNode: TAvlTreeNode;
NodeValues: TCodeToolsDefinesNodeValues;
s: string;
begin
while DefTempl <> nil do
begin
s := Prefix + 'Name="' + DefTempl.Name + '"';
s := s + ' Description="' + DefTempl.Description + '"';
s := s + ' Action="' + DefineActionNames[DefTempl.Action] + '"';
s := s + ' Variable="' + DefTempl.Variable + '"';
s := s + ' Value="' + dbgstr(DefTempl.Value) + '"';
if FNodeValues <> nil then
begin
AVLNode := FNodeValues.FindKey(DefTempl, @CompareNodeAndNodeValues);
if AVLNode <> nil then
begin
NodeValues := TCodeToolsDefinesNodeValues(AVLNode.Data);
if NodeValues.ValueParsed then
s := s + ' ParsedValue="' + dbgstr(NodeValues.ParsedValue) + '"';
if NodeValues.ExpressionCalculated then
s := s + ' ExpressionResult="' + dbgstr(NodeValues.ExpressionResult) + '"';
s := s + ' Executed="' + dbgs(NodeValues.Execute) + '"';
end;
end;
fReport.Add(s);
if DefTempl.FirstChild <> nil then
AddNodeReport(Prefix + ' ', DefTempl.FirstChild);
DefTempl := DefTempl.Next;
end;
end;
var
Dir: string;
Defines: TExpressionEvaluator;
i: integer;
OldOnCalculate: TDefTreeCalculate;
DefineTree: TDefineTree;
begin
Dir := ExpandFileName(GetCurrentDir);
DefineTree := CodeToolBoss.DefineTree;
DefineTree.ClearCache;// make sure the defines are reparsed
OldOnCalculate := DefineTree.OnCalculate;
DefineTree.OnCalculate := @DefineTreeCalculate;
try
Defines := DefineTree.GetDefinesForDirectory(Dir, False);
finally
DefineTree.OnCalculate := OldOnCalculate;
end;
fReport := TStringList.Create;
try
fReport.Add('Directory: ' + Dir);
if Defines <> nil then
begin
fReport.Add('Defines:');
for i := 0 to Defines.Count - 1 do
begin
fReport.Add(Defines.Names(i) + '=' + dbgstr(Defines.Values(i)));
end;
fReport.Add('');
end
else
begin
end;
// add all nodes to report
fReport.Add('Tree:');
AddNodeReport(' ', DefineTree.RootTemplate);
finally
DebugLn(fReport.Text);
FreeAndNil(fReport);
end;
end;
procedure TCodeToolsUtil.WriteUnitDirectives(Code: TCodeBuffer);
var
Scanner: TLinkScanner;
i: Integer;
Dir: PLSDirective;
FirstSortedIndex: integer;
LastSortedIndex: integer;
begin
if Code=nil then
exit;
// parse the unit
if not CodeToolBoss.ExploreUnitDirectives(Code,Scanner) then
raise Exception.Create('parser error');
scanner.WriteDebugReport;
DebugLn('-----------------------------------------------');
DebugLn(Scanner.CleanedSrc);
DebugLn('-----------------------------------------------');
writeln('Directives in compile order:');
for i:=0 to Scanner.DirectiveCount-1 do begin
Dir:=Scanner.Directives[i];
DebugLn([i,'/',Scanner.DirectiveCount,
' CleanPos=',Dir^.CleanPos,'=',Scanner.CleanedPosToStr(Dir^.CleanPos),
' Level=',Dir^.Level,' ',dbgs(Dir^.State),
' "',ExtractCommentContent(Scanner.CleanedSrc,Dir^.CleanPos,Scanner.NestedComments),'"']
);
end;
DebugLn('-----------------------------------------------');
DebugLn('Directives sorted for Code and SrcPos:');
for i:=0 to Scanner.DirectiveCount-1 do begin
Dir:=Scanner.DirectivesSorted[i];
DebugLn([i,'/',Scanner.DirectiveCount,
' CleanPos=',Dir^.CleanPos,'=',Scanner.CleanedPosToStr(Dir^.CleanPos),
' Level=',Dir^.Level,' ',dbgs(Dir^.State),
' "',ExtractCommentContent(Scanner.CleanedSrc,Dir^.CleanPos,Scanner.NestedComments),'"' ]
);
if Scanner.FindDirective(Code,Dir^.SrcPos,FirstSortedIndex,LastSortedIndex)
then begin
if FirstSortedIndex<LastSortedIndex then
DebugLn([' MULTIPLE: ',FirstSortedIndex,'-',LastSortedIndex]);
end else begin
raise Exception.Create('inconsistency: Scanner.FindDirective failed');
end;
DebugLn;
end;
DebugLn('-----------------------------------------------');
end;
procedure TCodeToolsUtil.WriteUnitInfo(code: TCodeBuffer);
begin
DebugLn('show unit info');
DebugLn('UnitPath:');
DebugLn(CodeToolBoss. GetUnitPathForDirectory(code.Filename));
DebugLn('IncPath:');
DebugLn(CodeToolBoss. GetIncludePathForDirectory(code.Filename));
DebugLn('SrcPath:');
DebugLn(CodeToolBoss. GetCompleteSrcPathForDirectory(code.Filename));
end;
procedure TCodeToolsUtil.CheckInactiveRegion(Code:TCodeBuffer;uri:String);
var
notify:TInactiveRegionsNotification;
regions:TRegionsItems;
input:TInputRegion;
Scanner: TLinkScanner;
i: Integer;
Dir: PLSDirective;
isAdd:Boolean;
cursorPos:Integer;
acode:Pointer;
line,col:Integer;
content:string;
begin
if Code=nil then
exit;
input:=nil;
// parse the unit
if not CodeToolBoss.ExploreUnitDirectives(Code,Scanner) then
exit;
isAdd:=False;
regions:=TRegionsItems.Create;
try
for i:=0 to Scanner.DirectiveCount-1 do begin
Dir:=Scanner.Directives[i];
if(Dir^.Code<>Pointer(Code)) then Continue;
content:=ExtractCommentContent(Scanner.CleanedSrc,Dir^.CleanPos,Scanner.NestedComments);
{$IfDef DEBUG}
DebugLn([i,'/',Scanner.DirectiveCount,
' CleanPos=',Dir^.CleanPos,'=',Scanner.CleanedPosToStr(Dir^.CleanPos),
' Level=',Dir^.Level,' ',dbgs(Dir^.State),
' "',content,'"',Dir^.Code]
);
{$EndIf}
if Dir^.State=lsdsInactive then
begin
if not isAdd then
begin
input:=TInputRegion(regions.Add);
Scanner.CleanedPosToCursor(Dir^.CleanPos,cursorPos,acode);
TSourceLog(acode).AbsoluteToLineCol(cursorPos,line,col);
input.startline:=line;
input.startCol:=col+length(content)+2;
isAdd:=True;
end;
end
else if Dir^.State=lsdsActive then
begin
if isAdd then
begin
Scanner.CleanedPosToCursor(Dir^.CleanPos,cursorPos,acode);
TSourceLog(acode).AbsoluteToLineCol(cursorPos,line,col);
input.endline:=line;
input.endCol:=col;
isAdd:=False;
end;
end;
end;
if assigned(input) and (input.endline=0) then
begin
input.endline:=999999;
end;
notify := TInactiveRegionsNotification.Create(uri,regions);
notify.Send;
notify.Free;
finally
regions.Free;
end;
end;
initialization
CodeUtilBoss:=TCodeToolsUtil.Create;
finalization
FreeAndNil(CodeUtilBoss);
end.