Skip to content

Commit 62d4d35

Browse files
committed
Added concept of action autorequire
1 parent 5c4f5b2 commit 62d4d35

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

locale/en-us/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ ACTION_ADD_DICT =
448448
'Add \'{}\' to workspace dict'
449449
ACTION_FIX_ADD_PAREN = -- TODO: need translate!
450450
'Add parentheses.'
451+
ACTION_AUTOREQUIRE = -- TODO: need translate!
452+
"Import {} as '{}'"
451453

452454
COMMAND_DISABLE_DIAG =
453455
'Disable diagnostics'

script/core/code-action.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local util = require 'utility'
44
local sp = require 'bee.subprocess'
55
local guide = require "parser.guide"
66
local converter = require 'proto.converter'
7+
local autoreq = require 'core.completion.auto-require'
8+
local rpath = require 'workspace.require-path'
9+
local furi = require 'file-uri'
710

811
---@param uri uri
912
---@param row integer
@@ -676,6 +679,66 @@ local function checkJsonToLua(results, uri, start, finish)
676679
}
677680
end
678681

682+
local function findRequireTargets(visiblePaths)
683+
local targets = {}
684+
for _, visible in ipairs(visiblePaths) do
685+
targets[#targets+1] = visible.name
686+
end
687+
return targets
688+
end
689+
690+
local function checkMissingRequire(results, uri, start, finish, diagnostics)
691+
local state = files.getState(uri)
692+
local text = files.getText(uri)
693+
if not state or not text then
694+
return
695+
end
696+
697+
local potentialGlobals = {}
698+
guide.eachSourceBetween(state.ast, start, finish, function (source)
699+
if source.type == 'getglobal' then
700+
potentialGlobals[#potentialGlobals+1] = { name = source[1], endpos = source.finish }
701+
end
702+
end)
703+
704+
local function addPotentialRequires(global)
705+
autoreq.check(state, global.name, global.endpos, function(moduleFile, stemname, targetSource)
706+
local visiblePaths = rpath.getVisiblePath(uri, furi.decode(moduleFile))
707+
if not visiblePaths or #visiblePaths == 0 then return end
708+
709+
for _, target in ipairs(findRequireTargets(visiblePaths)) do
710+
results[#results+1] = {
711+
title = lang.script('ACTION_AUTOREQUIRE', global.name, target),
712+
kind = 'refactor.rewrite',
713+
command = {
714+
title = 'autoRequire',
715+
command = 'lua.autoRequire',
716+
arguments = {
717+
{
718+
uri = guide.getUri(state.ast),
719+
target = moduleFile,
720+
name = global.name,
721+
requireName = target
722+
},
723+
},
724+
}
725+
}
726+
end
727+
end)
728+
end
729+
730+
-- TODO: Is there a better way to detect undefined-global?
731+
for _, diag in ipairs(diagnostics) do
732+
if diag.code == 'undefined-global' then
733+
for _, potglobal in ipairs(potentialGlobals) do
734+
if diag.message:find(potglobal.name) then
735+
addPotentialRequires(potglobal)
736+
end
737+
end
738+
end
739+
end
740+
end
741+
679742
return function (uri, start, finish, diagnostics)
680743
local ast = files.getState(uri)
681744
if not ast then
@@ -688,6 +751,7 @@ return function (uri, start, finish, diagnostics)
688751
checkSwapParams(results, uri, start, finish)
689752
--checkExtractAsFunction(results, uri, start, finish)
690753
checkJsonToLua(results, uri, start, finish)
754+
checkMissingRequire(results, uri, start, finish, diagnostics)
691755

692756
return results
693757
end

script/core/command/autoRequire.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ return function (data)
135135
local uri = data.uri
136136
local target = data.target
137137
local name = data.name
138+
local requireName = data.requireName
138139
local state = files.getState(uri)
139140
if not state then
140141
return
@@ -149,11 +150,13 @@ return function (data)
149150
return #a.name < #b.name
150151
end)
151152

152-
local result = askAutoRequire(uri, visiblePaths)
153-
if not result then
154-
return
153+
if not requireName then
154+
requireName = askAutoRequire(uri, visiblePaths)
155+
if not requireName then
156+
return
157+
end
155158
end
156159

157160
local offset, fmt = findInsertRow(uri)
158-
applyAutoRequire(uri, offset, name, result, fmt)
161+
applyAutoRequire(uri, offset, name, requireName, fmt)
159162
end

test/code_action/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,5 @@ local t = {
154154
-- edit = EXISTS,
155155
-- },
156156
--}
157+
158+
-- TODO: Add some tests for ACTION_AUTOREQUIRE

0 commit comments

Comments
 (0)