-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathmod.rs
More file actions
210 lines (186 loc) · 6.76 KB
/
Copy pathmod.rs
File metadata and controls
210 lines (186 loc) · 6.76 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
mod common;
mod decl;
mod doc;
mod flow;
mod infer_cache_manager;
mod lua;
mod unresolve;
use crate::{
CacheOptions, Emmyrc, FileId, InFiled, InferFailReason, LuaType, LuaTypeDeclId,
db_index::{DbIndex, WorkspaceId},
profile::Profile,
};
use emmylua_parser::{LuaBlock, LuaChunk, LuaDocGenericDeclList, LuaExpr};
use hashbrown::{HashMap, HashSet};
use infer_cache_manager::InferCacheManager;
use std::sync::Arc;
use unresolve::UnResolve;
pub(super) fn analyze_func_body_missing_return_flags_with<F>(
body: LuaBlock,
infer_expr_type: &mut F,
) -> Result<(bool, bool), InferFailReason>
where
F: FnMut(&LuaExpr) -> Result<LuaType, InferFailReason>,
{
lua::func_body::analyze_func_body_missing_return_flags_with(body, infer_expr_type)
}
pub fn analyze(
db: &mut DbIndex,
need_analyzed_files: Vec<InFiled<LuaChunk>>,
config: Arc<Emmyrc>,
cache_options: CacheOptions,
) {
if need_analyzed_files.is_empty() {
return;
}
let contexts = module_analyze(db, need_analyzed_files, config, cache_options);
for (workspace_id, mut context) in contexts {
context.workspace_id = Some(workspace_id);
let profile_log = format!("analyze workspace {}", workspace_id);
let _p = Profile::cond_new(&profile_log, context.tree_list.len() > 1);
run_analysis::<decl::DeclAnalysisPipeline>(db, &mut context);
run_analysis::<doc::DocAnalysisPipeline>(db, &mut context);
run_analysis::<flow::FlowAnalysisPipeline>(db, &mut context);
run_analysis::<lua::LuaAnalysisPipeline>(db, &mut context);
run_analysis::<unresolve::UnResolveAnalysisPipeline>(db, &mut context);
}
}
trait AnalysisPipeline {
fn analyze(db: &mut DbIndex, context: &mut AnalyzeContext);
}
fn run_analysis<T: AnalysisPipeline>(db: &mut DbIndex, context: &mut AnalyzeContext) {
T::analyze(db, context);
}
fn module_analyze(
db: &mut DbIndex,
need_analyzed_files: Vec<InFiled<LuaChunk>>,
config: Arc<Emmyrc>,
cache_options: CacheOptions,
) -> Vec<(WorkspaceId, AnalyzeContext)> {
if need_analyzed_files.len() == 1 {
let in_filed_tree = need_analyzed_files[0].clone();
let file_id = in_filed_tree.file_id;
if let Some(path) = db.get_vfs().get_file_path(&file_id).cloned() {
let path_str = match path.to_str() {
Some(path) => path,
None => {
log::warn!("file_id {:?} path not found", file_id);
return vec![];
}
};
let workspace_id = db
.get_module_index_mut()
.add_module_by_path(file_id, path_str);
let workspace_id = workspace_id.unwrap_or(WorkspaceId::MAIN);
let mut context = AnalyzeContext::new(config, cache_options);
context.add_tree_chunk(in_filed_tree);
return vec![(workspace_id, context)];
} else if db.get_vfs().is_remote_file(&file_id) {
let mut context = AnalyzeContext::new(config, cache_options);
context.add_tree_chunk(in_filed_tree);
return vec![(WorkspaceId::REMOTE, context)];
};
return vec![];
}
let _p = Profile::new("module analyze");
let mut file_tree_map: HashMap<WorkspaceId, Vec<InFiled<LuaChunk>>> = HashMap::new();
for in_filed_tree in need_analyzed_files {
let file_id = in_filed_tree.file_id;
if let Some(path) = db.get_vfs().get_file_path(&file_id).cloned() {
let path_str = match path.to_str() {
Some(path) => path,
None => {
log::warn!("file_id {:?} path not found", file_id);
continue;
}
};
let workspace_id = db
.get_module_index_mut()
.add_module_by_path(file_id, path_str);
let workspace_id = workspace_id.unwrap_or(WorkspaceId::MAIN);
file_tree_map
.entry(workspace_id)
.or_default()
.push(in_filed_tree);
} else if db.get_vfs().is_remote_file(&file_id) {
file_tree_map
.entry(WorkspaceId::REMOTE)
.or_default()
.push(in_filed_tree);
}
}
let mut contexts = Vec::new();
if let Some(std_lib) = file_tree_map.remove(&WorkspaceId::STD) {
let mut context = AnalyzeContext::new(config.clone(), cache_options);
context.tree_list = std_lib;
contexts.push((WorkspaceId::STD, context));
}
let mut main_vec = Vec::new();
for (workspace_id, tree_list) in file_tree_map {
let mut context = AnalyzeContext::new(config.clone(), cache_options);
context.tree_list = tree_list;
if workspace_id.is_library() || workspace_id.is_remote() {
contexts.push((workspace_id, context));
} else {
main_vec.push((workspace_id, context));
}
}
contexts.sort_by_key(|a| a.0);
contexts.extend(main_vec);
contexts
}
#[derive(Debug)]
pub struct AnalyzeContext {
tree_list: Vec<InFiled<LuaChunk>>,
#[allow(unused)]
config: Arc<Emmyrc>,
metas: HashSet<FileId>,
unresolves: Vec<(UnResolve, InferFailReason)>,
infer_manager: InferCacheManager,
pub workspace_id: Option<WorkspaceId>,
pending_type_generic_headers: Vec<PendingTypeGenericHeader>,
}
impl AnalyzeContext {
pub fn new(emmyrc: Arc<Emmyrc>, cache_options: CacheOptions) -> Self {
Self {
tree_list: Vec::new(),
config: emmyrc,
metas: HashSet::new(),
unresolves: Vec::new(),
infer_manager: InferCacheManager::new(cache_options),
workspace_id: None,
pending_type_generic_headers: Vec::new(),
}
}
pub fn add_meta(&mut self, file_id: FileId) {
self.metas.insert(file_id);
}
pub fn add_tree_chunk(&mut self, tree: InFiled<LuaChunk>) {
self.tree_list.push(tree);
}
pub fn add_unresolve(&mut self, un_resolve: UnResolve, reason: InferFailReason) {
self.unresolves.push((un_resolve, reason));
}
pub fn add_pending_type_generic_header(
&mut self,
file_id: FileId,
type_id: LuaTypeDeclId,
generic_decl_list: LuaDocGenericDeclList,
) {
self.pending_type_generic_headers
.push(PendingTypeGenericHeader {
file_id,
type_id,
generic_decl_list,
});
}
pub(super) fn take_pending_type_generic_headers(&mut self) -> Vec<PendingTypeGenericHeader> {
std::mem::take(&mut self.pending_type_generic_headers)
}
}
#[derive(Debug, Clone)]
pub(super) struct PendingTypeGenericHeader {
pub file_id: FileId,
pub type_id: LuaTypeDeclId,
pub generic_decl_list: LuaDocGenericDeclList,
}