Skip to content

Commit 5eaffcf

Browse files
mmahroussfda-odoo
authored andcommitted
[FIX] fix get_symbols refactoring
1 parent 43c23f6 commit 5eaffcf

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

server/src/features/ast_utils.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,29 @@ pub struct AstUtils {}
1919

2020
impl AstUtils {
2121

22-
pub fn get_expr<'a>(file_info_ast: &'a FileInfoAst, offset: u32) -> (Option<ExprOrIdent<'a>>, Option<ExprCall>) {
22+
23+
pub fn get_symbols<'a>(session: &mut SessionInfo, file_info_ast: &'a FileInfoAst, file_symbol: &Rc<RefCell<Symbol>>, offset: u32) -> (AnalyzeAstResult, Option<TextRange>, Option<ExprOrIdent<'a>>, Option<ExprCall>) {
2324
let mut expr: Option<ExprOrIdent<'a>> = None;
2425
let mut call_expr: Option<ExprCall> = None;
2526
for stmt in file_info_ast.get_stmts().unwrap().iter() {
2627
//we have to handle imports differently as symbols are not visible in file.
27-
if let Some(test_import) = Self::get_symbol_in_import(session, file_symbol, offset, stmt) {
28-
return test_import;
28+
if let Some((result, range)) = Self::get_symbol_in_import(session, file_symbol, offset, stmt) {
29+
return (result, range, None, None);
2930
}
3031
(expr, call_expr) = ExprFinderVisitor::find_expr_at(stmt, offset);
3132
if expr.is_some() {
3233
break;
3334
}
3435
}
35-
if expr.is_none() {
36+
let Some(expr) = expr else {
3637
warn!("expr not found");
37-
}
38-
(expr, call_expr)
38+
return (AnalyzeAstResult::default(), None, None, None);
39+
};
40+
let (result, range) = Self::get_symbol_from_expr(session, file_symbol, &expr, offset);
41+
(result, range, Some(expr), call_expr)
3942
}
4043

41-
pub fn get_symbols(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, offset: u32, expr: &ExprOrIdent) -> (AnalyzeAstResult, Option<TextRange>) {
44+
pub fn get_symbol_from_expr<'a>(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, expr: &ExprOrIdent<'a>, offset: u32) -> (AnalyzeAstResult, Option<TextRange>) {
4245
let parent_symbol = Symbol::get_scope_symbol(file_symbol.clone(), offset, matches!(expr, ExprOrIdent::Parameter(_)));
4346
AstUtils::build_scope(session, &parent_symbol);
4447
let from_module;
@@ -53,7 +56,6 @@ impl AstUtils {
5356
]));
5457
let analyse_ast_result: AnalyzeAstResult = Evaluation::analyze_ast(session, &expr, parent_symbol.clone(), &expr.range().end(), &mut context,false, &mut vec![]);
5558
(analyse_ast_result, Some(expr.range()))
56-
5759
}
5860

5961
pub fn flatten_expr(expr: &Expr) -> String {
@@ -82,9 +84,9 @@ impl AstUtils {
8284
}
8385
}
8486

85-
fn get_symbol_in_import(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, offset: u32, stmt: &Stmt) -> Option<(AnalyzeAstResult, Option<TextRange>, Option<ExprCall>)> {
87+
fn get_symbol_in_import(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, offset: u32, stmt: &Stmt) -> Option<(AnalyzeAstResult, Option<TextRange>)> {
8688
match stmt {
87-
//for all imports, the idea will be to check if we are on the last name of the import (then it has benn imported already and we can fallback on it),
89+
//for all imports, the idea will be to check if we are on the last name of the import (then it has been imported already and we can fallback on it),
8890
//or then take the full tree to the offset symbol and resolve_import on it as it was in a 'from' clause.
8991
Stmt::Import(stmt) => {
9092
for alias in stmt.names.iter() {
@@ -116,7 +118,7 @@ impl AstUtils {
116118
evaluations: vec![Evaluation::eval_from_symbol(&Rc::downgrade(&symbol), None)],
117119
diagnostics: vec![],
118120
};
119-
return Some((result, Some(range), None));
121+
return Some((result, Some(range)));
120122
}
121123
} else {
122124
let res = resolve_import_stmt(session, file_symbol, None, &[
@@ -134,7 +136,7 @@ impl AstUtils {
134136
).collect(),
135137
diagnostics: vec![],
136138
};
137-
return Some((result, Some(range), None));
139+
return Some((result, Some(range)));
138140
}
139141
}
140142
return None;
@@ -165,7 +167,7 @@ impl AstUtils {
165167
evaluations: vec![Evaluation::eval_from_symbol(&Rc::downgrade(&symbol), None)],
166168
diagnostics: vec![],
167169
};
168-
return Some((result, Some(range), None));
170+
return Some((result, Some(range)));
169171
}
170172
}
171173
},

server/src/features/definition.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl DefinitionFeature {
157157
let crate::core::evaluation::ExprOrIdent::Expr(Expr::Attribute(attr_expr)) = expr else {
158158
return;
159159
};
160-
let (analyse_ast_result, _range) = AstUtils::get_symbols(session, file_symbol, offset as u32, &crate::core::evaluation::ExprOrIdent::Expr(&attr_expr.value));
160+
let (analyse_ast_result, _range) = AstUtils::get_symbol_from_expr(session, file_symbol, &crate::core::evaluation::ExprOrIdent::Expr(&attr_expr.value), offset as u32);
161161
let eval_ptrs = analyse_ast_result.evaluations.iter().flat_map(|eval| Symbol::follow_ref(eval.symbol.get_symbol_ptr(), session, &mut None, false, false, None)).collect::<Vec<_>>();
162162
let maybe_module = file_symbol.borrow().find_module();
163163
let symbols = eval_ptrs.iter().flat_map(|eval_ptr| {
@@ -201,14 +201,13 @@ impl DefinitionFeature {
201201
let offset = file_info.borrow().position_to_offset(line, character);
202202
let file_info_ast_clone = file_info.borrow().file_info_ast.clone();
203203
let file_info_ast_ref = file_info_ast_clone.borrow();
204-
let (expr, call_expr) = AstUtils::get_expr(&file_info_ast_ref, offset as u32);
205-
let Some(expr) = expr else {
206-
return None;
207-
};
208-
let (analyse_ast_result, _range) = AstUtils::get_symbols(session, file_symbol, offset as u32, &expr);
204+
let (analyse_ast_result, _range, expr, call_expr) = AstUtils::get_symbols(session, &file_info_ast_ref, file_symbol, offset as u32);
209205
if analyse_ast_result.evaluations.is_empty() {
210206
return None;
211207
}
208+
let Some(expr) = expr else {
209+
return None; // Unreachable anyway
210+
};
212211
let mut links = vec![];
213212
let mut evaluations = analyse_ast_result.evaluations.clone();
214213
// Filter out magic fields

server/src/features/hover.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ impl HoverFeature {
1818
let offset = file_info.borrow().position_to_offset(line, character);
1919
let file_info_ast_clone = file_info.borrow().file_info_ast.clone();
2020
let file_info_ast_ref = file_info_ast_clone.borrow();
21-
let (expr, call_expr) = AstUtils::get_expr(&file_info_ast_ref, offset as u32);
22-
let Some(expr) = expr else {
23-
return None;
24-
};
25-
let (analyse_ast_result, range) = AstUtils::get_symbols(session, file_symbol, offset as u32, &expr);
21+
let (analyse_ast_result, range, expr, call_expr) = AstUtils::get_symbols(session, &file_info_ast_ref, file_symbol, offset as u32);
2622
let evals = analyse_ast_result.evaluations;
2723
if evals.is_empty() {
2824
return None;

0 commit comments

Comments
 (0)