aboutsummaryrefslogtreecommitdiff
path: root/src/server/indexer.odin
blob: 791f06d63083b099ecd52fa9264db40431054750 (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
package server

import "core:odin/ast"
import "core:fmt"
import "core:strings"
import "core:log"
import "core:slice"


Indexer :: struct {
	builtin_packages: [dynamic]string,
	index:            MemoryIndex,
}

indexer: Indexer

FuzzyResult :: struct {
	symbol: Symbol,
	score:  f32,
}

clear_index_cache :: proc() {
	memory_index_clear_cache(&indexer.index)
}

lookup :: proc(
	name: string,
	pkg: string,
	loc := #caller_location,
) -> (
	Symbol,
	bool,
) {
	if name == "" {
		return {}, false
	}

	if symbol, ok := memory_index_lookup(&indexer.index, name, pkg); ok {
		return symbol, true
	}

	return {}, false
}

fuzzy_search :: proc(name: string, pkgs: []string) -> ([]FuzzyResult, bool) {
	results, ok := memory_index_fuzzy_search(&indexer.index, name, pkgs)
	result := make([dynamic]FuzzyResult, context.temp_allocator)

	if !ok {
		return {}, false
	}

	for r in results {
		append(&result, r)
	}

	slice.sort_by(result[:], proc(i, j: FuzzyResult) -> bool {
		return j.score < i.score
	})

	return result[:], true
}