diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 13:52:11 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 13:52:11 +0800 |
commit | 743d1a3ea8aa964d8aabf13e597fef21bfb361ec (patch) | |
tree | 08b6f101aeb152060abd9b9847c8936d32874813 /server/src/matcher | |
parent | c4ce0145a7fd35fe1fa628b862e9963ffc8243cc (diff) | |
download | lua-language-server-743d1a3ea8aa964d8aabf13e597fef21bfb361ec.zip |
支持检查未使用的局部变量,并整理代码
Diffstat (limited to 'server/src/matcher')
-rw-r--r-- | server/src/matcher/compile.lua | 2 | ||||
-rw-r--r-- | server/src/matcher/diagnostics.lua | 82 |
2 files changed, 40 insertions, 44 deletions
diff --git a/server/src/matcher/compile.lua b/server/src/matcher/compile.lua index 68fdf9b6..02db109c 100644 --- a/server/src/matcher/compile.lua +++ b/server/src/matcher/compile.lua @@ -227,7 +227,7 @@ function mt:searchReturn(action) end end -function mt:setTable(var, tbl, mode) +function mt:setTable(var, tbl) if not var or not tbl then return end diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua index 9cd4f6ee..ffdab80e 100644 --- a/server/src/matcher/diagnostics.lua +++ b/server/src/matcher/diagnostics.lua @@ -1,49 +1,45 @@ -local DiagnosticSeverity = { - Error = 1, - Warning = 2, - Information = 3, - Hint = 4, -} - --[[ -/** - * Represents a related message and source code location for a diagnostic. This should be - * used to point to code locations that cause or related to a diagnostics, e.g when duplicating - * a symbol in a scope. - */ -export interface DiagnosticRelatedInformation { - /** - * The location of this related diagnostic information. - */ - location: Location; - - /** - * The message of this related diagnostic information. - */ - message: string; +data = { + start = 1, + finish = 1, + level = 'Error' or 'Warning' or 'Information' or 'Hint', + code = '', + message = '', } ]]-- -return function (ast, results) - local diagnostics = {} - - diagnostics[1] = { - range = { - start = { - line = 0, - character = 0, - }, - ['end'] = { - line = 0, - character = 10, - }, - }, - severity = DiagnosticSeverity.Warning, - code = 'I am code', - source = 'I am source', - message = 'I am message', - relatedInformation = nil, - } +local function searchUnusedLocals(results, callback) + for _, var in ipairs(results.vars) do + if var.type ~= 'local' then + goto NEXT_VAR + end + if var.key == 'self' + or var.key == '_' + or var.key == '_ENV' + then + goto NEXT_VAR + end + for _, info in ipairs(var) do + if info.type == 'get' then + goto NEXT_VAR + end + end + callback(var.source.start, var.source.finish, var.key) + ::NEXT_VAR:: + end +end - return diagnostics +return function (ast, results, lines) + local datas = {} + -- 搜索未使用的局部变量 + searchUnusedLocals(results, function (start, finish, code) + datas[#datas+1] = { + start = start, + finish = finish, + level = 'Warning', + code = code, + message = 'Unused local', -- LOCALE + } + end) + return datas end |