-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat.pas
More file actions
executable file
·1058 lines (956 loc) · 27.8 KB
/
Copy pathformat.pas
File metadata and controls
executable file
·1058 lines (956 loc) · 27.8 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
#!/usr/bin/env instantfpc
{$mode delphi}
{$H+}
uses
Classes,
SysUtils,
FileUtils in 'source/shared/FileUtils.pas';
type
TUnitCategory = (ucSystem, ucThirdParty, ucProject, ucRelative);
TRunMode = (rmFormat, rmCheck);
var
Mode: TRunMode;
InputFiles: TStringList;
FormattedCount: Integer;
CheckedCount: Integer;
{ ═══════════════════════════════════════════════════════════════════════════
Uses-Clause Formatting
═══════════════════════════════════════════════════════════════════════════ }
function IsUsesKeyword(const ALine: string): Boolean;
var
Trimmed: string;
begin
Trimmed := Trim(ALine);
if Length(Trimmed) < 4 then
Exit(False);
if LowerCase(Copy(Trimmed, 1, 4)) <> 'uses' then
Exit(False);
if Length(Trimmed) = 4 then
Exit(True);
Result := not (Trimmed[5] in ['A'..'Z', 'a'..'z', '0'..'9', '_']);
end;
function ClassifyUnit(const AName: string): TUnitCategory;
const
SystemUnits: array[0..20] of string = (
'classes', 'sysutils', 'generics.collections', 'generics.defaults',
'dateutils', 'strutils', 'math', 'typinfo', 'process', 'types',
'windows', 'ctypes', 'unix', 'baseunix', 'crt', 'dos', 'variants',
'syncobjs', 'contnrs', 'fgl', 'character'
);
var
Lower: string;
I: Integer;
begin
Lower := LowerCase(Trim(AName));
if Pos(' in ', Lower) > 0 then
Exit(ucRelative);
for I := Low(SystemUnits) to High(SystemUnits) do
if Lower = SystemUnits[I] then
Exit(ucSystem);
if Pos('goccia.', Lower) = 1 then
Exit(ucProject);
Result := ucThirdParty;
end;
function CompareUnitsCI(AList: TStringList; AIdx1, AIdx2: Integer): Integer;
begin
Result := CompareText(AList[AIdx1], AList[AIdx2]);
end;
function FormatUsesClause(const AUnits: TStringList): TStringList;
const
SectionCount = 4;
var
Lists: array[0..SectionCount - 1] of TStringList;
I, J, SecIdx, NonEmpty: Integer;
IsLast: Boolean;
begin
Result := TStringList.Create;
for I := 0 to SectionCount - 1 do
Lists[I] := TStringList.Create;
try
for I := 0 to AUnits.Count - 1 do
begin
case ClassifyUnit(AUnits[I]) of
ucSystem: Lists[0].Add(AUnits[I]);
ucThirdParty: Lists[1].Add(AUnits[I]);
ucProject: Lists[2].Add(AUnits[I]);
ucRelative: Lists[3].Add(AUnits[I]);
end;
end;
for I := 0 to SectionCount - 1 do
Lists[I].CustomSort(@CompareUnitsCI);
NonEmpty := 0;
for I := 0 to SectionCount - 1 do
if Lists[I].Count > 0 then
Inc(NonEmpty);
SecIdx := 0;
for I := 0 to SectionCount - 1 do
begin
if Lists[I].Count = 0 then
Continue;
Inc(SecIdx);
for J := 0 to Lists[I].Count - 1 do
begin
IsLast := (SecIdx = NonEmpty) and (J = Lists[I].Count - 1);
if IsLast then
Result.Add(' ' + Lists[I][J] + ';')
else
Result.Add(' ' + Lists[I][J] + ',');
end;
if SecIdx < NonEmpty then
Result.Add('');
end;
finally
for I := 0 to SectionCount - 1 do
Lists[I].Free;
end;
end;
function ContainsDirective(const AText: string): Boolean;
begin
Result := Pos('{$', UpperCase(AText)) > 0;
end;
function StripLineComment(const ALine: string): string;
var
I: Integer;
InStr: Boolean;
begin
I := 1;
InStr := False;
while I <= Length(ALine) do
begin
if InStr then
begin
if (ALine[I] = '''') then
begin
if (I < Length(ALine)) and (ALine[I + 1] = '''') then
begin
Inc(I, 2);
Continue;
end;
InStr := False;
end;
end
else
begin
if ALine[I] = '''' then
InStr := True
else if (ALine[I] = '/') and (I < Length(ALine)) and (ALine[I + 1] = '/') then
begin
Result := Copy(ALine, 1, I - 1);
Exit;
end
else if ALine[I] = '{' then
begin
Result := Copy(ALine, 1, I - 1);
Exit;
end;
end;
Inc(I);
end;
Result := ALine;
end;
function UpdateBlockState(const ALine: string; AInBlock: Boolean): Boolean;
var
K: Integer;
InStr: Boolean;
begin
K := 1;
InStr := False;
while K <= Length(ALine) do
begin
if AInBlock then
begin
if ALine[K] = '}' then
AInBlock := False;
end
else if InStr then
begin
if ALine[K] = '''' then
InStr := False;
end
else if ALine[K] = '''' then
InStr := True
else if ALine[K] = '{' then
AInBlock := True
else if (ALine[K] = '/') and (K < Length(ALine)) and (ALine[K + 1] = '/') then
Break;
Inc(K);
end;
Result := AInBlock;
end;
procedure FormatUsesInLines(const AInput: TStringList; const AOutput: TStringList);
var
I, J: Integer;
UsesContent, AfterUses, FullBlock, BeforeSC: string;
Units, Formatted: TStringList;
InBlock: Boolean;
begin
I := 0;
InBlock := False;
while I < AInput.Count do
begin
if InBlock then
begin
InBlock := UpdateBlockState(AInput[I], InBlock);
AOutput.Add(AInput[I]);
Inc(I);
Continue;
end;
InBlock := UpdateBlockState(AInput[I], InBlock);
if IsUsesKeyword(AInput[I]) then
begin
FullBlock := AInput[I];
J := I;
BeforeSC := StripLineComment(FullBlock);
while (Pos(';', BeforeSC) = 0) and (J + 1 < AInput.Count) do
begin
Inc(J);
FullBlock := FullBlock + #10 + AInput[J];
BeforeSC := StripLineComment(AInput[J]);
end;
if ContainsDirective(FullBlock) then
begin
while I <= J do
begin
AOutput.Add(AInput[I]);
Inc(I);
end;
Continue;
end;
AfterUses := Trim(AInput[I]);
if LowerCase(AfterUses) = 'uses' then
UsesContent := ''
else
UsesContent := Trim(Copy(AfterUses, 5, Length(AfterUses)));
J := I;
while (Pos(';', StripLineComment(UsesContent)) = 0) and (J + 1 < AInput.Count) do
begin
Inc(J);
UsesContent := UsesContent + ' ' + Trim(AInput[J]);
end;
Units := TStringList.Create;
try
while Pos(',', UsesContent) > 0 do
begin
Units.Add(Trim(Copy(UsesContent, 1, Pos(',', UsesContent) - 1)));
UsesContent := Trim(Copy(UsesContent, Pos(',', UsesContent) + 1, Length(UsesContent)));
end;
UsesContent := Trim(UsesContent);
if (Length(UsesContent) > 0) and (UsesContent[Length(UsesContent)] = ';') then
UsesContent := Trim(Copy(UsesContent, 1, Length(UsesContent) - 1));
if UsesContent <> '' then
Units.Add(UsesContent);
if Units.Count > 0 then
begin
Formatted := FormatUsesClause(Units);
try
AOutput.Add('uses');
AOutput.AddStrings(Formatted);
finally
Formatted.Free;
end;
end
else
AOutput.Add(AInput[I]);
finally
Units.Free;
end;
I := J + 1;
end
else
begin
AOutput.Add(AInput[I]);
Inc(I);
end;
end;
end;
{ ═══════════════════════════════════════════════════════════════════════════
Code Analysis Helpers
═══════════════════════════════════════════════════════════════════════════ }
function IsIdentChar(C: Char): Boolean;
begin
Result := C in ['A'..'Z', 'a'..'z', '0'..'9', '_'];
end;
function IsFuncDeclStart(const ALine: string): Boolean;
var
Trimmed: string;
begin
Trimmed := LowerCase(Trim(ALine));
Result := (Pos('function ', Trimmed) = 1) or (Pos('procedure ', Trimmed) = 1) or
(Pos('constructor ', Trimmed) = 1) or (Pos('destructor ', Trimmed) = 1) or
(Pos('class function ', Trimmed) = 1) or (Pos('class procedure ', Trimmed) = 1);
end;
function IsModifier(const AWord: string): Boolean;
var
Lower: string;
begin
Lower := LowerCase(AWord);
Result := (Lower = 'const') or (Lower = 'var') or (Lower = 'out') or (Lower = 'constref');
end;
function HasAPrefix(const AName: string): Boolean;
begin
Result := (Length(AName) > 1) and (AName[1] = 'A') and (AName[2] in ['A'..'Z']);
end;
function IsPascalCase(const AName: string): Boolean;
begin
if Length(AName) = 0 then
Exit(True);
Result := AName[1] in ['A'..'Z'];
end;
function IsPascalKeyword(const AWord: string): Boolean;
const
Keywords: array[0..14] of string = (
'as', 'at', 'do', 'if', 'in', 'is', 'of', 'on', 'or', 'to',
'and', 'end', 'for', 'not', 'set'
);
var
Lower: string;
I: Integer;
begin
Lower := LowerCase(AWord);
for I := Low(Keywords) to High(Keywords) do
if Lower = Keywords[I] then
Exit(True);
Result := False;
end;
function ExtractFuncName(const ADeclText: string): string;
var
Trimmed, NamePart: string;
SpacePos, ParenPos, DotPos, Idx: Integer;
begin
Result := '';
Trimmed := Trim(ADeclText);
if LowerCase(Copy(Trimmed, 1, 6)) = 'class ' then
Trimmed := Trim(Copy(Trimmed, 7, Length(Trimmed)));
SpacePos := Pos(' ', Trimmed);
if SpacePos = 0 then
Exit;
NamePart := Trim(Copy(Trimmed, SpacePos + 1, Length(Trimmed)));
ParenPos := Pos('(', NamePart);
if ParenPos > 0 then
NamePart := Trim(Copy(NamePart, 1, ParenPos - 1));
ParenPos := Pos(';', NamePart);
if ParenPos > 0 then
NamePart := Trim(Copy(NamePart, 1, ParenPos - 1));
ParenPos := Pos(':', NamePart);
if ParenPos > 0 then
NamePart := Trim(Copy(NamePart, 1, ParenPos - 1));
DotPos := 0;
for Idx := Length(NamePart) downto 1 do
if NamePart[Idx] = '.' then
begin
DotPos := Idx;
Break;
end;
if DotPos > 0 then
Result := Copy(NamePart, DotPos + 1, Length(NamePart))
else
Result := NamePart;
end;
function IsExternalDeclaration(const ALines: TStringList; AStartLine: Integer): Boolean;
var
DeclText: string;
K, Depth: Integer;
begin
DeclText := ALines[AStartLine];
Depth := 0;
for K := 1 to Length(DeclText) do
begin
if DeclText[K] = '(' then Inc(Depth)
else if DeclText[K] = ')' then Dec(Depth);
end;
while (Depth > 0) and (AStartLine + 1 < ALines.Count) do
begin
Inc(AStartLine);
DeclText := DeclText + ' ' + ALines[AStartLine];
for K := 1 to Length(ALines[AStartLine]) do
begin
if ALines[AStartLine][K] = '(' then Inc(Depth)
else if ALines[AStartLine][K] = ')' then Dec(Depth);
end;
end;
Result := Pos(' external ', LowerCase(DeclText)) > 0;
end;
function ReplaceWordInLine(const ALine, AOld, ANew: string;
var AInBlock: Boolean): string;
var
I, OldLen: Integer;
InStr: Boolean;
begin
Result := '';
OldLen := Length(AOld);
I := 1;
InStr := False;
while I <= Length(ALine) do
begin
if AInBlock then
begin
if ALine[I] = '}' then
AInBlock := False;
Result := Result + ALine[I];
Inc(I);
Continue;
end;
if InStr then
begin
if ALine[I] = '''' then
begin
if (I < Length(ALine)) and (ALine[I + 1] = '''') then
begin
Result := Result + ALine[I] + ALine[I + 1];
Inc(I, 2);
Continue;
end;
InStr := False;
end;
Result := Result + ALine[I];
Inc(I);
Continue;
end;
if ALine[I] = '''' then
begin
InStr := True;
Result := Result + ALine[I];
Inc(I);
Continue;
end;
if ALine[I] = '{' then
begin
if (I < Length(ALine)) and (ALine[I + 1] = '$') then
begin
Result := Result + ALine[I];
Inc(I);
Continue;
end;
AInBlock := True;
Result := Result + ALine[I];
Inc(I);
Continue;
end;
if (I < Length(ALine)) and (ALine[I] = '/') and (ALine[I + 1] = '/') then
begin
Result := Result + Copy(ALine, I, Length(ALine) - I + 1);
Exit;
end;
if (I + OldLen - 1 <= Length(ALine)) and
(CompareText(Copy(ALine, I, OldLen), AOld) = 0) then
begin
if ((I = 1) or (not IsIdentChar(ALine[I - 1]) and (ALine[I - 1] <> '.'))) and
((I + OldLen > Length(ALine)) or not IsIdentChar(ALine[I + OldLen])) then
begin
Result := Result + ANew;
Inc(I, OldLen);
Continue;
end;
end;
Result := Result + ALine[I];
Inc(I);
end;
end;
function StripCodeLine(const ALine: string; var AInBlock: Boolean): string;
var
I: Integer;
InStr: Boolean;
begin
SetLength(Result, Length(ALine));
for I := 1 to Length(ALine) do
Result[I] := ' ';
I := 1;
InStr := False;
while I <= Length(ALine) do
begin
if AInBlock then
begin
if ALine[I] = '}' then
AInBlock := False;
Inc(I);
Continue;
end;
if InStr then
begin
if ALine[I] = '''' then
begin
if (I < Length(ALine)) and (ALine[I + 1] = '''') then
begin
Inc(I, 2);
Continue;
end;
InStr := False;
end;
Inc(I);
Continue;
end;
if ALine[I] = '''' then
begin
InStr := True;
Inc(I);
Continue;
end;
if ALine[I] = '{' then
begin
AInBlock := True;
Inc(I);
Continue;
end;
if (I < Length(ALine)) and (ALine[I] = '/') and (ALine[I + 1] = '/') then
Break;
Result[I] := ALine[I];
Inc(I);
end;
end;
function CountKeywordOnLine(const AStripped, AKeyword: string): Integer;
var
Lower: string;
P, KLen: Integer;
begin
Result := 0;
Lower := LowerCase(AStripped);
KLen := Length(AKeyword);
P := 1;
while P + KLen - 1 <= Length(Lower) do
begin
if (Copy(Lower, P, KLen) = AKeyword) and
((P = 1) or not IsIdentChar(Lower[P - 1])) and
((P + KLen > Length(Lower)) or not IsIdentChar(Lower[P + KLen])) then
begin
Inc(Result);
P := P + KLen;
end
else
Inc(P);
end;
end;
function FindFuncEnd(const ALines: TStringList; ADeclEnd: Integer): Integer;
var
I, Depth: Integer;
Stripped: string;
InBlock, FoundBegin: Boolean;
begin
Result := -1;
Depth := 0;
FoundBegin := False;
InBlock := False;
for I := ADeclEnd + 1 to ALines.Count - 1 do
begin
Stripped := StripCodeLine(ALines[I], InBlock);
if not FoundBegin then
begin
if IsFuncDeclStart(Stripped) then
Exit(-1);
if (CountKeywordOnLine(Stripped, 'implementation') > 0) or
(CountKeywordOnLine(Stripped, 'type') > 0) or
(CountKeywordOnLine(Stripped, 'interface') > 0) then
Exit(-1);
end;
Depth := Depth + CountKeywordOnLine(Stripped, 'begin')
+ CountKeywordOnLine(Stripped, 'try')
+ CountKeywordOnLine(Stripped, 'case')
- CountKeywordOnLine(Stripped, 'end');
if (not FoundBegin) and (CountKeywordOnLine(Stripped, 'begin') > 0) then
FoundBegin := True;
if FoundBegin and (Depth <= 0) then
begin
Result := I;
Exit;
end;
end;
end;
function FindDeclEnd(const ALines: TStringList; ADeclStart: Integer): Integer;
var
K, Depth: Integer;
begin
Result := ADeclStart;
Depth := 0;
for K := 1 to Length(ALines[ADeclStart]) do
begin
if ALines[ADeclStart][K] = '(' then Inc(Depth)
else if ALines[ADeclStart][K] = ')' then Dec(Depth);
end;
while (Depth > 0) and (Result + 1 < ALines.Count) do
begin
Inc(Result);
for K := 1 to Length(ALines[Result]) do
begin
if ALines[Result][K] = '(' then Inc(Depth)
else if ALines[Result][K] = ')' then Dec(Depth);
end;
end;
end;
{ ═══════════════════════════════════════════════════════════════════════════
Auto-Fix: PascalCase Function Names
═══════════════════════════════════════════════════════════════════════════ }
function FixFuncNames(const ALines: TStringList): Boolean;
var
I, J, K: Integer;
FuncName, NewName: string;
OldNames, NewNames: TStringList;
InBlock: Boolean;
begin
Result := False;
OldNames := TStringList.Create;
NewNames := TStringList.Create;
try
for I := 0 to ALines.Count - 1 do
begin
if IsFuncDeclStart(ALines[I]) and not IsExternalDeclaration(ALines, I) then
begin
FuncName := ExtractFuncName(ALines[I]);
if (FuncName <> '') and not IsPascalCase(FuncName) then
begin
NewName := UpCase(FuncName[1]) + Copy(FuncName, 2, Length(FuncName));
K := OldNames.IndexOf(FuncName);
if K = -1 then
begin
OldNames.Add(FuncName);
NewNames.Add(NewName);
end;
end;
end;
end;
if OldNames.Count > 0 then
begin
Result := True;
for K := 0 to OldNames.Count - 1 do
begin
InBlock := False;
for J := 0 to ALines.Count - 1 do
ALines[J] := ReplaceWordInLine(ALines[J],
OldNames[K], NewNames[K], InBlock);
end;
end;
finally
OldNames.Free;
NewNames.Free;
end;
end;
{ ═══════════════════════════════════════════════════════════════════════════
Auto-Fix: Parameter A Prefix
═══════════════════════════════════════════════════════════════════════════ }
procedure ParseParamNames(const ADeclText: string;
const AOldNames, ANewNames: TStringList);
var
ParenStart, ParenEnd, ColonPos, Depth, K, Sp: Integer;
Inner, GroupStr, Rest, NamesStr, ParamName, FirstWord, NewName: string;
Groups: TStringList;
Ch: Char;
Current: string;
var
SemiPos: Integer;
begin
SemiPos := Pos(';', ADeclText);
ParenStart := Pos('(', ADeclText);
if (ParenStart = 0) or ((SemiPos > 0) and (ParenStart > SemiPos)) then
Exit;
Depth := 0;
ParenEnd := 0;
for K := ParenStart to Length(ADeclText) do
begin
if ADeclText[K] = '(' then Inc(Depth)
else if ADeclText[K] = ')' then
begin
Dec(Depth);
if Depth = 0 then
begin
ParenEnd := K;
Break;
end;
end;
end;
if ParenEnd = 0 then
Exit;
Inner := Trim(Copy(ADeclText, ParenStart + 1, ParenEnd - ParenStart - 1));
if Inner = '' then
Exit;
Groups := TStringList.Create;
try
Depth := 0;
Current := '';
for K := 1 to Length(Inner) do
begin
Ch := Inner[K];
if Ch in ['(', '['] then
begin
Inc(Depth);
Current := Current + Ch;
end
else if Ch in [')', ']'] then
begin
Dec(Depth);
Current := Current + Ch;
end
else if (Ch = ';') and (Depth = 0) then
begin
Groups.Add(Trim(Current));
Current := '';
end
else
Current := Current + Ch;
end;
if Trim(Current) <> '' then
Groups.Add(Trim(Current));
for K := 0 to Groups.Count - 1 do
begin
GroupStr := Trim(Groups[K]);
if GroupStr = '' then
Continue;
Rest := GroupStr;
FirstWord := '';
Sp := Pos(' ', Rest);
if Sp > 0 then
FirstWord := Copy(Rest, 1, Sp - 1);
if IsModifier(FirstWord) then
Rest := Trim(Copy(Rest, Length(FirstWord) + 1, Length(Rest)));
ColonPos := Pos(':', Rest);
if ColonPos > 0 then
NamesStr := Trim(Copy(Rest, 1, ColonPos - 1))
else
NamesStr := Trim(Rest);
while Pos(',', NamesStr) > 0 do
begin
ParamName := Trim(Copy(NamesStr, 1, Pos(',', NamesStr) - 1));
NamesStr := Trim(Copy(NamesStr, Pos(',', NamesStr) + 1, Length(NamesStr)));
if (ParamName <> 'Self') and (Length(ParamName) > 1) and
not HasAPrefix(ParamName) then
begin
NewName := 'A' + UpCase(ParamName[1]) + Copy(ParamName, 2, Length(ParamName));
if not IsPascalKeyword(NewName) and (AOldNames.IndexOf(ParamName) = -1) then
begin
AOldNames.Add(ParamName);
ANewNames.Add(NewName);
end;
end;
end;
ParamName := Trim(NamesStr);
if (ParamName <> '') and (ParamName <> 'Self') and (Length(ParamName) > 1) and
not HasAPrefix(ParamName) then
begin
NewName := 'A' + UpCase(ParamName[1]) + Copy(ParamName, 2, Length(ParamName));
if not IsPascalKeyword(NewName) and (AOldNames.IndexOf(ParamName) = -1) then
begin
AOldNames.Add(ParamName);
ANewNames.Add(NewName);
end;
end;
end;
finally
Groups.Free;
end;
end;
function FixParamNames(const ALines: TStringList): Boolean;
var
I, J, K, DeclEnd, BodyEnd: Integer;
DeclText: string;
OldNames, NewNames: TStringList;
InBlock: Boolean;
begin
Result := False;
I := 0;
while I < ALines.Count do
begin
if IsFuncDeclStart(ALines[I]) and not IsExternalDeclaration(ALines, I) then
begin
DeclEnd := FindDeclEnd(ALines, I);
DeclText := ALines[I];
for J := I + 1 to DeclEnd do
DeclText := DeclText + ' ' + Trim(ALines[J]);
OldNames := TStringList.Create;
NewNames := TStringList.Create;
try
ParseParamNames(DeclText, OldNames, NewNames);
if OldNames.Count > 0 then
begin
Result := True;
BodyEnd := FindFuncEnd(ALines, DeclEnd);
if BodyEnd = -1 then
BodyEnd := DeclEnd;
for K := 0 to OldNames.Count - 1 do
begin
InBlock := False;
for J := I to BodyEnd do
ALines[J] := ReplaceWordInLine(ALines[J],
OldNames[K], NewNames[K], InBlock);
end;
end;
finally
OldNames.Free;
NewNames.Free;
end;
I := DeclEnd + 1;
end
else
Inc(I);
end;
end;
{ ═══════════════════════════════════════════════════════════════════════════
Auto-Fix: Stray Spaces
═══════════════════════════════════════════════════════════════════════════ }
function FixStraySpaces(const ALines: TStringList): Boolean;
var
I, J, SpaceStart: Integer;
Line: string;
InStr, InLineComment, InBlockComment: Boolean;
begin
Result := False;
InBlockComment := False;
for I := 0 to ALines.Count - 1 do
begin
Line := ALines[I];
InStr := False;
InLineComment := False;
J := 1;
while J <= Length(Line) do
begin
if InLineComment then
Break;
if InStr then
begin
if Line[J] = '''' then
InStr := False;
Inc(J);
Continue;
end;
if InBlockComment then
begin
if Line[J] = '}' then
InBlockComment := False;
Inc(J);
Continue;
end;
if Line[J] = '''' then
InStr := True
else if Line[J] = '{' then
InBlockComment := True
else if (J + 1 <= Length(Line)) and (Line[J] = '/') and (Line[J + 1] = '/') then
InLineComment := True
else if (Line[J] = ' ') and (J > 1) and (Line[J - 1] <> ' ') and (not (Line[J - 1] in [#9, '(', ','])) then
begin
SpaceStart := J;
while (J + 1 <= Length(Line)) and (Line[J + 1] = ' ') do
Inc(J);
if (J + 1 <= Length(Line)) and (Line[J + 1] in [';', ')', ',']) then
begin
Delete(Line, SpaceStart, J - SpaceStart + 1);
Result := True;
J := SpaceStart;
Continue;
end;
end;
Inc(J);
end;
ALines[I] := Line;
end;
end;
{ ═══════════════════════════════════════════════════════════════════════════
File Processing
═══════════════════════════════════════════════════════════════════════════ }
function FormatFile(const AFilePath: string): Boolean;
var
Lines, ResultLines: TStringList;
begin
Result := False;
Lines := TStringList.Create;
ResultLines := TStringList.Create;
try
Lines.LoadFromFile(AFilePath);
FormatUsesInLines(Lines, ResultLines);
FixFuncNames(ResultLines);
FixParamNames(ResultLines);
FixStraySpaces(ResultLines);
if ResultLines.Text <> Lines.Text then
begin
Result := True;
if Mode = rmFormat then
ResultLines.SaveToFile(AFilePath);
end;
finally
Lines.Free;
ResultLines.Free;
end;
end;
procedure CollectProjectFiles(const ARoot: string; const AFiles: TStringList);
var
DprFiles, UnitFiles, SharedFiles, AppFiles: TStringList;
I: Integer;
BuildPas, FormatPas: string;
begin
DprFiles := FindAllFiles(ARoot + '/source/app', '.dpr');
UnitFiles := FindAllFiles(ARoot + '/source/units', '.pas');
SharedFiles := FindAllFiles(ARoot + '/source/shared', '.pas');
AppFiles := FindAllFiles(ARoot + '/source/app', '.pas');
try
for I := 0 to DprFiles.Count - 1 do
AFiles.Add(DprFiles[I]);
for I := 0 to UnitFiles.Count - 1 do
AFiles.Add(UnitFiles[I]);
for I := 0 to SharedFiles.Count - 1 do
AFiles.Add(SharedFiles[I]);
for I := 0 to AppFiles.Count - 1 do
AFiles.Add(AppFiles[I]);
BuildPas := ARoot + '/build.pas';
if FileExists(BuildPas) then
AFiles.Add(BuildPas);
FormatPas := ARoot + '/format.pas';
if FileExists(FormatPas) then
AFiles.Add(FormatPas);
finally
DprFiles.Free;
UnitFiles.Free;
SharedFiles.Free;
AppFiles.Free;
end;
AFiles.Sort;
end;
{ ═══════════════════════════════════════════════════════════════════════════
Main
═══════════════════════════════════════════════════════════════════════════ }
var
I: Integer;