Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit fa977a7

Browse files
Merge pull request #1726 from montegoulding/linecontinuationformatting
[[ Bugs 19951, 8228 ]] Line continuation formatting
2 parents 22f7294 + 6193589 commit fa977a7

File tree

11 files changed

+298
-23
lines changed

11 files changed

+298
-23
lines changed

Toolset/palettes/script editor/behaviors/revsecommoneditorbehavior.livecodescript

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,18 @@ private function textFormatGetLineIndent pLine
477477
return tResult
478478
end textFormatGetLineIndent
479479

480+
-- all chars after a continuation are treated as a comment
481+
-- this will work fine until someone uses format with multiple
482+
-- \"\" but there's not much getting around that
480483
private function lineIsContinued pLine
481-
if word -1 of pLine is "\" then
482-
return true
483-
else
484-
return false
485-
end if
484+
put lineStripComments(pLine) into pLine
485+
split pLine by quote
486+
repeat with tIndex = 1 to the number of elements of pLine step 2
487+
if pLine[tIndex] contains "\" then
488+
return true
489+
end if
490+
end repeat
491+
return false
486492
end lineIsContinued
487493

488494
private function textFormatGetContinuationIndent pLastLineNumber
@@ -493,15 +499,15 @@ end textFormatGetContinuationIndent
493499

494500
private function combineContinuedLine pLastLineNumber, @pTextLines
495501
local tContinuation
496-
put pTextLines[pLastLineNumber] into tContinuation
502+
put lineStripComments(pTextLines[pLastLineNumber]) into tContinuation
497503

498504
local tIndex
499505
repeat with tIndex = (pLastLineNumber - 1) down to 1
500506
if not lineIsContinued( pTextLines[tIndex]) then
501507
exit repeat
502508
end if
503509

504-
put pTextLines[tIndex] before tContinuation
510+
put lineStripComments(pTextLines[tIndex]) before tContinuation
505511
end repeat
506512
replace "\" with empty in tContinuation
507513
return tContinuation
@@ -537,10 +543,12 @@ private function textFormatLine pLine, pTextLines, @xPreviousLine
537543
end if
538544

539545
local tPreviousLine
546+
local tPreviousLineWasCombined = false
540547
if xPreviousLine > 0 then
541548
# This is the case where we have reached the end of a continued line. Here, we treat the continued line
542549
# as a single entity in order to calculate its indentation properties correctly for the line after it.
543550
if lineIsContinued(pTextLines[xPreviousLine - 1]) then
551+
put true into tPreviousLineWasCombined
544552
put combineContinuedLine(xPreviousLine, pTextLines) into tPreviousLine
545553
else
546554
put pTextLines[xPreviousLine] into tPreviousLine
@@ -571,15 +579,15 @@ private function textFormatLine pLine, pTextLines, @xPreviousLine
571579
# we always add the continuation indent because we combined the continued lines
572580
put kContinuationIndent into tIndentPreviousLineAdds
573581
else
574-
put textFormatIndentLineAdds(pTextLines[xPreviousLine]) into tIndentPreviousLineAdds
582+
put textFormatIndentLineAdds(tPreviousLine) into tIndentPreviousLineAdds
575583
end if
576584

577585
local tCurrentIndent
578586
repeat (the number of chars of tPreviousLineIndent + tIndentCurrentLineRemoves + tIndentPreviousLineAdds)
579587
put space after tCurrentIndent
580588
end repeat
581589

582-
put ((the number of chars of tPreviousLineIndent + tIndentCurrentLineRemoves + tIndentPreviousLineAdds) - the number of chars of tCurrentLineIndent) & comma into tResult
590+
put ((the number of chars of tPreviousLineIndent + tIndentCurrentLineRemoves + tIndentPreviousLineAdds) - the number of chars of tCurrentLineIndent) into tResult
583591

584592
-- Finally, calculate the expected next indent.
585593
local tNewIndent
@@ -596,6 +604,13 @@ private function textFormatLine pLine, pTextLines, @xPreviousLine
596604
put textFormatIndentLineAdds(pTextLines[pLine]) into tIndentCurrentLineAdds
597605
end if
598606

607+
if tPreviousLineWasContinued then
608+
local tCombinedLine
609+
put tPreviousLine && pTextLines[pLine] into tCombinedLine
610+
replace "\" with empty in tCombinedLine
611+
add textFormatIndentLineAdds(tCombinedLine) to tIndentCurrentLineAdds
612+
end if
613+
599614
if tIndentCurrentLineAdds < 0 then
600615
repeat -tIndentCurrentLineAdds times
601616
delete char 1 of tNewIndent
@@ -606,7 +621,7 @@ private function textFormatLine pLine, pTextLines, @xPreviousLine
606621
end repeat
607622
end if
608623

609-
put tNewIndent after tResult
624+
put comma & tNewIndent after tResult
610625

611626
if token 1 of pTextLines[pLine] is not empty then
612627
put pLine into xPreviousLine
@@ -2505,22 +2520,34 @@ end dragEnd
25052520
# Description
25062521
## Returns a line stripped of comments at the end of the line
25072522
private function lineStripComments pLine
2508-
local tOffset,tCommentChar
2509-
put offset("#", pLine) into tOffset
2510-
if tOffset = 0 then
2511-
put offset("--", pLine) into tOffset
2512-
put "--" into tCommentChar
2513-
else
2514-
put "#" into tCommentChar
2515-
end if
2523+
local tLine
2524+
split pLine by quote
2525+
2526+
repeat with tIndex = 1 to the number of elements of pLine step 2
2527+
local tDecommented
2528+
put __StripComment(pLine[tIndex]) into tDecommented
2529+
put tDecommented after tLine
2530+
if pLine[tIndex] is tDecommented and \
2531+
tIndex is not the number of elements of pLine then
2532+
put quote & pLine[tIndex+1] & quote after tLine
2533+
end if
2534+
end repeat
25162535

2517-
if tCommentChar is empty then
2518-
return pLine
2519-
else
2520-
return char 1 to tOffset of pLine
2521-
end if
2536+
return tLine
25222537
end lineStripComments
25232538

2539+
private function __StripComment pLine
2540+
local tOffset
2541+
repeat for each word tComment in "# -- // /*"
2542+
put offset(tComment, pLine) into tOffset
2543+
if tOffset is not 0 then
2544+
delete char tOffset to -1 of pLine
2545+
end if
2546+
end repeat
2547+
2548+
return pLine
2549+
end __StripComment
2550+
25242551
################################################################################
25252552

25262553
command actionCopy

notes/bugfix-19951.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix a range of edge case indentation issues related to line continuation

notes/bugfix-8228.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Indent scripts correctly when a comment is after the line continuation character
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
repeat \
2+
forever
3+
-- code
4+
end repeat
5+
6+
switch \
7+
tVar
8+
case "foo"
9+
-- code
10+
break
11+
case \
12+
"foo"
13+
-- code
14+
break
15+
default
16+
-- code
17+
break
18+
end switch
19+
20+
try
21+
-- code
22+
catch \
23+
tError
24+
-- code
25+
end try
26+
27+
if something \
28+
then
29+
-- code
30+
end if
31+
32+
put "foo" &\
33+
"bar"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (gDatf[tPost,i] <> "") and \
2+
(((gPoly[tPost] = "") and (gSurf[tPost] = gSurg[tPost,i])) or ((gPoly[tPost] <> "") and (gPoly[tPost] = gPolz[tPost,i]))) and \
3+
(gFra1[tPost,i] <> "") and (gFra2[tPost,i] <> "") and \
4+
((gPos2[tPost,i] <> "") or (quote is in gPos2[tPost,i])) and ((gPos3[tPost,i] <> "") or (quote is in gPos3[tPost,i])) and ((gPos4[tPost,i] <> "") or (quote is in gPos4[tPost,i])) and ((gPos5[tPost,i] <> "") or (quote is in gPos5[tPost,i])) and \
5+
(abs(gDist[tPost]) - abs(gDisu[tPost,i]) <= 0) and \ -- (abs(abs(gDist[tPost]) - abs(gDisu[tPost,i])) <= 440)
6+
(gBea5[tPost,i] <= 2.0) then
7+
put tab & gDatf[tPost,i] & tab & gPos2[tPost,i] & gPos3[tPost,i] & gPos4[tPost,i] & gPos5[tPost,i] & tab & gSco1[tPost,i] & tab & gSco2[tPost,i] & tab & gSco3[tPost,i] & tab & gOdds[tPost] & return after fld "Indey"
8+
end if
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
on Foo pParam
2+
-- code
3+
end Foo
4+
5+
command Foo pParam
6+
-- code
7+
end Foo
8+
9+
private command Foo pParam
10+
-- code
11+
end Foo
12+
13+
private on Foo pParam
14+
-- code
15+
end Foo
16+
17+
before Foo pParam
18+
-- code
19+
end Foo
20+
21+
after Foo pParam
22+
-- code
23+
end Foo
24+
25+
function Foo pParam
26+
-- code
27+
end Foo
28+
29+
private function Foo pParam
30+
-- code
31+
end Foo
32+
33+
setProp Foo pParam
34+
-- code
35+
end Foo
36+
37+
getProp Foo pParam
38+
-- code
39+
end Foo
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
if foo() then
2+
-- code
3+
else if bar() then
4+
-- code
5+
else
6+
-- code
7+
end if
8+
9+
if foo()
10+
then -- code
11+
else if bar() then
12+
-- code
13+
else
14+
-- code
15+
end if
16+
17+
if foo()
18+
then code
19+
else if bar() then
20+
-- code
21+
else code
22+
23+
if foo()
24+
then code
25+
else code
26+
27+
if foo() then code else code
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
repeat forever
2+
-- code
3+
end repeat
4+
5+
repeat while foo()
6+
-- code
7+
end repeat
8+
9+
repeat until foo()
10+
-- code
11+
end repeat
12+
13+
repeat 3
14+
-- code
15+
end repeat
16+
17+
repeat for 3
18+
-- code
19+
end repeat
20+
21+
repeat for 3 times
22+
-- code
23+
end repeat
24+
25+
repeat 3 times
26+
-- code
27+
end repeat
28+
29+
repeat with X = 1 to 5
30+
-- code
31+
end repeat
32+
33+
repeat with X = 1 to 5 step 2
34+
-- code
35+
end repeat
36+
37+
repeat with X = 5 down to 1
38+
-- code
39+
end repeat
40+
41+
repeat with X = 5 down to 1 step 2
42+
-- code
43+
end repeat
44+
45+
repeat for each line tLine in tFoo
46+
-- code
47+
end repeat
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
switch tFoo
2+
case "foo"
3+
-- code
4+
break
5+
default
6+
-- code
7+
break
8+
end switch
9+
10+
switch
11+
case tFoo begins with "foo"
12+
-- code
13+
break
14+
default
15+
-- code
16+
break
17+
end switch
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
try
2+
-- code
3+
catch tError
4+
-- code
5+
finally
6+
-- code
7+
end try

0 commit comments

Comments
 (0)