From c73bf00f76e46366ec32aa93196953ab1b63ef94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sun, 27 Sep 2020 00:15:23 +0800 Subject: =?UTF-8?q?=E9=83=A8=E5=88=86=E5=BF=AB=E9=80=9F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/code-action.lua | 102 ++++++++++++++++++++++++++++++++++++ script-beta/provider/capability.lua | 2 +- script-beta/provider/provider.lua | 14 +++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 script-beta/core/code-action.lua (limited to 'script-beta') diff --git a/script-beta/core/code-action.lua b/script-beta/core/code-action.lua new file mode 100644 index 00000000..de7ef6ac --- /dev/null +++ b/script-beta/core/code-action.lua @@ -0,0 +1,102 @@ +local files = require 'files' +local lang = require 'language' +local define = require 'proto.define' +local guide = require 'parser.guide' +local library = require 'library' + +local function disableDiagnostic(code, results) + results[#results+1] = { + title = lang.script('ACTION_DISABLE_DIAG', code), + kind = 'quickfix', + command = { + title = lang.script.COMMAND_DISABLE_DIAG, + command = 'lua.config', + arguments = { + { + key = 'Lua.diagnostics.disable', + action = 'add', + value = code, + } + } + } + } +end + +local function markGlobal(name, results) + results[#results+1] = { + title = lang.script('ACTION_MARK_GLOBAL', name), + kind = 'quickfix', + command = { + title = lang.script.COMMAND_MARK_GLOBAL, + command = 'lua.config', + arguments = { + { + key = 'Lua.diagnostics.globals', + action = 'add', + value = name, + } + } + } + } +end + +local function changeVersion(version, results) + results[#results+1] = { + title = lang.script('ACTION_RUNTIME_VERSION', version), + kind = 'quickfix', + command = { + title = lang.script.COMMAND_RUNTIME_VERSION, + command = 'lua.config', + arguments = { + { + key = 'Lua.runtime.version', + action = 'set', + value = version, + } + } + }, + } +end + +local function solveUndefinedGlobal(uri, diag, results) + local ast = files.getAst(uri) + local text = files.getText(uri) + local lines = files.getLines(uri) + local offset = define.offsetOfWord(lines, text, diag.range.start) + guide.eachSourceContain(ast.ast, offset, function (source) + if source.type ~= 'getglobal' then + return + end + + local name = guide.getName(source) + markGlobal(name, results) + + if library.other[name] then + for _, version in ipairs(library.other[name]) do + changeVersion(version, results) + end + end + end) +end + +local function solveDiagnostic(uri, diag, results) + if diag.code == 'undefined-global' then + solveUndefinedGlobal(uri, diag, results) + end + disableDiagnostic(diag.code, results) +end + +return function (uri, range, diagnostics) + local ast = files.getAst(uri) + if not ast then + return nil + end + + local results = {} + + for _, data in ipairs(diagnostics) do + solveDiagnostic(uri, data, results) + end + + return results +end diff --git a/script-beta/provider/capability.lua b/script-beta/provider/capability.lua index b5ae8e8b..abe7e7d5 100644 --- a/script-beta/provider/capability.lua +++ b/script-beta/provider/capability.lua @@ -17,7 +17,7 @@ m.initer = { }, documentSymbolProvider = true, documentHighlightProvider = true, - --codeActionProvider = true, + codeActionProvider = true, signatureHelpProvider = { triggerCharacters = { '(', ',' }, }, diff --git a/script-beta/provider/provider.lua b/script-beta/provider/provider.lua index 57ae637e..70c5fb50 100644 --- a/script-beta/provider/provider.lua +++ b/script-beta/provider/provider.lua @@ -523,3 +523,17 @@ proto.on('textDocument/documentSymbol', function (params) return symbols end) + +proto.on('textDocument/codeAction', function (params) + local core = require 'core.code-action' + local uri = params.textDocument.uri + local range = params.range + local diagnostics = params.context.diagnostics + local results = core(uri, range, diagnostics) + + if not results or #results == 0 then + return nil + end + + return results +end) -- cgit v1.2.3