diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-09-17 17:54:55 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-09-17 17:54:55 +0800 |
commit | db328eeed2ab7b67a179bb14edfdd0ad03c63bb1 (patch) | |
tree | c2ca7d95af9a6b6201b7333710c0aa666e4d4dd1 /script/parser/guide.lua | |
parent | bb5709481e7378901e8687beb7caac7521d48122 (diff) | |
download | lua-language-server-db328eeed2ab7b67a179bb14edfdd0ad03c63bb1.zip |
update
Diffstat (limited to 'script/parser/guide.lua')
-rw-r--r-- | script/parser/guide.lua | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 4915f206..e2262cfd 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -760,12 +760,38 @@ end --- 返回全文光标位置 ---@param state any ---@param position integer -function m.offsetOf(state, position) +function m.positionToOffset(state, position) local lines = state.lines local row, col = m.rowColOf(position) return (lines[row] or 1) + col - 1 end +function m.offsetToPosition(state, offset) + local lines = state.lines + local left = 0 + local right = #lines + local row = 0 + while true do + row = (left + right) // 2 + if row == left then + if right ~= left then + if lines[right] <= offset then + row = right + end + end + break + end + local start = lines[row] + if start > offset then + right = row + else + left = row + end + end + local col = offset - (lines[row] or 1) + return m.positionOf(row, col) +end + function m.lineContent(lines, text, row, ignoreNL) local line = lines[row] if not line then |