diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 21:12:58 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 21:12:58 +0800 |
commit | 28f7ece61d1040b374de6be4adc6c8c65c24805f (patch) | |
tree | 5602eb1e4c183d33c5812fa3086467bd6921ce18 /server-beta/src/parser | |
parent | 39de6077b1a1a6224d1acb52d17ab8ba7c885305 (diff) | |
download | lua-language-server-28f7ece61d1040b374de6be4adc6c8c65c24805f.zip |
重新整理坐标转换
Diffstat (limited to 'server-beta/src/parser')
-rw-r--r-- | server-beta/src/parser/guide.lua | 34 | ||||
-rw-r--r-- | server-beta/src/parser/lines.lua | 9 |
2 files changed, 22 insertions, 21 deletions
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index f4fd387c..b8593865 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -355,24 +355,24 @@ function m.eachSource(ast, callback) end end ---- 获取偏移对应的坐标(row从0开始,col为光标位置) +--- 获取偏移对应的坐标 ---@param lines table ---@return integer {name = 'row'} ---@return integer {name = 'col'} function m.positionOf(lines, offset) if offset < 1 then - return 0, 0 + return 1, 1 end local lastLine = lines[#lines] if offset > lastLine.finish then - return #lines - 1, lastLine.finish - lastLine.start + return #lines, lastLine.finish - lastLine.start + 1 end local min = 1 local max = #lines for _ = 1, 100 do if max <= min then local line = lines[min] - return min - 1, offset - line.start + return min, offset - line.start + 1 end local row = (max - min) // 2 + min local line = lines[row] @@ -381,50 +381,50 @@ function m.positionOf(lines, offset) elseif offset >= line.finish then min = row + 1 else - return row - 1, offset - line.start + return row, offset - line.start + 1 end end error('Stack overflow!') end ---- 获取坐标对应的偏移(row从0开始,col为光标位置) +--- 获取坐标对应的偏移 ---@param lines table ---@param row integer ---@param col integer ---@return integer {name = 'offset'} function m.offsetOf(lines, row, col) - if row < 0 then - return 0 + if row < 1 then + return 1 end - if row > #lines - 1 then + if row > #lines then local lastLine = lines[#lines] return lastLine.finish end - local line = lines[row + 1] - local len = line.finish - line.start + local line = lines[row] + local len = line.finish - line.start + 1 if col < 0 then return line.start elseif col > len then return line.finish else - return line.start + col + return line.start + col - 1 end end function m.lineContent(lines, text, row) - local line = lines[row + 1] + local line = lines[row] if not line then return '' end - return text:sub(line.start + 1, line.finish) + return text:sub(line.start, line.finish) end function m.lineRange(lines, row) - local line = lines[row + 1] + local line = lines[row] if not line then - return 0, 0 + return 1, 1 end - return line.start + 1, line.finish + return line.start, line.finish end function m.getKeyName(obj) diff --git a/server-beta/src/parser/lines.lua b/server-beta/src/parser/lines.lua index b36829de..c7961d13 100644 --- a/server-beta/src/parser/lines.lua +++ b/server-beta/src/parser/lines.lua @@ -3,9 +3,10 @@ local utf8Len = utf8.len _ENV = nil -local function Line(start, line, finish) - line.start = start - 1 +local function Line(start, line, range, finish) + line.start = start line.finish = finish - 1 + line.range = range - 1 return line end @@ -29,8 +30,8 @@ end local parser = m.P{ 'Lines', Lines = m.Ct(m.V'Line'^0 * m.V'LastLine'), -Line = m.Cp() * m.V'Indent' * (1 - m.V'Nl')^0 * m.V'Nl' * m.Cp() / Line, -LastLine= m.Cp() * m.V'Indent' * (1 - m.V'Nl')^0 * m.Cp() / Line, +Line = m.Cp() * m.V'Indent' * (1 - m.V'Nl')^0 * m.Cp() * m.V'Nl' * m.Cp() / Line, +LastLine= m.Cp() * m.V'Indent' * (1 - m.V'Nl')^0 * m.Cp() * m.Cp() / Line, Nl = m.P'\r\n' + m.S'\r\n', Indent = m.C(m.S' \t')^0 / Space, } |