diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-09 00:07:21 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-09 00:07:21 +0800 |
commit | 386f9ad25cd58dd7297b2725c9bd2bddc59fa936 (patch) | |
tree | 516d8ea232173516290243634871297df3213882 /script-beta/proto/define.lua | |
parent | 4e7cdc47cc0f3b43ff62dee7f7c1b2d58a0e169c (diff) | |
download | lua-language-server-386f9ad25cd58dd7297b2725c9bd2bddc59fa936.zip |
加个 offsetOfWord
Diffstat (limited to 'script-beta/proto/define.lua')
-rw-r--r-- | script-beta/proto/define.lua | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/script-beta/proto/define.lua b/script-beta/proto/define.lua index 98a56547..317eda6c 100644 --- a/script-beta/proto/define.lua +++ b/script-beta/proto/define.lua @@ -15,6 +15,21 @@ function m.offset(lines, text, position) return offset - 1 end +--- 获取 position 对应的光标位置(根据附近的单词) +---@param lines table +---@param text string +---@param position position +---@return integer +function m.offsetOfWord(lines, text, position) + local row = position.line + 1 + local start = guide.lineRange(lines, row) + local offset = utf8.offset(text, position.character + 1, start) if offset > #text + or text:sub(offset-1, offset):match '[%w_][^%w_]' then + offset = offset - 1 + end + return offset +end + --- 将光标位置转化为 position ---@alias position table ---@param lines table |