diff options
Diffstat (limited to 'server-beta')
-rw-r--r-- | server-beta/src/core/rename.lua | 57 | ||||
-rw-r--r-- | server-beta/src/provider/capability.lua | 4 | ||||
-rw-r--r-- | server-beta/src/provider/init.lua | 21 | ||||
-rw-r--r-- | server-beta/test/rename/init.lua | 16 |
4 files changed, 92 insertions, 6 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 diff --git a/server-beta/test/rename/init.lua b/server-beta/test/rename/init.lua index acfc4ae1..48e82546 100644 --- a/server-beta/test/rename/init.lua +++ b/server-beta/test/rename/init.lua @@ -57,7 +57,7 @@ function TEST(oldName, newName) local pos = oldScript:find('[^%w_]'..oldName..'[^%w_]') assert(pos) - local positions = core('', pos+1, newName) + local positions = core.rename('', pos+1, newName) local script = oldScript if positions then script = replace(script, positions) @@ -129,6 +129,20 @@ t["!!!"] = 1 a = t["!!!"] ]] +TEST ('a', '!!!') [[ +t = { + ['a'] = 0 +} +t.a = 1 +a = t.a +]] [[ +t = { + ["!!!"] = 0 +} +t["!!!"] = 1 +a = t["!!!"] +]] + TEST ('a', '"') [[ print(t[ "a" ]) ]] [[ |