Skip to content

Commit 98efc56

Browse files
authored
Merge pull request LuaLS#1711 from sewbacca/feature/auto-require-without-init
Feature/auto require without init
2 parents 6999dbb + 2492c1f commit 98efc56

File tree

2 files changed

+154
-55
lines changed

2 files changed

+154
-55
lines changed

script/core/completion/completion.lua

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -376,63 +376,79 @@ local function checkModule(state, word, position, results)
376376
goto CONTINUE
377377
end
378378
local path = furi.decode(uri)
379-
local fileName = path:match '[^/\\]*$'
380-
local stemName = fileName:gsub('%..+', '')
381-
if not locals[stemName]
382-
and not vm.hasGlobalSets(state.uri, 'variable', stemName)
383-
and not globals[stemName]
384-
and stemName:match(guide.namePatternFull)
385-
and matchKey(word, stemName) then
386-
local targetState = files.getState(uri)
387-
if not targetState then
388-
goto CONTINUE
389-
end
390-
local targetReturns = targetState.ast.returns
391-
if not targetReturns then
392-
goto CONTINUE
393-
end
394-
local targetSource = targetReturns[1] and targetReturns[1][1]
395-
if not targetSource then
396-
goto CONTINUE
397-
end
398-
if targetSource.type ~= 'getlocal'
399-
and targetSource.type ~= 'table'
400-
and targetSource.type ~= 'function' then
401-
goto CONTINUE
402-
end
403-
if targetSource.type == 'getlocal'
404-
and vm.getDeprecated(targetSource.node) then
405-
goto CONTINUE
406-
end
407-
results[#results+1] = {
408-
label = stemName,
409-
kind = define.CompletionItemKind.Variable,
410-
commitCharacters = { '.' },
411-
command = {
412-
title = 'autoRequire',
413-
command = 'lua.autoRequire',
414-
arguments = {
415-
{
416-
uri = guide.getUri(state.ast),
417-
target = uri,
418-
name = stemName,
379+
local relativePath = workspace.getRelativePath(path)
380+
local infos = rpath.getVisiblePath(uri, path)
381+
local testedStem = { }
382+
for _, sr in ipairs(infos) do
383+
local pattern = sr.searcher
384+
:gsub("(%p)", "%%%1")
385+
:gsub("%%%?", "(.-)")
386+
387+
local stemName = relativePath
388+
:match(pattern)
389+
:match("[%a_][%w_]*$")
390+
391+
if not stemName or testedStem[stemName] then
392+
goto INNER_CONTINUE
393+
end
394+
testedStem[stemName] = true
395+
396+
if not locals[stemName]
397+
and not vm.hasGlobalSets(state.uri, 'variable', stemName)
398+
and not globals[stemName]
399+
and matchKey(word, stemName) then
400+
local targetState = files.getState(uri)
401+
if not targetState then
402+
goto INNER_CONTINUE
403+
end
404+
local targetReturns = targetState.ast.returns
405+
if not targetReturns then
406+
goto INNER_CONTINUE
407+
end
408+
local targetSource = targetReturns[1] and targetReturns[1][1]
409+
if not targetSource then
410+
goto INNER_CONTINUE
411+
end
412+
if targetSource.type ~= 'getlocal'
413+
and targetSource.type ~= 'table'
414+
and targetSource.type ~= 'function' then
415+
goto INNER_CONTINUE
416+
end
417+
if targetSource.type == 'getlocal'
418+
and vm.getDeprecated(targetSource.node) then
419+
goto INNER_CONTINUE
420+
end
421+
results[#results+1] = {
422+
label = stemName,
423+
kind = define.CompletionItemKind.Variable,
424+
commitCharacters = { '.' },
425+
command = {
426+
title = 'autoRequire',
427+
command = 'lua.autoRequire',
428+
arguments = {
429+
{
430+
uri = guide.getUri(state.ast),
431+
target = uri,
432+
name = stemName,
433+
},
419434
},
420435
},
421-
},
422-
id = stack(targetSource, function (newSource) ---@async
423-
local md = markdown()
424-
md:add('md', lang.script('COMPLETION_IMPORT_FROM', ('[%s](%s)'):format(
425-
workspace.getRelativePath(uri),
426-
uri
427-
)))
428-
md:add('md', buildDesc(newSource))
429-
return {
430-
detail = buildDetail(newSource),
431-
description = md,
432-
--additionalTextEdits = buildInsertRequire(state, originUri, stemName),
433-
}
434-
end)
435-
}
436+
id = stack(targetSource, function (newSource) ---@async
437+
local md = markdown()
438+
md:add('md', lang.script('COMPLETION_IMPORT_FROM', ('[%s](%s)'):format(
439+
workspace.getRelativePath(uri),
440+
uri
441+
)))
442+
md:add('md', buildDesc(newSource))
443+
return {
444+
detail = buildDetail(newSource),
445+
description = md,
446+
--additionalTextEdits = buildInsertRequire(state, originUri, stemName),
447+
}
448+
end)
449+
}
450+
end
451+
::INNER_CONTINUE::
436452
end
437453
::CONTINUE::
438454
end

test/crossfile/completion.lua

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ function TEST(data)
108108
assert(eq(expect, result))
109109
end
110110

111+
local function WITH_CONFIG(cfg, f)
112+
local prev = { }
113+
for k, v in pairs(cfg) do
114+
prev[k] = config.get(nil, k)
115+
config.set(nil, k, v)
116+
end
117+
f()
118+
for k, v in pairs(prev) do
119+
config.set(nil, k, v)
120+
end
121+
end
122+
111123
TEST {
112124
{
113125
path = 'abc.lua',
@@ -951,3 +963,74 @@ TEST {
951963
},
952964
completion = EXISTS
953965
}
966+
967+
-- Find obscured modules
968+
969+
WITH_CONFIG({
970+
["Lua.runtime.pathStrict"] = true,
971+
["Lua.runtime.path"] = {
972+
"?/init.lua",
973+
"sub/?/init.lua",
974+
"obscure_path/?/?/init.lua"
975+
},
976+
}, function()
977+
TEST {
978+
{ path = 'myLib/init.lua', content = 'return {}' },
979+
{
980+
path = 'main.lua',
981+
main = true,
982+
content = [[
983+
myLib<??>
984+
]],
985+
},
986+
completion = EXISTS
987+
}
988+
989+
TEST {
990+
{ path = 'sub/myLib/init.lua', content = 'return {}' },
991+
{
992+
path = 'main.lua',
993+
main = true,
994+
content = [[
995+
myLib<??>
996+
]],
997+
},
998+
completion = EXISTS
999+
}
1000+
1001+
TEST {
1002+
{ path = 'sub/myLib/sublib/init.lua', content = 'return {}' },
1003+
{
1004+
path = 'main.lua',
1005+
main = true,
1006+
content = [[
1007+
sublib<??>
1008+
]],
1009+
},
1010+
completion = EXISTS
1011+
}
1012+
1013+
TEST {
1014+
{ path = 'sublib/init.lua', content = 'return {}' },
1015+
{
1016+
path = 'main.lua',
1017+
main = true,
1018+
content = [[
1019+
sublib<??>
1020+
]],
1021+
},
1022+
completion = EXISTS
1023+
}
1024+
1025+
TEST {
1026+
{ path = 'obscure_path/myLib/obscure/myLib/obscure/init.lua', content = 'return {}' },
1027+
{
1028+
path = 'main.lua',
1029+
main = true,
1030+
content = [[
1031+
obscure<??>
1032+
]],
1033+
},
1034+
completion = EXISTS
1035+
}
1036+
end)

0 commit comments

Comments
 (0)