diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-02-08 20:26:18 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-02-08 20:26:18 +0800 |
commit | 831eb9747853999a54154b55416a1dc9b4833356 (patch) | |
tree | e762883bb76ffef647014e2e826c1171fdce98c4 /script | |
parent | 02b4c6ac49073817f564390c89894dd2a48793c7 (diff) | |
download | lua-language-server-831eb9747853999a54154b55416a1dc9b4833356.zip |
fix #388 try to map the range
Diffstat (limited to 'script')
-rw-r--r-- | script/files.lua | 42 | ||||
-rw-r--r-- | script/string-merger.lua | 52 |
2 files changed, 72 insertions, 22 deletions
diff --git a/script/files.lua b/script/files.lua index 93e1e187..8a94cc22 100644 --- a/script/files.lua +++ b/script/files.lua @@ -505,8 +505,9 @@ end --- 获取 position 对应的光标位置 ---@param uri uri ---@param position position +---@param isFinish? boolean ---@return integer -function m.offset(uri, position) +function m.offset(uri, position, isFinish) local file = m.getFile(uri) local lines = m.getLines(uri) local text = m.getText(uri) @@ -526,7 +527,12 @@ function m.offset(uri, position) offset = utf8.offset(text, position.character + 1, start) or #text end if file._diffInfo then - offset = smerger.getOffset(file._diffInfo, offset) + local start, finish = smerger.getOffset(file._diffInfo, offset) + if isFinish then + offset = finish + else + offset = start + end end return offset end @@ -567,14 +573,15 @@ end --- 将应用差异前的offset转换为应用差异后的offset ---@param uri uri ---@param offset integer ----@return integer +---@return integer start +---@return integer finish function m.diffedOffset(uri, offset) local file = m.getFile(uri) if not file then - return offset + return offset, offset end if not file._diffInfo then - return offset + return offset, offset end return smerger.getOffset(file._diffInfo, offset) end @@ -582,14 +589,15 @@ end --- 将应用差异后的offset转换为应用差异前的offset ---@param uri uri ---@param offset integer ----@return integer +---@return integer start +---@return integer finish function m.diffedOffsetBack(uri, offset) local file = m.getFile(uri) if not file then - return offset + return offset, offset end if not file._diffInfo then - return offset + return offset, offset end return smerger.getOffsetBack(file._diffInfo, offset) end @@ -608,8 +616,9 @@ end --- 将光标位置转化为 position ---@param uri uri ---@param offset integer +---@param isFinish? boolean ---@return position -function m.position(uri, offset) +function m.position(uri, offset, isFinish) local file = m.getFile(uri) local lines = m.getLines(uri) local text = m.getText(uri) @@ -620,7 +629,12 @@ function m.position(uri, offset) } end if file._diffInfo then - offset = smerger.getOffsetBack(file._diffInfo, offset) + local start, finish = smerger.getOffsetBack(file._diffInfo, offset) + if isFinish then + offset = finish + else + offset = start + end lines = m.getOriginLines(uri) text = m.getOriginText(uri) end @@ -658,8 +672,8 @@ end ---@param offset2 integer function m.range(uri, offset1, offset2) local range = { - start = m.position(uri, offset1), - ['end'] = m.position(uri, offset2), + start = m.position(uri, offset1, false), + ['end'] = m.position(uri, offset2, true), } if range.start.character > 0 then range.start.character = range.start.character - 1 @@ -673,8 +687,8 @@ end ---@return integer start ---@return integer finish function m.unrange(uri, range) - local start = m.offset(uri, range.start) - local finish = m.offset(uri, range['end']) + local start = m.offset(uri, range.start, true) + local finish = m.offset(uri, range['end'], false) return start, finish end diff --git a/script/string-merger.lua b/script/string-merger.lua index f84fb34e..b2a63f02 100644 --- a/script/string-merger.lua +++ b/script/string-merger.lua @@ -78,31 +78,67 @@ end ---根据转换前的位置获取转换后的位置 ---@param info string.merger.infos ---@param offset integer ----@return integer +---@return integer start +---@return integer finish function m.getOffset(info, offset) local diff = getNearDiff(info, offset, 'start') if not diff then - return offset + return offset, offset end if offset <= diff.finish then - return diff.cstart + local start, finish + if offset == diff.start then + start = diff.cstart + end + if offset == diff.finish then + finish = diff.cfinish + end + if not start or not finish then + local soff = offset - diff.start + local pos = math.min(diff.cstart + soff, diff.cfinish) + start = start or pos + finish = finish or pos + end + if start > finish then + start = finish + end + return start, finish end - return offset - diff.finish + diff.cfinish + local pos = offset - diff.finish + diff.cfinish + return pos, pos end ---根据转换后的位置获取转换前的位置 ---@param info string.merger.infos ---@param offset integer ----@return integer +---@return integer start +---@return integer finish function m.getOffsetBack(info, offset) local diff = getNearDiff(info, offset, 'cstart') if not diff then - return offset + return offset, offset end if offset <= diff.cfinish then - return diff.start + local start, finish + if offset == diff.cstart then + start = diff.start + end + if offset == diff.cfinish then + finish = diff.finish + end + if not start or not finish then + local soff = offset - diff.cstart + local pos = math.min(diff.start + soff, diff.finish) + start = start or pos + finish = finish or pos + end + if start > finish then + start = finish + end + return start, finish end - return offset - diff.cfinish + diff.finish + local pos = offset - diff.cfinish + diff.finish + return pos, pos end return m |