diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-10-17 16:38:44 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-10-17 16:38:44 +0800 |
commit | bfb576d3eba9f12bb582a9f7e942c5eac387fe2b (patch) | |
tree | d5e3790aeca21994d4b9558453f40657383fcf63 /script/proto/converter.lua | |
parent | 3bdd74aa30a6175f1bbf973559241f99eb563dfe (diff) | |
download | lua-language-server-bfb576d3eba9f12bb582a9f7e942c5eac387fe2b.zip |
use `state` instead of `uri` for converter
The state may have changed when responding, so we need to use the state when requesting.
Try not to get the state on the spot.
Diffstat (limited to 'script/proto/converter.lua')
-rw-r--r-- | script/proto/converter.lua | 113 |
1 files changed, 68 insertions, 45 deletions
diff --git a/script/proto/converter.lua b/script/proto/converter.lua index 56b45c1f..d761042c 100644 --- a/script/proto/converter.lua +++ b/script/proto/converter.lua @@ -8,11 +8,23 @@ local m = {} ---@alias position {line: integer, character: integer} -local function rawPackPosition(uri, pos) +---@param row integer +---@param col integer +---@return position +function m.position(row, col) + return { + line = row, + character = col, + } +end + +---@param state parser.state +---@param pos integer +---@return position +local function rawPackPosition(state, pos) local row, col = guide.rowColOf(pos) if col > 0 then - local state = files.getState(uri) - local text = files.getText(uri) + local text = state.lua if state and text then local lineOffset = state.lines[row] if lineOffset then @@ -32,17 +44,18 @@ local function rawPackPosition(uri, pos) } end -local function diffedPackPosition(uri, pos) - local state = files.getState(uri) +---@param state parser.state +---@param pos integer +---@return position +local function diffedPackPosition(state, pos) local offset = guide.positionToOffset(state, pos) - local originOffset = files.diffedOffsetBack(uri, offset) - local originLines = files.getOriginLines(uri) - local originPos = guide.offsetToPositionByLines(originLines, originOffset) + local originOffset = files.diffedOffsetBack(state, offset) + local originPos = guide.offsetToPositionByLines(state.originLines, originOffset) local row, col = guide.rowColOf(originPos) if col > 0 then - local text = files.getOriginText(uri) + local text = state.originText if text then - local lineOffset = originLines[row] + local lineOffset = state.originLines[row] local finalOffset = math.min(lineOffset + col - 1, #text + 1) col = encoder.len(offsetEncoding, text, lineOffset, finalOffset) end @@ -53,22 +66,24 @@ local function diffedPackPosition(uri, pos) } end ----@param uri uri +---@param state parser.state ---@param pos integer ---@return position -function m.packPosition(uri, pos) - if files.hasDiffed(uri) then - return diffedPackPosition(uri, pos) +function m.packPosition(state, pos) + if files.hasDiffed(state) then + return diffedPackPosition(state, pos) else - return rawPackPosition(uri, pos) + return rawPackPosition(state, pos) end end -local function rawUnpackPosition(uri, position) +---@param state parser.state +---@param position position +---@return integer +local function rawUnpackPosition(state, position) local row, col = position.line, position.character if col > 0 then - local state = files.getState(uri) - local text = files.getText(uri) + local text = state.lua if state and text then local lineOffset = state.lines[row] local textOffset = encoder.offset(offsetEncoding, text, col + 1, lineOffset) @@ -81,61 +96,69 @@ local function rawUnpackPosition(uri, position) return pos end -local function diffedUnpackPosition(uri, position) - local row, col = position.line, position.character - local originLines = files.getOriginLines(uri) +---@param state parser.state +---@param position position +---@return integer +local function diffedUnpackPosition(state, position) + local row, col = position.line, position.character if col > 0 then - local text = files.getOriginText(uri) - if text then - local lineOffset = originLines[row] - if lineOffset then - local textOffset = encoder.offset(offsetEncoding, text, col + 1, lineOffset) - if textOffset and lineOffset then - col = textOffset - lineOffset - end + local lineOffset = state.originLines[row] + if lineOffset then + local textOffset = encoder.offset(offsetEncoding, state.originText, col + 1, lineOffset) + if textOffset and lineOffset then + col = textOffset - lineOffset end end end - local state = files.getState(uri) local originPos = guide.positionOf(row, col) - local originOffset = guide.positionToOffsetByLines(originLines, originPos) - local offset = files.diffedOffset(uri, originOffset) + local originOffset = guide.positionToOffsetByLines(state.originLines, originPos) + local offset = files.diffedOffset(state, originOffset) local pos = guide.offsetToPosition(state, offset) return pos end ----@param uri uri +---@param state parser.state ---@param position position ---@return integer -function m.unpackPosition(uri, position) - if files.hasDiffed(uri) then - return diffedUnpackPosition(uri, position) +function m.unpackPosition(state, position) + if files.hasDiffed(state) then + return diffedUnpackPosition(state, position) else - return rawUnpackPosition(uri, position) + return rawUnpackPosition(state, position) end end ---@alias range {start: position, end: position} ----@param uri uri +---@param state parser.state ---@param start integer ---@param finish integer ---@return range -function m.packRange(uri, start, finish) +function m.packRange(state, start, finish) local range = { - start = m.packPosition(uri, start), - ['end'] = m.packPosition(uri, finish), + start = m.packPosition(state, start), + ['end'] = m.packPosition(state, finish), } return range end ----@param uri uri +---@param start position +---@param finish position +---@return range +function m.range(start, finish) + return { + start = start, + ['end'] = finish, + } +end + +---@param state parser.state ---@param range range ---@return integer start ---@return integer finish -function m.unpackRange(uri, range) - local start = m.unpackPosition(uri, range.start) - local finish = m.unpackPosition(uri, range['end']) +function m.unpackRange(state, range) + local start = m.unpackPosition(state, range.start) + local finish = m.unpackPosition(state, range['end']) return start, finish end |