summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-07-05 15:04:41 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-07-05 15:04:41 +0800
commitd91adfe4ebd18099fde3e4389655d4532fc9b00c (patch)
treed15de422cd614f13c72a43a8f9ad375c43eba2da /server
parentb1afe6e48fc454fcc767c6a009fa2f59697b38c1 (diff)
downloadlua-language-server-d91adfe4ebd18099fde3e4389655d4532fc9b00c.zip
扩展优先级
Diffstat (limited to 'server')
-rw-r--r--server/locale/en-US/script.lni2
-rw-r--r--server/locale/zh-CN/script.lni2
-rw-r--r--server/src/core/diagnostics.lua19
-rw-r--r--server/src/method/workspace/executeCommand.lua36
-rw-r--r--server/test/diagnostics/init.lua5
5 files changed, 44 insertions, 20 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni
index ca6c3120..5a008b2d 100644
--- a/server/locale/en-US/script.lni
+++ b/server/locale/en-US/script.lni
@@ -11,7 +11,7 @@ DIAG_DUPLICATE_INDEX = 'Duplicate index `{}`.'
DIAG_PREVIOUS_CALL = 'Parsed as function call for the previous line. It may be necessary to add a `;` before.'
DIAG_OVER_MAX_ARGS = 'The function takes only {:d} parameters, but you passed {:d}.'
DIAG_OVER_MAX_ARGS = 'Only has {} variables, but you set {} values.'
-DIAG_AMBIGUITY_1 = 'Compute `0 {op} {num}` first. You may need to add brackets.'
+DIAG_AMBIGUITY_1 = 'Compute `{}` first. You may need to add brackets.'
DIAG_LOWERCASE_GLOBAL = 'Global variable in lowercase initial.'
DIAG_EMPTY_BLOCK = 'Empty block.'
DIAG_DIAGNOSTICS = 'Diagnostics.'
diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni
index ed15ddf0..c41fd367 100644
--- a/server/locale/zh-CN/script.lni
+++ b/server/locale/zh-CN/script.lni
@@ -11,7 +11,7 @@ DIAG_DUPLICATE_INDEX = '重复的索引 `{}`。'
DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。'
DIAG_OVER_MAX_ARGS = '函数只接收 {:d} 个参数,但你传了 {:d} 个。'
DIAG_OVER_MAX_VALUES = '只有 {} 个变量,但你设置了 {} 个值。'
-DIAG_AMBIGUITY_1 = '会优先运算 `0 {op} {num}`,你可能需要加个括号。'
+DIAG_AMBIGUITY_1 = '会优先运算 `{}`,你可能需要加个括号。'
DIAG_LOWERCASE_GLOBAL = '首字母小写的全局变量'
DIAG_EMPTY_BLOCK = '空代码块'
DIAG_DIAGNOSTICS = '诊断'
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index 458a4686..239c8121 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -261,12 +261,15 @@ function mt:searchAmbiguity1(callback)
if source.op ~= 'or' then
return
end
- local exp = source[2]
- if exp.op ~= '+' and exp.op ~= '-' then
- return
- end
- if exp[1][1] == 0 and exp[2].type == 'number' then
- callback(source.start, source.finish, exp.op, exp[2][1])
+ -- (a or 0) + c --> a or (0 + c)
+ -- 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 exp.op and not other.op then
+ callback(source.start, source.finish, exp.start, exp.finish)
+ end
end
end)
end
@@ -755,9 +758,9 @@ return function (vm, lines, uri)
}
end)
-- x or 0 + 1
- session:doDiagnostics(session.searchAmbiguity1, 'ambiguity-1', function (op, num)
+ session:doDiagnostics(session.searchAmbiguity1, 'ambiguity-1', function (start, finish)
return {
- message = lang.script('DIAG_AMBIGUITY_1', op, num),
+ message = lang.script('DIAG_AMBIGUITY_1', lines.buf:sub(start, finish)),
}
end)
-- 不允许定义首字母小写的全局变量(很可能是拼错或者漏删)
diff --git a/server/src/method/workspace/executeCommand.lua b/server/src/method/workspace/executeCommand.lua
index f22590d5..9b234fc1 100644
--- a/server/src/method/workspace/executeCommand.lua
+++ b/server/src/method/workspace/executeCommand.lua
@@ -133,22 +133,38 @@ function command.solve(lsp, data)
local start = lines:position(data.range.start.line + 1, data.range.start.character + 1)
local finish = lines:position(data.range['end'].line + 1, data.range['end'].character)
- local source = vm:eachSource(function (source)
- if source.op ~= 'or' then
+ local result = vm:eachSource(function (source)
+ if not isContainPos(source, start, finish) then
return
end
- local exp = source[2]
- if exp.op ~= '+' and exp.op ~= '-' then
+ if source.op ~= 'or' then
return
end
- if exp[1][1] == 0 and exp[2].type == 'number' then
- if source.start == start and source.finish == finish then
- return source
+ for x = 1, 2 do
+ local y = x % 2 + 1
+ local exp = source[x]
+ local other = source[y]
+ if exp.op and not 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
end
end
end)
- if not source then
+ log.debug(table.dump(result))
+
+ if not result then
return
end
@@ -158,11 +174,11 @@ function command.solve(lsp, data)
changes = {
[uri] = {
{
- range = posToRange(lines, source.start, source.start - 1),
+ range = posToRange(lines, result.start, result.start - 1),
newText = '(',
},
{
- range = posToRange(lines, source[2][1].finish + 1, source[2][1].finish),
+ range = posToRange(lines, result.finish + 1, result.finish),
newText = ')',
},
}
diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua
index 62996054..ebdb917a 100644
--- a/server/test/diagnostics/init.lua
+++ b/server/test/diagnostics/init.lua
@@ -453,3 +453,8 @@ TEST [[
local x
x = <!x or 0 + 1!>
]]
+
+TEST [[
+local x, y
+x = <!x + y or 0!>
+]]