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

Commit a3aed54

Browse files
authored
Merge pull request #2046 from BerndN/patch-8
[Bug 21896] Fix Autocomplete IF-structs
2 parents c4a3a28 + ceb5870 commit a3aed54

2 files changed

Lines changed: 310 additions & 0 deletions

File tree

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

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,17 @@ function autoCompleteCompletionRequired pLineNumber, pStructureType, pStructureN
18661866
local tScript
18671867
put textGetScript() into tScript
18681868

1869+
put autoCompleteInSlashCommentOrOutOfHandler(tScript, pLineNumber) into tOutOfBounds
1870+
if tOutOfBounds then
1871+
return false
1872+
end if
1873+
1874+
if pStructureType is "if" then
1875+
local tRequired
1876+
put autoCompleteIFsAreBalanced(pLineNumber, tScript) into tRequired
1877+
return tRequired
1878+
end if
1879+
18691880
local tInitialDepth
18701881
put autoCompleteSearchContext(tScript, pLineNumber, pStructureType, pStructureName, pCompletion) into tInitialDepth
18711882
put line pLineNumber + 1 to -1 of tScript into tScript
@@ -1924,6 +1935,304 @@ function autoCompleteCompletionRequired pLineNumber, pStructureType, pStructureN
19241935
return true
19251936
end autoCompleteCompletionRequired
19261937

1938+
function autoCompleteInSlashCommentOrOutOfHandler @pScript, pLineNumber
1939+
local tScript, tSlashStart, tSlashEnd, tNumItems, tNumLines, tSelChar
1940+
1941+
put slash & "*" into tSlashStart
1942+
put "*" & slash into tSlashEnd
1943+
1944+
put word 2 of the selectedChunk into tSelChar
1945+
if tSelChar is empty then
1946+
return empty
1947+
end if
1948+
1949+
set the lineDelimiter to tSlashStart
1950+
set the itemDelimiter to tSlashEnd
1951+
1952+
put the number of lines of char 1 to tSelChar of pScript into tNumLines
1953+
put the number of items of char 1 to tSelChar of pScript into tNumItems
1954+
1955+
set the lineDelimiter to return
1956+
set the itemDelimiter to comma
1957+
1958+
if tNumLines is not tNumItems then
1959+
return true
1960+
end if
1961+
1962+
local tLine, tHandlerStartsA, tUnNamedStructures
1963+
1964+
put "on,function,command,setProp,getProp,after,before,private" into tHandlerStartsA
1965+
split tHandlerStartsA by comma as set
1966+
1967+
put "if,repeat,try,switch,case" into tUnNamedStructures
1968+
1969+
put line pLineNumber of pScript into tLine
1970+
if tHandlerStartsA[token 1 of tLine] then return false
1971+
1972+
local tLineOffsetEnd, tLineOffsetEndSkip, tOffsetStartNameLine, tOffsetStartSkip, tLookFor, tHandlerName
1973+
put "end" into tLookFor
1974+
put empty into tHandlerName
1975+
put 0 into tOffsetStartSkip
1976+
1977+
put pLineNumber into tLineOffsetEndSkip
1978+
1979+
repeat
1980+
put lineOffset(tLookFor, pScript, tLineOffsetEndSkip) into tLineOffsetEnd
1981+
if tLineOffsetEnd = 0 then exit repeat
1982+
add tLineOffsetEnd to tLineOffsetEndSkip
1983+
1984+
put line tLineOffsetEndSkip of pScript into tLine
1985+
if token 1 of tLine is not tLookFor then next repeat
1986+
if token 2 of tLine is among the items of tUnNamedStructures then
1987+
next repeat
1988+
else
1989+
put token 2 of tLine into tHandlerName
1990+
exit repeat
1991+
end if
1992+
end repeat
1993+
1994+
if tHandlerName is empty then return true
1995+
1996+
repeat
1997+
put lineOffset(tHandlerName, pScript, tOffsetStartSkip) into tOffsetStartNameLine
1998+
if tOffsetStartNameLine = 0 then exit repeat
1999+
add tOffsetStartNameLine to tOffsetStartSkip
2000+
2001+
put line tOffsetStartSkip of pScript into tLine
2002+
if tHandlerStartsA[token 1 of tLine] then
2003+
if token 1 of tLine is "private" then
2004+
if token 3 of tLine is tHandlerName then
2005+
exit repeat
2006+
else
2007+
next repeat
2008+
end if
2009+
else
2010+
if token 2 of tLine is tHandlerName then
2011+
exit repeat
2012+
else
2013+
next repeat
2014+
end if
2015+
end if
2016+
end if
2017+
end repeat
2018+
2019+
if tOffsetStartSkip < pLineNumber then return false
2020+
2021+
return true
2022+
end autoCompleteInSlashCommentOrOutOfHandler
2023+
2024+
function autoCompleteIFsAreBalanced pLineNumber, pScript
2025+
local tHandlerStarts, tHandlerName
2026+
2027+
put "on,function,command,setProp,getProp,after,before,private" into tHandlerStarts
2028+
2029+
--put textGetScript() into tScript
2030+
2031+
repeat with i = pLineNumber down to 1
2032+
if token 1 of line i of pScript is among the items of tHandlerStarts then
2033+
exit repeat
2034+
end if
2035+
end repeat
2036+
2037+
delete line 1 to i - 1 of pScript
2038+
2039+
if token 1 of line 1 of pScript is "private" then
2040+
put token 3 of line 1 of pScript into tHandlerName
2041+
else
2042+
put token 2 of line 1 of pScript into tHandlerName
2043+
end if
2044+
2045+
local tCounter
2046+
repeat for each line aLine in pScript
2047+
add 1 to tCounter
2048+
if token 1 of aLine is "end" and token 2 of aLine is tHandlerName then
2049+
exit repeat
2050+
end if
2051+
end repeat
2052+
2053+
put line 1 to tCounter of pScript into pScript
2054+
2055+
-- remove slash asterisk comments
2056+
local tBeginAsterix, tEndAsterix, tCharBegin, tCharEnd
2057+
put slash & "*" into tBeginAsterix
2058+
put "*" & slash into tEndAsterix
2059+
2060+
repeat
2061+
put offset(tBeginAsterix, pScript) into tCharBegin
2062+
if tCharBegin is 0 then exit repeat
2063+
put offset(tEndAsterix, pScript) into tCharEnd
2064+
delete char tCharBegin to tCharEnd of pScript
2065+
end repeat
2066+
2067+
-- remove comments after backslash
2068+
-- and remove line-continuation
2069+
local tBackSlashStart, tReturnStart
2070+
put 0 into tBackSlashStart
2071+
repeat
2072+
put offset(backslash, pScript, tBackSlashStart) into tBackSlashStart
2073+
if tBackSlashStart is 0 then exit repeat
2074+
put offset(return, pScript, tBackSlashStart) into tReturnStart
2075+
delete char tBackSlashStart to tBackSlashStart + tReturnStart of pScript
2076+
put 0 into tBackSlashStart
2077+
end repeat
2078+
2079+
replace ";" with return in pScript -- remove ";"
2080+
2081+
local tDepth, tToken1, tElse, tIf_Then_CodeFlag, tElse_IFFlag
2082+
put "e" & "lse" into tElse -- avoid "tokenization"
2083+
put false into tIf_Then_CodeFlag
2084+
put false into tElse_IFFlag
2085+
put 0 into tDepth
2086+
2087+
repeat for each line aLine in pScript
2088+
put token 1 of aLine into tToken1
2089+
if tToken1 is empty then next repeat
2090+
2091+
######### get rid of one-liners
2092+
-- if foo() then code else code
2093+
if tToken1 is "if" and token -1 of aLine is not "then" and tElse is among the tokens of aLine then
2094+
next repeat
2095+
end if
2096+
2097+
-- if foo()
2098+
-- then code else code
2099+
if tToken1 is "then" and token -1 of aLine is not "then" and tElse is among the tokens of aLine then
2100+
next repeat
2101+
end if
2102+
######### end get rid of one-liners
2103+
2104+
######### special case of if-then-code
2105+
-- if bar() then code <-- can go on with else or end here
2106+
-- else
2107+
-- code
2108+
-- end if
2109+
-- or
2110+
-- if bar() then code <-- can end with else_code
2111+
-- -- optionally some lines else-if-then
2112+
-- else code
2113+
2114+
if tIf_Then_CodeFlag then
2115+
if tToken1 is tElse then
2116+
2117+
if the number of tokens of aLine = 1 then
2118+
put false into tIf_Then_CodeFlag
2119+
add 1 to tDepth
2120+
next repeat
2121+
end if
2122+
2123+
if token 2 of aLine is "if" and token -1 of aLine is "then" then
2124+
put false into tIf_Then_CodeFlag
2125+
add 1 to tDepth
2126+
next repeat
2127+
end if
2128+
2129+
if token 2 of aLine is "if" and token -1 of aLine is not "then" then
2130+
next repeat
2131+
end if
2132+
2133+
if token 2 of aLine is not "if" and the number of tokens of aLine > 1 then
2134+
put false into tIf_Then_CodeFlag
2135+
next repeat
2136+
end if
2137+
2138+
else
2139+
put false into tIf_Then_CodeFlag
2140+
end if
2141+
end if
2142+
2143+
-- condition of entering special case if-then-code
2144+
if tToken1 is "if" and "then" is among the tokens of aLine and \
2145+
token -1 of aLine is not "then" and tElse is not among the tokens of aLine then
2146+
put true into tIf_Then_CodeFlag
2147+
next repeat
2148+
end if
2149+
2150+
if tToken1 is "then" and \
2151+
token -1 of aLine is not "then" and tElse is not among the tokens of aLine then
2152+
put true into tIf_Then_CodeFlag
2153+
next repeat
2154+
end if
2155+
######### end special case: if-then-code
2156+
2157+
if tToken1 is "if" and token -1 of aLine is "then" then
2158+
add 1 to tDepth
2159+
end if
2160+
2161+
if tToken1 is "then" and token -1 of aLine is "then" then
2162+
add 1 to tDepth
2163+
end if
2164+
2165+
####################
2166+
-- if foo() then
2167+
-- code else code <--
2168+
if tElse is among the tokens of aLine and \
2169+
(tToken1 is not tElse) and \
2170+
(token -1 of aLine is not tElse) and \
2171+
(tToken1 is not "then" and tToken1 is not "if") then
2172+
subtract 1 from tDepth
2173+
end if
2174+
####################
2175+
2176+
####################
2177+
-- if foo() then
2178+
-- code
2179+
-- else code <--
2180+
if tToken1 is tElse and token -1 of aLine is not tElse and "if" is not among the tokens of aLine then
2181+
subtract 1 from tDepth
2182+
end if
2183+
####################
2184+
2185+
####################
2186+
-- special case else-if-code
2187+
-- if foo() then
2188+
-- code
2189+
-- else if bar() then code <-- can end here
2190+
-- or
2191+
-- if foo() then
2192+
-- code
2193+
-- else if bar() then code
2194+
-- else <-- or go on with else
2195+
-- code
2196+
2197+
if tElse_IFFlag then
2198+
if tToken1 is tElse then
2199+
2200+
if token 2 of aLine is "if" and token -1 of aLine is not "then" then
2201+
next repeat
2202+
end if
2203+
2204+
put false into tElse_IFFlag
2205+
next repeat
2206+
2207+
else
2208+
put false into tElse_IFFlag
2209+
subtract 1 from tDepth
2210+
end if
2211+
end if
2212+
2213+
-- special case else-if-code
2214+
-- also tests else-if without "then", "then" is in this case on following line
2215+
if tToken1 is tElse and token 2 of aLine is "if" and token -1 of aLine is not "then" then
2216+
put true into tElse_IFFlag
2217+
next repeat
2218+
end if
2219+
-- end special case else-if-code
2220+
####################
2221+
2222+
if tToken1 is "end" and token 2 of aLine is "if" then
2223+
subtract 1 from tDepth
2224+
end if
2225+
2226+
end repeat
2227+
2228+
if tDepth is 0 then
2229+
return false
2230+
else
2231+
return true
2232+
end if
2233+
2234+
end autoCompleteIFsAreBalanced
2235+
19272236
# Returns
19282237
# A comma separated list of the autocompleteable structures that have names. This is currently just handlers.
19292238
private function autoCompleteNamedStructures

notes/bugfix-21896.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix Autocomplete IF-structs

0 commit comments

Comments
 (0)