summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-19 16:16:27 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-19 16:16:27 +0800
commited1bf904bc15c97344dc59745c32d3f741eb1449 (patch)
tree5996f587858d62241f2bd483c7bcc0affca6e4d8
parent14f6d57ba711eb2d64470270ee7191181bfa25dc (diff)
downloadlua-language-server-ed1bf904bc15c97344dc59745c32d3f741eb1449.zip
转到定义
-rw-r--r--src/lsp.lua11
-rw-r--r--src/method/init.lua1
-rw-r--r--src/method/textDocument/definition.lua35
-rw-r--r--src/method/textDocument/didChange.lua5
-rw-r--r--src/method/textDocument/didOpen.lua4
-rw-r--r--src/parser/calcline.lua24
-rw-r--r--src/parser/init.lua5
-rw-r--r--src/service.lua (renamed from src/service/init.lua)6
-rw-r--r--src/service/definition.lua0
9 files changed, 76 insertions, 15 deletions
diff --git a/src/lsp.lua b/src/lsp.lua
index ab3ed3fe..6c7cee62 100644
--- a/src/lsp.lua
+++ b/src/lsp.lua
@@ -59,7 +59,6 @@ function mt:_readAsContent(header)
local id = res.id
local method = res.method
local params = res.params
- log.debug('收到协议:', method)
local response, err = self:_callback(method, params)
if response then
self:_send {
@@ -99,8 +98,8 @@ function mt:read(mode)
return self._input(mode)
end
-function mt:saveText(url, version, text)
- local obj = lsp._file[url]
+function mt:saveText(uri, version, text)
+ local obj = self._file[uri]
if obj then
if obj.version >= version then
return
@@ -108,15 +107,15 @@ function mt:saveText(url, version, text)
obj.version = version
obj.text = text
else
- lsp._file[url] = {
+ self._file[uri] = {
version = version,
text = text,
}
end
end
-function mt:loadText(url)
- local obj = lsp._file[url]
+function mt:loadText(uri)
+ local obj = self._file[uri]
if not obj then
return nil
end
diff --git a/src/method/init.lua b/src/method/init.lua
index 972c7e16..35a5a124 100644
--- a/src/method/init.lua
+++ b/src/method/init.lua
@@ -5,6 +5,7 @@ local function init(name)
end
init 'initialize'
+init 'textDocument/definition'
init 'textDocument/didOpen'
init 'textDocument/didChange'
diff --git a/src/method/textDocument/definition.lua b/src/method/textDocument/definition.lua
new file mode 100644
index 00000000..ef9f4c17
--- /dev/null
+++ b/src/method/textDocument/definition.lua
@@ -0,0 +1,35 @@
+local parser = require 'parser'
+local matcher = require 'matcher'
+
+return function (lsp, params)
+ log.debug(table.dump(params))
+ local uri = params.textDocument.uri
+ local text = lsp:loadText(uri)
+ if not text then
+ return nil, '找不到文件:' .. uri
+ end
+ -- lua是从1开始的,因此都要+1
+ local pos = parser.calcline.position(text, params.position.line + 1, params.position.character + 1)
+ local suc, start, finish = matcher.definition(text, pos)
+ if not suc then
+ return nil, start
+ end
+
+ local start_row, start_col = parser.calcline.rowcol(text, start)
+ local finish_row, finish_col = parser.calcline.rowcol(text, finish)
+
+ local response = {
+ uri = uri,
+ range = {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ character = finish_col - 1,
+ },
+ },
+ }
+ return response
+end
diff --git a/src/method/textDocument/didChange.lua b/src/method/textDocument/didChange.lua
index f8fe4320..09c62a6f 100644
--- a/src/method/textDocument/didChange.lua
+++ b/src/method/textDocument/didChange.lua
@@ -1,5 +1,8 @@
return function (lsp, params)
+ local doc = params.textDocument
+ local change = params.contentChanges
+ log.debug('更新文件:', doc.uri)
-- TODO 支持差量更新
- lsp:saveText(params.url, params.version, params.text)
+ lsp:saveText(doc.uri, doc.version, change[1].text)
return true
end
diff --git a/src/method/textDocument/didOpen.lua b/src/method/textDocument/didOpen.lua
index 79392a79..8be4fa89 100644
--- a/src/method/textDocument/didOpen.lua
+++ b/src/method/textDocument/didOpen.lua
@@ -1,4 +1,6 @@
return function (lsp, params)
- lsp:saveText(params.url, params.version, arams.text)
+ local doc = params.textDocument
+ log.debug('打开文件:', doc.uri)
+ lsp:saveText(doc.uri, doc.version, doc.text)
return true
end
diff --git a/src/parser/calcline.lua b/src/parser/calcline.lua
index ef0bfa3c..0c692a85 100644
--- a/src/parser/calcline.lua
+++ b/src/parser/calcline.lua
@@ -7,7 +7,6 @@ local NL = (m.P'\r\n' + m.S'\r\n') * m.Cp() / function (pos)
fl = pos
end
local ROWCOL = (NL + m.P(1))^0
-
local function rowcol(str, n)
row = 1
fl = 1
@@ -16,6 +15,28 @@ local function rowcol(str, n)
return row, col
end
+local function position(str, _row, _col)
+ local cur = 1
+ local row = 1
+ while true do
+ if row == _row then
+ return cur + _col - 1
+ elseif row > _row then
+ return cur - 1
+ end
+ local pos = str:find('[\r\n]', cur)
+ if not pos then
+ return #str
+ end
+ row = row + 1
+ if str:sub(pos, pos+1) == '\r\n' then
+ cur = pos + 2
+ else
+ cur = pos + 1
+ end
+ end
+end
+
local NL = m.P'\r\n' + m.S'\r\n'
local function line(str, row)
@@ -37,4 +58,5 @@ end
return {
rowcol = rowcol,
line = line,
+ position = position,
}
diff --git a/src/parser/init.lua b/src/parser/init.lua
index dd60dbc1..3216fa39 100644
--- a/src/parser/init.lua
+++ b/src/parser/init.lua
@@ -1,6 +1,7 @@
local api = {
- grammar = require 'parser.grammar',
- split = require 'parser.split',
+ grammar = require 'parser.grammar',
+ split = require 'parser.split',
+ calcline = require 'parser.calcline',
}
return api
diff --git a/src/service/init.lua b/src/service.lua
index 6244e63d..46a9ca37 100644
--- a/src/service/init.lua
+++ b/src/service.lua
@@ -26,14 +26,13 @@ local function listen(self, input, output)
end)
session:setOutput(function (buf)
io.write(buf)
- log.debug(buf)
end)
session:start(function (method, params)
local f = Method[method]
if f then
- local suc, res = pcall(f, session, params)
+ local suc, res, res2 = pcall(f, session, params)
if suc then
- return res
+ return res, res2
else
return nil, '发生运行时错误:' .. res
end
@@ -43,7 +42,6 @@ local function listen(self, input, output)
end
local mt = {
- definition = require 'service.definition',
listen = listen,
}
mt.__index = mt
diff --git a/src/service/definition.lua b/src/service/definition.lua
deleted file mode 100644
index e69de29b..00000000
--- a/src/service/definition.lua
+++ /dev/null