diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 14:56:13 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 14:56:13 +0800 |
commit | faffcd9d8d94ddc965adce302850195d8390d170 (patch) | |
tree | 9be5ffd74e831c95fbb108489fec6c91939216d1 | |
parent | e228f090618a68a96fee30c40b9d7d10e04ed12d (diff) | |
download | lua-language-server-faffcd9d8d94ddc965adce302850195d8390d170.zip |
重定义的局部变量
-rw-r--r-- | server/src/matcher/compile.lua | 1 | ||||
-rw-r--r-- | server/src/matcher/diagnostics.lua | 32 | ||||
-rw-r--r-- | server/src/method/textDocument/publishDiagnostics.lua | 3 |
3 files changed, 31 insertions, 5 deletions
diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua index 02db109c..9e2da220 100644 --- a/server/src/matcher/compile.lua +++ b/server/src/matcher/compile.lua @@ -64,6 +64,7 @@ function mt:createLocal(key, source, var) var = self:createVar('local', key, source) self:addInfo(var, 'local', source) end + var.redefinition = self.env.var[key] self.env.var[key] = var return var end diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua index e5daea31..62bc3cd1 100644 --- a/server/src/matcher/diagnostics.lua +++ b/server/src/matcher/diagnostics.lua @@ -92,9 +92,23 @@ local function serachSpaces(lines, callback) end end +local function searchRedefinition(results, callback) + for _, var in ipairs(results.vars) do + if var.type ~= 'local' then + goto NEXT_VAR + end + local shadow = var.redefinition + if not shadow then + goto NEXT_VAR + end + callback(var.source.start, var.source.finish, var.key) + ::NEXT_VAR:: + end +end + return function (ast, results, lines) local datas = {} - -- 搜索未使用的局部变量 + -- 未使用的局部变量 searchUnusedLocals(results, function (start, finish, code) datas[#datas+1] = { start = start, @@ -104,7 +118,7 @@ return function (ast, results, lines) message = 'Unused local', -- LOCALE } end) - -- 搜索读取未定义全局变量 + -- 读取未定义全局变量 searchUndefinedGlobal(results, function (start, finish, code) datas[#datas+1] = { start = start, @@ -114,7 +128,7 @@ return function (ast, results, lines) message = 'Undefined global', -- LOCALE } end) - -- 搜索未使用的Label + -- 未使用的Label searchUnusedLabel(results, function (start, finish, code) datas[#datas+1] = { start = start, @@ -124,7 +138,7 @@ return function (ast, results, lines) message = 'Unused label', -- LOCALE } end) - -- 所搜只有空格与制表符的行,以及后置了空格的行 + -- 只有空格与制表符的行,以及后置空格 serachSpaces(lines, function (start, finish, message) datas[#datas+1] = { start = start, @@ -133,5 +147,15 @@ return function (ast, results, lines) message = message, } end) + -- 重定义局部变量 + searchRedefinition(results, function (start, finish, code) + datas[#datas+1] = { + start = start, + finish = finish, + level = 'Warning', + code = code, + message = 'Redefined local', + } + end) return datas end diff --git a/server/src/method/textDocument/publishDiagnostics.lua b/server/src/method/textDocument/publishDiagnostics.lua index 5f3d1d28..08910ea5 100644 --- a/server/src/method/textDocument/publishDiagnostics.lua +++ b/server/src/method/textDocument/publishDiagnostics.lua @@ -49,6 +49,7 @@ local function createInfo(data, lines) severity = DiagnosticSeverity[data.level], code = data.code, message = data.message, + relatedInformation = data.relatedInformation, } return diagnostic end @@ -64,7 +65,7 @@ return function (lsp, params) -- 返回空表以清空之前的结果 return {} end - + local diagnostics = {} for i, data in ipairs(datas) do diagnostics[i] = createInfo(data, lines) |