summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server-beta/src/parser/guide.lua34
-rw-r--r--server-beta/src/parser/lines.lua9
-rw-r--r--server-beta/src/proto/define.lua6
3 files changed, 25 insertions, 24 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,
}
diff --git a/server-beta/src/proto/define.lua b/server-beta/src/proto/define.lua
index ee968021..5e6bd3cf 100644
--- a/server-beta/src/proto/define.lua
+++ b/server-beta/src/proto/define.lua
@@ -8,7 +8,7 @@ local m = {}
---@param position position
---@return integer
function m.offset(lines, text, position)
- local row = position.line
+ local row = position.line + 1
local start = guide.lineRange(lines, row)
local offset = utf8.offset(text, position.character + 1, start)
return offset
@@ -23,9 +23,9 @@ end
function m.position(lines, text, offset)
local row, col = guide.positionOf(lines, offset)
local start = guide.lineRange(lines, row)
- local ucol = utf8.len(text, start + 1, start + col, true)
+ local ucol = utf8.len(text, start, start + col - 1, true)
return {
- line = row,
+ line = row - 1,
character = ucol,
}
end