diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-26 17:09:08 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-26 17:09:08 +0800 |
commit | 5a46cba0367512b65322bf64125d5b9661c869bd (patch) | |
tree | b84e4b80fcc93db425d0e7670eaa0890b768573b /server/src | |
parent | 05aabcc4c4f731bd7df2bcee03d1d51fda6aab45 (diff) | |
download | lua-language-server-5a46cba0367512b65322bf64125d5b9661c869bd.zip |
自动完成哈希表的键
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/matcher/completion.lua | 20 | ||||
-rw-r--r-- | server/src/matcher/vm.lua | 18 |
2 files changed, 36 insertions, 2 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua index 83f51044..16be7b4b 100644 --- a/server/src/matcher/completion.lua +++ b/server/src/matcher/completion.lua @@ -232,12 +232,26 @@ local function searchAsArg(vm, inCall, inString, callback) end for _, v in ipairs(results) do if v ~= inString[1] then - callback(v, CompletionItemKind.Module) + callback(v, CompletionItemKind.File) end end end end +local function searchAsIndex(vm, pos, result, callback) + searchLocals(vm, pos, result.key, function (var) + callback(var, CompletionItemKind.Variable) + end) + for _, index in ipairs(vm.results.indexs) do + if matchKey(result.key, index) then + callback(index, CompletionItemKind.Property) + end + end + searchFields(result.key, vm.results.locals[1], nil, function (var) + callback(var, CompletionItemKind.Field) + end) +end + local function findClosePos(vm, pos) local curDis = math.maxinteger local parent = nil @@ -394,7 +408,9 @@ return function (vm, pos) if result.type == 'local' then searchAsGlobal(vm, pos, result, callback) elseif result.type == 'field' then - if result.parent and result.parent.value and result.parent.value.ENV == true then + if result.isIndex then + searchAsIndex(vm, pos, result, callback) + elseif result.parent and result.parent.value and result.parent.value.ENV == true then searchAsGlobal(vm, pos, result, callback) else searchAsSuffix(result, callback) diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index e2ea5a15..00e4d11f 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -70,6 +70,14 @@ local function readOnly(t) }) end +local function insertOnce(tbl, key) + if tbl[key] then + return + end + tbl[key] = true + tbl[#tbl+1] = key +end + local mt = {} mt.__index = mt @@ -190,7 +198,9 @@ function mt:buildTable(source) else if key.type == 'name' then local index = key[1] + insertOnce(self.results.indexs, index) local field = self:createField(tbl, index, key) + field.isIndex = true if value.type == 'list' then self:setValue(field, value[1], key) else @@ -216,6 +226,13 @@ function mt:buildTable(source) local field = self:createField(tbl, n) self:setValue(field, value) end + -- 处理写了一半的 key = value,把name类的数组元素视为哈希键 + if obj.type == 'name' then + local field = self.results.sources[obj] + if field then + field.isIndex = true + end + end end end return tbl @@ -1426,6 +1443,7 @@ local function compile(ast, lsp, uri) calls = {}, sources= {}, strings= {}, + indexs = {}, main = nil, }, libraryValue = {}, |