summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/method/init.lua3
-rw-r--r--server/src/method/initialize.lua2
-rw-r--r--server/src/method/textDocument/rename.lua51
3 files changed, 55 insertions, 1 deletions
diff --git a/server/src/method/init.lua b/server/src/method/init.lua
index bee00c90..7a465f98 100644
--- a/server/src/method/init.lua
+++ b/server/src/method/init.lua
@@ -8,11 +8,12 @@ init 'exit'
init 'initialize'
init 'initialized'
init 'shutdown'
-init 'textDocument/implementation'
init 'textDocument/definition'
init 'textDocument/didOpen'
init 'textDocument/didChange'
init 'textDocument/didClose'
+init 'textDocument/implementation'
+init 'textDocument/rename'
init 'textDocument/references'
return method
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua
index e1baa216..ac125ae8 100644
--- a/server/src/method/initialize.lua
+++ b/server/src/method/initialize.lua
@@ -8,6 +8,8 @@ return function (lsp, data)
implementationProvider = true,
-- 支持“查找引用”
referencesProvider = true,
+ -- 支持“重命名”
+ renameProvider = true,
-- 文本同步方式
textDocumentSync = {
-- 打开关闭文本时通知
diff --git a/server/src/method/textDocument/rename.lua b/server/src/method/textDocument/rename.lua
new file mode 100644
index 00000000..d152b5cd
--- /dev/null
+++ b/server/src/method/textDocument/rename.lua
@@ -0,0 +1,51 @@
+local parser = require 'parser'
+local matcher = require 'matcher'
+
+return function (lsp, params)
+ local start_clock = os.clock()
+ local uri = params.textDocument.uri
+ local newName = params.newName
+ local results, lines = lsp:loadText(uri)
+ if not results then
+ return {}
+ end
+ -- lua是从1开始的,因此都要+1
+ local position = lines:position(params.position.line + 1, params.position.character + 1, 'utf8')
+ local positions = matcher.references(results, position, true)
+ if not positions then
+ return {}
+ end
+
+ local TextEdit = {}
+ for i, position in ipairs(positions) do
+ local start, finish = position[1], position[2]
+ local start_row, start_col = lines:rowcol(start, 'utf8')
+ local finish_row, finish_col = lines:rowcol(finish, 'utf8')
+ TextEdit[i] = {
+ newText = newName,
+ range = {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ -- 这里不用-1,因为前端期待的是匹配完成后的位置
+ character = finish_col,
+ },
+ }
+ }
+ end
+
+ local response = {
+ changes = {
+ [uri] = TextEdit,
+ },
+ }
+ local passed_clock = os.clock() - start_clock
+ if passed_clock >= 0.01 then
+ log.warn(('[Find References] takes [%.3f] sec, size [%s] bits.'):format(passed_clock, #lines.buf))
+ end
+
+ return response
+end