diff options
-rw-r--r-- | locale/en-us/script.lua | 2 | ||||
-rw-r--r-- | script/core/code-action.lua | 64 | ||||
-rw-r--r-- | script/core/command/autoRequire.lua | 11 | ||||
-rw-r--r-- | test/code_action/init.lua | 2 |
4 files changed, 75 insertions, 4 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 3c2dd28b..5cb1c5ab 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -448,6 +448,8 @@ ACTION_ADD_DICT = 'Add \'{}\' to workspace dict' ACTION_FIX_ADD_PAREN = -- TODO: need translate! 'Add parentheses.' +ACTION_AUTOREQUIRE = -- TODO: need translate! +"Import {} as '{}'" COMMAND_DISABLE_DIAG = 'Disable diagnostics' diff --git a/script/core/code-action.lua b/script/core/code-action.lua index e226e03b..fa0e0f10 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -4,6 +4,9 @@ local util = require 'utility' local sp = require 'bee.subprocess' local guide = require "parser.guide" local converter = require 'proto.converter' +local autoreq = require 'core.completion.auto-require' +local rpath = require 'workspace.require-path' +local furi = require 'file-uri' ---@param uri uri ---@param row integer @@ -676,6 +679,66 @@ local function checkJsonToLua(results, uri, start, finish) } end +local function findRequireTargets(visiblePaths) + local targets = {} + for _, visible in ipairs(visiblePaths) do + targets[#targets+1] = visible.name + end + return targets +end + +local function checkMissingRequire(results, uri, start, finish, diagnostics) + local state = files.getState(uri) + local text = files.getText(uri) + if not state or not text then + return + end + + local potentialGlobals = {} + guide.eachSourceBetween(state.ast, start, finish, function (source) + if source.type == 'getglobal' then + potentialGlobals[#potentialGlobals+1] = { name = source[1], endpos = source.finish } + end + end) + + local function addPotentialRequires(global) + autoreq.check(state, global.name, global.endpos, function(moduleFile, stemname, targetSource) + local visiblePaths = rpath.getVisiblePath(uri, furi.decode(moduleFile)) + if not visiblePaths or #visiblePaths == 0 then return end + + for _, target in ipairs(findRequireTargets(visiblePaths)) do + results[#results+1] = { + title = lang.script('ACTION_AUTOREQUIRE', global.name, target), + kind = 'refactor.rewrite', + command = { + title = 'autoRequire', + command = 'lua.autoRequire', + arguments = { + { + uri = guide.getUri(state.ast), + target = moduleFile, + name = global.name, + requireName = target + }, + }, + } + } + end + end) + end + + -- TODO: Is there a better way to detect undefined-global? + for _, diag in ipairs(diagnostics) do + if diag.code == 'undefined-global' then + for _, potglobal in ipairs(potentialGlobals) do + if diag.message:find(potglobal.name) then + addPotentialRequires(potglobal) + end + end + end + end +end + return function (uri, start, finish, diagnostics) local ast = files.getState(uri) if not ast then @@ -688,6 +751,7 @@ return function (uri, start, finish, diagnostics) checkSwapParams(results, uri, start, finish) --checkExtractAsFunction(results, uri, start, finish) checkJsonToLua(results, uri, start, finish) + checkMissingRequire(results, uri, start, finish, diagnostics) return results end diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua index 32911d92..a96cc918 100644 --- a/script/core/command/autoRequire.lua +++ b/script/core/command/autoRequire.lua @@ -135,6 +135,7 @@ return function (data) local uri = data.uri local target = data.target local name = data.name + local requireName = data.requireName local state = files.getState(uri) if not state then return @@ -149,11 +150,13 @@ return function (data) return #a.name < #b.name end) - local result = askAutoRequire(uri, visiblePaths) - if not result then - return + if not requireName then + requireName = askAutoRequire(uri, visiblePaths) + if not requireName then + return + end end local offset, fmt = findInsertRow(uri) - applyAutoRequire(uri, offset, name, result, fmt) + applyAutoRequire(uri, offset, name, requireName, fmt) end diff --git a/test/code_action/init.lua b/test/code_action/init.lua index 2329ce4c..bef58a53 100644 --- a/test/code_action/init.lua +++ b/test/code_action/init.lua @@ -154,3 +154,5 @@ local t = { -- edit = EXISTS, -- }, --} + +-- TODO: Add some tests for ACTION_AUTOREQUIRE
\ No newline at end of file |