diff options
Diffstat (limited to 'server/src/method/textDocument')
-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 |