diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-03-12 18:36:58 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-03-12 18:36:58 +0800 |
commit | 1fa9ad87670ca2f6a27a708f6f039ed398e9fa2a (patch) | |
tree | 7492bd9e14faea2d2f2a46961c1c8f8bb26fadb5 /script/core/code-action.lua | |
parent | 36e59b4cee467b6db3c16d86df17e312608033da (diff) | |
download | lua-language-server-1fa9ad87670ca2f6a27a708f6f039ed398e9fa2a.zip |
quick fix: disable diagnostic in line/file
Diffstat (limited to 'script/core/code-action.lua')
-rw-r--r-- | script/core/code-action.lua | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua index ed13cab8..6929d75a 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -6,7 +6,60 @@ local util = require 'utility' local sp = require 'bee.subprocess' local vm = require 'vm' -local function disableDiagnostic(uri, code, results) +local function checkDisableByLuaDocExits(uri, row, mode, code) + local lines = files.getLines(uri) + local ast = files.getAst(uri) + local text = files.getOriginText(uri) + local line = lines[row] + if ast.ast.docs and line then + for _, doc in ipairs(ast.ast.docs) do + if doc.start >= line.start + and doc.finish <= line.finish then + if doc.type == 'doc.diagnostic' then + if doc.mode == mode then + if doc.names then + return { + start = doc.finish, + finish = doc.finish, + newText = text:sub(doc.finish, doc.finish) + .. ', ' + .. code + } + else + return { + start = doc.finish, + finish = doc.finish, + newText = text:sub(doc.finish, doc.finish) + .. ': ' + .. code + } + end + end + end + end + end + end + return nil +end + +local function checkDisableByLuaDocInsert(uri, row, mode, code) + local lines = files.getLines(uri) + local ast = files.getAst(uri) + local text = files.getOriginText(uri) + -- 先看看上一行是不是已经有了 + -- 没有的话就插入一行 + local line = lines[row] + return { + start = line.start, + finish = line.start, + newText = '---@diagnostic ' .. mode .. ': ' .. code .. '\n' + .. text:sub(line.start, line.start) + } +end + +local function disableDiagnostic(uri, code, start, results) + local lines = files.getLines(uri) + local row = guide.positionOf(lines, start) results[#results+1] = { title = lang.script('ACTION_DISABLE_DIAG', code), kind = 'quickfix', @@ -23,6 +76,24 @@ local function disableDiagnostic(uri, code, results) } } } + local function pushEdit(title, edit) + results[#results+1] = { + title = title, + kind = 'quickfix', + edit = { + changes = { + [uri] = { edit } + } + } + } + end + + pushEdit(lang.script('ACTION_DISABLE_DIAG_LINE', code), + checkDisableByLuaDocExits (uri, row - 1, 'disable-next-line', code) + or checkDisableByLuaDocInsert(uri, row, 'disable-next-line', code)) + pushEdit(lang.script('ACTION_DISABLE_DIAG_FILE', code), + checkDisableByLuaDocExits (uri, 1, 'disable', code) + or checkDisableByLuaDocInsert(uri, 1, 'disable', code)) end local function markGlobal(uri, name, results) @@ -248,7 +319,7 @@ local function solveTrailingSpace(uri, diag, results) } end -local function solveDiagnostic(uri, diag, results) +local function solveDiagnostic(uri, diag, start, results) if diag.source == lang.script.DIAG_SYNTAX_CHECK then solveSyntax(uri, diag, results) return @@ -267,15 +338,15 @@ local function solveDiagnostic(uri, diag, results) elseif diag.code == 'trailing-space' then solveTrailingSpace(uri, diag, results) end - disableDiagnostic(uri, diag.code, results) + disableDiagnostic(uri, diag.code, start, results) end -local function checkQuickFix(results, uri, diagnostics) +local function checkQuickFix(results, uri, start, diagnostics) if not diagnostics then return end for _, diag in ipairs(diagnostics) do - solveDiagnostic(uri, diag, results) + solveDiagnostic(uri, diag, start, results) end end @@ -476,7 +547,7 @@ return function (uri, start, finish, diagnostics) local results = {} - checkQuickFix(results, uri, diagnostics) + checkQuickFix(results, uri, start, diagnostics) checkSwapParams(results, uri, start, finish) --checkExtractAsFunction(results, uri, start, finish) checkJsonToLua(results, uri, start, finish) |