summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorsumneko <sumneko@hotmail.com>2019-04-15 14:46:03 +0800
committersumneko <sumneko@hotmail.com>2019-04-15 14:46:03 +0800
commit31cb7ffd42ba378b1d8f7688c81a0af8562d2d86 (patch)
tree9c1f70ef88e11b0f149e744e92eb9e305c330a0c /server
parentdb94d34ffad249c83022ef861677dfda7c6e3f36 (diff)
downloadlua-language-server-31cb7ffd42ba378b1d8f7688c81a0af8562d2d86.zip
诊断支持重复索引
Diffstat (limited to 'server')
-rw-r--r--server/locale/en-US/script.lni1
-rw-r--r--server/locale/zh-CN/script.lni1
-rw-r--r--server/src/core/diagnostics.lua52
-rw-r--r--server/test/diagnostics/init.lua8
4 files changed, 62 insertions, 0 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni
index 1bec292e..5da02108 100644
--- a/server/locale/en-US/script.lni
+++ b/server/locale/en-US/script.lni
@@ -6,6 +6,7 @@ DIAG_UNDEF_ENV_CHILD = 'Undefined variable `{}` (overloaded `_ENV` ).'
DIAG_UNDEF_FENV_CHILD = 'Undefined variable `{}` (inside module).'
DIAG_UNUSED_LABEL = 'Unused label `{}`.'
DIAG_REDEFINED_LOCAL = 'Redefined local `{}`.'
+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_AMBIGUITY_1 = 'Compute `0 {op} {num}` first. You may need to add brackets.'
diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni
index cca71763..a0fe9701 100644
--- a/server/locale/zh-CN/script.lni
+++ b/server/locale/zh-CN/script.lni
@@ -6,6 +6,7 @@ DIAG_UNDEF_ENV_CHILD = '未定义的变量 `{}`(重载了 `_ENV` )。'
DIAG_UNDEF_FENV_CHILD = '未定义的变量 `{}`(处于模块中)。'
DIAG_UNUSED_LABEL = '未使用的标签 `{}`。'
DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。'
+DIAG_DUPLICATE_INDEX = '重复的索引 `{}`。'
DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。'
DIAG_OVER_MAX_ARGS = '函数只接收 {:d} 个参数,但你传了 {:d} 个。'
DIAG_AMBIGUITY_1 = '会优先运算 `0 {op} {num}`,你可能需要加个括号。'
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index 7bf43675..57703bea 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -272,6 +272,50 @@ function mt:searchLowercaseGlobal(callback)
end)
end
+function mt:searchDuplicateIndex(callback)
+ self.vm:eachSource(function (source)
+ if source.type ~= 'table' then
+ return
+ end
+ local mark = {}
+ for _, obj in ipairs(source) do
+ if obj.type == 'pair' then
+ local key = obj[1]
+ local name
+ if key.index then
+ if key.type == 'string' then
+ name = key[1]
+ end
+ elseif key.type == 'name' then
+ name = key[1]
+ end
+ if name then
+ if mark[name] then
+ mark[name][#mark[name]+1] = key
+ else
+ mark[name] = { key }
+ end
+ end
+ end
+ end
+ for name, defs in pairs(mark) do
+ if #defs > 1 then
+ local related = {}
+ for i = 1, #defs do
+ related[i] = {
+ start = defs[i].start,
+ finish = defs[i].finish,
+ uri = self.uri,
+ }
+ end
+ for i = 2, #defs do
+ callback(defs[i].start, defs[i].finish, name, related)
+ end
+ end
+ end
+ end)
+end
+
function mt:doDiagnostics(func, code, callback)
if config.config.diagnostics.disable[code] then
return
@@ -414,5 +458,13 @@ return function (vm, lines, uri)
}
end
end)
+ -- 构建表时重复定义field
+ session:doDiagnostics(session.searchDuplicateIndex, 'duplicate-index', function (key, related)
+ return {
+ level = DiagnosticSeverity.Warning,
+ message = lang.script('DIAG_DUPLICATE_INDEX', key),
+ related = related,
+ }
+ end)
return session.datas
end
diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua
index 15b13c03..be54188f 100644
--- a/server/test/diagnostics/init.lua
+++ b/server/test/diagnostics/init.lua
@@ -255,3 +255,11 @@ TEST [[
TEST [[
X = table[<!x!>]
]]
+
+TEST [[
+return {
+ x = 1,
+ y = 2,
+ <!x!> = 3,
+}
+]]