Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` A regression related to type narrow and generic param introduced since `v3.10.1`
* `New` Support importing `enum` through class name suffix matching in quick fixes, allowing the import of `enum` from `table.table.enum; return table`.
* `FIX` Parse storagePath to improve reliability of resolving ${addons} placeholder
* `FIX` Reference should also look in tablefield
* `FIX` Determine that the index of `{...}` is an integer when iterating
Expand Down
7 changes: 4 additions & 3 deletions script/core/code-action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,13 @@ local function checkMissingRequire(results, uri, start, finish)
end

local function addRequires(global, endpos)
autoreq.check(state, global, endpos, function(moduleFile, _stemname, _targetSource)
autoreq.check(state, global, endpos, function (moduleFile, _stemname, _targetSource, fullKeyPath)
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', target, global),
title = lang.script('ACTION_AUTOREQUIRE', target .. (fullKeyPath or ''), global),
kind = 'refactor.rewrite',
command = {
title = 'autoRequire',
Expand All @@ -711,7 +711,8 @@ local function checkMissingRequire(results, uri, start, finish)
uri = guide.getUri(state.ast),
target = moduleFile,
name = global,
requireName = target
requireName = target,
fullKeyPath = fullKeyPath,
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions script/core/command/autoRequire.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ local function askAutoRequire(uri, visiblePaths)
return nameMap[result]
end

local function applyAutoRequire(uri, row, name, result, fmt)
local function applyAutoRequire(uri, row, name, result, fmt, fullKeyPath)
local quotedResult = ('%q'):format(result)
if fmt.quot == "'" then
quotedResult = ([['%s']]):format(quotedResult:sub(2, -2)
Expand All @@ -119,7 +119,7 @@ local function applyAutoRequire(uri, row, name, result, fmt)
if fmt.col and fmt.col > #text then
sp = (' '):rep(fmt.col - #text - 1)
end
text = ('local %s%s= require%s\n'):format(name, sp, quotedResult)
text = ('local %s%s= require%s%s\n'):format(name, sp, quotedResult, fullKeyPath)
client.editText(uri, {
{
start = guide.positionOf(row, 0),
Expand Down Expand Up @@ -159,6 +159,6 @@ return function (data)

local offset, fmt = findInsertRow(uri)
if offset and fmt then
applyAutoRequire(uri, offset, name, requireName, fmt)
applyAutoRequire(uri, offset, name, requireName, fmt, data.fullKeyPath or '')
end
end
59 changes: 59 additions & 0 deletions script/core/completion/auto-require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local rpath = require 'workspace.require-path'
local vm = require 'vm'
local matchKey = require 'core.matchkey'

local ipairs = ipairs

---@class auto-require
local m = {}

Expand Down Expand Up @@ -36,6 +38,7 @@ end
function m.check(state, word, position, callback)
local globals = util.arrayToHash(config.get(state.uri, 'Lua.diagnostics.globals'))
local locals = guide.getVisibleLocals(state.ast, position)
local hit = false
for uri in files.eachFile(state.uri) do
if uri == guide.getUri(state.ast) then
goto CONTINUE
Expand Down Expand Up @@ -85,12 +88,68 @@ function m.check(state, word, position, callback)
and vm.getDeprecated(targetSource.node) then
goto INNER_CONTINUE
end
hit = true
callback(uri, stemName, targetSource)
end
::INNER_CONTINUE::
end
::CONTINUE::
end
-- 如果没命中, 则检查枚举
if not hit then
local docs = vm.getDocSets(state.uri)
for _, doc in ipairs(docs) do
if doc.type ~= 'doc.enum' or vm.getDeprecated(doc) then
goto CONTINUE
end
-- 检查枚举名是否匹配
if not (doc.enum[1] == word or doc.enum[1]:match(".*%.([^%.]*)$") == word) then
goto CONTINUE
end
local uri = guide.getUri(doc)
local targetState = files.getState(uri)
if not targetState then
goto CONTINUE
end
local targetSource = m.getTargetSource(targetState)
if not targetSource or (targetSource.type ~= 'getlocal' and targetSource.type ~= 'table') or vm.getDeprecated(targetSource.node) then
goto CONTINUE
end
-- 枚举的完整路径
local fullKeyPath = ""
local node = doc.bindSource.parent
while node do
-- 检查是否可见
if not vm.isVisible(state.ast, node) then
goto CONTINUE
end
if node.type == 'setfield' or node.type == 'getfield' then
fullKeyPath = "." .. node.field[1] .. fullKeyPath
end
if node.type == 'getlocal' then
node = node.node
break
end
node = node.node
end
-- 匹配导出的值, 确定最终路径
if targetSource.node == node then
hit = true
elseif targetSource.type == 'table' then
for _, value in ipairs(targetSource) do
if value.value.node == node then
fullKeyPath = "." .. value.value[1] .. fullKeyPath
hit = true
break
end
end
end
if hit then
callback(guide.getUri(doc), nil, nil, fullKeyPath)
end
::CONTINUE::
end
end
end

files.watch(function (ev, uri)
Expand Down
2 changes: 1 addition & 1 deletion test/command/auto-require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function TEST(text)
files.setText(TESTURI, text)
EditResult = nil
local row, fmt = findInsertRow(TESTURI)
applyAutoRequire(TESTURI, row, name, name, fmt)
applyAutoRequire(TESTURI, row, name, name, fmt, "")
assert(util.equal(EditResult, expect))
files.remove(TESTURI)
end
Expand Down
Loading