emmylua_code_analysis/compilation/
mod.rs1mod analyzer;
2mod test;
3
4use std::sync::Arc;
5
6use crate::{
7 db_index::DbIndex, semantic::SemanticModel, Emmyrc, FileId, InFiled, LuaIndex, LuaInferCache,
8};
9
10#[derive(Debug)]
11pub struct LuaCompilation {
12 db: DbIndex,
13 emmyrc: Arc<Emmyrc>,
14}
15
16impl LuaCompilation {
17 pub fn new(emmyrc: Arc<Emmyrc>) -> Self {
18 let mut compilation = Self {
19 db: DbIndex::new(),
20 emmyrc: emmyrc.clone(),
21 };
22
23 compilation.db.update_config(emmyrc.clone());
24 compilation
25 }
26
27 pub fn get_semantic_model(&self, file_id: FileId) -> Option<SemanticModel> {
28 let cache = LuaInferCache::new(file_id, Default::default());
29 let tree = self.db.get_vfs().get_syntax_tree(&file_id)?;
30 Some(SemanticModel::new(
31 file_id,
32 &self.db,
33 cache,
34 self.emmyrc.clone(),
35 tree.get_chunk_node(),
36 ))
37 }
38
39 pub fn update_index(&mut self, file_ids: Vec<FileId>) {
40 let mut need_analyzed_files = vec![];
41 for file_id in file_ids {
42 let tree = match self.db.get_vfs().get_syntax_tree(&file_id) {
43 Some(tree) => tree,
44 None => {
45 log::warn!("file_id {:?} not found in vfs", file_id);
46 continue;
47 }
48 };
49 need_analyzed_files.push(InFiled {
50 file_id,
51 value: tree.get_chunk_node(),
52 });
53 }
54
55 analyzer::analyze(&mut self.db, need_analyzed_files, self.emmyrc.clone());
56 }
57
58 pub fn remove_index(&mut self, file_ids: Vec<FileId>) {
59 self.db.remove_index(file_ids);
60 }
61
62 pub fn clear_index(&mut self) {
63 self.db.clear();
64 }
65
66 pub fn get_db(&self) -> &DbIndex {
67 &self.db
68 }
69
70 pub fn get_db_mut(&mut self) -> &mut DbIndex {
71 &mut self.db
72 }
73
74 pub fn update_config(&mut self, config: Arc<Emmyrc>) {
75 self.emmyrc = config.clone();
76 self.db.update_config(config);
77 }
78}