diff options
author | sumneko <sumneko@hotmail.com> | 2019-04-22 14:52:14 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-04-22 14:52:14 +0800 |
commit | efa8bfce8228bc615e2870cbc4394bef0ab3cb9d (patch) | |
tree | c5c89bc67473c1525de1e1d912bab8517f59e7a9 | |
parent | adb221773bbfffcec5e486a8e4f52e44d594508b (diff) | |
download | lua-language-server-efa8bfce8228bc615e2870cbc4394bef0ab3cb9d.zip |
全局值跳转等待编译完成(转到定义超时1秒,查找引用超时5秒)
-rw-r--r-- | server/src/core/definition.lua | 56 | ||||
-rw-r--r-- | server/src/core/references.lua | 11 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 1 | ||||
-rw-r--r-- | server/src/method/textDocument/definition.lua | 28 | ||||
-rw-r--r-- | server/src/method/textDocument/references.lua | 20 | ||||
-rw-r--r-- | server/src/timer.lua | 4 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 4 | ||||
-rw-r--r-- | server/test/definition/emmy.lua | 20 | ||||
-rw-r--r-- | server/test/definition/method.lua | 65 |
9 files changed, 93 insertions, 116 deletions
diff --git a/server/src/core/definition.lua b/server/src/core/definition.lua index 335d2185..a704cb57 100644 --- a/server/src/core/definition.lua +++ b/server/src/core/definition.lua @@ -19,54 +19,38 @@ local function parseValueSimily(callback, vm, source, lsp) end) end -local function parseValueCrossFile(callback, vm, source, lsp) - local value = source:bindValue() - value:eachInfo(function (info, src) - if src.uri == value.uri then - if info.type == 'local' or info.type == 'set' or info.type == 'return' then - callback(src) - end - end - end) - return nil -end - local function parseLocal(callback, vm, source, lsp) - local positions = {} local loc = source:bindLocal() local locSource = loc:getSource() - --if locSource:get 'arg' then - -- callback(locSource) - --end - local value = source:bindValue() - if value and value.uri ~= '' and value.uri ~= vm.uri then - parseValueCrossFile(callback, vm, source, lsp) - end callback(locSource) - if #positions == 0 then - return nil - end - return positions end local function parseValue(callback, vm, source, lsp) - if source:bindValue() then - source:bindValue():eachInfo(function (info, src) + local value = source:bindValue() + local isGlobal + if value then + isGlobal = value:isGlobal() + value:eachInfo(function (info, src) if info.type == 'set' or info.type == 'local' or info.type == 'return' then - callback(src) + if vm.uri == src:getUri() then + if source.id > src.id then + callback(src) + end + elseif value.uri == src:getUri() then + callback(src) + end end end) end local parent = source:get 'parent' if parent then parent:eachInfo(function (info, src) - if info[1] == source[1] then - if info.type == 'set child' then - callback(src) - end + if info.type == 'set child' and info[1] == source[1] then + callback(src) end end) end + return isGlobal end local function parseLabel(callback, vm, label, lsp) @@ -122,10 +106,12 @@ return function (vm, source, lsp) return nil end local list, callback = makeList(source) + local isGlobal if source:bindLocal() then parseLocal(callback, vm, source, lsp) - elseif source:bindValue() then - parseValue(callback, vm, source, lsp) + end + if source:bindValue() then + isGlobal = parseValue(callback, vm, source, lsp) --parseValueSimily(callback, vm, source, lsp) end if source:bindLabel() then @@ -135,11 +121,11 @@ return function (vm, source, lsp) jumpUri(callback, vm, source, lsp) end if source:get 'in index' then - parseValue(callback, vm, source, lsp) + isGlobal = parseValue(callback, vm, source, lsp) --parseValueSimily(callback, vm, source, lsp) end if source:get 'target class' then parseClass(callback, vm, source) end - return list + return list, isGlobal end diff --git a/server/src/core/references.lua b/server/src/core/references.lua index 10d8b2df..d07e48c7 100644 --- a/server/src/core/references.lua +++ b/server/src/core/references.lua @@ -1,13 +1,13 @@ local findSource = require 'core.find_source' local function parseResult(vm, source, declarat, callback) + local isGlobal if source:bindLabel() then source:bindLabel():eachInfo(function (info, src) if (declarat and info.type == 'set') or info.type == 'get' then callback(src) end end) - return end if source:bindLocal() then local loc = source:bindLocal() @@ -22,7 +22,6 @@ local function parseResult(vm, source, declarat, callback) callback(src) end end) - return end if source:bindFunction() then if declarat then @@ -40,6 +39,9 @@ local function parseResult(vm, source, declarat, callback) callback(src) end end) + if source:bindValue():isGlobal() then + isGlobal = true + end end local parent = source:get 'parent' if parent then @@ -51,6 +53,7 @@ local function parseResult(vm, source, declarat, callback) end end) end + return isGlobal end return function (vm, pos, declarat) @@ -60,7 +63,7 @@ return function (vm, pos, declarat) end local positions = {} local mark = {} - parseResult(vm, source, declarat, function (src) + local isGlobal = parseResult(vm, source, declarat, function (src) if mark[src] then return end @@ -78,5 +81,5 @@ return function (vm, pos, declarat) uri, } end) - return positions + return positions, isGlobal end diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index 82ff9640..5478e744 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -13,7 +13,6 @@ return function (lsp) capabilities = { hoverProvider = true, definitionProvider = true, - implementationProvider = true, referencesProvider = true, renameProvider = true, documentSymbolProvider = true, diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua index fe8d38a5..50076c1c 100644 --- a/server/src/method/textDocument/definition.lua +++ b/server/src/method/textDocument/definition.lua @@ -26,9 +26,9 @@ local function findResult(lsp, params) checkWorkSpaceComplete(lsp, source) - local positions = core.definition(vm, source, lsp) + local positions, isGlobal = core.definition(vm, source, lsp) if not positions then - return nil + return nil, isGlobal end local locations = {} @@ -70,10 +70,10 @@ local function findResult(lsp, params) end if #locations == 0 then - return nil + return nil, isGlobal end - return locations + return locations, isGlobal end local LastTask @@ -83,27 +83,17 @@ return function (lsp, params) LastTask:remove() LastTask = nil end - local result = findResult(lsp, params) - if result then - return result - end return function (response) - local count = 0 + local clock = os.clock() LastTask = ac.loop(0.1, function () - local result = findResult(lsp, params) - if result then - response(result) - LastTask:remove() - LastTask = nil - return - end - count = count + 1 - if lsp:isWaitingCompile() and count < 10 then + local result, isGlobal = findResult(lsp, params) + if isGlobal and lsp:isWaitingCompile() and os.clock() - clock < 1 then return end - response(nil) + response(result) LastTask:remove() LastTask = nil end) + LastTask:onTimer() end end diff --git a/server/src/method/textDocument/references.lua b/server/src/method/textDocument/references.lua index d969e919..397b1491 100644 --- a/server/src/method/textDocument/references.lua +++ b/server/src/method/textDocument/references.lua @@ -4,7 +4,7 @@ local LastTask local function findReferences(lsp, uri, position, declarat) local vm = lsp:getVM(uri) - local positions = core.references(vm, position, declarat) + local positions, isGlobal = core.references(vm, position, declarat) if not positions then return nil end @@ -48,10 +48,10 @@ local function findReferences(lsp, uri, position, declarat) end if #locations == 0 then - return nil + return nil, isGlobal end - return locations + return locations, isGlobal end return function (lsp, params) @@ -71,20 +71,16 @@ return function (lsp, params) local position = lines:positionAsChar(params.position.line + 1, params.position.character) return function (response) + local clock = os.clock() LastTask = ac.loop(0.1, function (t) - local positions = findReferences(lsp, uri, position, declarat) - if positions then - response(positions) - t:remove() - LastTask = nil - return - end - if lsp:isWaitingCompile() then + local positions, isGlobal = findReferences(lsp, uri, position, declarat) + if isGlobal and lsp:isWaitingCompile() and os.clock() - clock < 5 then return end - response(nil) + response(positions) t:remove() LastTask = nil end) + LastTask:onTimer() end end diff --git a/server/src/timer.lua b/server/src/timer.lua index 0ae5094f..f477dccf 100644 --- a/server/src/timer.lua +++ b/server/src/timer.lua @@ -193,6 +193,10 @@ function mt:remaining() return getRemaining(self) / 1000.0 end +function mt:onTimer() + self:_onTimer() +end + function ac.wait(timeout, onTimer) local t = setmetatable({ ['_timeout'] = mathMax(mathFloor(timeout * 1000.0), 1), diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 7f5250eb..a6d5f9ea 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -939,11 +939,9 @@ function mt:doFunction(action) if source.type == 'index' then local index = self:getIndex(source) parent:setChild(index, value, source[1]) - parent:addInfo('set child', source, index) elseif source.type == 'name' then local index = source[1] parent:setChild(index, value, source) - parent:addInfo('set child', source, index) end source:bindValue(value, 'set') @@ -968,11 +966,9 @@ function mt:doFunction(action) if source.type == 'index' then local index = self:getIndex(source) parent:setChild(index, value, source[1]) - parent:addInfo('set child', source, index) elseif source.type == 'name' then local index = source[1] parent:setChild(index, value, source) - parent:addInfo('set child', source, index) end source:bindValue(value, 'set') end diff --git a/server/test/definition/emmy.lua b/server/test/definition/emmy.lua index a98f334f..73a5440c 100644 --- a/server/test/definition/emmy.lua +++ b/server/test/definition/emmy.lua @@ -19,13 +19,13 @@ local obj obj:<?cast?>() ]] ---TEST [[ ------@class A ---local <!mt!> = {} ---function mt:cast() ---end --- ------@type A ---local obj ---<?obj?>:cast() ---]] +TEST [[ +---@class A +local <!mt!> = {} +function mt:cast() +end + +---@type A +local <!obj!> +<?obj?>:cast() +]] diff --git a/server/test/definition/method.lua b/server/test/definition/method.lua index ad9fea08..08b56f61 100644 --- a/server/test/definition/method.lua +++ b/server/test/definition/method.lua @@ -69,37 +69,40 @@ setmetatable(api, { __index = mt }) api:<?method1?>() ]] ---TEST [[ ---local mt ---function mt:x() --- self.<?init?>() ---end --- ---local obj = setmetatable({}, { __index = mt }) ---obj.<!init!> = 1 ---]] --- ---TEST [[ ---local mt ---function mt:x() --- self.<?init?>() ---end --- ---local obj = setmetatable({ <!init!> = 1 }, { __index = mt }) ---]] --- ---TEST [[ ---local mt ---function mt:x() --- self.a.<?out?>() ---end --- ---local obj = setmetatable({ --- a = { --- <!out!> = 1, --- } ---}, { __index = mt }) ---]] +TEST [[ +local mt +function mt:x() + self.<?init?>() +end + +local obj = setmetatable({}, { __index = mt }) +obj.<!init!> = 1 +obj:x() +]] + +TEST [[ +local mt +function mt:x() + self.<?init?>() +end + +local obj = setmetatable({ <!init!> = 1 }, { __index = mt }) +obj:x() +]] + +TEST [[ +local mt +function mt:x() + self.a.<?out?>() +end + +local obj = setmetatable({ + a = { + <!out!> = 1, + } +}, { __index = mt }) +obj:x() +]] TEST [[ local sm = setmetatable |