diff options
-rw-r--r-- | server-beta/src/parser/guide.lua | 16 | ||||
-rw-r--r-- | server-beta/src/proto/interface.lua | 59 | ||||
-rw-r--r-- | server-beta/src/proto/provider.lua | 26 |
3 files changed, 89 insertions, 12 deletions
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index 061efaec..7cec136d 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -230,4 +230,20 @@ function m.offsetOf(lines, row, col) end end +function m.lineContent(lines, text, row) + local line = lines[row] + if not line then + return '' + end + return text:sub(line.start + 1, line.finish) +end + +function m.lineRange(lines, row) + local line = lines[row] + if not line then + return 0, 0 + end + return line.start + 1, line.finish +end + return m diff --git a/server-beta/src/proto/interface.lua b/server-beta/src/proto/interface.lua new file mode 100644 index 00000000..21be1cdf --- /dev/null +++ b/server-beta/src/proto/interface.lua @@ -0,0 +1,59 @@ +local guide = require 'parser.guide' + +local m = {} + + +--- 获取 position 对应的光标位置 +---@param lines table +---@param text string +---@param position position +---@return integer +function m.offset(lines, text, position) + local row = position.line + local start = guide.lineRange(lines, row) + local col = utf8.offset(text, position.character, start) + local offset = guide.offsetOf(lines, row, col) + return offset +end + +--- 将光标位置转化为 position +---@alias position table +---@param lines table +---@param text string +---@param offset integer +---@return position +function m.position(lines, text, offset) + local row, col = guide.positionOf(lines, offset) + local start = guide.lineRange(lines, row) + local ucol = utf8.len(text, start, col, true) + return { + line = row, + character = ucol, + } +end + +--- 将2个光标位置转化为 range +---@alias range table +---@param lines table +---@param text string +---@param offset1 integer +---@param offset2 integer +function m.range(lines, text, offset1, offset2) + return { + start = m.position(lines, text, offset1), + ['end'] = m.position(lines, text, offset2), + } +end + +---@alias location table +---@param uri string +---@param range range +---@return location +function m.location(uri, range) + return { + uri = uri, + range = range, + } +end + +return m diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua index 84463db9..41b41fdd 100644 --- a/server-beta/src/proto/provider.lua +++ b/server-beta/src/proto/provider.lua @@ -1,9 +1,10 @@ -local util = require 'utility' -local cap = require 'proto.capability' -local pub = require 'pub' -local task = require 'task' -local files = require 'files' -local proto = require 'proto.proto' +local util = require 'utility' +local cap = require 'proto.capability' +local pub = require 'pub' +local task = require 'task' +local files = require 'files' +local proto = require 'proto.proto' +local interface = require 'proto.interface' proto.on('initialize', function (params) --log.debug(util.dump(params)) @@ -58,13 +59,14 @@ proto.on('textDocument/hover', function () end) proto.on('textDocument/definition', function (params) - local core = require 'core.definition' - local uri = params.textDocument.uri - local ast = files.getAst(uri) - local text = files.getText(uri) - local clock = os.clock() + local clock = os.clock() + local core = require 'core.definition' + local uri = params.textDocument.uri + local ast = files.getAst(uri) + local text = files.getText(uri) + local offset = interface.offset(ast.lines, text, params.position) local result, correct repeat - result, correct = core(ast, text, pos) + result, correct = core(ast, text, offset) until correct or os.clock() - clock >= 1.0 end) |