summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-17 23:27:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-17 23:27:29 +0800
commit0805814ac575b0f610c4831548cb7f74b39ee37c (patch)
treef50705e74d6154ae13846b7a741be73cb29f817d /server-beta/src
parent5e91ce2128ed7b44044a701b4a8d17c6df831a56 (diff)
downloadlua-language-server-0805814ac575b0f610c4831548cb7f74b39ee37c.zip
支持 prepareRename
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/core/rename.lua57
-rw-r--r--server-beta/src/provider/capability.lua4
-rw-r--r--server-beta/src/provider/init.lua21
3 files changed, 77 insertions, 5 deletions
diff --git a/server-beta/src/core/rename.lua b/server-beta/src/core/rename.lua
index dbeb56b7..0d932a4f 100644
--- a/server-beta/src/core/rename.lua
+++ b/server-beta/src/core/rename.lua
@@ -198,8 +198,7 @@ local function rename(source, newname, callback)
searcher.eachRef(source, function (info)
callback(info.source, info.source.start, info.source.finish, newname)
end)
- end
- if source.type == 'local' then
+ elseif source.type == 'local' then
return ofLocal(source, newname, callback)
elseif source.type == 'setlocal'
or source.type == 'getlocal' then
@@ -215,7 +214,36 @@ local function rename(source, newname, callback)
return true
end
-return function (uri, pos, newname)
+local function prepareRename(source)
+ if source.type == 'label'
+ or source.type == 'goto'
+ or source.type == 'local'
+ or source.type == 'setlocal'
+ or source.type == 'getlocal'
+ or source.type == 'field'
+ or source.type == 'method'
+ or source.type == 'tablefield'
+ or source.type == 'setglobal'
+ or source.type == 'getglobal' then
+ return source, source[1]
+ elseif source.type == 'string' then
+ local parent = source.parent
+ if not parent then
+ return nil
+ end
+ if parent.type == 'setindex'
+ or parent.type == 'getindex'
+ or parent.type == 'tableindex' then
+ return source, source[1]
+ end
+ return nil
+ end
+ return nil
+end
+
+local m = {}
+
+function m.rename(uri, pos, newname)
local ast = files.getAst(uri)
if not ast then
return nil
@@ -243,3 +271,26 @@ return function (uri, pos, newname)
end
return results
end
+
+function m.prepareRename(uri, pos)
+ local ast = files.getAst(uri)
+ if not ast then
+ return nil
+ end
+
+ local result
+ guide.eachSourceContain(ast.ast, pos, function(source)
+ local res, text = prepareRename(source)
+ if res then
+ result = {
+ start = source.start,
+ finish = source.finish,
+ text = text,
+ }
+ end
+ end)
+
+ return result
+end
+
+return m
diff --git a/server-beta/src/provider/capability.lua b/server-beta/src/provider/capability.lua
index b2b7ee3a..aa95c758 100644
--- a/server-beta/src/provider/capability.lua
+++ b/server-beta/src/provider/capability.lua
@@ -12,7 +12,9 @@ m.initer = {
hoverProvider = true,
definitionProvider = true,
referencesProvider = true,
- renameProvider = true,
+ renameProvider = {
+ prepareProvider = true,
+ },
--documentSymbolProvider = true,
documentHighlightProvider = true,
--codeActionProvider = true,
diff --git a/server-beta/src/provider/init.lua b/server-beta/src/provider/init.lua
index 1dc092c7..41a1aed5 100644
--- a/server-beta/src/provider/init.lua
+++ b/server-beta/src/provider/init.lua
@@ -239,7 +239,7 @@ proto.on('textDocument/rename', function (params)
local lines = files.getLines(uri)
local text = files.getText(uri)
local offset = define.offset(lines, text, params.position)
- local result = core(uri, offset, params.newName)
+ local result = core.rename(uri, offset, params.newName)
if not result then
return nil
end
@@ -259,6 +259,25 @@ proto.on('textDocument/rename', function (params)
return workspaceEdit
end)
+proto.on('textDocument/prepareRename', function (params)
+ local core = require 'core.rename'
+ local uri = params.textDocument.uri
+ if not files.exists(uri) then
+ return nil
+ end
+ local lines = files.getLines(uri)
+ local text = files.getText(uri)
+ local offset = define.offset(lines, text, params.position)
+ local result = core.prepareRename(uri, offset)
+ if not result then
+ return nil
+ end
+ return {
+ range = define.range(lines, text, result.start, result.finish),
+ placeholder = result.text,
+ }
+end)
+
proto.on('textDocument/completion', function (params)
--log.info(util.dump(params))
return nil