summaryrefslogtreecommitdiff
path: root/script/method/workspace/symbol.lua
blob: 70755ee06a6a901f5546bc6d08e1f549ce787856 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
local matchKey = require 'core.matchKey'

local SymbolKind = {
    File = 1,
    Module = 2,
    Namespace = 3,
    Package = 4,
    Class = 5,
    Method = 6,
    Property = 7,
    Field = 8,
    Constructor = 9,
    Enum = 10,
    Interface = 11,
    Function = 12,
    Variable = 13,
    Constant = 14,
    String = 15,
    Number = 16,
    Boolean = 17,
    Array = 18,
    Object = 19,
    Key = 20,
    Null = 21,
    EnumMember = 22,
    Struct = 23,
    Event = 24,
    Operator = 25,
    TypeParameter = 26,
}

local function convertRange(lines, range)
    local start_row,  start_col  = lines:rowcol(range.start)
    local finish_row, finish_col = lines:rowcol(range.finish)
    local result = {
        start = {
            line = start_row - 1,
            character = start_col - 1,
        },
        ['end'] = {
            line = finish_row - 1,
            -- 这里不用-1,因为前端期待的是匹配完成后的位置
            character = finish_col,
        },
    }
    return result
end

local function getParentName(source)
    local parentValue = source:get 'parent'
    if not parentValue then
        return
    end
    local pSource = parentValue:getSource()
    if not pSource then
        return
    end
    local name = pSource[1]
    if type(name) ~= 'string' or name == '' then
        return
    end
    return name
end

local function collect(results, source, uri, lines)
    if source:action() ~= 'set' then
        return
    end
    local kind = SymbolKind.Variable
    local contain = getParentName(source)
    local value = source:bindValue()
    if value and value:getFunction() then
        kind = SymbolKind.Function
    else
        if source:get 'global' then
            kind = SymbolKind.Namespace
        elseif source:get 'table index' then
            kind = SymbolKind.EnumMember
        end
    end
    results[#results+1] = {
        name = source[1],
        kind = kind,
        containerName = contain,
        location = {
            uri = uri,
            range = convertRange(lines, source),
        }
    }
end

local function searchVM(lsp, results, query, uri)
    local vm, lines = lsp:getVM(uri)
    if not vm then
        return
    end
    vm:eachSource(function (src)
        if src.type == 'name' then
            if src[1] == '' then
                return
            end
            if matchKey(query, src[1]) then
                collect(results, src, uri, lines)
            end
        end
    end)
end

--- @param lsp LSP
--- @param params table
return function (lsp, params)
    local query = params.query
    if #query <= 0 then
        return {}
    end
    local results = {}

    for uri in lsp:eachFile() do
        searchVM(lsp, results, query, uri)
    end

    return results
end