forked from genericptr/pascal-language-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
codeUtils.pas
478 lines (419 loc) · 12.8 KB
/
codeUtils.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
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
// 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 codeUtils;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
uses
SysUtils, Classes, fpjson,
CodeTree, PascalReaderTool, PascalParserTool, IdentCompletionTool;
type
{ TIdentifierListItemHelper }
TIdentifierListItemHelper = class helper for TIdentifierListItem
function ParamPairList: string;
function ParamNameList: string;
function ParamTypeList: string;
end;
{ TLongString }
TLongString = record
private
LastPos: LongInt;
public
S: AnsiString;
procedure Clear;
procedure Rewind;
procedure Add(part: AnsiString); overload;
function Add(Part: AnsiString; Offset, Len: Integer): Boolean; overload;
function Last: Char; inline;
end;
{ TJSONSerializedArray }
type
TJSONSerializedArray = class (TJSONString)
protected
function GetAsJSON: TJSONStringType; override;
end;
{ Functions }
function GetIdentifierAtPos(Tool: TPascalReaderTool; StartPos: Longint; aSkipAmp: Boolean = true; IncludeDot: Boolean = false; IncludeOps: Boolean = false): String;
function FindIdentifierClass(Identifier: TIdentifierListItem): ShortString;
function IsNodeObjectMember(Node: TCodeTreeNode): Boolean;
function IdentifierContext(Identifier: TIdentifierListItem; out DetailString: ShortString; out ObjectMember: boolean): ShortString;
function ParseParamList(RawList: String): TStringList; overload;
function ParseParamList(RawList: String; AsSnippet: boolean): String; overload;
function ConvertBytesToHumanReadable(bytes: cardinal): ShortString;
procedure PrintIdentifierTree(Identifier: TIdentifierListItem);
implementation
function FindIdentifierClass(Identifier: TIdentifierListItem): ShortString;
var
Node: TCodeTreeNode;
begin
result := '';
Node := Identifier.Node;
while Node <> nil do
begin
if Node.Desc = ctnClass then
begin
result := Identifier.Tool.ExtractClassName(Node, false);
exit;
end;
Node := Node.Parent;
end;
end;
function IsNodeObjectMember(Node: TCodeTreeNode): Boolean;
begin
result := false;
while Node <> nil do
begin
if Node.Desc in AllClassObjects then
exit(true);
Node := Node.Parent;
end;
end;
function GetIdentifierAtPos(Tool: TPascalReaderTool; StartPos: Longint; aSkipAmp: Boolean; IncludeDot: Boolean; IncludeOps: Boolean): String;
function GetIdentifier(Identifier: PChar; aSkipAmp, IncludeDot, IncludeOps: Boolean): string;
const
OpChar = ['+', '*', '-', '/', '<', '>', '=', ':'];
IdentStartChar = ['a'..'z','A'..'Z','_'] + OpChar;
IdentChar = ['a'..'z','A'..'Z','_','0'..'9'] + OpChar;
var
len: integer;
begin
if (Identifier=nil) then begin
Result:='';
exit;
end;
if (Identifier^ in IdentStartChar) or ((Identifier^='&') and ((Identifier[1] in IdentStartChar))) then begin
len:=0;
if (Identifier^='&') then
begin
if aSkipAmp then
inc(Identifier)
else
inc(len);
end;
while (Identifier[len] in IdentChar) or (IncludeDot and (Identifier[len] = '.')) do inc(len);
SetLength(Result,len);
if len>0 then
Move(Identifier[0],Result[1],len);
end else
Result:='';
end;
var
Source: String;
begin
Source := Tool.Scanner.CleanedSrc;
Result := GetIdentifier(@Source[StartPos], aSkipAmp, IncludeDot, IncludeOps);
end;
function IdentifierContext(Identifier: TIdentifierListItem; out DetailString: ShortString; out ObjectMember: boolean): ShortString;
var
Node: TCodeTreeNode;
Container, TypeName, UnitName: ShortString;
begin
result := '';
DetailString := '';
ObjectMember := false;
if Identifier.Node = nil then
exit;
case Identifier.Node.Desc of
ctnProcedure,
ctnVarDefinition,
ctnTypeDefinition,
ctnProperty,
ctnConstDefinition,
ctnEnumerationType:
begin
TypeName := '';
UnitName := ExtractFileName(Identifier.Tool.MainFilename);
// find type for variables
if Identifier.Node.Desc = ctnVarDefinition then
begin
Node := Identifier.Tool.FindTypeNodeOfDefinition(Identifier.Node);
if Node <> nil then
TypeName := GetIdentifierAtPos(Identifier.Tool, Node.StartPos);
end;
Node := Identifier.Node;
while Node <> nil do
begin
if Node.Desc in AllClassObjects then
begin
Container := Identifier.Tool.ExtractClassName(Node, false);
if TypeName <> '' then
begin
result := TypeName+' -> '+Container;
DetailString := Container+'.'+Identifier.Identifier+': '+TypeName+' ('+UnitName+')';
end
else
begin
result := Container;
DetailString := Container+'.'+Identifier.Identifier+' ('+UnitName+')';
end;
ObjectMember := true;
exit;
end;
Node := Node.Parent;
end;
if TypeName <> '' then
result := TypeName;
// show unit name for global procedures
if Identifier.Node.Desc = ctnProcedure then
DetailString := 'Unit '+UnitName
// find type definition
else if Identifier.Node.Desc = ctnTypeDefinition then
//DetailString := Identifier.Tool.ExtractNode(Identifier.Node, [])
DetailString := 'Unit '+UnitName
// show contants values as details
else if Identifier.Node.Desc = ctnConstDefinition then
begin
//DetailString := Identifier.Tool.ExtractNode(Identifier.Node, []);
Node:=Identifier.Tool.FindTypeNodeOfDefinition(Identifier.Node);
if Node<>nil then
DetailString:=Identifier.Tool.ExtractNode(Node,[]);
end;
end;
ctnEnumIdentifier:
begin
Node := Identifier.Node;
while Node <> nil do
begin
if Node.Desc = ctnTypeDefinition then
begin
//result := Identifier.Tool.ExtractNode(Node, []);
Node := Identifier.Tool.FindTypeNodeOfDefinition(Identifier.Node);
if Node <> nil then
result := GetIdentifierAtPos(Identifier.Tool, Node.StartPos);
DetailString := 'Enum ('+result+')';
ObjectMember := true;
exit;
end;
Node := Node.Parent;
end;
end;
end;
end;
procedure PrintIdentifierTree(Identifier: TIdentifierListItem);
var
Node: TCodeTreeNode;
Details: ShortString;
ObjectMember: boolean;
begin
writeln(StdErr, Identifier.Identifier, ' -> ', IdentifierContext(Identifier, Details, ObjectMember));
Node := Identifier.Node;
while Node <> nil do
begin
writeln(StdErr, ' ', Node.DescAsString, ' children=', Node.ChildCount);
writeln(StdErr);
Node := Node.Parent;
end;
end;
{ TJSONSerializedArray }
function TJSONSerializedArray.GetAsJSON: TJSONStringType;
begin
// already serialized so return the raw string
result := GetAsString;
end;
{ TLongString }
procedure TLongString.Clear;
begin
S := '';
end;
function TLongString.Last: Char;
begin
result := S[High(S)];
end;
procedure TLongString.Rewind;
begin
SetLength(S, LastPos);
LastPos := Length(S);
end;
function TLongString.Add(Part: AnsiString; Offset, Len: Integer): Boolean;
begin
Assert(Offset + Len <= Length(Part), IntToStr(Offset)+'-'+IntToStr(Len)+' is > '+IntToStr(Length(Part)));
// zero length inserts do nothing
if (Len = 0) or ((Len - Offset) = 0) then
exit(false);
LastPos := Length(S);
SetLength(S, LastPos + Len);
Move(Part[Offset + 1], S[LastPos + 1], Len);
Result := true;
end;
procedure TLongString.Add(Part: AnsiString);
begin
LastPos := Length(S);
S += Part;
end;
{ TIdentifierListItemHelper }
function TIdentifierListItemHelper.ParamPairList: string;
var
ANode: TCodeTreeNode;
begin
Result:='';
ANode:=Node;
if (ANode<>nil) and (ANode.Desc=ctnProcedure) then begin
Result:=Tool.ExtractProcHead(ANode,
[phpWithoutClassKeyword,
phpWithoutClassName,
phpWithoutName,
phpWithParameterNames,
phpWithoutBrackets,
phpWithoutSemicolon,
phpDoNotAddSemicolon
]);
end;
end;
function TIdentifierListItemHelper.ParamTypeList: string;
var
ANode: TCodeTreeNode;
begin
Result:='';
ANode:=Node;
if (ANode<>nil) and (ANode.Desc=ctnProcedure) then begin
Result:=Tool.ExtractProcHead(ANode,
[phpWithoutClassKeyword,
phpWithoutClassName,
phpWithoutName,
phpWithoutBrackets,
phpWithoutSemicolon,
phpDoNotAddSemicolon
]);
Result:=StringReplace(Result, ',', '', []);
end;
end;
function TIdentifierListItemHelper.ParamNameList: string;
var
ANode: TCodeTreeNode;
begin
Result:='';
ANode:=Node;
if (ANode<>nil) and (ANode.Desc=ctnProcedure) then begin
Result:=Tool.ExtractProcHead(ANode,
[phpWithoutBrackets,
phpWithoutClassKeyword,
phpWithoutClassName,
phpWithoutName,
phpWithoutSemicolon,
phpDoNotAddSemicolon,
phpWithoutParamTypes,
phpWithParameterNames
]);
end;
end;
{ Functions }
function ConvertBytesToHumanReadable(bytes: cardinal): ShortString;
const
kMaxUnits = 9;
var
units: array[0..kMaxUnits - 1] of char = ('b', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
multiplier: integer = 1000; // 1024 is 10.5 and lower
exponent: integer = 0;
begin
while (bytes >= multiplier) and (exponent < kMaxUnits) do
begin
bytes := trunc(bytes / multiplier);
exponent += 1;
end;
result := IntToStr(bytes)+units[exponent];
end;
function SplitString (s: string; delimiter: char): TStringArray;
var
i: integer;
c: char;
part: string = '';
parts: TStringArray;
begin
SetLength(parts, 0);
for i := 1 to Length(s) do
begin
c := s[i];
if (c = delimiter) or (i = Length(s)) then
begin
if (i = Length(s)) then
part += c;
SetLength(parts, Length(parts) + 1);
parts[High(parts)] := part;
part := '';
end
else
part += c;
end;
result := parts;
end;
function ParseParamList(RawList: String): TStringList;
const
kPairDelimiter = ': ';
var
Text: String = '';
I, J: Integer;
Types, Names, Pair: TStringArray;
begin
Result := TStringList.Create;
// split types
Types := SplitString(RawList, ';');
for I := 0 to Length(Types) - 1 do
begin
// split name/type pair
Pair := SplitString(Types[I], ':');
if Length(Pair) <> 2 then
continue;
// split names
Names := SplitString(Pair[0], ',');
for J := 0 to Length(Names) - 1 do
Result.Add(Names[J]+kPairDelimiter+Pair[1]);
if Length(Names) > 0 then
continue;
Result.Add(Pair[0]+kPairDelimiter+Pair[1]);
end;
end;
function ParseParamList(RawList: String; AsSnippet: boolean): String;
const
kParamDelimiter = ', ';
kPairDelimiter = ': ';
var
Text: String = '';
CurrentIndex: Integer = 0;
I, J: Integer;
Types, Names, Pair: TStringArray;
begin
// split types
Types := SplitString(RawList, ';');
for I := 0 to Length(Types) - 1 do
begin
// split name/type pair
Pair := SplitString(Types[I], ':');
if Length(Pair) <> 2 then
continue;
// split names
Names := SplitString(Pair[0], ',');
for J := 0 to Length(Names) - 1 do
begin
if AsSnippet then
Text += '${'+IntToStr(CurrentIndex + 1)+':'+Names[J]+kPairDelimiter+Pair[1]+'}'+kParamDelimiter
else
Text += Names[J]+kPairDelimiter+Pair[1]+kParamDelimiter;
Inc(CurrentIndex);
end;
if Length(Names) > 0 then
continue;
if AsSnippet then
Text += '${'+IntToStr(CurrentIndex + 1)+':'+Pair[0]+kPairDelimiter+Pair[1]+'}'+kParamDelimiter
else
Text += Pair[0]+kPairDelimiter+Pair[1]+kParamDelimiter;
Inc(CurrentIndex);
end;
// trim trailing comma
Text := Trim(Text);
if (Length(Text) > 0) and (Text[Length(Text)] = ',') then
SetLength(Text, Length(Text) - 1);
Result := Text;
end;
end.