summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
authorSewbacca <sebastian.kalus@kolabnow.com>2023-06-23 20:51:27 +0200
committerSewbacca <sebastian.kalus@kolabnow.com>2023-06-23 21:36:29 +0200
commit62d4d35faf325788355ea9854bee54383db5acdb (patch)
treed371d101b109e6bea2917d8604f0b25431b6d998 /script/core
parent5c4f5b20595a6d2452569a5b9d2007e2bb19e18b (diff)
downloadlua-language-server-62d4d35faf325788355ea9854bee54383db5acdb.zip
Added concept of action autorequire
Diffstat (limited to 'script/core')
-rw-r--r--script/core/code-action.lua64
-rw-r--r--script/core/command/autoRequire.lua11
2 files changed, 71 insertions, 4 deletions
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