1use std::collections::HashMap;
2
3use brink_ir::suppressions::{Suppressions, parse_suppressions};
4use brink_ir::{
5 Diagnostic, DiagnosticCode, FileId, HirFile, SymbolManifest, lower, lower_single_knot,
6 lower_top_level,
7};
8use brink_syntax::ast::AstNode as _;
9use brink_syntax::{Parse, parse_with_cache};
10use rowan::{GreenNode, NodeCache};
11use tracing::debug;
12
13use crate::file_state::{FileState, TopLevelEntry};
14use crate::include_graph::IncludeGraph;
15use crate::knot_cache::KnotEntry;
16
17pub struct ProjectDb {
23 files: HashMap<FileId, FileState>,
24 path_to_id: HashMap<String, FileId>,
25 id_to_path: HashMap<FileId, String>,
26 next_id: u32,
27 include_graph: IncludeGraph,
28 node_cache: NodeCache,
29}
30
31impl ProjectDb {
32 pub fn new() -> Self {
34 Self {
35 files: HashMap::new(),
36 path_to_id: HashMap::new(),
37 id_to_path: HashMap::new(),
38 next_id: 0,
39 include_graph: IncludeGraph::new(),
40 node_cache: NodeCache::default(),
41 }
42 }
43
44 pub fn set_file(&mut self, path: &str, source: String) -> FileId {
46 let file_id = self.get_or_create_id(path);
47
48 let parse = parse_with_cache(&source, &mut self.node_cache);
49 let tree = parse.tree();
50
51 let knot_entries: Vec<KnotEntry> = tree
53 .knots()
54 .map(|knot_ast| {
55 let green = knot_ast.syntax().green().into();
56 let offset = knot_ast.syntax().text_range().start();
57 let (knot, manifest, diagnostics) = lower_single_knot(file_id, &knot_ast);
58 KnotEntry {
59 green,
60 offset,
61 knot,
62 manifest,
63 diagnostics,
64 }
65 })
66 .collect();
67
68 let top_level = Self::lower_top_level_entry(file_id, &tree);
70
71 let (hir, manifest, mut diagnostics) =
73 Self::assemble(file_id, &knot_entries, &top_level, &tree);
74 diagnostics.extend(Self::syntax_diagnostics(file_id, &parse));
76
77 let suppressions = parse_suppressions(&source);
78
79 let state = FileState {
80 source,
81 parse,
82 knot_entries,
83 top_level,
84 hir,
85 manifest,
86 diagnostics,
87 suppressions,
88 };
89
90 let include_ids: Vec<FileId> = state
92 .hir
93 .includes
94 .iter()
95 .filter_map(|inc| {
96 let resolved = resolve_include_path(path, &inc.file_path);
97 self.path_to_id.get(&resolved).copied()
98 })
99 .collect();
100 self.include_graph.update(file_id, include_ids);
101
102 self.files.insert(file_id, state);
103
104 debug!(path, id = file_id.0, "set_file complete");
105 file_id
106 }
107
108 pub fn update_file(&mut self, path: &str, source: String) -> FileId {
111 let file_id = self.get_or_create_id(path);
112
113 if !self.files.contains_key(&file_id) {
115 return self.set_file(path, source);
116 }
117
118 let parse = parse_with_cache(&source, &mut self.node_cache);
119 let tree = parse.tree();
120
121 let top_level = Self::lower_top_level_entry(file_id, &tree);
123
124 let new_knot_asts: Vec<_> = tree.knots().collect();
126 let old_state = self.files.get(&file_id);
127
128 let mut knot_entries = Vec::with_capacity(new_knot_asts.len());
129 let mut reused = 0u32;
130
131 for (i, knot_ast) in new_knot_asts.iter().enumerate() {
132 let new_green: GreenNode = knot_ast.syntax().green().into();
133
134 let new_offset = knot_ast.syntax().text_range().start();
135 let reuse_entry = old_state
136 .and_then(|s| s.knot_entries.get(i))
137 .filter(|old| old.green == new_green && old.offset == new_offset);
138
139 if let Some(old_entry) = reuse_entry {
140 knot_entries.push(KnotEntry {
141 green: new_green,
142 offset: new_offset,
143 knot: old_entry.knot.clone(),
144 manifest: old_entry.manifest.clone(),
145 diagnostics: old_entry.diagnostics.clone(),
146 });
147 reused += 1;
148 } else {
149 let (knot, manifest, diagnostics) = lower_single_knot(file_id, knot_ast);
150 knot_entries.push(KnotEntry {
151 green: new_green,
152 offset: new_offset,
153 knot,
154 manifest,
155 diagnostics,
156 });
157 }
158 }
159
160 debug!(
161 path,
162 total = new_knot_asts.len(),
163 reused,
164 "knot diff complete"
165 );
166
167 let (hir, manifest, mut diagnostics) =
168 Self::assemble(file_id, &knot_entries, &top_level, &tree);
169 diagnostics.extend(Self::syntax_diagnostics(file_id, &parse));
170
171 let suppressions = parse_suppressions(&source);
172
173 let state = FileState {
174 source,
175 parse,
176 knot_entries,
177 top_level,
178 hir,
179 manifest,
180 diagnostics,
181 suppressions,
182 };
183
184 let include_ids: Vec<FileId> = state
186 .hir
187 .includes
188 .iter()
189 .filter_map(|inc| {
190 let resolved = resolve_include_path(path, &inc.file_path);
191 self.path_to_id.get(&resolved).copied()
192 })
193 .collect();
194 self.include_graph.update(file_id, include_ids);
195
196 self.files.insert(file_id, state);
197
198 file_id
199 }
200
201 pub fn remove_file(&mut self, path: &str) {
203 if let Some(id) = self.path_to_id.remove(path) {
204 self.id_to_path.remove(&id);
205 self.files.remove(&id);
206 self.include_graph.remove(id);
207 }
208 }
209
210 pub fn file_id(&self, path: &str) -> Option<FileId> {
212 self.path_to_id.get(path).copied()
213 }
214
215 pub fn file_path(&self, id: FileId) -> Option<&str> {
217 self.id_to_path.get(&id).map(String::as_str)
218 }
219
220 pub fn file_ids(&self) -> impl Iterator<Item = FileId> + '_ {
222 let mut ids: Vec<_> = self.files.keys().copied().collect();
223 ids.sort_by_key(|id| id.0);
224 ids.into_iter()
225 }
226
227 pub fn file_ids_topo(&self, entry: FileId) -> Vec<FileId> {
230 let all: Vec<_> = self.files.keys().copied().collect();
231 self.include_graph.topological_order(entry, &all)
232 }
233
234 pub fn parse(&self, id: FileId) -> Option<&Parse> {
236 self.files.get(&id).map(|s| &s.parse)
237 }
238
239 pub fn hir(&self, id: FileId) -> Option<&HirFile> {
241 self.files.get(&id).map(|s| &s.hir)
242 }
243
244 pub fn manifest(&self, id: FileId) -> Option<&SymbolManifest> {
246 self.files.get(&id).map(|s| &s.manifest)
247 }
248
249 pub fn source(&self, id: FileId) -> Option<&str> {
251 self.files.get(&id).map(|s| s.source.as_str())
252 }
253
254 pub fn file_diagnostics(&self, id: FileId) -> Option<&[Diagnostic]> {
256 self.files.get(&id).map(|s| s.diagnostics.as_slice())
257 }
258
259 pub fn suppressions(&self, id: FileId) -> Option<&Suppressions> {
261 self.files.get(&id).map(|s| &s.suppressions)
262 }
263
264 pub fn rebuild_include_graph(&mut self) {
270 let file_list: Vec<(FileId, String)> = self
271 .files
272 .keys()
273 .filter_map(|&id| self.id_to_path.get(&id).map(|p| (id, p.clone())))
274 .collect();
275
276 for (file_id, file_path) in &file_list {
277 if let Some(state) = self.files.get(file_id) {
278 let include_ids: Vec<FileId> = state
279 .hir
280 .includes
281 .iter()
282 .filter_map(|inc| {
283 let resolved = resolve_include_path(file_path, &inc.file_path);
284 self.path_to_id.get(&resolved).copied()
285 })
286 .collect();
287 self.include_graph.update(*file_id, include_ids);
288 }
289 }
290 }
291
292 pub fn find_cycle(&self) -> Option<Vec<FileId>> {
296 self.include_graph.find_cycle()
297 }
298
299 pub fn compute_projects(&self) -> Vec<(FileId, Vec<FileId>)> {
303 let all: Vec<_> = self.files.keys().copied().collect();
304 self.include_graph.compute_projects(&all)
305 }
306
307 pub fn analysis_inputs_for(
311 &self,
312 file_ids: &[FileId],
313 ) -> Vec<(FileId, HirFile, SymbolManifest)> {
314 let mut inputs: Vec<_> = file_ids
315 .iter()
316 .filter_map(|&id| {
317 let state = self.files.get(&id)?;
318 Some((id, state.hir.clone(), state.manifest.clone()))
319 })
320 .collect();
321 inputs.sort_by_key(|(id, _, _)| id.0);
322 inputs
323 }
324
325 pub fn analysis_inputs(&self) -> Vec<(FileId, HirFile, SymbolManifest)> {
330 let mut inputs: Vec<_> = self
331 .files
332 .iter()
333 .map(|(&id, state)| (id, state.hir.clone(), state.manifest.clone()))
334 .collect();
335 inputs.sort_by_key(|(id, _, _)| id.0);
336 inputs
337 }
338
339 pub fn file_metadata(&self) -> Vec<(FileId, String, String)> {
343 let mut meta: Vec<_> = self
344 .files
345 .keys()
346 .filter_map(|&id| {
347 let path = self.id_to_path.get(&id)?.clone();
348 let source = self.files.get(&id)?.source.clone();
349 Some((id, path, source))
350 })
351 .collect();
352 meta.sort_by_key(|(id, _, _)| id.0);
353 meta
354 }
355
356 fn get_or_create_id(&mut self, path: &str) -> FileId {
359 if let Some(&id) = self.path_to_id.get(path) {
360 return id;
361 }
362 let id = FileId(self.next_id);
363 self.next_id += 1;
364 self.path_to_id.insert(path.to_string(), id);
365 self.id_to_path.insert(id, path.to_string());
366 id
367 }
368
369 fn lower_top_level_entry(
370 file_id: FileId,
371 tree: &brink_syntax::ast::SourceFile,
372 ) -> TopLevelEntry {
373 let green_children = Self::collect_top_level_green(tree);
374 let (root_content, top_level_knots, manifest, diagnostics) = lower_top_level(file_id, tree);
375 TopLevelEntry {
376 green_children,
377 root_content,
378 top_level_knots,
379 manifest,
380 diagnostics,
381 }
382 }
383
384 fn collect_top_level_green(tree: &brink_syntax::ast::SourceFile) -> Vec<GreenNode> {
386 use brink_syntax::SyntaxKind;
387
388 tree.syntax()
389 .children()
390 .filter(|child| child.kind() != SyntaxKind::KNOT_DEF)
391 .map(|child| child.green().into())
392 .collect()
393 }
394
395 fn assemble(
397 file_id: FileId,
398 knot_entries: &[KnotEntry],
399 top_level: &TopLevelEntry,
400 tree: &brink_syntax::ast::SourceFile,
401 ) -> (HirFile, SymbolManifest, Vec<Diagnostic>) {
402 let (mut full_hir, _full_manifest, _full_diag) = lower(file_id, tree);
411
412 full_hir.knots = knot_entries.iter().filter_map(|e| e.knot.clone()).collect();
415 full_hir.knots.extend(top_level.top_level_knots.clone());
416 full_hir.root_content = top_level.root_content.clone();
417
418 let mut manifest = top_level.manifest.clone();
420 for entry in knot_entries {
421 merge_manifest_into(&mut manifest, &entry.manifest);
422 }
423
424 let mut diagnostics = top_level.diagnostics.clone();
426 for entry in knot_entries {
427 diagnostics.extend(entry.diagnostics.iter().cloned());
428 }
429
430 (full_hir, manifest, diagnostics)
431 }
432
433 fn syntax_diagnostics(file_id: FileId, parse: &Parse) -> Vec<Diagnostic> {
436 parse
437 .errors()
438 .iter()
439 .map(|e| Diagnostic {
440 file: file_id,
441 range: e.range,
442 message: e.message.clone(),
443 code: DiagnosticCode::E037,
444 })
445 .collect()
446 }
447}
448
449impl Default for ProjectDb {
450 fn default() -> Self {
451 Self::new()
452 }
453}
454
455fn merge_manifest_into(dst: &mut SymbolManifest, src: &SymbolManifest) {
457 dst.knots.extend(src.knots.iter().cloned());
458 dst.stitches.extend(src.stitches.iter().cloned());
459 dst.variables.extend(src.variables.iter().cloned());
460 dst.constants.extend(src.constants.iter().cloned());
461 dst.lists.extend(src.lists.iter().cloned());
462 dst.externals.extend(src.externals.iter().cloned());
463 dst.labels.extend(src.labels.iter().cloned());
464 dst.list_items.extend(src.list_items.iter().cloned());
465 dst.locals.extend(src.locals.iter().cloned());
466 dst.unresolved.extend(src.unresolved.iter().cloned());
467 dst.docs
468 .extend(src.docs.iter().map(|(k, v)| (k.clone(), v.clone())));
469}
470
471pub fn resolve_include_path(from_file: &str, include_path: &str) -> String {
480 let joined = match from_file.rfind('/') {
481 Some(i) => format!("{}/{include_path}", &from_file[..i]),
482 None => include_path.to_string(),
483 };
484 normalize_path(&joined)
485}
486
487fn normalize_path(path: &str) -> String {
493 let absolute = path.starts_with('/');
494 let mut out: Vec<&str> = Vec::new();
495 for seg in path.split('/') {
496 match seg {
497 "" | "." => {}
498 ".." if matches!(out.last(), Some(&s) if s != "..") => {
499 out.pop();
500 }
501 s => out.push(s),
502 }
503 }
504 let joined = out.join("/");
505 if absolute {
506 format!("/{joined}")
507 } else {
508 joined
509 }
510}
511
512pub fn compute_relative_path(from_file: &str, to_file: &str) -> String {
519 let mut from_dirs: Vec<&str> = from_file.split('/').collect();
520 from_dirs.pop(); let to_all: Vec<&str> = to_file.split('/').collect();
522 let Some((to_name, to_dirs)) = to_all.split_last() else {
523 return to_file.to_owned();
524 };
525
526 let mut k = 0;
528 while k < from_dirs.len() && k < to_dirs.len() && from_dirs[k] == to_dirs[k] {
529 k += 1;
530 }
531
532 let mut parts: Vec<&str> = Vec::new();
533 parts.extend(std::iter::repeat_n("..", from_dirs.len() - k));
534 parts.extend_from_slice(&to_dirs[k..]);
535 parts.push(to_name);
536 parts.join("/")
537}
538
539#[cfg(test)]
540mod path_tests {
541 use super::{compute_relative_path, resolve_include_path};
542
543 #[test]
544 fn resolve_forward_includes() {
545 assert_eq!(
546 resolve_include_path("src/main.ink", "utils.ink"),
547 "src/utils.ink"
548 );
549 assert_eq!(resolve_include_path("story.ink", "other.ink"), "other.ink");
550 assert_eq!(resolve_include_path("a/b/c.ink", "d/e.ink"), "a/b/d/e.ink");
551 }
552
553 #[test]
554 fn resolve_normalizes_dot_and_dotdot() {
555 assert_eq!(resolve_include_path("a/b/c.ink", "../d.ink"), "a/d.ink");
556 assert_eq!(resolve_include_path("a/b/c.ink", "./d.ink"), "a/b/d.ink");
557 assert_eq!(resolve_include_path("a/b/c.ink", "../../d.ink"), "d.ink");
558 assert_eq!(
559 resolve_include_path("a/b/c.ink", "../x/../d.ink"),
560 "a/d.ink"
561 );
562 }
563
564 #[test]
565 fn compute_relative_is_inverse_of_resolve() {
566 let cases = [
568 ("main.ink", "scenes/intro.ink"), ("a/b/c.ink", "a/d.ink"), ("a/b/c.ink", "a/b/renamed.ink"), ("scenes/intro.ink", "lib.ink"), ("a/b/c.ink", "x/y/z.ink"), ("main.ink", "other.ink"), ];
575 for (from, to) in cases {
576 let rel = compute_relative_path(from, to);
577 assert_eq!(
578 resolve_include_path(from, &rel),
579 to,
580 "round-trip failed for from={from} to={to} rel={rel}",
581 );
582 }
583 }
584
585 #[test]
586 fn resolve_preserves_absolute_paths() {
587 assert_eq!(
590 resolve_include_path("/proj/tier3/main.ink", "included.ink"),
591 "/proj/tier3/included.ink",
592 );
593 assert_eq!(
594 resolve_include_path("/proj/a/b/c.ink", "../d.ink"),
595 "/proj/a/d.ink"
596 );
597 }
598
599 #[test]
600 fn compute_relative_rename_in_place_is_bare_name() {
601 assert_eq!(
602 compute_relative_path("a/b/c.ink", "a/b/renamed.ink"),
603 "renamed.ink"
604 );
605 assert_eq!(
606 compute_relative_path("main.ink", "renamed.ink"),
607 "renamed.ink"
608 );
609 }
610}