summaryrefslogtreecommitdiff
path: root/script-beta
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-09-09 00:07:21 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-09-09 00:07:21 +0800
commit386f9ad25cd58dd7297b2725c9bd2bddc59fa936 (patch)
tree516d8ea232173516290243634871297df3213882 /script-beta
parent4e7cdc47cc0f3b43ff62dee7f7c1b2d58a0e169c (diff)
downloadlua-language-server-386f9ad25cd58dd7297b2725c9bd2bddc59fa936.zip
加个 offsetOfWord
Diffstat (limited to 'script-beta')
-rw-r--r--script-beta/proto/define.lua15
-rw-r--r--script-beta/provider/provider.lua2
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