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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
package server
import "shared:common"
import "core:strings"
import "core:odin/ast"
import "core:odin/parser"
import path "core:path/slashpath"
import "core:log"
import "core:path/filepath"
import "core:fmt"
import "core:os"
import "core:mem"
import "core:runtime"
fullpaths: [dynamic]string
walk_directories :: proc(info: os.File_Info, in_err: os.Errno) -> (err: os.Errno, skip_dir: bool) {
if info.is_dir {
return 0, false
}
if info.fullpath == "" {
return 0, false
}
if strings.contains(info.name, ".odin") {
append(&fullpaths, strings.clone(info.fullpath, runtime.default_allocator()))
}
return 0, false
}
resolve_references :: proc(ast_context: ^AstContext, position_context: ^DocumentPositionContext) -> ([]common.Location, bool) {
locations := make([dynamic]common.Location, 0, context.allocator)
fullpaths = make([dynamic]string, 10, context.allocator)
resolve_flag: ResolveReferenceFlag
reference := ""
symbol: Symbol
ok: bool
pkg := ""
if position_context.selector != nil {
} else if position_context.call != nil {
} else if position_context.identifier != nil {
resolve_flag = .Identifier
ident := position_context.identifier.derived.(^ast.Ident)
reference = ident.name
symbol, ok = resolve_location_identifier(ast_context, ident^)
location := common.Location {
range = common.get_token_range(position_context.identifier^, string(ast_context.file.src)),
uri = strings.clone(symbol.uri, runtime.default_allocator()),
}
append(&locations, location)
}
if !ok {
return {}, false
}
symbol_uri := strings.clone(symbol.uri, context.allocator)
symbol_pkg := strings.clone(symbol.pkg, context.allocator)
symbol_range := symbol.range
temp_arena: mem.Arena
mem.init_arena(&temp_arena, make([]byte, mem.Megabyte*25, runtime.default_allocator()))
context.allocator = mem.arena_allocator(&temp_arena)
{
context.temp_allocator = context.allocator
filepath.walk(filepath.dir(os.args[0], context.temp_allocator), walk_directories)
}
for fullpath in fullpaths {
data, ok := os.read_entire_file(fullpath, context.allocator)
if !ok {
log.errorf("failed to read entire file for indexing %v", fullpath)
continue
}
p := parser.Parser {
err = log_error_handler,
warn = log_warning_handler,
flags = {.Optional_Semicolons},
}
dir := filepath.base(filepath.dir(fullpath, context.allocator))
pkg := new(ast.Package)
pkg.kind = .Normal
pkg.fullpath = fullpath
pkg.name = dir
if dir == "runtime" {
pkg.kind = .Runtime
}
file := ast.File {
fullpath = fullpath,
src = string(data),
pkg = pkg,
}
ok = parser.parse_file(&p, &file)
if !ok {
log.errorf("error in parse file for indexing %v", fullpath)
continue
}
uri := common.create_uri(fullpath, context.allocator)
document := Document {
ast = file,
}
document.uri = uri
document.text = transmute([]u8)file.src
document.used_text = len(file.src)
document_setup(&document)
parse_imports(&document, &common.config)
in_pkg := false
for pkg in document.imports {
if pkg.name == symbol_pkg || symbol.pkg == ast_context.document_package {
in_pkg = true
}
}
if in_pkg {
symbols_and_nodes := resolve_entire_file(&document, reference, resolve_flag, context.allocator)
for k, v in symbols_and_nodes {
if v.symbol.uri == symbol_uri && v.symbol.range == symbol_range {
location := common.Location {
range = common.get_token_range(v.node^, string(document.text)),
uri = strings.clone(v.symbol.uri, runtime.default_allocator()),
}
append(&locations, location)
}
}
}
delete(fullpath)
free_all(context.allocator)
}
delete(fullpaths)
delete(temp_arena.data)
delete(symbol_uri)
return locations[:], true
}
get_references :: proc(document: ^Document, position: common.Position) -> ([]common.Location, bool) {
ast_context := make_ast_context(document.ast, document.imports, document.package_name, document.uri.uri, document.fullpath)
position_context, ok := get_document_position_context(document, position, .Hover)
get_globals(document.ast, &ast_context)
ast_context.current_package = ast_context.document_package
if position_context.function != nil {
get_locals(document.ast, position_context.function, &ast_context, &position_context)
}
return resolve_references(&ast_context, &position_context)
}
|