aboutsummaryrefslogtreecommitdiff
path: root/src/server/indexer.odin
blob: 581bd3777c766cdc53b41e59598a4b0944b591ff (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
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,
}

lookup :: proc(name: string, pkg: string, loc := #caller_location) -> (Symbol, bool) {
	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
}