diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-10-19 17:30:51 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-10-19 17:30:51 +0800 |
commit | d9b4f493f08d55f479cebfcc6f16130ba0d6b35f (patch) | |
tree | 75fbf25231cac4e688e8a3b5dce9b67c119629ea /server-beta/src | |
parent | c1e44ccc9fed6166b52d08c6e48d1c21f27f5f6a (diff) | |
download | lua-language-server-d9b4f493f08d55f479cebfcc6f16130ba0d6b35f.zip |
在主线程用时编译
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/definition.lua | 10 | ||||
-rw-r--r-- | server-beta/src/files.lua | 47 | ||||
-rw-r--r-- | server-beta/src/proto/provider.lua | 9 |
3 files changed, 34 insertions, 32 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 |