summaryrefslogtreecommitdiff
path: root/script/parser
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-09-07 16:28:31 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-09-07 16:28:31 +0800
commita815d3f8c22cbbb44c3977cf73f87943283c213c (patch)
treeb0c1a89008db3b8da20e8aa00536c1d07774476c /script/parser
parentefe82edce8af8160c3a6e0977bee935f7f8655d5 (diff)
downloadlua-language-server-a815d3f8c22cbbb44c3977cf73f87943283c213c.zip
fix #1529
Diffstat (limited to 'script/parser')
-rw-r--r--script/parser/compile.lua1
-rw-r--r--script/parser/guide.lua16
-rw-r--r--script/parser/lines.lua1
3 files changed, 16 insertions, 2 deletions
diff --git a/script/parser/compile.lua b/script/parser/compile.lua
index 931f438d..45678976 100644
--- a/script/parser/compile.lua
+++ b/script/parser/compile.lua
@@ -3826,6 +3826,7 @@ local function initState(lua, version, options)
comms = {},
lines = {
[0] = 1,
+ size = #lua,
},
options = options or {},
}
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index e4faf47f..e8a53240 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -812,12 +812,24 @@ end
---@param col integer
---@return integer
function m.positionOf(row, col)
- return row * 10000 + col
+ return row * 10000 + math.min(col, 10000 - 1)
end
function m.positionToOffsetByLines(lines, position)
local row, col = m.rowColOf(position)
- return (lines[row] or 1) + col - 1
+ if row < 0 then
+ return 0
+ end
+ if row > #lines then
+ return lines.size
+ end
+ local offset = lines[row] + col - 1
+ if lines[row + 1] and offset >= lines[row + 1] then
+ return lines[row + 1] - 1
+ elseif offset > lines.size then
+ return lines.size
+ end
+ return offset
end
--- 返回全文光标位置
diff --git a/script/parser/lines.lua b/script/parser/lines.lua
index 964aabf4..ffd021e6 100644
--- a/script/parser/lines.lua
+++ b/script/parser/lines.lua
@@ -6,6 +6,7 @@ return function (text)
local current = 1
local lines = {}
lines[0] = 1
+ lines.size = #text
local i = 0
while true do
local pos = sfind(text,'[\r\n]', current)