diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-01-26 10:34:12 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-01-26 10:34:12 +0800 |
commit | 64022d030508555890daba49502c23ea077fc2c3 (patch) | |
tree | 0bb4f4cbf39d729d7aeafb74491de6c79fd3c309 | |
parent | 1b7188fdad1ccb560797e17f2b892bfcb3ccabf3 (diff) | |
download | lua-language-server-64022d030508555890daba49502c23ea077fc2c3.zip |
fix
-rw-r--r-- | script/files.lua | 60 | ||||
-rw-r--r-- | script/provider/provider.lua | 4 |
2 files changed, 58 insertions, 6 deletions
diff --git a/script/files.lua b/script/files.lua index 30281a17..7cdbf343 100644 --- a/script/files.lua +++ b/script/files.lua @@ -370,7 +370,17 @@ end function m.getOriginLines(uri) - + uri = getUriKey(uri) + local file = m.fileMap[uri] + if not file then + return nil + end + local lines = m.originLinesMap[uri] + if not lines then + lines = parser:lines(file.originText) + m.originLinesMap[uri] = lines + end + return lines end --- 获取原始uri @@ -397,7 +407,7 @@ end ---@alias position table --- 获取 position 对应的光标位置 ----@param uri uri +---@param uri uri ---@param position position ---@return integer function m.offset(uri, position) @@ -407,6 +417,10 @@ function m.offset(uri, position) if not file then return 0 end + if file._diffInfo then + lines = m.getOriginLines(uri) + text = m.getOriginText(uri) + end local row = position.line + 1 local start = guide.lineRange(lines, row) if start <= 0 or start > #text then @@ -430,22 +444,56 @@ function m.offsetOfWord(uri, position) if not file then return 0 end + if file._diffInfo then + lines = m.getOriginLines(uri) + text = m.getOriginText(uri) + end local row = position.line + 1 local start = guide.lineRange(lines, row) if start <= 0 or start > #text then return #text + 1 end local offset = utf8.offset(text, position.character + 1, start) or (#text + 1) + if file._diffInfo then + offset = smerger.getOffset(file._diffInfo, offset) + end if offset > #text or text:sub(offset-1, offset):match '[%w_][^%w_]' then offset = offset - 1 end - if file._diffInfo then - offset = smerger.getOffset(file._diffInfo, offset) - end return offset end +--- 将应用差异前的offset转换为应用差异后的offset +---@param uri uri +---@param offset integer +---@return integer +function m.diffedOffset(uri, offset) + local file = m.getFile(uri) + if not file then + return offset + end + if not file._diffInfo then + return offset + end + return smerger.getOffset(file._diffInfo, offset) +end + +--- 将应用差异后的offset转换为应用差异前的offset +---@param uri uri +---@param offset integer +---@return integer +function m.diffedOffsetBack(uri, offset) + local file = m.getFile(uri) + if not file then + return offset + end + if not file._diffInfo then + return offset + end + return smerger.getOffsetBack(file._diffInfo, offset) +end + --- 将光标位置转化为 position ---@param uri uri ---@param offset integer @@ -462,6 +510,8 @@ function m.position(uri, offset) end if file._diffInfo then offset = smerger.getOffsetBack(file._diffInfo, offset) + lines = m.getOriginLines(uri) + text = m.getOriginText(uri) end local row, col = guide.positionOf(lines, offset) local start, finish = guide.lineRange(lines, row, true) diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 923b6c1f..74ee11db 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -191,7 +191,9 @@ proto.on('textDocument/didChange', function (params) local text = files.getOriginText(uri) or '' for _, change in ipairs(changes) do if change.range then - local start, finish = files.unrange(uri, change.range) + local start, finish = files.unrange(uri, change.range, true) + start = files.diffedOffsetBack(uri, start) + finish = files.diffedOffsetBack(uri, finish) text = text:sub(1, start) .. change.text .. text:sub(finish + 1) else text = change.text |