diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 22:15:47 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-09-23 22:15:47 +0800 |
commit | c95e9c91236bccb4593a0d9d218dca78bcb06ae0 (patch) | |
tree | 08109d0b434bdc799758936fc759d89ae8dd0266 /script/proto | |
parent | 291a7b3a7fcaee5bc761722ca46265f3cd7f70f3 (diff) | |
download | lua-language-server-c95e9c91236bccb4593a0d9d218dca78bcb06ae0.zip |
update
Diffstat (limited to 'script/proto')
-rw-r--r-- | script/proto/converter.lua | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/script/proto/converter.lua b/script/proto/converter.lua index 53b65b12..5fb73c14 100644 --- a/script/proto/converter.lua +++ b/script/proto/converter.lua @@ -3,13 +3,9 @@ local files = require 'files' local m = {} - ---@alias position {line: integer, character: integer} ----@param uri uri ----@param pos integer ----@return position -function m.packPosition(uri, pos) +local function rawPackPosition(uri, pos) local row, col = guide.rowColOf(pos) if col > 0 then local state = files.getState(uri) @@ -25,10 +21,39 @@ function m.packPosition(uri, pos) } end ----@param uri uri ----@param position position ----@return integer -function m.unpackPosition(uri, position) + +local function diffedPackPosition(uri, originPos) + local state = files.getState(uri) + local originLines = files.getOriginLines(uri) + local originOffset = guide.positionToOffsetByLines(originLines, originPos) + local offset = files.diffedOffsetBack(uri, originOffset) + local pos = guide.offsetToPosition(state, offset) + local row, col = guide.rowColOf(pos) + if col > 0 then + local text = files.getOriginText(uri) + if text then + local lineOffset = originLines[row] + col = utf8.len(text, lineOffset, lineOffset + col - 1, true) + end + end + return { + line = row, + character = col, + } +end + +---@param uri uri +---@param pos integer +---@return position +function m.packPosition(uri, pos) + if files.hasDiffed(uri) then + return diffedPackPosition(uri, pos) + else + return rawPackPosition(uri, pos) + end +end + +local function rawUnpackPosition(uri, position) local row, col = position.line, position.character if col > 0 then local state = files.getState(uri) @@ -42,6 +67,35 @@ function m.unpackPosition(uri, position) return pos end +local function diffedUnpackPosition(uri, position) + local row, col = position.line, position.character + local originLines = files.getOriginLines(uri) + if col > 0 then + local text = files.getOriginText(uri) + if text then + local lineOffset = originLines[row] + col = utf8.offset(text, col + 1, lineOffset) - lineOffset + end + end + local state = files.getState(uri) + local originPos = guide.positionOf(row, col) + local originOffset = guide.positionToOffsetByLines(originLines, originPos) + local offset = files.diffedOffset(originOffset) + local pos = guide.offsetToPosition(state, offset) + return pos +end + +---@param uri uri +---@param position position +---@return integer +function m.unpackPosition(uri, position) + if files.hasDiffed(uri) then + return diffedUnpackPosition(uri, position) + else + return rawUnpackPosition(uri, position) + end +end + ---@alias range {start: position, end: position} ---@param uri uri |