-
Notifications
You must be signed in to change notification settings - Fork 6
/
stack_revRefactor_.livecodescript
3797 lines (3431 loc) · 129 KB
/
stack_revRefactor_.livecodescript
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
Script "stack_revRefactor_"
/*
# Name: stack "revRefactor"
# ID: stack "revRefactor"
*/
Script "stack_revRefactor_"
/*
# Name: stack "revRefactor"
# ID: stack "revRefactor"
*/
/**
* LiveCode Refactoring support
*
* Mark Wieder
* Ah, Software
* https://www.ahsoftware.net
*
* licensing per the About box
*
* public handlers here:
* activateGLX2se : use the glx2 script editor
* activateIDEse : use the LiveCode IDE script editor
* refactorSEName
*
* this set of commands builds the menus and selects menuItems from them
* revHookBuildScriptEditorMenu
* revHookScriptEditorMenuPick
* revHookBuildScriptEditorContextMenu
* revHookScriptEditorContextMenuPick
*
* declareLocalVariable
*
*/
--> * history
/**
2012.05.29 mdw
* Added extra dimension to sUndoPointArray to support multiple tabs
* Localized any direct access to sUndoPointArray to UndoRetrive and Undo.Store
2012.05.31.mdw
* Moved low-level Undo code to mainstack
* Added extra type parameter "refactor" for multiple undo types
2012.08.02 mdw
* Fixed DoReplace if keyword is end of line, as in "exit grunt"
2019.01.04 mdw
* Extracted from glx2 for use in IDE's script editor
2019.01.27 mdw
* Added support for getters and setters
2019.03.08 mdw fixed getter/setter private declaration
* partially implemented cascading dispatch command
2019.03.20 mdw release 1.0.1
* added "Go Back" to Edit menu
2019.04.07 mdw version 1.0.4
* needed to use rugged ID rather than long id when talking to the SE
* need to use long id for the undo mechanism
* RenameHandler now only modifies external scripts if there is no handler of the same name in them.
* Still not ideal, but probably the best compromise for now.
* Same for ChangeSignature.
2019.04.25 mdw version 1.0.5
* AddDocsAtLine now allows for clicking anywhere in a handler to create documentation for that handler
* RenameVariable had misplaced lineCount incrementer : fixed per Bernd
2019.05.02 mdw version 1.0.6
* modified FindOrphans to catch orphan "put <xyz>" statements
* listUndeclaredVariablesIn had a misplaced chopIf statement -- bn
2019.05.03 mdw version 1.0.7
* move or copy without substacks was throwing an error
* explictVars is *not* set with strict compilation mode
* added tooltips to orphan code display since it isn't resizable
2019.05.07 mdw
* fixed the object selector for CopyOrMove in scriptObjectsOfObject
* added bn's patches to ensure no duplicate copies
2019.05.08 mdw
* listOrphansOfType needed tweaking after refactoring (irony rears its ugly head)
* don't remember why filteredScript was trying to filter out regex "\**"
2019.05.11 mdw version 1.0.9
* serious refactoring of availableHandlersOf because:
* it wasn't returning the right information if there were unapplied edits
* and it needed a multidimensional array to catch duplicate handlers
2019.05.11 mdw version 1.0.10
* reworked DoReplace to handle quoted strings
* and that required a one-line change to ChangeSignatureInScript
2019.05.12 mdw
* FindDuplicates now flags ALL occurences of duplicate handlers
* Constants are no longer flagged as unused (oops)...
* and this uncovered the fact that the scope of script-local variables was being reported incorrectly
* now catching getProp and setProp handlers as well as commands and functions
* and reporting them as such for unused and duplicate lists
*
* lots of tweaking to handle backslashes
* and ensure that script-local constants aren't flagged as unused
2019.05.13 mdw
* addedd a missing removeCommentsFrom() call to FindOrphans
* group duplicate handlers by type rather than sorting by line number
* added unused parameters to FindOrphans list
* also catching @parameters properly now
* improved speed of removing block comments (bn's function)
2019.05.14 mdw
* removed some unused local variables
* removed unused parameters from AddDocsAtLine, AddTestAtLine, ChangeSignature, scriptObjectsOfObject, CurrentObjectID
* unused parameter list is now sorted numerically
* increased speed of blockComments and quotes removal significantly
* DoReplace can now rename handler parameters properly
* added removeBracketsAndQuotesFrom
* can now rename variables inside merge strings
2019.05.26 mdw
* added more constants for maintainability
* moved script from frontscripts to backscripts to avoid recursion
* this resulted in being able to remove the cruft from the revHook message handlers
2019.07.28 mdw
* Add Documentation got the parameter list wrong for private handlers.
2022.11.22 mdw
* removed busy cursor from findOrphans for speed.
2022.11.26 mdw
* ensureNamingConvention reports proper line numbers for local variables
*/
/*
* Known issues:
removeCommentsFrom strips variable usage from merge strings.
* If the command name and variable are the same, the variable is flagged as being used.
* obviously it's not a good coding style, but still...
command hello
local hello
end hello
* still a problem with multiline puts (the script editor also trips over this):
on mouseUp
local tText
put "what" & " no " &\/* any*/
"more" into tText
end mouseUp
*/
local kDebugLog? = true
-- some fake constants
-- because rValues can't be expressions
constant kCodeField = "Script"
local main = "revNewScriptEditor 1"
constant kGLX2 = "glx2ScriptEditor"
constant kRightBrackets = "]]"
constant kLeftBrackets = "[["
local kRightBracketReplacement = "]]"
local kLeftBracketReplacement = "[["
local kReference = "@"
local kQuote = "'"
local sBreadCrumbs
local sTime # used for timing handlers here
constant kNotImplemented = "Not yet implemented. Coming Real Soon Now"
constant kThisScript = "This script only"
constant kKeywords = "end,private,on,command,function,getProp,setProp,before,after"
constant kMessageNames = "item,url,msg,regex,back,front,char,each,line,word"
constant kContainerNames = "stack,card,cd,button,btn,field,fld,scrollbar,sb,image,img,graphic,grc,group,gp,player,widget"
constant self = "revRefactor"
constant kNamingConvention = true
local sNewParamsArray
local sScriptArray
local sObjectHandlersArray
command activateGLX2se
put kGLX2 into main
fillFakeConstants
end activateGLX2se
command activateIDEse
put "revNewScriptEditor 1" into main
fillFakeConstants
end activateIDEse
private command fillFakeConstants
put numtochar(3) & space into kReference
put numtochar(5) into kLeftBracketReplacement
put numtochar(6) into kRightBracketReplacement
put numtochar(7) into kQuote
end fillFakeConstants
# getter function to retrieve the name of the current script editor
function refactorSEName
return main
end refactorSEName
--> IDE editor menu functions
/*
* These two commands generate the menus to show the Refactor commands
* revHookBuildScriptEditorContextMenu generates the control-dropdown menu
* revHookBuildScriptEditorMenu generates the menuBar menu items
*/
on revHookBuildScriptEditorContextMenu pObjectID, pSelectedText, @pText, @pModifiedText
put generateRefactoringSubmenu() after pText
# if there's somewhere to go back to then
if sBreadCrumbs["count"] > 0 then
put cr & "Go Back" after line 1 of pText
end if
put pText into pModifiedText
pass revHookBuildScriptEditorContextMenu
end revHookBuildScriptEditorContextMenu
on revHookBuildScriptEditorMenu pMenuName, @pMenu, @pModifiedMenu
if pMenuName is "Edit" then
put generateRefactoringSubmenu() after pMenu
if there is a stack kGLX2 then
put pMenu into button "Edit" of stack kGLX2
end if
else if pMenuName is "Help" then
put "Refactor help" after pMenu
end if
put pMenu into pModifiedMenu
pass revHookBuildScriptEditorMenu
end revHookBuildScriptEditorMenu
constant kRefactors = "Rename Handler,Rename Variable,Convert Literal To Constant,Change Signature,Safe Delete,Move Handler To,Copy Handler To,Create Getter and Setter,Add Documentation,Add Test,Code quality"
constant kMenuItemConvertGlobal = "Convert Global to"
constant kMenuItemConvertVariable = "Convert Variable to"
constant kMenuItemCreateGetterAndSetter = "Create Getter and Setter"
constant kMenuItemExtractTo = "Extract to"
constant kMenuItemFindOrphans = "Find Orphan Code"
constant kMenuItemUndoLast = "Undo Last Refactor"
constant kMenuSubItemToScriptLocal = "Script Local"
constant kMenuSubItemToProperty = "Property"
constant kMenuSubItemToParameter = "Parameter"
private function generateRefactoringSubmenu
local tAddedText
put "-" & cr & "Refactoring" & cr after tAddedText
set the itemDelimiter to comma
repeat for each item tRefactor in kRefactors
put tab & tRefactor & cr after tAddedText
end repeat
put tab & kMenuItemConvertGlobal & cr after tAddedText
put tab & tab & kMenuSubItemToScriptLocal & cr after tAddedText
put tab & tab & "Getter and Setter" & cr after tAddedText
put tab & tab & kMenuSubItemToProperty & cr after tAddedText
put tab & kMenuItemConvertVariable & cr after tAddedText
put tab & tab & kMenuSubItemToScriptLocal & cr after tAddedText
put tab & tab & kMenuSubItemToParameter & cr after tAddedText
put tab & tab & kMenuSubItemToProperty & cr after tAddedText
put tab & kMenuItemExtractTo & cr after tAddedText
put tab & tab & "Command" & cr after tAddedText
put tab & tab & "Function" & cr after tAddedText
put tab & kMenuItemFindOrphans & cr after tAddedText
put tab & kMenuItemUndoLast & cr after tAddedText
put tab & "Ensure Naming Convention" & cr after tAddedText
return tAddedText
end generateRefactoringSubmenu
private command refactorMenuPick pWhich
local tObject
local tSelectedText
local tSelectedChunk
delete variable sObjectHandlersArray
delete variable sScriptArray
delete variable sNewParamsArray
put the short name of this stack into main -- bn's patch for multiple editors
set the itemDelimiter to "|"
if item 1 of pWhich is "Refactoring" then
lock screen
try
put the selectedChunk into tSelectedChunk
if the selectedtext is empty then
click at the selectedLoc
put the clickText into tSelectedText
if tSelectedChunk is not empty then
select tSelectedChunk
end if
else
put the selectedtext into tSelectedText
end if
catch e
end try
unlock screen
try
switch item 2 of pWhich
case "Convert Literal To Constant"
ConvertLiteralToConstant tSelectedText
break
case "Change Signature"
ChangeSignature --tSelectedText
break
case kMenuItemConvertGlobal
ConvertGlobalTo tSelectedText, item 3 of pWhich
break
case kMenuItemConvertVariable
ConvertLocalVariableTo tSelectedText, item 3 of pWhich
break
case kMenuItemCreateGetterAndSetter
CreateGetterAndSetterFor tSelectedText
break
case kMenuItemExtractTo
Extract tSelectedText, item 3 of pWhich
break
case "Safe Delete"
SafeDelete tSelectedText
break
case "Move Handler To"
MoveHandler tSelectedText
break
case "Copy Handler To"
CopyHandler tSelectedText
break
case "Rename Handler"
renameHandler tSelectedText
break
case "Rename Variable"
renameVariable tSelectedText
break
case "Add Test"
AddTestAtLine # the selectedline
break
case "Add Documentation"
AddDocsAtLine # the selectedline
break
case "Code quality"
put CurrentObjectID() into tObject
get codeQualityOf(tObject)
break
case kMenuItemFindOrphans
FindOrphans
break
case kMenuItemUndoLast
put CurrentObjectID() into tObject
Undo.Pop tObject
--setSEDirtyFlagFor tObject, false
break
case "Ensure Naming Convention"
EnsureNamingConvention
break
default
throw kNotImplemented
break
end switch
catch e
answer e
end try
else
local tMatches, tLine, tSelectedHandler
set the itemdelimiter to comma
try
if the selectedtext is empty then
click at the selectedLoc
put the clickText into tSelectedHandler
else
put the selectedtext into tSelectedHandler
end if
catch e
end try
switch item 1 of pWhich
case "Go Back"
local tIndex, tDestination, tCount
put sBreadCrumbs["count"] into tCount
put sBreadCrumbs[tCount]["line"] into tLine
put sBreadCrumbs[tCount]["destination"] into tDestination
if tCount > 0 then
put tCount-1 into sBreadCrumbs["count"]
end if
if tDestination is not empty then
dispatch "revSEGoExecutionPoint" to stack main with tDestination, tLine, true, true
end if
break
case "Go to local definition"
case "Go to definition"
# hijack Go to definition so we can record where we're going and where we've been
put word 2 of the selectedline into tLine
put CurrentObjectID() into tObject
dispatch function "seMatchingDefinitions" to stack main with tSelectedHandler, tObject
if it is "handled" then
put the result into tMatches
if tMatches is not empty then
put sBreadCrumbs["count"] into tCount
if tCount is empty then
put 0 into tCount
end if
add 1 to tCount
put tLine into sBreadCrumbs[tCount]["line"]
put tObject into sBreadCrumbs[tCount]["destination"]
put tCount into sBreadCrumbs["count"]
end if
end if
break
default
end switch
end if
end refactorMenuPick
on revHookScriptEditorMenuPick pMenuName, pWhich
switch pMenuName
case "Edit"
refactorMenuPick pWhich
break
case "Help"
if pWhich is "Refactor help" then
show stack "revRefactor"
go to card "main" of stack "revRefactor"
end if
break
end switch
pass revHookScriptEditorMenuPick
end revHookScriptEditorMenuPick
on revHookScriptEditorContextMenuPick pWhich
refactorMenuPick pWhich
pass revHookScriptEditorContextMenuPick
end revHookScriptEditorContextMenuPick
--> Refactor support
private function scriptFromEditor pObject
local tReturnValue
lock screen
if pObject is not empty then
edit the script of pObject
end if
dispatch function "textGetScript" to group "Editor" of stack main
if it is "unhandled" then
throw "unhandled function textGetScript"
else
put the result into tReturnValue
end if
unlock screen
return tReturnValue
end scriptFromEditor
private command createGetterAndSetterFor pVariableName
local tScript
put scriptFromEditor() into tScript
insertGetterAndSetter pVariableName, tScript
ConvertLocalVariableToScriptLocal pVariableName, tScript
SetScript tScript
end createGetterAndSetterFor
/**
* ConvertLiteralToConstant
*/
private command convertLiteralToConstant pLiteral
local tScript
local tLineToInsert
local tConstant
local tInsertionPoint
local tSuccess?
local tObject
put false into tSuccess?
# select the whole word
if pLiteral is a number then
click at the selectedLoc
put the clickText into pLiteral
end if
if pLiteral is empty then
exit ConvertLiteralToConstant
end if
put CurrentObjectID() into tObject
put scriptFromEditor() into tScript
# find line for insertion
put InsertionPoint(tScript) into tInsertionPoint
# create constant
put pLiteral into tConstant
replace quote with empty in tConstant
replace space with "_" in tConstant
replace "\" with empty in tConstant
put "k" before tConstant
if "constant" && tConstant is not in tScript then
put "constant" && tConstant && "= " into tLineToInsert
if pLiteral is a number then
else
if char 1 of pLiteral is not quote then
put quote & pLiteral & quote into pLiteral
end if
end if
put pLiteral & cr after tLineToInsert
end if
local tLine
put word 2 of the clickline into tLine
replace pLiteral with tConstant in line tLine of tScript
# insert tLineToInsert at insertion point
if tInsertionPoint is not 0 then
put tLineToInsert before line tInsertionPoint of tScript
SetScript tScript, tObject
put true into tSuccess?
else
Undo.Pop tObject, "Undo.RestoreScript"
end if
return tSuccess?
end convertLiteralToConstant
/**
* ConvertGlobalToLocal
*/
private command convertGlobalTo pVariableName, pType
local tType
local tScript, tGlobalScript
local tObject
if pType is empty then
answer "Convert to" with kMenuSubItemToScriptLocal and "Getter and Setter" and kMenuSubItemToProperty and "Cancel"
put it into tType
else
put pType into tType
end if
put CurrentObjectID() into tObject
put scriptFromEditor() into tScript
# see if the global was declared
put filteredScript("global *" & pVariableName & "*", tScript) into tGlobalScript
if tGlobalScript is not empty then
# remove global declarations
do "global" && pVariableName & ";delete variable" && pVariableName
if tType is not "Cancel" then
set the itemdelimiter to comma
switch tType
case "this handler only"
# find start of handler + 1
addLocalVariableToHandler pVariableName, tScript
break
case "Getter and Setter"
insertGetterAndSetter pVariableName, tScript
case kMenuSubItemToScriptLocal
repeat for each line tLine in tGlobalScript
RemoveDeclaration pVariableName, "global", tLine, 0, tScript
end repeat
# find line for insertion
insertAtStartOfScript "local" && pVariableName, tScript
break
case kMenuSubItemToProperty
ConvertLocalVariableTo pVariableName, kMenuSubItemToProperty
exit ConvertGlobalTo
break
end switch # scope of global
SetScript tScript, tObject
end if
end if
end convertGlobalTo
private command addLocalVariableToHandler pVariableName, @pScript
local tInsertionPoint
local tStartLineNumber, tEndLineNumber
local tGlobalScript
put ContextForCurrentLine() into tInsertionPoint
put word 3 of tInsertionPoint into tStartLineNumber
put word 4 of tInsertionPoint into tEndLineNumber
put line tStartLineNumber+1 to tEndLineNumber of pScript into tGlobalScript
repeat for each line tLine in tGlobalScript
RemoveDeclaration pVariableName, "global", tLine, 0, pScript
end repeat
put cr & tab & "local" && pVariableName after line tStartLineNumber of pScript
end addLocalVariableToHandler
/**
* Public because it's invoked from field "fldUndeclared" of card "Undeclared"
*/
command declareLocalVariable pVariableName
local tScript
put scriptFromEditor() into tScript
addLocalVariableToHandler pVariableName, tScript
SetScript tScript, CurrentObjectID()
end declareLocalVariable
private command removeDeclaration pVariableName, pType, pLine, pOffset, @pScript
local tLinePos
local tLine, tReplacedLine
constant kTypes = "global,local"
# TODO: See if it's safe to delete the declaration
# by seeing if it's used anywhere else
# this includes globals and script locals
put lineoffset(pLine, pScript, pOffset) into tLinePos
put line tLinePos of pScript into tLine
set the itemdelimiter to comma
if word 1 of tLine is among the items of kTypes then
replace pVariableName with space in tLine
if the number of words in tLine is 1 then
put empty into line tLinePos of pScript
delete line tLinePos of pScript
else
put line tLinePos of pScript into tLine
put replacetext(tLine, "(?i)[, ]*"& regexWholeWordFrom(pVariableName), "") into tReplacedLine
chopIf comma, tReplacedLine
replace "global," with "global " in tReplacedLine
replace "local," with "local " in tReplacedLine
if word 1 to -1 of tReplacedLine is not empty then
put tReplacedLine into line tLinePos of pScript
end if
end if
end if
return pScript
end removeDeclaration
private function regexWholeWordFrom pWord
return "\b" & pWord & "\b"
end regexWholeWordFrom
/**
* ConvertLocalVariableTo
* convert local variable to
* script-local variable
* parameter of the containing handler
* property
*/
private command convertLocalVariableTo pVariableName, pType
local tType
local tScript
local tLocalScript, tLocalScriptArray
local tInsertionPoint
local tStartLineNumber, tEndLineNumber
local tObject
local tLineCount
if pType is empty then
answer "Convert to" with kMenuSubItemToScriptLocal and kMenuSubItemToParameter and kMenuSubItemToProperty and "Cancel"
put it into tType
else
put pType into tType
end if
if tType is not "Cancel" then
put CurrentObjectID() into tObject
put scriptFromEditor() into tScript
set the itemdelimiter to comma
switch tType
case kMenuSubItemToScriptLocal
# see if the variable was declared
put filteredScript("local *" & pVariableName & "*", tScript) into tLocalScriptArray
put tLocalScriptArray into tLocalScript
# no local declaration
if tLocalScript is empty then
if filteredScript("global *" & pVariableName & "*", tScript) is empty then
Undo.Pop tObject, "Undo.RestoreScript"
insertAtStartOfScript "local" && pVariableName, tScript
else
# convert the local global to a script local
ConvertLocalVariableToScriptLocal pVariableName, tScript
end if
else
# already declared, so there's more work to do
ConvertLocalVariableToScriptLocal pVariableName, tScript
end if
break
case kMenuSubItemToParameter
local tWordCount
# find start of handler
put ContextForCurrentLine() into tInsertionPoint
put word 3 of tInsertionPoint into tStartLineNumber
put word 4 of tInsertionPoint into tEndLineNumber
put line tStartLineNumber+1 to tEndLineNumber of tScript into tLocalScript
# ensure the parameter does not already exist
put 2 into tWordCount
if pVariableName is not among the words of line tStartLineNumber of tScript then
if word 1 of line tStartLineNumber of tScript is "private" then
add 1 to tWordCount
end if
# see if there are already any parameters
if the number of words in line tStartLineNumber of tScript > tWordCount then
put comma after line tStartLineNumber of tScript
end if
put space & pVariableName after line tStartLineNumber of tScript
end if
if variableIsInHandler?(pVariableName, tLocalScript) is false then
# must be script-local, so grab the whole script
put tScript into tLocalScript
put 0 into tStartLineNumber
put -1 into tEndLineNumber
end if
put 1 into tLineCount
repeat for each line tLine in tLocalScript
switch word 1 of tLine
case "global"
case "local"
RemoveDeclaration pVariableName, word 1 of tLine, tLine, 0, tLocalScript
insertText tLocalScript, tStartLineNumber, tEndLineNUmber, tScript
end switch
add 1 to tLineCount
end repeat
break
case kMenuSubItemToProperty
local tTargetObject
ask "property of what object?"
if it is not empty then
put it into tTargetObject
set the wholematches to true
local tLines
put the number of lines in tScript into tLines
# find start of handler
put ContextForCurrentLine() into tInsertionPoint
put word 3 of tInsertionPoint into tStartLineNumber
put word 4 of tInsertionPoint into tEndLineNumber
put line tStartLineNumber+1 to tEndLineNumber of tScript into tLocalScript
# see if we're dealing with a script-local or handler-local variable
# see if it's in the current handler
if variableIsInHandler?(pVariableName, tLocalScript) is false then
# must be script-local, so grab the whole script
put tScript into tLocalScript
put 0 into tStartLineNumber
put -1 into tEndLineNumber
end if
put 1 into tLineCount
repeat for each line tLine in tLocalScript
switch word 1 of tLine
case "end"
if char 1 to 3 of tLine is "end" then
# at end of current handler
if tStartLineNumber is not zero then
exit repeat
end if
end if
break
case "global"
case "local"
if pVariableName is in tLine then
RemoveDeclaration pVariableName, word 1 of tLine, tLine, 0, tLocalScript
insertText tLocalScript, tStartLineNumber, tEndLineNUmber, tScript
# see if we deleted a line; adjust the line count if so
if the number of lines in tScript < tLines then
put the number of lines in tScript into tLines
subtract 1 from tLineCount
end if
end if
break
default
if pVariableName is in tLine then
ConvertVariableToProperty pVariableName, tTargetObject, tLine
put tLine into line tLineCount+tStartLineNumber of tScript
end if
break
end switch
add 1 to tLineCount
end repeat
else
throw kNotImplemented
exit ConvertLocalVariableTo
end if
default
end switch # tType
SetScript tScript, tObject
end if # tType is "Cancel"
end convertLocalVariableTo
private command insertText pText, pStartLineNumber, pEndLineNUmber, @pScript
put pText into line pStartLineNumber+1 to pEndLineNumber of pScript
end insertText
private command insertGetterAndSetter pVar, @pScript
local kCRLF, kTAB
constant kGetter = "function [[pVar]][[kCRLF]][[kTAB]]return [[pVar]][[kCRLF]]end [[pVar]][[kCRLF]]"
constant kSetter = "command set[[pVar]]To pValue [[kCRLF]][[kTAB]]put pValue into [[pVar]][[kCRLF]]end set[[pVar]]To[[kCRLF]]"
# set up the fake constants bc we can't have that kind of constant in LC
put crlf into kCRLF
put tab into kTAB
insertAtStartOfScript merge(kSetter), pScript
insertAtStartOfScript merge(kGetter), pScript
end insertGetterAndSetter
private function variableIsInHandler? pVariableName, pScript
# see if it's in the current handler
filter pScript with "*local *" & pVariableName & "*"
return pScript is not empty
end variableIsInHandler?
private function globalIsInHandler? pVariableName, pScript
-- see if it's in the current handler
filter pScript with "*global *" & pVariableName & "*"
return pScript is not empty
end globalIsInHandler?
/**
* ConvertLocalVariableToScriptLocal
* command
* @pVariableName
* @@pScript
*
* At this point we know the variable is already declared somewhere
*/
private command convertLocalVariableToScriptLocal pVariableName, @pScript
local tInsertionPoint
local tStartLineNumber, tEndLineNumber
local tLocalScript
put ContextForCurrentLine() into tInsertionPoint
put word 3 of tInsertionPoint into tStartLineNumber
put word 4 of tInsertionPoint into tEndLineNumber
put line tStartLineNumber+1 to tEndLineNumber of pScript into tLocalScript
if variableIsInHandler?(pVariableName, tLocalScript) is false \
and globalIsInHandler?(pVariableName, tLocalScript) is false then
# must be script-local, so grab the whole script
put pScript into tLocalScript
put 0 into tStartLineNumber
put -1 into tEndLineNumber
else
insertAtStartOfScript "local" && pVariableName, pScript
add 1 to tStartLineNumber
add 1 to tEndLineNumber
repeat for each line tLine in tLocalScript
switch word 1 of tLine
case "global"
case "local"
if pVariableName is in tLine then
RemoveDeclaration pVariableName, word 1 of tLine, tLine, 0, tLocalScript
insertText tLocalScript, tStartLineNumber, tEndLineNUmber, pScript
end if
end switch
end repeat
end if
end convertLocalVariableToScriptLocal
private command convertVariableToProperty pVariableName, pObject, @pScriptLine
constant kSet = "set the [[pVariableName]] of [[pObject]] to"
if "put" is word 1 of pScriptLine and pVariableName is word -1 of pScriptLine then
put merge(kSet) into word 1 of pScriptLine
delete word -2 to -1 of pScriptLine
else
put replacetext(pScriptLine, "(?i)"& regexWholeWordFrom(pVariableName), "the" && pVariableName && "of" && pObject) into pScriptLine
end if
return pScriptLine
end convertVariableToProperty
/**
* ChangeSignature
* change the order and/or number of parameters
*/
private command changeSignature
local tList
local tScript
local tInsertionPoint
local tStartLineNumber
local tEndLineNumber
local tLine
local tHandlerName, tNewHandlerName
local tParamsArray
local tNewSignature
local tType
local x
# see if it's a command or function
put HandlerType() into tType
put scriptFromEditor() into tScript
set the itemDelimiter to comma
set the wholematches to true
put ContextForCurrentLine() into tInsertionPoint
put word 3 of tInsertionPoint into tStartLineNumber
put word 4 of tInsertionPoint into tEndLineNumber
put line tStartLineNumber of tScript into tLine
put 0 into x
repeat while char -1 of tLine is "\"
add 1 to x
put line tStartLineNumber+x of tScript into char -1 of tLine
end repeat
if x is not 0 then
put " to " & tStartLineNumber+x after tStartLineNumber
end if
if word 1 of tLine is "private" then
delete word 1 of tLine
end if
put word 2 of tLine into tHandlerName
# put the parameter locations in an array
# whose keys are the parameters
put OriginalParams(tLine) into tParamsArray
ask "Edit Signature" with line tStartLineNumber of tScript
if it is not empty and it is not line tStartLineNumber of tScript then
put it into tNewSignature
# if it's a private handler there's no need to check other scripts
if word 1 of tLine is not "private" then
put WhereUsed(tHandlerName, tType) into tList
end if
# adjust this script to start with
ChangeSignatureInEditorScript tStartLineNumber, tEndLineNumber, tHandlerName, tNewSignature, tParamsArray, tScript
SetScript tScript
if tList is empty then
# this script only
else
# WhereUsed() returned list of other scripts where used
repeat for each line tObject in tList
put the script of tObject into tScript
if word 1 of tNewSignature is "private" then
put word 3 of tNewSignature into tNewHandlerName
else
put word 2 of tNewSignature into tNewHandlerName
end if
if ShouldSkipScript?(tScript) then
else
ChangeSignatureInScript tHandlerName, tNewHandlerName, sNewParamsArray, tScript
SetScript tScript, tObject
end if
end repeat
end if
else
# user wants to cancel, not change signature
end if
end changeSignature
private function originalParams pLine
local tOldParams
local tParamsArray
local x
# put the parameter locations in an array
# whose keys are the parameters
set the itemdelimiter to comma
put word 3 to -1 of pLine into tOldParams
put token 1 to -1 of tOldParams into tOldParams
repeat with x=1 to the number of items in tOldParams
put x into tParamsArray[word 1 of item x of tOldParams]
end repeat
return tParamsArray
end originalParams
/**
* ChangeSignatureInEditorScript
*
* Change the command or function declaration,
* then call ChangeSignatureInScript to change calls to the handler
*/
private command changeSignatureInEditorScript pStartLineNumber, pEndLineNumber, pHandlerName, pNewSignature, pParamsArray, @pScript
local tNewHandlerName
local tParamOffset
local tIsPrivate
local tParameters
local tOldPosition
local tObject
local x
set the itemDelimiter to comma
set the wholematches to true
put false into tIsPrivate
put CurrentObjectID() into tObject
delete variable sNewParamsArray
# replace the handler declaration itself
put pNewSignature into line pStartLineNumber of pScript
put 3 into tParamOffset
if word 1 of pNewSignature is "private" then
put true into tIsPrivate
delete word 1 of pNewSignature
end if
put word 2 of pNewSignature into tNewHandlerName
# change the end of the declaration
replace pHandlerName with tNewHandlerName in line pEndLineNumber of pScript
# gather the parameter positions list
put 1 into x
put word tParamOffset to -1 of pNewSignature into tParameters
repeat for each item tParameter in tParameters
# see if the parameter is in the old list
put pParamsArray[word 1 of tParameter] into tOldPosition
if tOldPosition is not empty then
# tNewParamsArray is a transposition array
# whose keys are the new position
# and values are the old position
put tOldPosition into sNewParamsArray[x]
else
put numtochar(3) & word 1 of tParameter into sNewParamsArray[x]
end if
add 1 to x
end repeat
# now change all calls to this handler
ChangeSignatureInScript pHandlerName, tNewHandlerName, sNewParamsArray, pScript
end changeSignatureInEditorScript
/**
* ChangeSignatureInScript
*