summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/matcher/find_result.lua3
-rw-r--r--server/src/matcher/init.lua1
-rw-r--r--server/src/matcher/rename.lua62
-rw-r--r--server/src/method/textDocument/rename.lua2
4 files changed, 67 insertions, 1 deletions
diff --git a/server/src/matcher/find_result.lua b/server/src/matcher/find_result.lua
index b9313510..0b06ccad 100644
--- a/server/src/matcher/find_result.lua
+++ b/server/src/matcher/find_result.lua
@@ -9,6 +9,7 @@ return function (results, pos)
return {
type = 'var',
var = var,
+ info = info,
}
end
end
@@ -19,6 +20,7 @@ return function (results, pos)
return {
type = 'dots',
dots = dots,
+ info = info,
}
end
end
@@ -29,6 +31,7 @@ return function (results, pos)
return {
type = 'label',
label = label,
+ info = info,
}
end
end
diff --git a/server/src/matcher/init.lua b/server/src/matcher/init.lua
index af604289..e0134c5b 100644
--- a/server/src/matcher/init.lua
+++ b/server/src/matcher/init.lua
@@ -2,6 +2,7 @@ local api = {
definition = require 'matcher.definition',
implementation = require 'matcher.implementation',
references = require 'matcher.references',
+ rename = require 'matcher.rename',
compile = require 'matcher.compile',
}
diff --git a/server/src/matcher/rename.lua b/server/src/matcher/rename.lua
new file mode 100644
index 00000000..f67ec581
--- /dev/null
+++ b/server/src/matcher/rename.lua
@@ -0,0 +1,62 @@
+local findResult = require 'matcher.find_result'
+
+local function tryMeta(var)
+ local keys = {}
+ repeat
+ if var.childs.meta then
+ local metavar = var.childs.meta
+ for i = #keys, 1, -1 do
+ local key = keys[i]
+ metavar = metavar.childs[key]
+ if not metavar then
+ return nil
+ end
+ end
+ return metavar
+ end
+ keys[#keys+1] = var.key
+ var = var.parent
+ until not var
+ return nil
+end
+
+local function parseResult(result)
+ local positions = {}
+ local tp = result.type
+ local key = result.info.source[1]
+ if tp == 'var' then
+ local var = result.var
+ for _, info in ipairs(var) do
+ if info.source[1] == key then
+ positions[#positions+1] = {info.source.start, info.source.finish}
+ end
+ end
+ local metavar = tryMeta(var)
+ if metavar then
+ for _, info in ipairs(metavar) do
+ if info.source[1] == key then
+ positions[#positions+1] = {info.source.start, info.source.finish}
+ end
+ end
+ end
+ elseif tp == 'label' then
+ local label = result.label
+ for _, info in ipairs(label) do
+ if info.source[1] == key then
+ positions[#positions+1] = {info.source.start, info.source.finish}
+ end
+ end
+ else
+ error('Unknow result type:' .. result.type)
+ end
+ return positions
+end
+
+return function (results, pos)
+ local result = findResult(results, pos)
+ if not result then
+ return nil
+ end
+ local positions = parseResult(result)
+ return positions
+end
diff --git a/server/src/method/textDocument/rename.lua b/server/src/method/textDocument/rename.lua
index ac1e8fc6..1340226b 100644
--- a/server/src/method/textDocument/rename.lua
+++ b/server/src/method/textDocument/rename.lua
@@ -10,7 +10,7 @@ return function (lsp, params)
end
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1, 'utf8')
- local positions = matcher.references(results, position, true)
+ local positions = matcher.rename(results, position)
if not positions then
return {}
end