glua_code_analysis/db_index/accessor_func/
mod.rs1use std::collections::HashMap;
2
3use glua_parser::LuaSyntaxId;
4use smol_str::SmolStr;
5
6use super::traits::LuaIndex;
7use crate::{FileId, LuaTypeDeclId};
8
9#[derive(Debug, Clone)]
10pub struct AccessorFuncAnnotation {
11 pub name_param_index: usize,
12 pub file_id: FileId,
13}
14
15#[derive(Debug, Default)]
16pub struct AccessorFuncAnnotationIndex {
17 by_name: HashMap<SmolStr, Vec<AccessorFuncAnnotation>>,
18 by_file: HashMap<FileId, Vec<SmolStr>>,
19}
20
21impl AccessorFuncAnnotationIndex {
22 pub fn new() -> Self {
23 Self::default()
24 }
25
26 pub fn register(&mut self, name: SmolStr, annotation: AccessorFuncAnnotation) {
27 let file_id = annotation.file_id;
28 self.by_name
29 .entry(name.clone())
30 .or_default()
31 .push(annotation);
32 self.by_file.entry(file_id).or_default().push(name);
33 }
34
35 pub fn contains_name(&self, name: &str) -> bool {
36 self.by_name.contains_key(name)
37 }
38
39 pub fn get_annotations(&self, name: &str) -> Option<&Vec<AccessorFuncAnnotation>> {
40 self.by_name.get(name)
41 }
42}
43
44impl LuaIndex for AccessorFuncAnnotationIndex {
45 fn remove(&mut self, file_id: FileId) {
46 if let Some(names) = self.by_file.remove(&file_id) {
47 for name in names {
48 if let Some(annotations) = self.by_name.get_mut(&name) {
49 annotations.retain(|annotation| annotation.file_id != file_id);
50 if annotations.is_empty() {
51 self.by_name.remove(&name);
52 }
53 }
54 }
55 }
56 }
57
58 fn clear(&mut self) {
59 self.by_name.clear();
60 self.by_file.clear();
61 }
62}
63
64#[derive(Debug, Clone)]
65pub struct AccessorFuncCallMetadata {
66 pub syntax_id: LuaSyntaxId,
67 pub owner_type_id: LuaTypeDeclId,
68 pub accessor_name: String,
69 pub name_arg_syntax_id: Option<LuaSyntaxId>,
70}
71
72#[derive(Debug, Default)]
73pub struct AccessorFuncCallIndex {
74 calls: HashMap<FileId, Vec<AccessorFuncCallMetadata>>,
75}
76
77impl AccessorFuncCallIndex {
78 pub fn new() -> Self {
79 Self::default()
80 }
81
82 pub fn add_call(&mut self, file_id: FileId, metadata: AccessorFuncCallMetadata) {
83 self.calls.entry(file_id).or_default().push(metadata);
84 }
85
86 pub fn iter(&self) -> impl Iterator<Item = (&FileId, &Vec<AccessorFuncCallMetadata>)> {
87 self.calls.iter()
88 }
89}
90
91impl LuaIndex for AccessorFuncCallIndex {
92 fn remove(&mut self, file_id: FileId) {
93 self.calls.remove(&file_id);
94 }
95
96 fn clear(&mut self) {
97 self.calls.clear();
98 }
99}