diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-28 14:22:35 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-28 14:22:35 +0800 |
commit | a6af19e85b8209043bd0cb62ba3bfae92e82badb (patch) | |
tree | cf7d8dd2cf1713e208757cc1bd98263d10f5ce64 /server/src/method | |
parent | d021ae89300334f58689590e1cd122126f6de974 (diff) | |
download | lua-language-server-a6af19e85b8209043bd0cb62ba3bfae92e82badb.zip |
标记全局变量
Diffstat (limited to 'server/src/method')
-rw-r--r-- | server/src/method/textDocument/codeAction.lua | 77 |
1 files changed, 56 insertions, 21 deletions
diff --git a/server/src/method/textDocument/codeAction.lua b/server/src/method/textDocument/codeAction.lua index 04a07e3b..e85a8aa3 100644 --- a/server/src/method/textDocument/codeAction.lua +++ b/server/src/method/textDocument/codeAction.lua @@ -1,31 +1,66 @@ -return function (lsp, params) - local uri = params.textDocument.uri - local result = {} - - for _, data in ipairs(params.context.diagnostics) do - if data.code then - result[#result+1] = { - title = ('禁用诊断(%s)'):format(data.code), - kind = 'quickfix', - diagnostics = {data}, - command = { - title = '测试', - command = 'config', - arguments = { - { - key = {'diagnostics', 'disable'}, - action = 'add', - value = data.code, - } +local function solveDiagnostic(lsp, uri, data, callback) + if data.code then + callback { + title = ('禁用诊断(%s)'):format(data.code), + kind = 'quickfix', + diagnostics = {data}, + command = { + title = '禁用诊断', + command = 'config', + arguments = { + { + key = {'diagnostics', 'disable'}, + action = 'add', + value = data.code, } } } + } + end + if data.code == 'undefined-global' then + local vm, lines, text = lsp:getVM(uri) + if not vm then + return + end + local start = lines:position(data.range.start.line + 1, data.range.start.character + 1) + local finish = lines:position(data.range['end'].line + 1, data.range['end'].character) + local name = text:sub(start, finish) + if #name < 0 or name:find('[^%w_]') then + return end + callback { + title = ('标记 `%s` 为已定义的全局变量'):format(name), + kind = 'quickfix', + diagnostics = {data}, + command = { + title = '标记全局变量', + command = 'config', + arguments = { + { + key = {'diagnostics', 'globals'}, + action = 'add', + value = name, + } + } + }, + } + end +end + +return function (lsp, params) + local uri = params.textDocument.uri + + local results = {} + + for _, data in ipairs(params.context.diagnostics) do + solveDiagnostic(lsp, uri, data, function (result) + results[#results+1] = result + end) end - if #result == 0 then + if #results == 0 then return nil end - return result + return results end |