diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-12-09 20:28:18 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-12-09 20:28:18 +0800 |
commit | d803249a1aedafad9b08349ed16c941bf4c3ba4e (patch) | |
tree | e25c274c1c1f16eddefa3ca572202f7536c6b7d8 /script-beta/core | |
parent | 9e193c1d17d64137191c5aa9e4f39c18a795396f (diff) | |
download | lua-language-server-d803249a1aedafad9b08349ed16c941bf4c3ba4e.zip |
自动完成搜索局部变量
Diffstat (limited to 'script-beta/core')
-rw-r--r-- | script-beta/core/completion.lua | 51 | ||||
-rw-r--r-- | script-beta/core/matchkey.lua | 30 |
2 files changed, 81 insertions, 0 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index e69de29b..d3a7a022 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -0,0 +1,51 @@ +local ckind = require 'define.CompletionItemKind' +local files = require 'files' +local guide = require 'parser.guide' +local matchKey = require 'core.matchKey' + +local function findWord(text, offset) + for i = offset, 1, -1 do + if not text:sub(i, i):match '[%w_]' then + if i == offset then + return nil + end + return text:sub(i+1, offset) + end + end + return nil +end + +local function checkLocal(ast, word, offset, results) + guide.getVisibleLocalNames(ast.ast, offset, function (name) + if matchKey(word, name) then + results[#results+1] = { + label = name, + kind = ckind.Variable, + } + end + end) +end + +local function tryWord(ast, text, offset, results) + local word = findWord(text, offset) + if not word then + return nil + end + checkLocal(ast, word, offset, results) +end + +return function (uri, offset) + local ast = files.getAst(uri) + if not ast then + return nil + end + local text = files.getText(uri) + local results = {} + + tryWord(ast, text, offset, results) + + if #results == 0 then + return nil + end + return results +end diff --git a/script-beta/core/matchkey.lua b/script-beta/core/matchkey.lua new file mode 100644 index 00000000..b46250cb --- /dev/null +++ b/script-beta/core/matchkey.lua @@ -0,0 +1,30 @@ +return function (me, other) + if me == other then + return true + end + if me == '' then + return true + end + if #me > #other then + return false + end + local lMe = me:lower() + local lOther = other:lower() + if lMe == lOther:sub(1, #lMe) then + return true + end + local chars = {} + for i = 1, #lOther do + local c = lOther:sub(i, i) + chars[c] = (chars[c] or 0) + 1 + end + for i = 1, #lMe do + local c = lMe:sub(i, i) + if chars[c] and chars[c] > 0 then + chars[c] = chars[c] - 1 + else + return false + end + end + return true +end |