summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script-beta/core/code-action.lua102
-rw-r--r--script-beta/provider/capability.lua2
-rw-r--r--script-beta/provider/provider.lua14
3 files changed, 117 insertions, 1 deletions
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)