summaryrefslogtreecommitdiff
path: root/server/src/core/rename.lua
blob: f42da08b8b0de7fb637dd621a1a5c693e17b14e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local findResult = require 'core.find_result'
local parser = require 'parser'

local function parseResult(result, source, newName)
    local positions = {}
    local tp = result.type
    if tp == 'local' or tp == 'field' then
        local key = source[1]
        if result.hide then
            return positions
        end
        if source.index then
            if not parser.grammar(newName, 'Exp') then
                return positions
            end
        else
            if not parser.grammar(newName, 'Name') then
                return positions
            end
        end
        local mark = {}
        for _, info in ipairs(result) do
            if not mark[info.source] then
                mark[info.source] = info
                if info.source[1] == key then
                    positions[#positions+1] = {info.source.start, info.source.finish}
                end
            end
        end
    elseif tp == 'label' then
        if not parser.grammar(newName, 'Name') then
            return positions
        end
        local label = result.label
        for _, info in ipairs(label) do
            positions[#positions+1] = {info.source.start, info.source.finish}
        end
    else
        error('Unknow result type:' .. result.type)
    end
    return positions
end

return function (vm, pos, newName)
    local result, source = findResult(vm, pos)
    if not result then
        return nil
    end
    local positions = parseResult(result, source, newName)
    return positions
end