summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/core/completion.lua6
-rw-r--r--server/src/core/definition.lua24
-rw-r--r--server/src/core/find_result.lua41
-rw-r--r--server/src/core/find_source.lua36
-rw-r--r--server/src/core/init.lua2
-rw-r--r--server/src/core/references.lua4
-rw-r--r--server/src/core/rename.lua4
-rw-r--r--server/src/method/textDocument/definition.lua2
-rw-r--r--server/src/method/textDocument/hover.lua2
-rw-r--r--server/src/method/textDocument/implementation.lua2
-rw-r--r--server/src/vm/multi.lua14
-rw-r--r--server/src/vm/source.lua9
-rw-r--r--server/src/vm/value.lua2
-rw-r--r--server/src/vm/vm.lua8
-rw-r--r--server/test/crossfile/definition.lua4
-rw-r--r--server/test/crossfile/hover.lua2
-rw-r--r--server/test/definition/init.lua4
-rw-r--r--server/test/find_lib/init.lua2
-rw-r--r--server/test/hover/init.lua2
-rw-r--r--server/test/type_inference/init.lua2
20 files changed, 89 insertions, 83 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua
index 98654a65..57e006e9 100644
--- a/server/src/core/completion.lua
+++ b/server/src/core/completion.lua
@@ -1,4 +1,4 @@
-local findResult = require 'core.find_result'
+local findSource = require 'core.find_source'
local hover = require 'core.hover'
local CompletionItemKind = {
@@ -513,7 +513,7 @@ end
local function searchSpecial(vm, pos, callback)
-- 尝试 #
- local _, source = findResult(vm, pos, 2)
+ local _, source = findSource(vm, pos, 2)
if source and source.indexName and source[1].op == '#'
then
local label = source.indexName .. '+1'
@@ -558,7 +558,7 @@ return function (vm, pos, word)
end
searchSpecial(vm, pos, callback)
if not inString then
- local result, source = findResult(vm, pos)
+ local result, source = findSource(vm, pos)
if not isValidResult(result) then
result, source = findClosePos(vm, pos)
end
diff --git a/server/src/core/definition.lua b/server/src/core/definition.lua
index 05e62f4f..e4af5d16 100644
--- a/server/src/core/definition.lua
+++ b/server/src/core/definition.lua
@@ -125,27 +125,15 @@ local function parseResultAsVar(vm, result, lsp)
return positions
end
-local function parseResultAsValue(vm, value, lsp)
- local tp = value:getType()
- local positions = {}
- if tp == 'string' then
- -- require 'XXX' 专用
- positions[#positions+1] = {
- 0,
- 0,
- value.uri,
- }
- end
- return positions
+local function parseValue(vm, value, lsp)
+
end
-return function (vm, result, lsp)
- if not result then
+return function (vm, source, lsp)
+ if not source then
return nil
end
- if result.type == 'value' then
- return parseResultAsValue(vm, result, lsp)
- else
- return parseResultAsVar(vm, result, lsp)
+ if source:bindValue() then
+ return parseValue(vm, source:bindValue(), lsp)
end
end
diff --git a/server/src/core/find_result.lua b/server/src/core/find_result.lua
deleted file mode 100644
index a562f192..00000000
--- a/server/src/core/find_result.lua
+++ /dev/null
@@ -1,41 +0,0 @@
-local function isContainPos(obj, pos)
- if obj.start <= pos and obj.finish + 1 >= pos then
- return true
- end
- return false
-end
-
-local function isValidSource(source)
- return source.type ~= 'simple'
-end
-
-local function findAtPos(results, pos, level)
- local res = {}
- for _, source in ipairs(results.sources) do
- if isValidSource(source) and isContainPos(source, pos) then
- res[#res+1] = {
- object = source.bind,
- source = source,
- range = source.finish - source.start,
- }
- if not source.bind then
- error('Miss source object')
- end
- end
- end
- if #res == 0 then
- return nil
- end
- table.sort(res, function (a, b)
- return a.range < b.range
- end)
- local data = res[level or 1]
- if not data then
- return nil
- end
- return data.object, data.source
-end
-
-return function (vm, pos, level)
- return findAtPos(vm.results, pos, level)
-end
diff --git a/server/src/core/find_source.lua b/server/src/core/find_source.lua
new file mode 100644
index 00000000..52b42501
--- /dev/null
+++ b/server/src/core/find_source.lua
@@ -0,0 +1,36 @@
+local function isContainPos(obj, pos)
+ if obj.start <= pos and obj.finish + 1 >= pos then
+ return true
+ end
+ return false
+end
+
+local function isValidSource(source)
+ return source.start ~= nil
+end
+
+local function findAtPos(sources, pos, level)
+ local res = {}
+ for _, source in ipairs(sources) do
+ if isValidSource(source) and isContainPos(source, pos) then
+ res[#res+1] = source
+ end
+ end
+ if #res == 0 then
+ return nil
+ end
+ table.sort(res, function (a, b)
+ local rangeA = a.finish - a.start
+ local rangeB = b.finish - b.start
+ return rangeA < rangeB
+ end)
+ local source = res[level or 1]
+ if not source then
+ return nil
+ end
+ return source
+end
+
+return function (vm, pos, level)
+ return findAtPos(vm.sources, pos, level)
+end
diff --git a/server/src/core/init.lua b/server/src/core/init.lua
index 148871a1..1a556320 100644
--- a/server/src/core/init.lua
+++ b/server/src/core/init.lua
@@ -5,7 +5,7 @@ local api = {
rename = require 'core.rename',
hover = require 'core.hover',
diagnostics = require 'core.diagnostics',
- findResult = require 'core.find_result',
+ findSource = require 'core.find_source',
findLib = require 'core.find_lib',
completion = require 'core.completion',
signature = require 'core.signature',
diff --git a/server/src/core/references.lua b/server/src/core/references.lua
index 05ba99a9..1d1915f0 100644
--- a/server/src/core/references.lua
+++ b/server/src/core/references.lua
@@ -1,4 +1,4 @@
-local findResult = require 'core.find_result'
+local findSource = require 'core.find_source'
local function parseResult(vm, result, declarat, callback)
local tp = result.type
@@ -46,7 +46,7 @@ local function parseResult(vm, result, declarat, callback)
end
return function (vm, pos, declarat)
- local result = findResult(vm, pos)
+ local result = findSource(vm, pos)
if not result then
return nil
end
diff --git a/server/src/core/rename.lua b/server/src/core/rename.lua
index 655c1b9a..f3132f77 100644
--- a/server/src/core/rename.lua
+++ b/server/src/core/rename.lua
@@ -1,4 +1,4 @@
-local findResult = require 'core.find_result'
+local findSource = require 'core.find_source'
local parser = require 'parser'
local function parseResult(result, source, newName)
@@ -40,7 +40,7 @@ local function parseResult(result, source, newName)
end
return function (vm, pos, newName)
- local result, source = findResult(vm, pos)
+ local result, source = findSource(vm, pos)
if not result then
return nil
end
diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua
index f8aa5bf5..8c8a2bb7 100644
--- a/server/src/method/textDocument/definition.lua
+++ b/server/src/method/textDocument/definition.lua
@@ -24,7 +24,7 @@ return function (lsp, params)
end
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1)
- local result = core.findResult(vm, position)
+ local result = core.findSource(vm, position)
if not result then
return nil
end
diff --git a/server/src/method/textDocument/hover.lua b/server/src/method/textDocument/hover.lua
index 9c98b8f0..5dea17cb 100644
--- a/server/src/method/textDocument/hover.lua
+++ b/server/src/method/textDocument/hover.lua
@@ -9,7 +9,7 @@ return function (lsp, params)
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1)
- local result, source = core.findResult(vm, position)
+ local result, source = core.findSource(vm, position)
if not result then
return nil
end
diff --git a/server/src/method/textDocument/implementation.lua b/server/src/method/textDocument/implementation.lua
index 7117aba1..d6bf35bf 100644
--- a/server/src/method/textDocument/implementation.lua
+++ b/server/src/method/textDocument/implementation.lua
@@ -21,7 +21,7 @@ return function (lsp, params)
end
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1)
- local result = core.findResult(vm, position)
+ local result = core.findSource(vm, position)
if not result then
return nil
end
diff --git a/server/src/vm/multi.lua b/server/src/vm/multi.lua
index ffaae1c0..93900a36 100644
--- a/server/src/vm/multi.lua
+++ b/server/src/vm/multi.lua
@@ -4,8 +4,18 @@ local mt = {}
mt.__index = mt
mt.type = 'multi'
-function mt:push(value)
- self[#self+1] = value
+function mt:push(value, isLast)
+ if value.type == 'list' then
+ if isLast then
+ for _, v in ipairs(value) do
+ self[#self+1] = v
+ end
+ else
+ self[#self+1] = value[1]
+ end
+ else
+ self[#self+1] = value
+ end
end
function mt:get(index)
diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua
index e2de6a27..1e9001d1 100644
--- a/server/src/vm/source.lua
+++ b/server/src/vm/source.lua
@@ -26,6 +26,15 @@ function mt:bindFunction(func)
end
end
+function mt:bindValue(value, action)
+ if value then
+ self._bindValue = value
+ value:addInfo(action, self)
+ else
+ return self._bindValue
+ end
+end
+
function mt:setUri(uri)
self._uri = uri
end
diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua
index f8824db6..1079c206 100644
--- a/server/src/vm/value.lua
+++ b/server/src/vm/value.lua
@@ -184,7 +184,7 @@ function mt:addInfo(tp, source)
if source and not source.start then
error('Miss start: ' .. table.dump(source))
end
- self[#self] = {
+ self[#self+1] = {
type = tp,
source = source or getDefaultSource(),
}
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index 62f057d2..3d53feee 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -450,6 +450,7 @@ function mt:getSimple(simple, max)
local first = simple[1]
self:instantSource(first)
local value = self:getExp(first)
+ first:bindValue(value, 'get')
if not max then
max = #simple
elseif max < 0 then
@@ -472,8 +473,10 @@ function mt:getSimple(simple, max)
local child = source[1]
local index = self:getIndex(child)
value = value:getChild(index) or createValue('any')
+ source:bindValue(value, 'get')
elseif source.type == 'name' then
value = value:getChild(source[1]) or createValue('any')
+ source:bindValue(value, 'get')
elseif source.type == ':' then
object = value
elseif source.type == '.' then
@@ -664,8 +667,8 @@ end
function mt:getMultiByList(list)
local multi = createMulti()
- for _, exp in ipairs(list) do
- multi:push(self:getExp(exp))
+ for i, exp in ipairs(list) do
+ multi:push(self:getExp(exp), i == #list)
end
return multi
end
@@ -722,6 +725,7 @@ function mt:setOne(var, value)
value = createValue('nil')
end
self:instantSource(var)
+ var:bindValue(value, 'set')
if var.type == 'name' then
self:setName(var[1], var, value)
elseif var.type == 'simple' then
diff --git a/server/test/crossfile/definition.lua b/server/test/crossfile/definition.lua
index 458154bc..3fd8d3fe 100644
--- a/server/test/crossfile/definition.lua
+++ b/server/test/crossfile/definition.lua
@@ -63,8 +63,8 @@ function TEST(datas)
local sourceVM = lsp:getVM(sourceUri)
assert(sourceVM)
local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
- local result = core.findResult(sourceVM, sourcePos)
- local positions = core.definition(sourceVM, result, lsp)
+ local source = core.findSource(sourceVM, sourcePos)
+ local positions = core.definition(sourceVM, source, lsp)
assert(positions and positions[1])
local start, finish, valueUri = positions[1][1], positions[1][2], positions[1][3]
assert(valueUri == targetUri)
diff --git a/server/test/crossfile/hover.lua b/server/test/crossfile/hover.lua
index a4479280..03f3c6d5 100644
--- a/server/test/crossfile/hover.lua
+++ b/server/test/crossfile/hover.lua
@@ -72,7 +72,7 @@ function TEST(data)
local sourceVM = lsp:loadVM(sourceUri)
assert(sourceVM)
local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
- local result, source = core.findResult(sourceVM, sourcePos)
+ local result, source = core.findSource(sourceVM, sourcePos)
local hover = core.hover(result, source, lsp)
assert(hover)
if data.hover.description then
diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua
index 19e353cc..f2b2833c 100644
--- a/server/test/definition/init.lua
+++ b/server/test/definition/init.lua
@@ -45,8 +45,8 @@ function TEST(script)
local vm = buildVM(ast)
assert(vm)
- local result = core.findResult(vm, pos)
- local positions = core.definition(vm, result, nil)
+ local source = core.findSource(vm, pos)
+ local positions = core.definition(vm, source, nil)
if positions then
assert(founded(target, positions))
else
diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua
index e55b33af..eb4686bc 100644
--- a/server/test/find_lib/init.lua
+++ b/server/test/find_lib/init.lua
@@ -13,7 +13,7 @@ function TEST(fullkey)
assert(ast)
local vm = core.vm(ast)
assert(vm)
- local result = core.findResult(vm, pos)
+ local result = core.findSource(vm, pos)
local _, name = core.findLib(result)
assert(name == fullkey)
end
diff --git a/server/test/hover/init.lua b/server/test/hover/init.lua
index ca8cf823..cda10baa 100644
--- a/server/test/hover/init.lua
+++ b/server/test/hover/init.lua
@@ -12,7 +12,7 @@ function TEST(script)
local ast = parser:ast(new_script)
local vm = core.vm(ast)
assert(vm)
- local result, source = core.findResult(vm, pos)
+ local result, source = core.findSource(vm, pos)
local hover = core.hover(result, source)
if expect then
assert(hover)
diff --git a/server/test/type_inference/init.lua b/server/test/type_inference/init.lua
index 0962c20d..ef0ef001 100644
--- a/server/test/type_inference/init.lua
+++ b/server/test/type_inference/init.lua
@@ -12,7 +12,7 @@ function TEST(res)
local ast = parser:ast(new_script)
local vm = core.vm(ast)
assert(vm)
- local result = core.findResult(vm, pos)
+ local result = core.findSource(vm, pos)
assert(result)
assert(res == result.value:getType())
end