diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 16:38:02 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-07 16:38:02 +0800 |
commit | 2b03350caa83e66d6bb4cd73be3809ac9cea2225 (patch) | |
tree | e6d8bbc4869a314c2c277e3506d8e89f6f18f68b /server | |
parent | fb8c4ab6a0532d2d38fcbf89b6aff54741c45daf (diff) | |
download | lua-language-server-2b03350caa83e66d6bb4cd73be3809ac9cea2225.zip |
重定义时显示关联信息
Diffstat (limited to 'server')
-rw-r--r-- | server/src/matcher/diagnostics.lua | 15 | ||||
-rw-r--r-- | server/src/method/textDocument/publishDiagnostics.lua | 27 | ||||
-rw-r--r-- | server/src/service.lua | 1 |
3 files changed, 37 insertions, 6 deletions
diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua index cce5b196..74066283 100644 --- a/server/src/matcher/diagnostics.lua +++ b/server/src/matcher/diagnostics.lua @@ -91,7 +91,7 @@ local function serachSpaces(lines, callback) end end -local function searchRedefinition(results, callback) +local function searchRedefinition(results, uri, callback) for _, var in ipairs(results.vars) do if var.type ~= 'local' then goto NEXT_VAR @@ -105,12 +105,18 @@ local function searchRedefinition(results, callback) if not shadow then goto NEXT_VAR end - callback(var.source.start, var.source.finish, var.key) + callback(var.source.start, var.source.finish, var.key, { + { + start = shadow.source.start, + finish = shadow.source.finish, + uri = uri, + } + }) ::NEXT_VAR:: end end -return function (ast, results, lines) +return function (ast, results, lines, uri) local datas = {} -- 未使用的局部变量 searchUnusedLocals(results, function (start, finish, key) @@ -149,12 +155,13 @@ return function (ast, results, lines) } end) -- 重定义局部变量 - searchRedefinition(results, function (start, finish, key) + searchRedefinition(results, uri, function (start, finish, key, related) datas[#datas+1] = { start = start, finish = finish, level = 'Warning', message = ('Redefined local `%s`'):format(key), + related = related, } end) return datas diff --git a/server/src/method/textDocument/publishDiagnostics.lua b/server/src/method/textDocument/publishDiagnostics.lua index 38f4c81b..bfca0621 100644 --- a/server/src/method/textDocument/publishDiagnostics.lua +++ b/server/src/method/textDocument/publishDiagnostics.lua @@ -48,8 +48,30 @@ local function createInfo(data, lines) range = getRange(data.start, data.finish, lines), severity = DiagnosticSeverity[data.level], message = data.message, - relatedInformation = data.relatedInformation, } + if data.related then + local related = {} + for i, info in ipairs(data.related) do + local message = info.message + if not message then + local start_line = lines:rowcol(info.start) + local finish_line = lines:rowcol(info.finish) + local chars = {} + for n = start_line, finish_line do + chars[#chars+1] = lines:line(n) + end + message = table.concat(chars, '\n') + end + related[i] = { + message = message, + location = { + uri = info.uri, + range = getRange(info.start, info.finish, lines), + } + } + end + diagnostic.relatedInformation = related + end return diagnostic end @@ -57,8 +79,9 @@ return function (lsp, params) local results = params.results local ast = params.ast local lines = params.lines + local uri = params.uri - local datas = matcher.diagnostics(ast, results, lines) + local datas = matcher.diagnostics(ast, results, lines, uri) if not datas then -- 返回空表以清空之前的结果 diff --git a/server/src/service.lua b/server/src/service.lua index 56b1bd5d..b3ff0d07 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -209,6 +209,7 @@ function mt:compileText(uri) ast = ast, results = obj.results, lines = obj.lines, + uri = uri, } return obj |