summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-29 17:15:16 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-29 17:15:16 +0800
commitfd8737e3129c766752f81a69484bb073be7f72a0 (patch)
treeedf6bb96e793b61cb923928d5728376b662dc56b /server/src
parent9992c649cde19d4db8c9a9a7b47eb71bb2eaa283 (diff)
downloadlua-language-server-fd8737e3129c766752f81a69484bb073be7f72a0.zip
缓存语法树
Diffstat (limited to 'server/src')
-rw-r--r--server/src/lsp.lua48
-rw-r--r--server/src/method/textDocument/definition.lua9
-rw-r--r--server/src/service.lua7
3 files changed, 42 insertions, 22 deletions
diff --git a/server/src/lsp.lua b/server/src/lsp.lua
index 4a88aba6..1ac2c6c0 100644
--- a/server/src/lsp.lua
+++ b/server/src/lsp.lua
@@ -111,6 +111,7 @@ function mt:saveText(uri, version, text)
version = version,
text = text,
}
+ self._need_compile[uri] = true
end
end
@@ -119,28 +120,46 @@ function mt:loadText(uri)
if not obj then
return nil
end
- return obj.text
+ self:compileText(uri)
+ return obj.ast, obj.lines
+end
+
+function mt:compileText(uri)
+ local obj = self._file[uri]
+ if not obj then
+ return
+ end
+ if not self._need_compile[uri] then
+ return
+ end
+ self._need_compile[uri] = nil
+ local ast, err = parser:ast(obj.text)
+ if not ast then
+ log.error(err[1].err)
+ end
+ obj.ast = ast
+ obj.lines = parser:lines(obj.text)
end
function mt:removeText(uri)
self._file[uri] = nil
end
-function mt:start(method)
+function mt:setMethod(method)
self._method = method
- while true do
- local header = self:read 'l'
- if not header then
- return
- end
- if header:sub(1, #'Content-Length') == 'Content-Length' then
- self:_readAsContent(header)
- elseif header:sub(1, #'Content-Type') == 'Content-Type' then
- else
- log.error('错误的协议头:', header)
- end
+end
+
+function mt:runStep()
+ local header = self:read 'l'
+ if not header then
+ return
+ end
+ if header:sub(1, #'Content-Length') == 'Content-Length' then
+ self:_readAsContent(header)
+ elseif header:sub(1, #'Content-Type') == 'Content-Type' then
+ else
+ log.error('错误的协议头:', header)
end
- return true
end
function mt:stop()
@@ -151,5 +170,6 @@ end
return function ()
return setmetatable({
_file = {},
+ _need_compile = {},
}, mt)
end
diff --git a/server/src/method/textDocument/definition.lua b/server/src/method/textDocument/definition.lua
index 0878e29c..f5fb6472 100644
--- a/server/src/method/textDocument/definition.lua
+++ b/server/src/method/textDocument/definition.lua
@@ -3,16 +3,11 @@ local matcher = require 'matcher'
return function (lsp, params)
local uri = params.textDocument.uri
- local text = lsp:loadText(uri)
- if not text then
+ local ast, lines = lsp:loadText(uri)
+ if not ast then
return nil, '找不到文件:' .. uri
end
local start_clock = os.clock()
- local ast, err = parser:ast(text)
- local lines = parser:lines(text)
- if not ast then
- return nil, err
- end
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1)
local suc, start, finish = matcher.definition(ast, position, 'utf8')
diff --git a/server/src/service.lua b/server/src/service.lua
index f8d1fc53..9e11d82f 100644
--- a/server/src/service.lua
+++ b/server/src/service.lua
@@ -28,7 +28,7 @@ local function listen(self, input, output)
session:setOutput(function (buf)
io.write(buf)
end)
- session:start(function (method, params)
+ session:setMethod(function (method, params)
local optional
if method:sub(1, 2) == '$/' then
method = method:sub(3)
@@ -49,6 +49,11 @@ local function listen(self, input, output)
return nil, '没有注册方法:' .. method
end
end)
+
+ while true do
+ session:runStep()
+ sleep(1)
+ end
end
local mt = {