summaryrefslogtreecommitdiff
path: root/server-beta/src/core/rename.lua
blob: a2b1b3fc9fa64fb53e2b22bf86166c7a6e59bd20 (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
73
74
75
76
77
78
79
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