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 | |
parent | 54559007c3918a9b08f8c11172ba54e4944416c3 (diff) | |
download | lua-language-server-7937b23423c65f41718edc684cb310d23e8da78c.zip |
支持中文
Diffstat (limited to 'src')
-rw-r--r-- | src/method/textDocument/definition.lua | 6 | ||||
-rw-r--r-- | src/parser/calcline.lua | 33 |
2 files changed, 35 insertions, 4 deletions
diff --git a/src/method/textDocument/definition.lua b/src/method/textDocument/definition.lua index d9378b58..9601cc4a 100644 --- a/src/method/textDocument/definition.lua +++ b/src/method/textDocument/definition.lua @@ -8,14 +8,14 @@ return function (lsp, params) return nil, '找不到文件:' .. uri end -- lua是从1开始的,因此都要+1 - local pos = parser.calcline.position(text, params.position.line + 1, params.position.character + 1) + local pos = parser.calcline.position_utf8(text, params.position.line + 1, params.position.character + 1) local suc, start, finish = matcher.definition(text, pos) if not suc then return {} end - local start_row, start_col = parser.calcline.rowcol(text, start) - local finish_row, finish_col = parser.calcline.rowcol(text, finish) + local start_row, start_col = parser.calcline.rowcol_utf8(text, start) + local finish_row, finish_col = parser.calcline.rowcol_utf8(text, finish) local response = { uri = uri, 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, } |