summaryrefslogtreecommitdiff
path: root/server-beta
diff options
context:
space:
mode:
Diffstat (limited to 'server-beta')
-rw-r--r--server-beta/src/core/definition.lua10
-rw-r--r--server-beta/src/files.lua47
-rw-r--r--server-beta/src/proto/provider.lua9
-rw-r--r--server-beta/test/definition/init.lua6
4 files changed, 35 insertions, 37 deletions
diff --git a/server-beta/src/core/definition.lua b/server-beta/src/core/definition.lua
index 0647572e..b3ec7cf8 100644
--- a/server-beta/src/core/definition.lua
+++ b/server-beta/src/core/definition.lua
@@ -1,9 +1,11 @@
local guide = require 'parser.guide'
+local engineer = require 'core.engineer'
-return function (file, offset)
+return function (ast, text, offset)
local results = {}
- guide.eachSourceContain(file.ast.ast, offset, function (source)
- file.searcher:eachDef(source, function (src)
+ local searcher = engineer(ast)
+ guide.eachSourceContain(ast.ast, offset, function (source)
+ searcher:eachDef(source, function (src)
if src.type == 'setfield'
or src.type == 'getfield'
or src.type == 'tablefield' then
@@ -17,7 +19,7 @@ return function (file, offset)
src = src.method
end
results[#results+1] = {
- uri = file.uri,
+ uri = ast.uri,
source = source,
target = src,
}
diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua
index 6b924aeb..a0346010 100644
--- a/server-beta/src/files.lua
+++ b/server-beta/src/files.lua
@@ -4,6 +4,7 @@ local task = require 'task'
local config = require 'config'
local glob = require 'glob'
local furi = require 'file-uri'
+local parser = require 'parser'
local m = {}
@@ -42,23 +43,6 @@ function m.setText(uri, text)
return
end
file.text = text
- if file.compiling then
- pub.removeTask(file.compiling)
- end
- file.compiling = pub.syncTask('compile', text, function (ast)
- file.compiling = nil
- file.ast = ast
- if ast then
- ast.uri = originUri
- end
- local onCompiledList = file.onCompiledList
- if onCompiledList then
- file.onCompiledList = nil
- for _, callback in ipairs(onCompiledList) do
- callback()
- end
- end
- end)
end
--- 监听编译完成
@@ -95,22 +79,37 @@ function m.remove(uri)
m.fileMap[uri] = nil
end
---- 获取文件语法树(异步)
+--- 获取文件语法树
function m.getAst(uri)
if platform.OS == 'Windows' then
uri = uri:lower()
end
local file = m.fileMap[uri]
- if not file.compiling then
- return file.ast
+ if file.ast == nil then
+ local state, err = parser:compile(file.text, 'lua', config.config.runtime.version)
+ if state then
+ file.ast = state
+ else
+ log.error(err)
+ file.ast = false
+ return nil
+ end
end
- pub.jumpQueue(file.compiling)
- task.wait(function (waker)
- m.onCompiled(file, waker)
- end)
return file.ast
end
+--- 获取文件行信息
+function m.getLines(uri)
+ if platform.OS == 'Windows' then
+ uri = uri:lower()
+ end
+ local file = m.fileMap[uri]
+ if not file.lines then
+ file.lines = parser:lines(file.text)
+ end
+ return file.lines
+end
+
--- 判断文件名相等
function m.eq(a, b)
if platform.OS == 'Windows' then
diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua
index 4eb1a669..0c029a45 100644
--- a/server-beta/src/proto/provider.lua
+++ b/server-beta/src/proto/provider.lua
@@ -154,8 +154,9 @@ proto.on('textDocument/definition', function (params)
if not ast then
return nil
end
+ local lines = files.getLines(uri)
local text = files.getText(uri)
- local offset = inte.offset(ast.lines, text, params.position)
+ local offset = inte.offset(lines, text, params.position)
local result = core(ast, text, offset)
if not result then
return nil
@@ -163,9 +164,9 @@ proto.on('textDocument/definition', function (params)
local response = {}
for i, info in ipairs(result) do
response[i] = inte.locationLink(info.uri
- , inte.range(ast.lines, text, info.target.start - 1, info.target.finish)
- , inte.range(ast.lines, text, info.target.start - 1, info.target.finish)
- , inte.range(ast.lines, text, info.source.start - 1, info.source.finish)
+ , inte.range(lines, text, info.target.start - 1, info.target.finish)
+ , inte.range(lines, text, info.target.start - 1, info.target.finish)
+ , inte.range(lines, text, info.source.start - 1, info.source.finish)
)
end
return response
diff --git a/server-beta/test/definition/init.lua b/server-beta/test/definition/init.lua
index f5a663cf..54e0be4b 100644
--- a/server-beta/test/definition/init.lua
+++ b/server-beta/test/definition/init.lua
@@ -42,12 +42,8 @@ function TEST(script)
local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ')
local ast = parser:compile(new_script, 'lua', 'Lua 5.3')
assert(ast)
- local file = {
- ast = ast,
- searcher = engineer(ast),
- }
- local results = core(file, pos)
+ local results = core(ast, new_script, pos)
if results then
local positions = {}
for i, result in ipairs(results) do