-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoRefactorFuncLib.pkg
More file actions
4451 lines (4112 loc) · 220 KB
/
Copy pathoRefactorFuncLib.pkg
File metadata and controls
4451 lines (4112 loc) · 220 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
Use cRefactorFuncLib.pkg
//Use SupplementalRefactoringClasses.pkg
// *** Refactor Function Library ***
// The object below contains all public refactoring functions.
/* RULES FOR USING META-TAGS:
There are four parameters accepted by the procdure RegisterInterface that is used to
collect info about a function when the program starts. The first one
is a handle that cannot be used for our purposes. The second one is the function name.
The third contains the types and names of the parameters and return type for the function.
That leaves only one more RegisterInterface param we can use, and that is the "sComment".
The { Description } meta-tag content is what ends up in the sComment parameter.
As we need to handle more than four parameters, the RegisterInterface has been made in
such a way that other meta-tags embedded within the { Description } meta-tag, are
parsed and moved to various struct members.
Important Note: For the { Description... meta-tag Aligned Multiline Strings are used
That is strings that starts and ended with a """ (triple-quote character).
Embedded in { Description } meta-tags, *other* meta-tags are being used
to describe other characterestica for a function. Those are all parts
that are parsed by the RegisterInterface class procedure.
Studio Note: The Studio (DF 25) gets very confused when tripple quotes are used with the
{ Description } meta-tag. Both the Code Explorer and Code Completions
stops working. Also the Problem Resolution panel shows issues for
text inside the meta-tag.
It is therefor advised to edit and test new refactor functions in
another package, and then publish it here.
The {Published} meta-tag must be set to True for a function to show up at all in function
lists of the running program.
Sample:
{ Published = True }
The { Description } meta-tag is used as a compound tag, as it will contain other meta-tags.
As described above it starts with a tripple quote, and ends with a tripple quote symbol.
Sample:
{ Description = """
Some text that can span
several lines, yada-yada-yada.
""" }
The { MethodType } tag describes the type of function, as this is vital for the
refactoring engine to know how a function should be called. It must be one of the members
from this list;
eStandardFunction - One source line at a time will be passed for these functions.
eRemoveFunction - One source line at a time will be passed for these functions.
eEditorFunction - A source file as a string array will be passed
eReportFunction - A source file as a string array. Makes no source changes.
eReportFunctionAll - Makes no source changes.
eOtherFunction - A source file as a string array will be passed.
eOtherFunctionAll - All selected files as a string array will be passed.
Sample:
{ MethodType = eStandardFunction }
{ SummaryText } A short description text that is used by the cRefactorEngine post run report summary.
Sample:
{ SummaryText = Split line If/Else line }
When a refactoring function uses parameters for the "sParameter" param, possible
values are entered in an { EnumList } meta-tag.
If an { EnumList } meta-tag is used, it needs to be followed by two other meta-tags;
{ InitialValue } This is the default value from the EnumList, and
{ ParamHelp } This will be the help text presented to the user when hovering the mouse
over the grid column item. This needs to be the last item.
Write '\n' to insert a carrige return characters to line up ParamHelp
text.
Sample:
{ EnumList = "eSplitBySpaceAndSemicolon, eSplitBySemicolon, eSplitToBeginEndBlock" }
{ InitialValue = eSplitToBeginEndBlock }
{ ParamHelp = Valid Values;
\neSplitBySpaceAndSemicolon - Split line with space and semicolon,
\neSplitBySemicolon - Split line with semicolon,
\neSplitToBeginEndBlock - Use a Begin/End block }
""" }
*/
// Warning: Do not change the following object line as it is searched for,
// by the cExportImportFunctions logic.
Object oRefactorFuncLib is a cRefactorFuncLib
/* *** User defined functions are included here ***
Use the file to add your own special refactoring function(s) that are particular to
a special code base/project, and has no general interest.
*/
#Include UserDefinedRefactorFunctions.pkg
/* *** General Purpose Refactoring Functions ***
These are the pre-made refactoring functions
delivered with the workspace. If there is a need
to add private functions that is of no public
interest, add them to thís package:
UserDefinedRefactorFunctions.pkg
If however, new functions are developed that could
be of public interest, please use the TestBench's
'JSON Export/Import facility to export new functions
and email the JSON file to: support@rdctools.com
There must be at least *one blank line* between each
function. The blank lines are used by the
cExportImportFunctions logic when scanning the source code.
*/
{ Published = False }
{ Description = """
Note: ALWAYS SELECT THIS FUNCTION, PRIOR USAGE OF OTHER REFACTORING FUNCTIONS!
Else other refactoring functions will not work properly.
Inserts a missing space between a closing parenthesis/bracket and a keyword.
Example: ))Begin -> )) Begin or ]Set -> ] Set
The DataFlex compiler allows this, but the missing space causes refactoring
functions to fail, because the tokenizer cannot split the tokens correctly.
{ MethodType = eStandardFunction }
{ SummaryText = ALWAYS USE WITH THE FIRST RUN IN A WORKSPACE! Adds a missing space (blank) after a ')' or a ']', and before a keyword }
""" }
Function A_InsertSpaceAfterParenthesis String ByRef sLine String sParameter Returns Boolean
String sChar sText
Integer iPos iAsc iIndent
Boolean bChanged
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True) Begin
Function_Return False
End
// Work on the original sLine directly, scanning for ')' followed by a letter or underscore.
// Use a simple forward search with Pos, starting after the last match each time.
// We track indentation to know where code starts, but scan the full sLine.
Move False to bChanged
Move (Pos(")", sLine)) to iPos
While (iPos <> 0 and iPos < Length(sLine))
Move (Mid(sLine, 1, (iPos + 1))) to sChar
Move (Ascii(sChar)) to iAsc
// A-Z = 65-90, a-z = 97-122, _ = 95
If ((iAsc >= 65 and iAsc <= 90) or (iAsc >= 97 and iAsc <= 122) or (iAsc = 95)) Begin
Move (Insert(CS_S, sLine, (iPos + 1))) to sLine
Move True to bChanged
End
// Search for next ')' after current position
Move (Length(sLine)) to iIndent
If ((iPos + 1) < iIndent) Begin
Move (Mid(sLine, (iIndent - iPos), (iPos + 1))) to sText
Move (Pos(")", sText)) to iIndent
If (iIndent <> 0) Begin
Move (iPos + iIndent) to iPos
End
Else Begin
Move 0 to iPos
End
End
Else Begin
Move 0 to iPos
End
Loop
// Also handle ']' (closing square bracket) followed immediately by a letter or underscore.
// Example: [Found ]Set -> [Found ] Set
Move (Pos("]", sLine)) to iPos
While (iPos <> 0 and iPos < Length(sLine))
Move (Mid(sLine, 1, (iPos + 1))) to sChar
Move (Ascii(sChar)) to iAsc
// A-Z = 65-90, a-z = 97-122, _ = 95
If ((iAsc >= 65 and iAsc <= 90) or (iAsc >= 97 and iAsc <= 122) or (iAsc = 95)) Begin
Move (Insert(CS_S, sLine, (iPos + 1))) to sLine
Move True to bChanged
End
// Search for next ']' after current position
Move (Length(sLine)) to iIndent
If ((iPos + 1) < iIndent) Begin
Move (Mid(sLine, (iIndent - iPos), (iPos + 1))) to sText
Move (Pos("]", sText)) to iIndent
If (iIndent <> 0) Begin
Move (iPos + iIndent) to iPos
End
Else Begin
Move 0 to iPos
End
End
Else Begin
Move 0 to iPos
End
Loop
Function_Return bChanged
End_Function
{ Published = True }
{ Description = """
Changes legacy Calc/MoveInt/MoveStr/MoveNum/MoveReal -> 'Move' command.
The legacy commands compiles but does not work with the Studio's statement completion.
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: Calc/MoveInt/MoveStr/MoveReal -> Move }
{ ModRecipe = 4533 | command: Calc }
""" }
Function ChangeCalcToMoveStatement String ByRef sLine String sParameter Returns Boolean
Boolean bChanged bFound
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
// Check if line contains one of the keywords and that none of the keywords are within quotes.
Get IsKeywordInLine "calc|moveint|movenum|movereal|movestr" TokenizerData.asLineTokens to bFound
If (bFound = False) Begin
Function_Return False
End
Move False to bChanged
If (bChanged = False) Get _SubstituteCalcKeyword (&sLine) "calc" 4 to bChanged
If (bChanged = False) Get _SubstituteCalcKeyword (&sLine) "moveint" 7 to bChanged
If (bChanged = False) Get _SubstituteCalcKeyword (&sLine) "movenum" 7 to bChanged
If (bChanged = False) Get _SubstituteCalcKeyword (&sLine) "movereal" 8 to bChanged
If (bChanged = False and TokenizerData.bIsMoveStatement = False) Begin
Get _SubstituteCalcKeyword (&sLine) "movestr" 7 to bChanged
End
Function_Return bChanged
End_Function
{ Published = True }
{ Description = """
Changes legacy Current_Object -> Self
Replaces the legacy keyword 'Current_Object' with 'Self'
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: Legacy Current_Object -> Self }
""" }
Function ChangeCurrent_ObjectToSelf String ByRef sLine String sParameter Returns Boolean
String sChar
Boolean bFound
Integer iPos iItem
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Move (SearchArray(CS_Current_Object, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem = -1) Begin
Move (Pos(Lowercase(CS_Current_Object), Lowercase(sLine))) to iPos
If (iPos <> 0) Begin
Move (Mid(sLine, 1, (iPos -1))) to sChar
If (sChar <> "(") Begin
Function_Return False
End
End
Else Begin
Function_Return False
End
End
Get ReplaceLineToken TokenizerData sLine CS_Current_Object CS_Self to sLine
// Note! We call ourself recursively if there are more than one "current_object".
Move (Pos(Lowercase(CS_Current_Object), Lowercase(sLine))) to iPos
If (iPos <> 0) Begin
Get ChangeCurrent_ObjectToSelf (&sLine) "" to bFound
End
Function_Return True
End_Function
{ Published = True }
{ Description = """
Replaces legacy DfTrue and DFFalse with True or False
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = DFTrue -> True }
""" }
Function ChangeDfTrueDfFalse String ByRef sLine String sParameter Returns Boolean
Boolean bFound
String sNewBoolean
Integer iPos
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
// Note: The sKeywords string must be separated by "|" _and_ end with a "|".
Get IsKeywordInLine (CS_DfTrue + CS_D + CS_DfFalse + CS_D) TokenizerData.asLineTokens to bFound
If (bFound = False) Begin
Function_Return False
End
Move (Trim(sLine)) to sLine
Repeat
Move CS_True to sNewBoolean
Move (Pos(Lowercase(CS_DfTrue), Lowercase(TokenizerData.sOverstrikeLine))) to iPos
If (iPos = 0) Begin
Move (Pos(Lowercase(CS_DfFalse), Lowercase(TokenizerData.sOverstrikeLine))) to iPos
Move CS_False to sNewBoolean
End
If (iPos <> 0) Begin
Move (Overstrike("||" + sNewBoolean, sLine, iPos)) to sLine
Move (Replace("||", sLine, "")) to sLine
Move (Overstrike("||" + sNewBoolean, TokenizerData.sOverstrikeLine, iPos)) to TokenizerData.sOverstrikeLine
Move (Replace("||", TokenizerData.sOverstrikeLine, "")) to TokenizerData.sOverstrikeLine
End
Until (iPos = 0)
Move (TokenizerData.sIndentation + String(sLine)) to sLine
Function_Return True
End_Function
{ Published = True }
{ Description = """
Replaces legacy GetAddress command with function AddressOf.
Example: GetAddress of sVal to aAddress --> Move (AddressOf(sVal)) to aAddress
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = GetAddress -> (AddressOf( }
{ ModRecipe = 4533 | command: GetAddress }
""" }
Function ChangeGetAddress String ByRef sLine String sParameter Returns Boolean
Boolean bFound
tTokenizer TokenizerData
tToken Token
Get pTokenizer to TokenizerData
// Deliberately NO bIsInFunction/bIsInProcedure guard here - those are the
// lexer's sticky inside-a-body scope flags, and a GetAddress command lives
// almost exclusively inside method bodies. The old guard disabled the
// function on every real occurrence (cWinFunc.pkg was never changed), while
// the single-line fixtures carry no scope, so the tests stayed green.
If (TokenizerData.bLineIsReady = True or TokenizerData.bHasEndSemiColon = True or ;
TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Get IsKeywordInLine (CS_GetAddress + CS_D) TokenizerData.asLineTokens to bFound
If (bFound = False) Begin
Function_Return False
End
// What DOES need protecting is a line that merely MENTIONS GetAddress - a
// declaration ("Function GetAddress Returns String") or an expression use
// ("Entry_Item (GetAddress(...))"). Require the COMMAND position, like
// ChangeSysdate4. FirstLeftCommand ONLY: an If-prefixed nose form is left
// untouched on purpose - the rebuild below is whole-line and would lose it.
Move TokenizerData.FirstLeftCommand to Token
If (Lowercase(Token.sCode) <> Lowercase(CS_GetAddress)) Begin
Function_Return False
End
// The rebuild needs both sides. The no-"of" variant ("GetAddress sVal to
// pVal") leaves OfStatement empty and would emit a broken
// "Move (AddressOf())" - leave that shape untouched.
If (TokenizerData.OfStatement.sCode = "" or TokenizerData.ToStatement.sCode = "") Begin
Function_Return False
End
If (TokenizerData.sEndComment <> "") Begin
Move (TokenizerData.sIndentation + CS_Move * "(" + CS_AddressOf + "(" + TokenizerData.OfStatement.sCode + "))" * CS_To * String(TokenizerData.ToStatement.sCode) * String(TokenizerData.sEndComment)) to sLine
End
Else Begin
Move (TokenizerData.sIndentation + CS_Move * "(" + CS_AddressOf + "(" + TokenizerData.OfStatement.sCode + "))" * CS_To * String(TokenizerData.ToStatement.sCode)) to sLine
End
Function_Return True
End_Function
{ Published = True }
{ Description = """
Changes command "IFNOT" into an expression
Example: IFNOT Found Begin --> If (Not(Found)) Begin
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = IFNOT command -> If (not('indicator')) }
""" }
Function ChangeIfNotCommandToExpression String ByRef sLine String sParameter Returns Boolean
tTokenizer TokenizerData
Integer iItem iPos iSize iCount
String sCode sToken
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Move (SearchArray("IFNOT", TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem = -1) Begin
Function_Return False
End
Move sLine to sCode
// Locate IFNOT boundary-aware on the MASKED line (position-aligned
// with sLine) via _FindKeywordPos - a bare Pos could match inside an
// identifier or a string literal before the real IFNOT (2026-06-10
// audit).
Get _FindKeywordPos (TokenizerData.sIndentation + TokenizerData.sOverstrikeLine) "ifnot" True 1 to iPos
Move (Left(sCode, (iPos + Length("ifnot")))) to sCode
If (iPos <> 0) Begin
Move (Overstrike("|||||", sCode, iPos)) to sCode
Move (Replace("|||||", sCode, CS_If)) to sCode
Increment iItem
Move TokenizerData.asLineTokens[iItem] to sToken
Append sCode "(" CS_Not "(" sToken ")) "
Move (TokenizerData.iTokenCount) to iSize
Decrement iSize
For iCount from (iItem +1) to iSize
Append sCode TokenizerData.asLineTokens[iCount] CS_S
Loop
End
Move (RTrim(sCode)) to sCode
If (sLine <> sCode) Begin
Move sCode to sLine
Function_Return True
End
Function_Return False
End_Function
{ Published = True }
{ Description = """
Changes legacy Insert command to use the function Insert.
Example: Insert "," In sText At 2 --> Move (Insert(",", sText, 2)) to sText
If sOne Eq "A" Insert "B" in sOne at 2 --> If sOne Eq "A" Move (Insert("B", sOne, 2)) to sOne
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Insert command -> (Insert( }
{ ModRecipe = 4531 | command: Insert }
""" }
Function ChangeInsertCommandToFunction String ByRef sLine String sParameter Returns Boolean
Boolean bChanged bFound
String sNose sLineLC sToken sVar1 sVar2 sText sInPos
Integer iPos iItem
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Move (SearchArray(CS_Insert, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem = -1) Begin
Function_Return False
End
Move (Trim(sLine)) to sLine
Move "" to sNose
Move (Lowercase(sLine)) to sLineLC
// If anything left of the CS_Insert command, save it in "sNose" (start string)
If (iItem > 0) Begin
Move (Pos(Lowercase(CS_Insert), sLineLC)) to iPos
Move (Left(sLine, (iPos -1))) to sNose
Move (Replace(sNose, sLine, "")) to sLine
End
// Remove the CS_Insert command
Move TokenizerData.asLineTokens[iItem] to sToken
Move (Replace(sToken, sLine, "")) to sLine
// Get the first variable
Move TokenizerData.asLineTokens[iItem + 1] to sVar1
// Find and remove the "IN" keyword.
Move (SearchArray(CS_In, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Move TokenizerData.asLineTokens[iItem] to sToken
Move (Replace(sToken, sLine, "")) to sLine
Move TokenizerData.asLineTokens[iItem + 1] to sVar2
End
// Find and remove the "AT" keyword, and save the next parameter which is the position to insert at.
Move (SearchArray(CS_At, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Move TokenizerData.asLineTokens[iItem] to sToken
Move (Replace(sToken, sLine, "")) to sLine
// Keep the position as a STRING (verbatim token). It can be a variable or expression - 'at iIndex',
// 'at (Length(s))' - not just a literal. Moving it to an Integer makes DF EVALUATE it as a numeric
// expression: that faults err 54 "Invalid symbol in expression" for a non-numeric symbol, and would
// silently yield 0 even when it didn't fault (losing the position).
Move TokenizerData.asLineTokens[iItem + 1] to sInPos
End
Move "" to sText
If (sNose <> "") Begin
Move sNose to sText
Move (Trim(sText) + CS_S) to sText
End
Append sText CS_Move CS_S "(" CS_Insert "(" sVar1 "," sVar2 "," sInPos "))" " " CS_To CS_S sVar2
Get AssembleOutputLine sText TokenizerData to sLine
Function_Return True
End_Function
{ Published = True }
{ Description = """
Replaces IN command with expression operator Contains.
It also swap places for the two variables involved and adds paranthesis.
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: 'In' to 'Contains' }
""" }
Function ChangeInToContains String ByRef sLine String sParameter Returns Boolean
String sOrg sPattern sMatched sRemain sHost sSub sText sAfter sBefore sExpression
Integer iSize iCount iItem
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Move (SearchArray(CS_With, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Function_Return False
End
Move (SearchArray(CS_Insert, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Function_Return False
End
Move (SearchArray(CS_Pos, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Function_Return False
End
Move (SearchArray(CS_In, TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem = -1) Begin
Function_Return False
End
Move sLine to sOrg
Move sLine to sText
Move TokenizerData.asLineTokens[iItem -1] to sAfter
Move TokenizerData.asLineTokens[iItem +1] to sBefore
Decrement iItem
Move ("(" + sBefore * CS_Contains * String(sAfter) + ")") to sExpression
// Set the "Before" array item to the expression
Move sExpression to TokenizerData.asLineTokens[iItem]
// Add expression to the expression array:
Move (SizeOfArray(TokenizerData.aExpressions)) to iSize
Move sExpression to TokenizerData.aExpressions[iSize].sExpression
Move (Length(sExpression)) to TokenizerData.aExpressions[iSize].iLength
Move (Pos(sAfter, sOrg)) to TokenizerData.aExpressions[iSize].iStartPos
If (TokenizerData.BooleanIndicator.AsBoolean.sCode <> "") Begin
Move sExpression to TokenizerData.BooleanIndicator.AsBoolean.sCode
End
Increment iItem
// Remove the two items we have concatenated.
Move (RemoveFromArray(TokenizerData.asLineTokens, iItem)) to TokenizerData.asLineTokens
Move (RemoveFromArray(TokenizerData.asLineTokens, iItem)) to TokenizerData.asLineTokens
Move (SizeOfArray(TokenizerData.asLineTokens)) to TokenizerData.iTokenCount
Get RebuildLineFromTokens TokenizerData.asLineTokens 0 to sText
Set pTokenizer to TokenizerData
If (sText <> sOrg) Begin
Get AssembleOutputLine (Trim(sText)) TokenizerData to sLine
End
Function_Return (sLine <> sOrg)
End_Function
// Text that was part of the Description, but was moved to here as it made the
// Description meta-tag to large, leading to a compilation problem.
// Not touched: `Find EQ <table>.<field>` (constant form), `-1` (recnum
// mode), integer literals outside these contexts, comments, and string
// literals (the tokenizer overlays string contents so they are immune),
// and any line that does not reference the named table.
//
{ Published = True }
{ Description = """
Changes index-number references in Find / vFind / Constrained_Find /
Constrained_Clear / For_all commands from one index number to another,
SCOPED to a specific table. Useful after a table-index restructure
that renumbers indexes for one table.
Rewrites BOTH `Index.<N>` and bare-integer forms in these positions:
Find / Send Find / Get Find : the <N> after a mode keyword
(EQ, GT, LT, GE, LE, NE,
FIRST, LAST, NEXT, PREV)
vFind <tbl> <N> <mode> : the 2nd argument after vFind
Constrained_Find <mode> <tbl> by <N>
Constrained_Clear <mode> <tbl> by <N>
For_all <tbl> {by|down} <N> : the <N> after by/down
Table identification (case-insensitive):
Direct token equal to the table name, OR
DDO form: object named exactly `o<TableName>_DD` or
`o<TableName>_DataDictionary` (the two common
DataFlex conventions). Other DDO naming conventions
are NOT recognised - rewrite those manually.
sParameter format: "<TableName>,<from>,<to>" - three comma-separated
values; the table name and two positive (and unequal) integers.
Example: "Customer,3,5".
{ MethodType = eStandardFunction }
{ EnumList = "ANY" }
{ SummaryText = Changed: index number in Find/Constrained/For_all commands }
{ ParamHelp = Three comma-separated values: the table name, the FROM
\nindex number, and the TO index number. Example "Customer,3,5"
\nchanges every Index.3 / by 3 / vFind Customer.X 3 ... reference
\nfor the Customer table to use 5 instead. Both indexes must be
\npositive (DataFlex indexes are 1-based; -1 = recnum mode, which
\nis never modified). DDO form recognised: o<TableName>_DD and
\no<TableName>_DataDictionary (case-insensitive). Other DDO
\nnaming conventions need to be handled manually. }
""" }
Function ChangeIndexNumber String ByRef sLine String sParameter Returns Boolean
String sOrgLine sFromStr sToStr sToken sTokenLC sRebuilt sNewLine sLineLC sMode
String sKwLC sLineLCNow sNextChar sTableName sTableNameLC sRemainder
Integer iFromIndex iToIndex iPos iPos2 iSize iItem iNext iCandidateIdx
Integer iKwLen iSearchFrom iAbsPos iEndPos iPass
Boolean bChanged bIsForAll bFoundContext bDone bRunPass bTableMatch
tTokenizer TokenizerData
// ---- Parse and validate sParameter (expect "<TableName>,<from>,<to>") ----
Move (Trim(sParameter)) to sParameter
Move (Pos(",", sParameter)) to iPos
If (iPos = 0) ;
Function_Return False
Move (Trim(Left(sParameter, iPos - 1))) to sTableName
Move (Trim(Mid(sParameter, CI_EOL, iPos + 1))) to sRemainder
Move (Pos(",", sRemainder)) to iPos2
If (iPos2 = 0) ;
Function_Return False
Move (Trim(Left(sRemainder, iPos2 - 1))) to sFromStr
Move (Trim(Mid(sRemainder, CI_EOL, iPos2 + 1))) to sToStr
If (sTableName = "") ;
Function_Return False
Move (Integer(sFromStr)) to iFromIndex
Move (Integer(sToStr)) to iToIndex
If (iFromIndex <= 0 or iToIndex <= 0 or iFromIndex = iToIndex) ;
Function_Return False
// Normalize back to canonical digit strings (strips any "+03" oddities).
Move (String(iFromIndex)) to sFromStr
Move (String(iToIndex)) to sToStr
Move (Lowercase(sTableName)) to sTableNameLC
// ---- Guard: skip blank/comment/image/in-command/variable-decl lines ----
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or ;
TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
// ---- Quick line filter: must contain at least one of the five trigger
// keywords. Avoids accidentally renaming `Index.N` symbols in
// unrelated lines. Listed explicitly (vfind / constrained_find are
// technically redundant against `contains "find"` since "find" is a
// substring of both, but listed for clarity of intent). ----
Move (Lowercase(TokenizerData.sOverstrikeLine)) to sLineLC
If (not(sLineLC contains "find") and ;
not(sLineLC contains "vfind") and ;
not(sLineLC contains "constrained_find") and ;
not(sLineLC contains "constrained_clear") and ;
not(sLineLC contains "for_all")) Begin
Function_Return False
End
// ---- Table scoping: line must reference sTableName as either a
// direct token OR via the DDO naming convention. ----
Get _HasWordInLine sLineLC sTableNameLC to bTableMatch
If (bTableMatch = False) Begin
Get _HasWordInLine sLineLC ("o" + sTableNameLC + "_dd") to bTableMatch
End
If (bTableMatch = False) Begin
Get _HasWordInLine sLineLC ("o" + sTableNameLC + "_datadictionary") to bTableMatch
End
If (bTableMatch = False) ;
Function_Return False
Move sLine to sOrgLine
Move False to bChanged
Move (SizeOfArray(TokenizerData.asLineTokens)) to iSize
Decrement iSize
If (iSize < 0) ;
Function_Return False
// ---- Walk tokens. Two independent checks per token: ----
// (a) Token IS `Index.<from>` -> rewrite digit, preserve "Index." case.
// (b) Token IS the leading keyword of a recognized context (Find /
// vFind / Constrained_Find / Constrained_Clear / For_all)
// whose index slot holds the bare integer <from>.
For iItem from 0 to iSize
Move TokenizerData.asLineTokens[iItem] to sToken
Move (Lowercase(sToken)) to sTokenLC
// (a) Index.<from> -> Index.<to>
If (sTokenLC = ("index." + sFromStr)) Begin
Move (Pos(".", sToken)) to iPos
Move (Left(sToken, iPos) + sToStr) to TokenizerData.asLineTokens[iItem]
Move True to bChanged
End
// (b1) Find (Send Find / Get Find / inline Find): walk forward to
// a mode keyword; the token after the mode is the index slot.
If (sTokenLC = Lowercase(CS_Find)) Begin
Move 0 to iCandidateIdx
Move False to bFoundContext
Move (iItem + 1) to iNext
While (iNext <= iSize and bFoundContext = False)
Move (Lowercase(TokenizerData.asLineTokens[iNext])) to sMode
If (sMode = "eq" or sMode = "gt" or sMode = "lt" or ;
sMode = "ge" or sMode = "le" or sMode = "ne" or ;
sMode = "first" or sMode = "last" or ;
sMode = "next" or sMode = "prev") Begin
Move True to bFoundContext
If ((iNext + 1) <= iSize) Move (iNext + 1) to iCandidateIdx
End
Increment iNext
Loop
If (iCandidateIdx > 0) Begin
If (TokenizerData.asLineTokens[iCandidateIdx] = sFromStr) Begin
Move sToStr to TokenizerData.asLineTokens[iCandidateIdx]
Move True to bChanged
End
End
End
// (b2) vFind <tbl> <N> <mode> -> index is exactly iItem+2.
If (sTokenLC = "vfind") Begin
If ((iItem + 2) <= iSize) Begin
If (TokenizerData.asLineTokens[iItem + 2] = sFromStr) Begin
Move sToStr to TokenizerData.asLineTokens[iItem + 2]
Move True to bChanged
End
End
End
// (b3) Constrained_Find / Constrained_Clear / For_all
// -> walk forward for `by` (or `down` for For_all);
// the token after is the index slot. -1 (recnum) is
// never touched because the FROM check requires a
// positive integer match.
If (sTokenLC = "constrained_find" or sTokenLC = "constrained_clear" or sTokenLC = "for_all") Begin
Move (sTokenLC = "for_all") to bIsForAll
Move 0 to iCandidateIdx
Move False to bFoundContext
Move (iItem + 1) to iNext
While (iNext <= iSize and bFoundContext = False)
Move (Lowercase(TokenizerData.asLineTokens[iNext])) to sMode
If (sMode = "by" or (bIsForAll = True and sMode = "down")) Begin
Move True to bFoundContext
If ((iNext + 1) <= iSize) Move (iNext + 1) to iCandidateIdx
End
Increment iNext
Loop
If (iCandidateIdx > 0) Begin
If (TokenizerData.asLineTokens[iCandidateIdx] = sFromStr) Begin
Move sToStr to TokenizerData.asLineTokens[iCandidateIdx]
Move True to bChanged
End
End
End
Loop
// ---- Reassemble line if anything actually changed via tokens ----
If (bChanged = True) Begin
Get RebuildLineFromTokens TokenizerData.asLineTokens 0 to sRebuilt
Get AssembleOutputLine sRebuilt TokenizerData to sNewLine
If (sNewLine <> sOrgLine) Begin
Move sNewLine to sLine
End
End
// ---- Textual fallback for `by <from>` / `down <from>` patterns. ----
// The framework tokenizer bundles legacy-operator expressions
// (e.g. on "Constrained_Clear EQ MyTable by 3" the EQ-mode causes
// "EQ MyTable by 3" to collapse into a single expression token),
// so the per-token walk above can't see `by` as a separate token.
// Find / vFind have a tokenizer-level exclusion so they aren't
// bundled; Constrained_Find / Constrained_Clear / For_all don't.
// We catch the missed cases here with a direct case-insensitive
// search on sLine, only for lines that already passed the
// Constrained_*/For_all line filter. Digit-boundary check on
// the trailing char prevents `by 30` from matching `by 3`.
If (sLineLC contains "constrained_find" or ;
sLineLC contains "constrained_clear" or ;
sLineLC contains "for_all") Begin
// Two passes: " by " always, then " down " only for For_all lines.
For iPass from 0 to 1
If (iPass = 0) Begin
Move " by " to sKwLC
Move True to bRunPass
End
Else Begin
Move " down " to sKwLC
Move (sLineLC contains "for_all") to bRunPass
End
If (bRunPass = True) Begin
Move (Length(sKwLC)) to iKwLen
Move (Lowercase(sLine)) to sLineLCNow
Move 1 to iSearchFrom
Move False to bDone
While (iSearchFrom <= Length(sLineLCNow) and bDone = False)
Move (Pos(sKwLC + sFromStr, Mid(sLineLCNow, CI_EOL, iSearchFrom))) to iAbsPos
If (iAbsPos = 0) Begin
Move True to bDone
End
Else Begin
Move (iAbsPos + iSearchFrom - 1) to iAbsPos
Move (iAbsPos + iKwLen + Length(sFromStr)) to iEndPos
Move "" to sNextChar
If (iEndPos <= Length(sLine)) Move (Mid(sLine, 1, iEndPos)) to sNextChar
If (sNextChar = "" or not("0123456789" contains sNextChar)) Begin
Move (Left(sLine, iAbsPos + iKwLen - 1) + sToStr + Mid(sLine, CI_EOL, iEndPos)) to sLine
Move (Lowercase(sLine)) to sLineLCNow
Move (iAbsPos + iKwLen + Length(sToStr)) to iSearchFrom
End
Else Begin
Move (iAbsPos + 1) to iSearchFrom
End
End
Loop
End
Loop
End
Function_Return (sLine <> sOrgLine)
End_Function
{ Published = True }
{ Description = """
Adds the default '.pkg' file extension to a Use statement that lacks one.
Example: Use cMyClass -> Use cMyClass.pkg
Leaves Use statements that already carry an extension (.pkg, .vw, ...) untouched.
{ MethodType = eStandardFunction }
{ FnGroup = Cleanup }
{ SummaryText = Added '.pkg' extension to a Use statement }
{ ModRecipe = 4535 | file extension }
""" }
Function AddUseFileExtension String ByRef sLine String sParameter Returns Boolean
Boolean bIsUse
String sLC sFileName
Integer iUsePos iStart iEnd
Get _IsUseLine sLine to bIsUse
If (bIsUse = False) ;
Function_Return False
// Locate the filename token - the word right after "Use ".
Move (Lowercase(sLine)) to sLC
Move (Pos("use ", sLC)) to iUsePos
Move (iUsePos + 4) to iStart
While (Mid(sLine, 1, iStart) = " ")
Increment iStart
Loop
Move iStart to iEnd
While (iEnd <= Length(sLine) and Mid(sLine, 1, iEnd) <> " ")
Increment iEnd
Loop
Move (Mid(sLine, (iEnd - iStart), iStart)) to sFileName
// Skip if there is no filename, or it already carries an extension.
If ((sFileName = "") or (sFileName contains ".")) ;
Function_Return False
// Insert ".pkg" immediately after the filename; indent + trailing comment are preserved.
Move (Left(sLine, (iEnd - 1)) + ".pkg" + Mid(sLine, CI_EOL, iEnd)) to sLine
Function_Return True
End_Function
{ Published = True }
{ Description = """
Changes legacy Left command with Left.
Example: Left sVar 5 to sLeft -> Move (Left(sVar, 5)) to sLeft
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: 'Left' command to function: 'Left' }
{ ModRecipe = 4531 | command: Left }
""" }
Function ChangeLeftCommandToFunction String ByRef sLine String sParameter Returns Boolean
Boolean bChanged
Get SingleCommandSyntaxToFunction (&sLine) CS_Left to bChanged
Function_Return bChanged
End_Function
{ Published = True }
{ Description = """
Modernises a legacy global Indicator declaration to a Boolean global variable. DataFlex
Indicator declarations are always GLOBAL, so the modern form is a Global_Variable Boolean.
Example: Indicator User -> Global_Variable Boolean User
Multiple names on one line are kept: Indicator A B C -> Global_Variable Boolean A B C
Owns the "Indicator. Use Boolean" variant of 4538 (the DECLARATION). 4538 is a SHARED number:
the "Indicate. Use Move command" + "...without parentheses" ([Found] usage) variants route to
ChangeLegacyIndicators / ChangeSquareBracketsIndicators - hence the match-text on every 4538 tag.
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: Indicator declaration -> Global_Variable Boolean }
{ ModRecipe = 4538 | Use Boolean }
""" }
Function ChangeIndicatorToBoolean String ByRef sLine String sParameter Returns Boolean
tTokenizer TokenizerData
Integer iPos
Get pTokenizer to TokenizerData
// Only a standalone global "Indicator <name> [more names]" declaration: the FIRST code token is
// exactly "Indicator" and at least one name follows. The first-token gate ignores incidental
// uses - "Move Indicator_State to x", a "Property Indicator ..." inside a class, comments, etc.
If (TokenizerData.bIsInCommand = True or TokenizerData.bIsInImage = True) ;
Function_Return False
If (SizeOfArray(TokenizerData.asLineTokens) < 2) ;
Function_Return False
If (Lowercase(TokenizerData.asLineTokens[0]) <> "indicator") ;
Function_Return False
// Splice the leading "Indicator" keyword (9 chars, whole word) -> "Global_Variable Boolean";
// the indentation, the name list, and any trailing comment after it are preserved verbatim.
Get _FindKeywordPos sLine "Indicator" True 1 to iPos
If (iPos = 0) ;
Function_Return False
Move (Left(sLine, (iPos - 1)) + "Global_Variable Boolean" + Mid(sLine, (Length(sLine) - iPos - 8), (iPos + 9))) to sLine
Function_Return True
End_Function
// ChangeLegacyIndicators - limitations + extra examples (kept as // comments to keep the
// {Description} under the meta-tag size limit; runtime Help reads the {Description} body only):
// - Handles at most two booleans within one square bracket, e.g. [Found Select].
// - Leaves the line unchanged if it contains a GROUP or ALL indicator directive.
// More examples:
// - [Select] Indicate Select as Windowindex Eq Fieldindex --> Move (WindowIndex = FieldIndex) to Select
// - [Found|Not Found|FindErr|Not FindErr] While -> e.g. While (Not(Found))
// - [Found] Indicate Found as Invoice.CustNum eq Customer.Number -> If (Found) Move (Invoice.CustNum eq Customer.Number) to Found
// - If [Not Found] Reread hTable -> If (Not(Found)) Reread hTable
{ Published = True }
{ Description = """
Changes legacy 'Indicate' to expression
Paired with ChangeSquareBracketsIndicators: THIS function does the deep transform but
deliberately skips lines containing Find/Send/Constrain - the sibling sweeps those up.
Run both together; this one then reports the bulk of the changes.
Examples:
- Indicate Found as True --> Move True to Found
- [Found] and [FindErr] indicator statements.
- [Found| Command -> e.g. If Found Command
- While [Not Found] -> e.g. While (Not(Found))
- [~Found] begin -> If (Not(Found)) Begin
- [Found ~Found] Begin -> If (Found and Not(Found)) Begin
{ MethodType = eStandardFunction }
{ FnGroup = Modernization }
{ SummaryText = Changed: [Found] -> (Not(Found)) }
{ EnumList = "eLeaveSelect, eModernizeSelect" }
{ InitialValue = eLeaveSelect }
{ ModRecipe = 4538 | Use Move command }
{ ModRecipe = 4538 | without parentheses }
{ ParamHelp = eLeaveSelect keeps [SELECT] as-is (safe default). eModernizeSelect also needs AddUseOldFMACCommands selected (it defines the SELECT macro). }
""" }
Function ChangeLegacyIndicators String ByRef sLine String sParameter Returns Boolean
Boolean bChanged bStartBracket bHasBracket bIndicateStart bIfStart
String sToken sOverstrikeLine sNot sTo sMove sIf sFirstCmd sSecondCmd sExpression sOrgLine sVarName sNewCode
Integer iItem iSize iPos iLeftBracketNo iStart iEnd iBracketTokenCount
Boolean bSkip bModernizeSelect
tTokenizer TokenizerData
Get pTokenizer to TokenizerData
If (TokenizerData.bLineIsReady = True or TokenizerData.bIsInCommand = True or TokenizerData.bIsInImage = True or TokenizerData.bIsVariableDeclaration = True) Begin
Function_Return False
End
Get HasBracket TokenizerData.BooleanIndicator to bHasBracket
If (TokenizerData.BooleanIndicator.bHasIndicateCommand = False and bHasBracket = False) Begin
Function_Return False
End
// SELECT-handling gate. By default we respect CS_NonRefactorableIndicators and skip
// lines whose indicator has no valid modern bare form, leaving the harmless [...]
// warning - bare-ifying e.g. SELECT would turn that warning into a compile error.
// The user opts in with eModernizeSelect when OldFMACCommands.pkg is in the build.
// sParameter arrives as the enum NAME from the UI combo ("eModernizeSelect") or as
// the numeric VALUE from tests - resolve via _ParameterIsEnum; a bare Move of the
// name into the Integer enum left the gate at eLeaveSelect, so the opt-in never
// worked from the UI (2026-06-10). NO num_arguments guard: it is unreliable here
// (nested sends/dynamic engine dispatch can clobber it), and an omitted parameter
// arrives as "" which _ParameterIsEnum already treats as no-match.
Get _ParameterIsEnum sParameter "eModernizeSelect" eModernizeSelect to bModernizeSelect
If (bModernizeSelect = False) Begin
Get _IndicatorIsNonRefactorable TokenizerData to bSkip
If (bSkip = True) ;
Function_Return False
End
// Use sOverstrikeLine so string literal content cannot trigger a false-positive match.
If (Lowercase(TokenizerData.sOverstrikeLine) contains (Lowercase(CS_Find) + CS_S) or ;
Lowercase(TokenizerData.sOverstrikeLine) contains (Lowercase(CS_Send) + CS_S) or ;
Lowercase(TokenizerData.sOverstrikeLine) contains (Lowercase(CS_Constrain) + CS_S)) Begin
Function_Return False
End
Move (SearchArray("GROUP", TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Function_Return False
End
Move (SearchArray("ALL", TokenizerData.asLineTokens, Desktop, RefFunc(DFSTRICMP))) to iItem
If (iItem <> -1) Begin
Function_Return False
End
// A LEADING bracket indicator + an explicit If + an Indicate command on ONE line (e.g.
// '[Found] If sResult ne "" Indicate Found as StatLog.Function_Name eq sData') is a 3-way compound
// the plain Case logic below cannot compose (it mangled such lines into e.g.
// "If Indicate (Found) to Found"). Build an explicit guarded block instead:
// If (<indicator> and (<if-condition>)) Begin