diff options
-rw-r--r-- | script-beta/proto/define.lua | 15 | ||||
-rw-r--r-- | script-beta/provider/provider.lua | 2 |
2 files changed, 16 insertions, 1 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 diff --git a/script-beta/provider/provider.lua b/script-beta/provider/provider.lua index 8a8bcef6..410410c2 100644 --- a/script-beta/provider/provider.lua +++ b/script-beta/provider/provider.lua @@ -258,7 +258,7 @@ proto.on('textDocument/rename', function (params) end local lines = files.getLines(uri) local text = files.getText(uri) - local offset = define.offset(lines, text, params.position) + local offset = define.offsetOfWord(lines, text, params.position) local result = core.rename(uri, offset, params.newName) if not result then return nil |