diff options
-rw-r--r-- | server/src/core/diagnostics.lua | 31 | ||||
-rw-r--r-- | server/src/method/workspace/executeCommand.lua | 49 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 5 |
3 files changed, 60 insertions, 25 deletions
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 77898b17..1a72d504 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -271,19 +271,36 @@ local opMap = { ['..'] = true, } +local literalMap = { + ['number'] = true, + ['boolean'] = true, + ['string'] = true, + ['table'] = true, +} + function mt:searchAmbiguity1(callback) self.vm:eachSource(function (source) if source.op ~= 'or' then return end - -- (a or 0) + c --> a or (0 + c) + local first = source[1] + local second = source[2] -- a + (b or 0) --> (a + b) or 0 - for x = 1, 2 do - local y = x % 2 + 1 - local exp = source[x] - local other = source[y] - if opMap[exp.op] and not opMap[other.op] then - callback(source.start, source.finish, exp.start, exp.finish) + do + if opMap[first.op] + and not second.op + and literalMap[second.type] + then + callback(source.start, source.finish, first.start, first.finish) + end + end + -- (a or 0) + c --> a or (0 + c) + do + if opMap[second.op] + and not first.op + and literalMap[second[1].type] + then + callback(source.start, source.finish, second.start, second.finish) end end end) diff --git a/server/src/method/workspace/executeCommand.lua b/server/src/method/workspace/executeCommand.lua index bec55e03..95d90fec 100644 --- a/server/src/method/workspace/executeCommand.lua +++ b/server/src/method/workspace/executeCommand.lua @@ -138,6 +138,13 @@ local opMap = { ['..'] = true, } +local literalMap = { + ['number'] = true, + ['boolean'] = true, + ['string'] = true, + ['table'] = true, +} + function command.solve(lsp, data) local uri = data.uri local vm, lines = lsp:getVM(uri) @@ -155,24 +162,30 @@ function command.solve(lsp, data) if source.op ~= 'or' then return end - for x = 1, 2 do - local y = x % 2 + 1 - local exp = source[x] - local other = source[y] - if opMap[exp.op] and not opMap[other.op] then - if x == 1 then - -- (a + b) or c --> a + (b or c) - return { - start = source[1][2].start, - finish = source[2].finish, - } - else - -- a or (b + c) --> (a or b) + c - return { - start = source[1].start, - finish = source[2][1].finish, - } - end + local first = source[1] + local second = source[2] + -- (a + b) or 0 --> a + (b or 0) + do + if opMap[first.op] + and not second.op + and literalMap[second.type] + then + return { + start = source[1][2].start, + finish = source[2].finish, + } + end + end + -- a or (b + c) --> (a or b) + c + do + if opMap[second.op] + and not first.op + and literalMap[second[1].type] + then + return { + start = source[1].start, + finish = source[2][1].finish, + } end end end) diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index ebdb917a..97cd6ccd 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -458,3 +458,8 @@ TEST [[ local x, y x = <!x + y or 0!> ]] + +TEST [[ +local x, y, z +x = x and y or '' .. z +]] |