diff options
-rw-r--r-- | server-beta/src/core/rename.lua | 80 | ||||
-rw-r--r-- | server-beta/test.lua | 2 | ||||
-rw-r--r-- | server-beta/test/rename/init.lua | 20 |
3 files changed, 91 insertions, 11 deletions
diff --git a/server-beta/src/core/rename.lua b/server-beta/src/core/rename.lua new file mode 100644 index 00000000..a2b1b3fc --- /dev/null +++ b/server-beta/src/core/rename.lua @@ -0,0 +1,80 @@ +local files = require 'files' +local searcher = require 'searcher' +local guide = require 'parser.guide' + +local function checkSource(source) + if source.type == 'field' + or source.type == 'method' + or source.type == 'tablefield' + or source.type == 'string' + or source.type == 'local' + or source.type == 'setlocal' + or source.type == 'getlocal' + or source.type == 'setglobal' + or source.type == 'getglobal' + or source.type == 'label' + or source.type == 'goto' then + return true + end + return false +end + +local function rename(source) + if source.type == 'field' + or source.type == 'method' + or source.type == 'tablefield' + or source.type == 'string' + or source.type == 'local' + or source.type == 'setlocal' + or source.type == 'getlocal' + or source.type == 'setglobal' + or source.type == 'getglobal' + or source.type == 'label' + or source.type == 'goto' then + return source + end + if source.type == 'setfield' + or source.type == 'getfield' + or source.type == 'tablefield' then + return source.field + end + if source.type == 'setindex' + or source.type == 'getindex' + or source.type == 'tableindex' then + return source.index + end + if source.type == 'setmethod' + or source.type == 'getmethod' then + return source.method + end + return nil +end + +return function (uri, pos, newname) + local ast = files.getAst(uri) + if not ast then + return nil + end + local results = {} + guide.eachSourceContain(ast.ast, pos, function(source) + if not checkSource(source) then + return + end + searcher.eachRef(source, function (info) + local src = rename(info.source) + if not src then + return + end + results[#results+1] = { + start = src.start, + finish = src.finish, + text = newname, + uri = guide.getRoot(src).uri, + } + end) + end) + if #results == 0 then + return nil + end + return results +end diff --git a/server-beta/test.lua b/server-beta/test.lua index 17d1f2bf..7213d596 100644 --- a/server-beta/test.lua +++ b/server-beta/test.lua @@ -40,7 +40,7 @@ local function main() test 'definition' test 'diagnostics' test 'highlight' - --test 'rename' + test 'rename' --test 'type_inference' --test 'find_lib' --test 'hover' diff --git a/server-beta/test/rename/init.lua b/server-beta/test/rename/init.lua index 646401a0..fdb47ebe 100644 --- a/server-beta/test/rename/init.lua +++ b/server-beta/test/rename/init.lua @@ -1,6 +1,5 @@ -local core = require 'core' -local parser = require 'parser' -local buildVM = require 'vm' +local core = require 'core.rename' +local files = require 'files' local function catch_target(script) local list = {} @@ -10,7 +9,10 @@ local function catch_target(script) if not start then break end - list[#list+1] = { start + 2, finish - 2 } + list[#list+1] = { + start = start + 2, + finish = finish - 2, + } cur = finish + 1 end return list @@ -22,7 +24,7 @@ local function founded(targets, results) end for _, target in ipairs(targets) do for _, result in ipairs(results) do - if target[1] == result[1] and target[2] == result[2] then + if target.start == result.start and target.finish == result.finish then goto NEXT end end @@ -34,17 +36,15 @@ end function TEST(newName) return function (script) + files.removeAll() local target = catch_target(script) local start = script:find('<?', 1, true) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:parse(new_script, 'lua', 'Lua 5.3') - assert(ast) - local vm = buildVM(ast) - assert(vm) + files.setText('', new_script) - local positions = core.rename(vm, pos, newName) + local positions = core('', pos, newName) if positions then assert(founded(target, positions)) else |