diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-11-19 16:44:00 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-11-19 16:44:00 +0800 |
commit | 7937b23423c65f41718edc684cb310d23e8da78c (patch) | |
tree | e1c94c351ef86531f753fcba608092a2088fef4d /src/parser/calcline.lua | |
parent | 54559007c3918a9b08f8c11172ba54e4944416c3 (diff) | |
download | lua-language-server-7937b23423c65f41718edc684cb310d23e8da78c.zip |
支持中文
Diffstat (limited to 'src/parser/calcline.lua')
-rw-r--r-- | src/parser/calcline.lua | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/parser/calcline.lua b/src/parser/calcline.lua index 0c692a85..26f475d9 100644 --- a/src/parser/calcline.lua +++ b/src/parser/calcline.lua @@ -15,6 +15,13 @@ local function rowcol(str, n) return row, col end +local function rowcol_utf8(str, n) + row = 1 + fl = 1 + ROWCOL:match(str:sub(1, n)) + return row, utf8.len(str, fl, n) +end + local function position(str, _row, _col) local cur = 1 local row = 1 @@ -37,6 +44,28 @@ local function position(str, _row, _col) end end +local function position_utf8(str, _row, _col) + local cur = 1 + local row = 1 + while true do + if row == _row then + return utf8.offset(str, _col, cur) + elseif row > _row then + return cur - 1 + end + local pos = str:find('[\r\n]', cur) + if not pos then + return #str + end + row = row + 1 + if str:sub(pos, pos+1) == '\r\n' then + cur = pos + 2 + else + cur = pos + 1 + end + end +end + local NL = m.P'\r\n' + m.S'\r\n' local function line(str, row) @@ -57,6 +86,8 @@ end return { rowcol = rowcol, - line = line, + rowcol_utf8 = rowcol_utf8, position = position, + position_utf8 = position_utf8, + line = line, } |