diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-29 11:49:29 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-29 11:49:29 +0800 |
commit | d2884244100983a6a84d456cbe399889d8dde392 (patch) | |
tree | d36601c4058de7320a6cae5bfee62bf7548eedbd /server | |
parent | f443b2f2ee4f7fc7d67856259b3e976674a9bdc9 (diff) | |
download | lua-language-server-d2884244100983a6a84d456cbe399889d8dde392.zip |
必须验证表是局部变量声明时直接赋值的
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/completion.lua | 4 | ||||
-rw-r--r-- | server/src/core/document_symbol.lua | 56 | ||||
-rw-r--r-- | server/src/core/vm.lua | 13 | ||||
-rw-r--r-- | server/test/document_symbol/init.lua | 42 |
4 files changed, 89 insertions, 26 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) diff --git a/server/test/document_symbol/init.lua b/server/test/document_symbol/init.lua index 7f88f63e..699f50cd 100644 --- a/server/test/document_symbol/init.lua +++ b/server/test/document_symbol/init.lua @@ -231,7 +231,7 @@ local y = true name = 'y', detail = 'local y: {}', kind = SymbolKind.Variable, - range = {60, 60}, + range = {60, 65}, selectionRange = {60, 60}, }, }, @@ -244,3 +244,43 @@ local y = true selectionRange = {77, 77}, }, } + +TEST [[ +local t = { + a = 1, + b = 2, + c = 3, +} +]] +{ + [1] = { + name = 't', + detail = EXISTS, + kind = SymbolKind.Variable, + range = {7, 46}, + selectionRange = {7, 7}, + children = { + [1] = { + name = 'a', + detail = 'field a: number = 1', + kind = SymbolKind.Struct, + range = {17, 17}, + selectionRange = {17, 17}, + }, + [2] = { + name = 'b', + detail = 'field b: number = 2', + kind = SymbolKind.Struct, + range = {28, 28}, + selectionRange = {28, 28}, + }, + [3] = { + name = 'c', + detail = 'field c: number = 3', + kind = SymbolKind.Struct, + range = {39, 39}, + selectionRange = {39, 39}, + }, + } + } +} |