summaryrefslogtreecommitdiff
path: root/server/src/core/rename.lua
blob: 98257ddb5291a9c19117189e9bce1d4c788807be (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local findSource = require 'core.find_source'
local parser = require 'parser'

local function parseResult(source, newName)
    local positions = {}
    if source:bindLabel() then
        if not parser.grammar(newName, 'Name') then
            return nil
        end
        source:bindLabel():eachInfo(function (info, src)
            positions[#positions+1] = { src.start, src.finish }
        end)
        return positions
    end
    if source:bindLocal() then
        local loc = source:bindLocal()
        if loc:get 'hide' then
            return nil
        end
        if source:get 'in 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 = {}
        loc:eachInfo(function (info, src)
            if not mark[src] then
                mark[src] = info
                positions[#positions+1] = { src.start, src.finish }
            end
        end)
        return positions
    end
    if source:bindValue() and source:get 'parent' then
        if source:get 'in 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 parent = source:get 'parent'
        local mark = {}
        parent:eachInfo(function (info, src)
            if not mark[src] then
                mark[src] = info
                if info.type == 'get child' or info.type == 'set child' then
                    if info[1] == source[1] then
                        positions[#positions+1] = {src.start, src.finish}
                    end
                end
            end
        end)
        return positions
    end
    return nil
end

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