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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package server
import "core:fmt"
import "core:slice"
import "core:strings"
import "src:common"
MemoryIndex :: struct {
collection: SymbolCollection,
last_package_name: string,
last_package: ^map[string]Symbol,
}
make_memory_index :: proc(collection: SymbolCollection) -> MemoryIndex {
return MemoryIndex{collection = collection}
}
memory_index_clear_cache :: proc(index: ^MemoryIndex) {
index.last_package_name = ""
index.last_package = nil
}
memory_index_lookup :: proc(index: ^MemoryIndex, name: string, pkg: string) -> (Symbol, bool) {
if index.last_package_name == pkg && index.last_package != nil {
return index.last_package[name]
}
if _pkg, ok := &index.collection.packages[pkg]; ok {
index.last_package = &_pkg.symbols
index.last_package_name = pkg
return _pkg.symbols[name]
} else {
index.last_package = nil
index.last_package_name = ""
}
return {}, false
}
score_name :: proc(matchers: []^common.FuzzyMatcher, name: string) -> (f32, bool) {
score := f32(1)
for matcher in matchers {
s, ok := common.fuzzy_match(matcher, name)
if ok != 1 {
return 0, false
}
score *= s
}
return score, true
}
memory_index_fuzzy_search :: proc(
index: ^MemoryIndex,
name: string,
pkgs: []string,
current_file: string,
resolve_fields := false,
limit := 0,
) -> (
[]FuzzyResult,
bool,
) {
symbols := make([dynamic]FuzzyResult, 0, context.temp_allocator)
fields := strings.fields(name, context.temp_allocator)
matchers := make([dynamic]^common.FuzzyMatcher, 0, len(fields), context.temp_allocator)
for field in fields {
append(&matchers, common.make_fuzzy_matcher(field))
}
top := 100
current_pkg := get_package_from_filepath(current_file)
for pkg in pkgs {
if pkg, ok := index.collection.packages[pkg]; ok {
for _, symbol in pkg.symbols {
if should_skip_private_symbol(symbol, current_pkg, current_file) {
continue
}
if resolve_fields {
// TODO: this only does the top level fields, we may want to travers all the way down in the future
#partial switch v in symbol.value {
case SymbolStructValue:
for name, i in v.names {
full_name := fmt.tprintf("%s.%s", symbol.name, name)
if score, ok := score_name(matchers[:], full_name); ok {
s := symbol
construct_struct_field_symbol(&s, symbol.name, v, i)
s.name = full_name
result := FuzzyResult {
symbol = s,
score = score,
}
append(&symbols, result)
}
}
case SymbolBitFieldValue:
for name, i in v.names {
full_name := fmt.tprintf("%s.%s", symbol.name, name)
if score, ok := score_name(matchers[:], full_name); ok {
s := symbol
construct_bit_field_field_symbol(&s, symbol.name, v, i)
s.name = full_name
result := FuzzyResult {
symbol = s,
score = score,
}
append(&symbols, result)
}
}
case SymbolGenericValue:
for name, i in v.field_names {
full_name := fmt.tprintf("%s.%s", symbol.name, name)
if score, ok := score_name(matchers[:], full_name); ok {
s := symbol
s.name = full_name
s.type = .Field
s.range = v.ranges[i]
result := FuzzyResult {
symbol = s,
score = score,
}
append(&symbols, result)
}
}
}
}
if score, ok := score_name(matchers[:], symbol.name); ok {
result := FuzzyResult {
symbol = symbol,
score = score,
}
append(&symbols, result)
}
}
}
}
slice.sort_by(symbols[:], proc(i, j: FuzzyResult) -> bool {
return j.score < i.score
})
if limit > 0 {
return symbols[:min(limit, len(symbols))], true
} else if name == "" {
return symbols[:], true
} else {
return symbols[:min(top, len(symbols))], true
}
}
|