diff options
-rw-r--r-- | server/src/core/completion.lua | 41 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 2 | ||||
-rw-r--r-- | server/src/core/global.lua | 5 | ||||
-rw-r--r-- | server/src/core/value.lua | 58 | ||||
-rw-r--r-- | server/src/core/vm.lua | 22 | ||||
-rw-r--r-- | server/test/completion/init.lua | 4 |
6 files changed, 49 insertions, 83 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 4d735d9d..36517518 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -129,7 +129,7 @@ local function sortPairs(t) end end -local function searchFields(name, source, parent, object, callback) +local function searchFields(name, parent, callback) if not parent or not parent.value then return end @@ -141,14 +141,6 @@ local function searchFields(name, source, parent, object, callback) if type(key) ~= 'string' then goto CONTINUE end - if object then - if not field.value or field.value:getType() ~= 'function' then - goto CONTINUE - end - end - if field.source == source then - goto CONTINUE - end if matchKey(name, key) then map[key] = field end @@ -241,7 +233,7 @@ local function getDocument(var, source) end local function searchAsLocal(vm, word, pos, result, callback) - searchFields(word, result.source, vm.results.locals[1], nil, function (var) + searchFields(word, vm.results.locals[1], nil, function (var) callback(var, CompletionItemKind.Variable) end) @@ -252,7 +244,7 @@ local function searchAsLocal(vm, word, pos, result, callback) end local function searchAsArg(vm, word, pos, result, callback) - searchFields(word, result.source, vm.results.locals[1], nil, function (var) + searchFields(word, vm.results.locals[1], function (var) if var.value.lib then return end @@ -267,7 +259,7 @@ local function searchAsGlobal(vm, word, pos, result, callback) searchLocals(vm, pos, word, function (var) callback(var, CompletionItemKind.Variable) end) - searchFields(word, result.source, vm.results.locals[1], nil, function (var) + searchFields(word, vm.results.locals[1], function (var) callback(var, CompletionItemKind.Field) end) searchKeyWords(word, function (name) @@ -276,7 +268,7 @@ local function searchAsGlobal(vm, word, pos, result, callback) end local function searchAsSuffix(result, word, callback) - searchFields(word, result.source, result.parent, result.source.object, function (var) + searchFields(word, result.parent, function (var) callback(var, CompletionItemKind.Field) end) end @@ -338,7 +330,7 @@ local function searchAsIndex(vm, word, pos, result, callback) callback(index.key, CompletionItemKind.Property) end end - searchFields(word, result.source, vm.results.locals[1], nil, function (var) + searchFields(word, vm.results.locals[1], function (var) callback(var, CompletionItemKind.Field) end) end @@ -383,7 +375,6 @@ local function findClosePos(vm, pos) type = 'field', parent = parent, key = '', - source = source, } return result, source end @@ -531,21 +522,11 @@ end local function clearList(list, source) local key = source[1] - -- 如果只有一个结果且是自己,则不显示 - if #list == 1 then - if list[1].label == key then - list[1] = nil - end - return - end - -- 如果有多个结果,则将完全符合的放到最前面 - if #list > 1 then - for i, v in ipairs(list) do - if v.label == key then - table.remove(list, i) - table.insert(list, 1, v) - return - end + -- 清除自己 + for i, v in ipairs(list) do + if v.label == key then + table.remove(list, i) + return end end end diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 2e0e0066..40e089b6 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -47,7 +47,7 @@ function mt:searchUndefinedGlobal(callback) if field.value.lib then goto NEXT_VAR end - if field.source.uri ~= self.vm.uri then + if not field.uris[self.vm.uri] then goto NEXT_VAR end if type(index) ~= 'string' then diff --git a/server/src/core/global.lua b/server/src/core/global.lua index a8f5092d..7be76818 100644 --- a/server/src/core/global.lua +++ b/server/src/core/global.lua @@ -22,11 +22,8 @@ function mt:markGet(uri) end function mt:clearGlobal(uri) - self.get[uri] = nil - if not self.set[uri] then - return - end self.set[uri] = nil + self.get[uri] = nil local globalValue = self.lsp.globalValue if not globalValue then return diff --git a/server/src/core/value.lua b/server/src/core/value.lua index d1933684..6105c97c 100644 --- a/server/src/core/value.lua +++ b/server/src/core/value.lua @@ -64,17 +64,16 @@ function mt:createField(name, source) local field = { type = 'field', key = name, - source = source or getDefaultSource(), + uris = {}, } if not self._child then self._child = {} end + self._child[name] = field + local uri = source and source.uri or '' - if not self._child[uri] then - self._child[uri] = {} - end - self._child[uri][name] = field + field.uris[uri] = true self:inference('table', 0.5) @@ -82,23 +81,18 @@ function mt:createField(name, source) end function mt:rawGetField(name, source) - local uri = source and source.uri or '' if not self._child then return nil end - if self._child[uri] then - local field = self._child[uri][name] - if field then - return field - end - end - for _, childs in pairs(self._child) do - local field = childs[name] - if field then - return field - end + local field = self._child[name] + if not field then + return nil end - return nil + + local uri = source and source.uri or '' + field.uris[uri] = true + + return field end function mt:getMeta(name, source) @@ -155,18 +149,10 @@ function mt:rawEachField(callback, mark) if not self._child then return nil end - if not mark then - mark = {} - end - for _, childs in pairs(self._child) do - for name, field in pairs(childs) do - if not mark[name] then - mark[name] = true - local res = callback(name, field) - if res ~= nil then - return res - end - end + for name, field in pairs(self._child) do + local res = callback(name, field) + if res ~= nil then + return res end end return nil @@ -196,10 +182,12 @@ end function mt:removeUri(uri) if self._child then - self._child[uri] = nil - self:rawEachField(function (key, field) - field.value:removeUri(uri) - end) + for name, field in pairs(self._child) do + field.uris[uri] = nil + if not next(field.uris) then + self._child[name] = nil + end + end end if self._info then self._info[uri] = nil @@ -276,7 +264,7 @@ return function (tp, source, value) -- TODO lib里的多类型 local self = setmetatable({ source = source or getDefaultSource(), - uri = source and source.uri, + uri = source and source.uri or '', }, mt) if value ~= nil then self:setValue(value) diff --git a/server/src/core/vm.lua b/server/src/core/vm.lua index 0ad858e2..dbc2df65 100644 --- a/server/src/core/vm.lua +++ b/server/src/core/vm.lua @@ -5,13 +5,17 @@ local createValue = require 'core.value' local LibraryValue = {} local LibraryChild = {} -local function getDefaultSource() - return { start = 0, finish = 0 } -end - local mt = {} mt.__index = mt +function mt:getDefaultSource() + return { + start = 0, + finish = 0, + uri = self.uri, + } +end + function mt:createDummyVar(source, value) if source and source.bind then return source.bind @@ -19,7 +23,7 @@ function mt:createDummyVar(source, value) local loc = { type = 'local', key = '', - source = source or getDefaultSource(), + source = source or self:getDefaultSource(), } if source then @@ -40,7 +44,7 @@ function mt:createLocal(key, source, value) local loc = { type = 'local', key = key, - source = source or getDefaultSource(), + source = source or self:getDefaultSource(), close = self.scope.block.finish, } @@ -136,7 +140,7 @@ function mt:addInfo(var, type, source, value) end local info = { type = type, - source = source or getDefaultSource(), + source = source or self:getDefaultSource(), value = value, } if not self.results.infos[var] then @@ -161,7 +165,7 @@ end function mt:createDots(index, source) local dots = { type = 'dots', - source = source or getDefaultSource(), + source = source or self:getDefaultSource(), func = self:getCurrentFunction(), index = index, } @@ -1437,7 +1441,7 @@ local function compile(ast, lsp, uri) main = nil, }, lsp = lsp, - uri = uri, + uri = uri or '', }, mt) -- 创建初始环境 diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index 0a7a0618..2838e8ba 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -121,10 +121,6 @@ zabcde@ ]] { { - label = 'zabcde', - kind = CompletionItemKind.Variable, - }, - { label = 'zabcdefg', kind = CompletionItemKind.Variable, } |