From ed1bf904bc15c97344dc59745c32d3f741eb1449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 19 Nov 2018 16:16:27 +0800 Subject: =?UTF-8?q?=E8=BD=AC=E5=88=B0=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lsp.lua | 11 ++++--- src/method/init.lua | 1 + src/method/textDocument/definition.lua | 35 ++++++++++++++++++++++ src/method/textDocument/didChange.lua | 5 +++- src/method/textDocument/didOpen.lua | 4 ++- src/parser/calcline.lua | 24 ++++++++++++++- src/parser/init.lua | 5 ++-- src/service.lua | 52 ++++++++++++++++++++++++++++++++ src/service/definition.lua | 0 src/service/init.lua | 54 ---------------------------------- 10 files changed, 126 insertions(+), 65 deletions(-) create mode 100644 src/method/textDocument/definition.lua create mode 100644 src/service.lua delete mode 100644 src/service/definition.lua delete mode 100644 src/service/init.lua 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.lua b/src/service.lua new file mode 100644 index 00000000..46a9ca37 --- /dev/null +++ b/src/service.lua @@ -0,0 +1,52 @@ +local sleep = require 'ffi.sleep' +local ext = require 'process.ext' +local lsp = require 'lsp' +local Method= require 'method' + +local function listen(self, input, output) + if input then + log.info('指定输入文件,路径为:', input) + fs.create_directories(input:parent_path()) + io.input(io.open(input:string(), 'rb')) + else + ext.set_filemode(io.stdin, 'b') + end + if output then + log.info('指定输出文件,路径为:', output) + fs.create_directories(output:parent_path()) + io.output(io.open(output:string(), 'wb')) + else + ext.set_filemode(io.stdout, 'b') + end + io.output():setvbuf 'no' + + local session = lsp() + session:setInput(function (mode) + return io.read(mode) + end) + session:setOutput(function (buf) + io.write(buf) + end) + session:start(function (method, params) + local f = Method[method] + if f then + local suc, res, res2 = pcall(f, session, params) + if suc then + return res, res2 + else + return nil, '发生运行时错误:' .. res + end + end + return nil, '没有注册方法:' .. method + end) +end + +local mt = { + listen = listen, +} +mt.__index = mt + +return function () + local session = setmetatable({}, mt) + return session +end diff --git a/src/service/definition.lua b/src/service/definition.lua deleted file mode 100644 index e69de29b..00000000 diff --git a/src/service/init.lua b/src/service/init.lua deleted file mode 100644 index 6244e63d..00000000 --- a/src/service/init.lua +++ /dev/null @@ -1,54 +0,0 @@ -local sleep = require 'ffi.sleep' -local ext = require 'process.ext' -local lsp = require 'lsp' -local Method= require 'method' - -local function listen(self, input, output) - if input then - log.info('指定输入文件,路径为:', input) - fs.create_directories(input:parent_path()) - io.input(io.open(input:string(), 'rb')) - else - ext.set_filemode(io.stdin, 'b') - end - if output then - log.info('指定输出文件,路径为:', output) - fs.create_directories(output:parent_path()) - io.output(io.open(output:string(), 'wb')) - else - ext.set_filemode(io.stdout, 'b') - end - io.output():setvbuf 'no' - - local session = lsp() - session:setInput(function (mode) - return io.read(mode) - 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) - if suc then - return res - else - return nil, '发生运行时错误:' .. res - end - end - return nil, '没有注册方法:' .. method - end) -end - -local mt = { - definition = require 'service.definition', - listen = listen, -} -mt.__index = mt - -return function () - local session = setmetatable({}, mt) - return session -end -- cgit v1.2.3