diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-09 10:33:10 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-09 10:33:10 +0800 |
commit | 9e40b80887ebf4b9284127e7e898387ffc1bf9c8 (patch) | |
tree | eda976b74bd0d30ddbd0c76e3092cbe8063d0f7b | |
parent | 1521c242ee21c5a8af993fa1d52ef5ac3e382767 (diff) | |
download | lua-language-server-9e40b80887ebf4b9284127e7e898387ffc1bf9c8.zip |
自动完成全局变量时检查当前可见的同名局部变量
-rw-r--r-- | script-beta/core/completion.lua | 13 | ||||
-rw-r--r-- | test-beta/completion/init.lua | 26 |
2 files changed, 37 insertions, 2 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index c5cb2e1b..0dd0f3d1 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -406,7 +406,7 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul } end -local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results) +local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results, locals) local fields = {} for _, src in ipairs(refs) do if src.type == 'library' then @@ -422,6 +422,9 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res goto CONTINUE end local name = key:sub(3) + if locals and locals[name] then + goto CONTINUE + end if not matchKey(word, name) then goto CONTINUE end @@ -450,6 +453,12 @@ local function checkField(ast, word, start, offset, parent, oop, results) checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results) end +local function checkGlobal(ast, word, start, offset, parent, oop, results) + local locals = guide.getVisibleLocals(ast.ast, offset) + local refs = vm.getFields(parent, 'deep') + checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results, locals) +end + local function checkTableField(ast, word, start, results) local source = guide.eachSourceContain(ast.ast, start, function (source) if source.start == start @@ -840,7 +849,7 @@ local function tryWord(ast, text, offset, results) checkLocal(ast, word, start, results) checkTableField(ast, word, start, results) local env = guide.getENV(ast.ast, start) - checkField(ast, word, start, offset, env, false, results) + checkGlobal(ast, word, start, offset, env, false, results) end end end diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua index de22e3f4..c69e69fe 100644 --- a/test-beta/completion/init.lua +++ b/test-beta/completion/init.lua @@ -158,6 +158,32 @@ ass$ } TEST [[ +local assert = 1 +ass$ +]] +{ + { + label = 'assert', + kind = define.CompletionItemKind.Variable, + }, +} + +TEST [[ +local assert = 1 +_G.ass$ +]] +{ + { + label = 'assert', + kind = define.CompletionItemKind.Function, + }, + { + label = 'assert()', + kind = define.CompletionItemKind.Snippet, + }, +} + +TEST [[ local function ffff(a, b) end ff$ |