diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-05 17:54:12 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-05 17:54:12 +0800 |
commit | 663a54fd01e069626e7e41bdab0223f01980711f (patch) | |
tree | 93fc0e69dbca444117a9867d7d601ac32fba58e7 /server/src | |
parent | 2d19737aab53701661a41933e12df08edf078bc1 (diff) | |
download | lua-language-server-663a54fd01e069626e7e41bdab0223f01980711f.zip |
重命名符号
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/method/init.lua | 3 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 2 | ||||
-rw-r--r-- | server/src/method/textDocument/rename.lua | 51 |
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 |