summaryrefslogtreecommitdiff
path: root/script/parser
diff options
context:
space:
mode:
Diffstat (limited to 'script/parser')
-rw-r--r--script/parser/guide.lua24
-rw-r--r--script/parser/lines.lua20
2 files changed, 14 insertions, 30 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 28a275cf..b65d9680 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -759,17 +759,19 @@ function m.positionOf(row, col)
return row * 10000 + col
end
+function m.positionToOffsetByLines(lines, position)
+ local row, col = m.rowColOf(position)
+ return lines[row] + col - 1
+end
+
--- 返回全文光标位置
---@param state any
---@param position integer
function m.positionToOffset(state, position)
- local lines = state.lines
- local row, col = m.rowColOf(position)
- return lines[row] + col - 1
+ return m.positionToOffsetByLines(state.lines, position)
end
-function m.offsetToPosition(state, offset)
- local lines = state.lines
+function m.offsetToPositionByLines(lines, offset)
local left = 0
local right = #lines
local row = 0
@@ -794,16 +796,8 @@ function m.offsetToPosition(state, offset)
return m.positionOf(row, col)
end
-function m.lineContent(lines, text, row, ignoreNL)
- local line = lines[row]
- if not line then
- return ''
- end
- if ignoreNL then
- return text:sub(line.start, line.range)
- else
- return text:sub(line.start, line.finish)
- end
+function m.offsetToPosition(state, offset)
+ return m.offsetToPositionByLines(state.lines, offset)
end
local isSetMap = {
diff --git a/script/parser/lines.lua b/script/parser/lines.lua
index 06023df1..964aabf4 100644
--- a/script/parser/lines.lua
+++ b/script/parser/lines.lua
@@ -5,30 +5,20 @@ local ssub = string.sub
return function (text)
local current = 1
local lines = {}
- local i = 1
+ lines[0] = 1
+ local i = 0
while true do
local pos = sfind(text,'[\r\n]', current)
if not pos then
break
end
- local line = {
- start = current - 1,
- range = pos - 1,
- }
- lines[i] = line
i = i + 1
if ssub(text, pos, pos + 1) == '\r\n' then
- current = pos + 2
- line.finish = pos + 1
+ current = pos + 2
else
- current = pos + 1
- line.finish = pos
+ current = pos + 1
end
+ lines[i] = current
end
- lines[i] = {
- start = current - 1,
- finish = #text,
- range = #text,
- }
return lines
end