diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-04 21:11:25 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-04 21:11:25 +0800 |
commit | 8f505a0b999d1432dfc107133e7ac2ba591cbed2 (patch) | |
tree | 2c8fa5d0f71232fa18811dcf1750a209f1502e06 | |
parent | 6376ac1a2d98280304221243b00a1bc8cac74dc3 (diff) | |
download | lua-language-server-8f505a0b999d1432dfc107133e7ac2ba591cbed2.zip |
更新实现
-rw-r--r-- | server-beta/src/core/diagnostics/redefined-local.lua | 33 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/trailing-space.lua | 58 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/undefined-global.lua | 6 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/unused-label.lua | 23 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/unused-local.lua | 2 | ||||
-rw-r--r-- | server-beta/src/parser/compile.lua | 2 | ||||
-rw-r--r-- | server-beta/src/searcher/eachRef.lua | 7 | ||||
-rw-r--r-- | server-beta/test/diagnostics/init.lua | 6 |
8 files changed, 125 insertions, 12 deletions
diff --git a/server-beta/src/core/diagnostics/redefined-local.lua b/server-beta/src/core/diagnostics/redefined-local.lua index b3d19c21..a1b0e77a 100644 --- a/server-beta/src/core/diagnostics/redefined-local.lua +++ b/server-beta/src/core/diagnostics/redefined-local.lua @@ -1,3 +1,32 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + guide.eachSourceType(ast.ast, 'local', function (source) + local name = source[1] + if name == '_' + or name == '_ENV' then + return + end + local exist = guide.getLocal(source, name, source.start) + if exist then + callback { + start = source.start, + finish = source.finish, + message = lang.script('DIAG_REDEFINED_LOCAL', name), + related = { + { + start = exist.start, + finish = exist.finish, + uri = uri, + } + }, + } + end + end) end diff --git a/server-beta/src/core/diagnostics/trailing-space.lua b/server-beta/src/core/diagnostics/trailing-space.lua index b3d19c21..ef7fbef1 100644 --- a/server-beta/src/core/diagnostics/trailing-space.lua +++ b/server-beta/src/core/diagnostics/trailing-space.lua @@ -1,3 +1,57 @@ -return function () - +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' + +local function isInString(ast, offset) + local result = false + guide.eachSourceType(ast, 'string', function (source) + if offset >= source.start and offset <= source.finish then + result = true + end + end) + return result +end + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + local lines = files.getLines(uri) + local text = files.getText(uri) + + for i = 1, #lines do + local start = lines[i].start + 1 + local finish = lines[i].finish - 1 + local line = text:sub(start, finish) + if line:find '^[ \t]+[\r\n]*$' then + local offset = guide.offsetOf(lines, i-1, start-1) + if isInString(ast.ast, offset) then + goto NEXT_LINE + end + callback { + start = start, + finish = finish, + message = lang.script.DIAG_LINE_ONLY_SPACE, + } + goto NEXT_LINE + end + + local pos = line:find '[ \t]+[\r\n]*$' + if pos then + start = start + pos - 1 + local offset = guide.offsetOf(lines, i-1, start-1) + if isInString(ast.ast, offset) then + goto NEXT_LINE + end + callback { + start = start, + finish = finish, + message = lang.script.DIAG_LINE_POST_SPACE, + } + goto NEXT_LINE + end + + ::NEXT_LINE:: + end end diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua index 888064dc..6c9abaca 100644 --- a/server-beta/src/core/diagnostics/undefined-global.lua +++ b/server-beta/src/core/diagnostics/undefined-global.lua @@ -1,7 +1,5 @@ local files = require 'files' -local guide = require 'parser.guide' local searcher = require 'searcher' -local define = require 'proto.define' local lang = require 'language' local library = require 'library' local config = require 'config' @@ -12,9 +10,9 @@ return function (uri, callback) return end - -- 先遍历一次所有该文件中的全局变量 + -- 先遍历一次该文件中的全局变量 -- 如果变量有 set 行为,则做标记 - -- 然后再遍历一次,对所有的行为打上标记 + -- 然后再遍历一次,对所有的行为打上相同标记 local hasSet = {} searcher.eachGlobal(ast.ast, function (info) if hasSet[info.source] ~= nil then diff --git a/server-beta/src/core/diagnostics/unused-label.lua b/server-beta/src/core/diagnostics/unused-label.lua index b3d19c21..e6d998ba 100644 --- a/server-beta/src/core/diagnostics/unused-label.lua +++ b/server-beta/src/core/diagnostics/unused-label.lua @@ -1,3 +1,22 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local define = require 'proto.define' +local lang = require 'language' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + guide.eachSourceType(ast.ast, 'label', function (source) + if not source.ref then + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_UNUSED_LABEL', source[1]), + } + end + end) end diff --git a/server-beta/src/core/diagnostics/unused-local.lua b/server-beta/src/core/diagnostics/unused-local.lua index d110a714..e6052e39 100644 --- a/server-beta/src/core/diagnostics/unused-local.lua +++ b/server-beta/src/core/diagnostics/unused-local.lua @@ -8,7 +8,7 @@ local function hasGet(loc) return false end for _, ref in ipairs(loc.ref) do - if ref.mode == 'get' then + if ref.type == 'getlocal' then return true end end diff --git a/server-beta/src/parser/compile.lua b/server-beta/src/parser/compile.lua index 9db0c3e3..81a1ba4f 100644 --- a/server-beta/src/parser/compile.lua +++ b/server-beta/src/parser/compile.lua @@ -360,6 +360,8 @@ local function compileGoTo(obj) } return end + label.ref = obj + -- 如果有局部变量在 goto 与 label 之间声明, -- 并在 label 之后使用,则算作语法错误 diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua index 7b2e3a27..649206e6 100644 --- a/server-beta/src/searcher/eachRef.lua +++ b/server-beta/src/searcher/eachRef.lua @@ -274,7 +274,12 @@ local function ofGoTo(source, callback) end local function ofLabel(source, callback) - + if source.ref then + callback { + source = source.ref, + mode = 'get', + } + end end local function ofMain(source, callback) diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua index b0438eb2..0c2b077f 100644 --- a/server-beta/test/diagnostics/init.lua +++ b/server-beta/test/diagnostics/init.lua @@ -143,6 +143,12 @@ local _ENV ]] TEST [[ +local x +return x, function (<!x!>) +end +]] + +TEST [[ print(1) _ENV = nil ]] |