Skip to content

Commit f859ed4

Browse files
committed
resolve #691
1 parent e6a689c commit f859ed4

File tree

8 files changed

+150
-6
lines changed

8 files changed

+150
-6
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# changelog
22

3+
## 2.5.0
4+
* `NEW` setting `Lua.runtime.pathStrict`
5+
36
## 2.4.7
47
`2021-10-27`
58
* `FIX` [#762](https://github.com/sumneko/lua-language-server/issues/762)

script/config/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ local Template = {
151151
"?.lua",
152152
"?/init.lua",
153153
},
154+
['Lua.runtime.pathStrict'] = Type.Boolean >> false,
154155
['Lua.runtime.special'] = Type.Hash(Type.String, Type.String),
155156
['Lua.runtime.meta'] = Type.String >> '${version} ${language} ${encoding}',
156157
['Lua.runtime.unicodeName'] = Type.Boolean,

script/core/command/autoRequire.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ return function (data)
135135
end
136136

137137
local path = furi.decode(target)
138-
local visiblePaths = rpath.getVisiblePath(path, config.get 'Lua.runtime.path')
138+
local visiblePaths = rpath.getVisiblePath(path)
139139
if not visiblePaths or #visiblePaths == 0 then
140140
return
141141
end

script/core/completion.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position
885885
goto CONTINUE
886886
end
887887
local path = workspace.getRelativePath(uri)
888-
local infos = rpath.getVisiblePath(path, config.get 'Lua.runtime.path')
888+
local infos = rpath.getVisiblePath(path)
889889
for _, info in ipairs(infos) do
890890
if matchKey(literal, info.expect) then
891891
if not collect[info.expect] then

script/workspace/require-path.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ local function getOnePath(path, searcher)
2727
return nil
2828
end
2929

30-
function m.getVisiblePath(path, searchers, strict)
30+
function m.getVisiblePath(path)
31+
local searchers = config.get 'Lua.runtime.path'
32+
local strict = config.get 'Lua.runtime.pathStrict'
3133
path = path:gsub('^[/\\]+', '')
3234
local uri = furi.encode(path)
3335
local libraryPath = files.getLibraryPath(uri)
@@ -91,7 +93,9 @@ files.watch(function (ev)
9193
end)
9294

9395
config.watch(function (key, value, oldValue)
94-
if key == 'Lua.completion.requireSeparator' then
96+
if key == 'Lua.completion.requireSeparator'
97+
or key == 'Lua.runtime.path'
98+
or key == 'Lua.runtime.pathStrict' then
9599
m.flush()
96100
end
97101
end)

script/workspace/workspace.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,22 @@ function m.findUrisByFilePath(path)
391391
return resultCache[path].results, resultCache[path].posts
392392
end
393393
tracy.ZoneBeginN('findUrisByFilePath #1')
394+
local strict = config.get 'Lua.runtime.pathStrict'
394395
local results = {}
395396
local posts = {}
396397
for uri in files.eachFile() do
397398
if not uri:find(lpath, 1, true) then
398399
goto CONTINUE
399400
end
401+
local relat = m.getRelativePath(uri)
400402
local pathLen = #path
401-
local curPath = furi.decode(uri)
403+
local curPath = relat
402404
local curLen = #curPath
403405
local seg = curPath:sub(curLen - pathLen, curLen - pathLen)
404406
if seg == '/' or seg == '\\' or seg == '' then
407+
if strict and seg ~= '' then
408+
goto CONTINUE
409+
end
405410
local see = curPath:sub(curLen - pathLen + 1, curLen)
406411
if see == path then
407412
results[#results+1] = uri

test/crossfile/completion.lua

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function TEST(data)
8787
local pos
8888
for _, info in ipairs(data) do
8989
local uri = furi.encode(info.path)
90-
local script = info.content
90+
local script = info.content or ''
9191
if info.main then
9292
local newScript, catched = catch(script, '?')
9393
pos = catched['?'][1][1]
@@ -565,8 +565,114 @@ TEST {
565565
]],
566566
main = true,
567567
},
568+
completion = nil,
568569
}
569570

571+
TEST {
572+
{ path = 'f/a.lua' },
573+
{ path = 'f/b.lua' },
574+
{
575+
path = 'c.lua',
576+
content = [[
577+
require '<??>'
578+
]],
579+
main = true,
580+
},
581+
completion = {
582+
{
583+
label = 'a',
584+
kind = CompletionItemKind.Reference,
585+
textEdit = EXISTS,
586+
},
587+
{
588+
label = 'b',
589+
kind = CompletionItemKind.Reference,
590+
textEdit = EXISTS,
591+
},
592+
{
593+
label = 'f.a',
594+
kind = CompletionItemKind.Reference,
595+
textEdit = EXISTS,
596+
},
597+
{
598+
label = 'f.b',
599+
kind = CompletionItemKind.Reference,
600+
textEdit = EXISTS,
601+
},
602+
}
603+
}
604+
605+
TEST {
606+
{ path = 'f/a.lua' },
607+
{ path = 'f/b.lua' },
608+
{
609+
path = 'c.lua',
610+
content = [[
611+
require 'a<??>'
612+
]],
613+
main = true,
614+
},
615+
completion = {
616+
{
617+
label = 'a',
618+
kind = CompletionItemKind.Reference,
619+
textEdit = EXISTS,
620+
},
621+
{
622+
label = 'f.a',
623+
kind = CompletionItemKind.Reference,
624+
textEdit = EXISTS,
625+
},
626+
}
627+
}
628+
629+
config.set('Lua.runtime.pathStrict', true)
630+
631+
TEST {
632+
{ path = 'f/a.lua' },
633+
{ path = 'f/b.lua' },
634+
{
635+
path = 'c.lua',
636+
content = [[
637+
require '<??>'
638+
]],
639+
main = true,
640+
},
641+
completion = {
642+
{
643+
label = 'f.a',
644+
kind = CompletionItemKind.Reference,
645+
textEdit = EXISTS,
646+
},
647+
{
648+
label = 'f.b',
649+
kind = CompletionItemKind.Reference,
650+
textEdit = EXISTS,
651+
},
652+
}
653+
}
654+
655+
TEST {
656+
{ path = 'f/a.lua' },
657+
{ path = 'f/b.lua' },
658+
{
659+
path = 'c.lua',
660+
content = [[
661+
require 'a<??>'
662+
]],
663+
main = true,
664+
},
665+
completion = {
666+
{
667+
label = 'f.a',
668+
kind = CompletionItemKind.Reference,
669+
textEdit = EXISTS,
670+
},
671+
}
672+
}
673+
674+
config.set('Lua.runtime.pathStrict', false)
675+
570676
TEST {
571677
{
572678
path = 'xxx.lua',

test/crossfile/definition.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ TEST {
117117
},
118118
}
119119

120+
config.set('Lua.runtime.pathStrict', true)
121+
TEST {
122+
{
123+
path = 'aaa/bbb.lua',
124+
content = '',
125+
},
126+
{
127+
path = 'b.lua',
128+
content = 'require "<?bbb?>"',
129+
},
130+
}
131+
132+
TEST {
133+
{
134+
path = 'aaa/bbb.lua',
135+
content = '<!!>',
136+
},
137+
{
138+
path = 'b.lua',
139+
content = 'require "<?aaa.bbb?>"',
140+
},
141+
}
142+
143+
config.set('Lua.runtime.pathStrict', false)
144+
120145
TEST {
121146
{
122147
path = 'a.lua',

0 commit comments

Comments
 (0)