summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-29 11:49:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-29 11:49:29 +0800
commitd2884244100983a6a84d456cbe399889d8dde392 (patch)
treed36601c4058de7320a6cae5bfee62bf7548eedbd /server/src
parentf443b2f2ee4f7fc7d67856259b3e976674a9bdc9 (diff)
downloadlua-language-server-d2884244100983a6a84d456cbe399889d8dde392.zip
必须验证表是局部变量声明时直接赋值的
Diffstat (limited to 'server/src')
-rw-r--r--server/src/core/completion.lua4
-rw-r--r--server/src/core/document_symbol.lua56
-rw-r--r--server/src/core/vm.lua13
3 files changed, 48 insertions, 25 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua
index 0373e394..8ad1b90c 100644
--- a/server/src/core/completion.lua
+++ b/server/src/core/completion.lua
@@ -291,8 +291,8 @@ local function searchAsIndex(vm, pos, result, callback)
callback(var, CompletionItemKind.Variable)
end)
for _, index in ipairs(vm.results.indexs) do
- if matchKey(result.key, index) then
- callback(index, CompletionItemKind.Property)
+ if matchKey(result.key, index.key) then
+ callback(index.key, CompletionItemKind.Property)
end
end
searchFields(result.key, vm.results.locals[1], nil, function (var)
diff --git a/server/src/core/document_symbol.lua b/server/src/core/document_symbol.lua
index af9021e3..5697daf8 100644
--- a/server/src/core/document_symbol.lua
+++ b/server/src/core/document_symbol.lua
@@ -76,27 +76,56 @@ local function buildFunction(vm, func)
}
end
-local function buildLocal(vm, loc)
- if loc.source.start == 0 then
+local function isLocalTable(var)
+ if not var.value or var.value.type ~= 'table' then
+ return false
+ end
+ if var.value.source.start == 0 then
+ return false
+ end
+ if var.source == var.value.declarat then
+ return true
+ end
+ return false
+end
+
+local function buildVar(vm, var)
+ if var.source.start == 0 then
return nil
end
- if loc.value and loc.value.type == 'function' then
+ if var.value and var.value.type == 'function' then
return nil
end
- if loc.hide then
+ if var.hide then
return nil
end
- if loc.key == '_' then
+ local key = var.key
+ if key == '_' then
return nil
end
- local range = { loc.source.start, loc.source.finish }
- local hvr = hover(loc, loc.source)
+ if type(key) ~= 'string' then
+ key = ('[%s]'):format(key)
+ end
+ local range
+ if isLocalTable(var) then
+ range = { var.source.start, var.value.source.finish }
+ else
+ range = { var.source.start, var.source.finish }
+ end
+ local hvr = hover(var, var.source)
+ local kind
+ if var.source.isIndex then
+ kind = SymbolKind.Struct
+ else
+ kind = SymbolKind.Variable
+ end
return {
- name = loc.key,
- detail = hvr.label,
- kind = SymbolKind.Variable,
+ name = key,
+ -- 前端不支持多行
+ detail = hvr.label:gsub('[\r\n]', ''),
+ kind = kind,
range = range,
- selectionRange = range,
+ selectionRange = { var.source.start, var.source.finish },
}
end
@@ -136,7 +165,10 @@ return function (vm)
symbols[#symbols+1] = buildFunction(vm, func)
end
for _, loc in ipairs(vm.results.locals) do
- symbols[#symbols+1] = buildLocal(vm, loc)
+ symbols[#symbols+1] = buildVar(vm, loc)
+ end
+ for _, index in ipairs(vm.results.indexs) do
+ symbols[#symbols+1] = buildVar(vm, index)
end
local packedSymbols = packSymbols(symbols)
diff --git a/server/src/core/vm.lua b/server/src/core/vm.lua
index 755b2e7c..6a0aa887 100644
--- a/server/src/core/vm.lua
+++ b/server/src/core/vm.lua
@@ -70,14 +70,6 @@ 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
@@ -209,9 +201,8 @@ function mt:buildTable(source)
end
else
if key.type == 'name' then
- local index = key[1]
- insertOnce(self.results.indexs, index)
- local field = self:createField(tbl, index, key)
+ local field = self:createField(tbl, key[1], key)
+ self.results.indexs[#self.results.indexs+1] = field
key.isIndex = true
if value.type == 'list' then
self:setValue(field, value[1], key)