Skip to content

Commit be03391

Browse files
mmahroussfda-odoo
authored andcommitted
[IMP] hover and gotodef for inverse_name
1 parent dac8073 commit be03391

File tree

8 files changed

+74
-17
lines changed

8 files changed

+74
-17
lines changed

server/src/core/evaluation.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ impl Evaluation {
894894
}
895895
}
896896
//1: find __init__ method
897-
let init = base_sym.borrow().get_member_symbol(session, &S!("__init__"), module.clone(), true, false, false, false);
897+
let init = base_sym.borrow().get_member_symbol(session, &S!("__init__"), module.clone(), true, false, false, false, false);
898898
let mut found_hook = false;
899899
if let Some(init) = init.0.first() {
900900
let init_sym_file = init.borrow().get_file().as_ref().unwrap().upgrade().unwrap().clone();
@@ -1052,7 +1052,7 @@ impl Evaluation {
10521052
}
10531053
}
10541054
let is_super = ibase.is_weak() && ibase.as_weak().is_super;
1055-
let (attributes, mut attributes_diagnostics) = base_loc.borrow().get_member_symbol(session, &expr.attr.to_string(), module.clone(), false, false, true, is_super);
1055+
let (attributes, mut attributes_diagnostics) = base_loc.borrow().get_member_symbol(session, &expr.attr.to_string(), module.clone(), false, false, false, true, is_super);
10561056
for diagnostic in attributes_diagnostics.iter_mut(){
10571057
diagnostic.range = FileMgr::textRange_to_temporary_Range(&expr.range())
10581058
}
@@ -1278,7 +1278,7 @@ impl Evaluation {
12781278
for base_eval_ptr in base_eval_ptrs.iter() {
12791279
let EvaluationSymbolPtr::WEAK(base_sym_weak_eval) = base_eval_ptr else {continue};
12801280
let Some(base_sym) = base_sym_weak_eval.weak.upgrade() else {continue};
1281-
let (operator_functions, diags) = base_sym.borrow().get_member_symbol(session, &S!(method), module.clone(), true, false, false, false);
1281+
let (operator_functions, diags) = base_sym.borrow().get_member_symbol(session, &S!(method), module.clone(), true, false, true, false, false);
12821282
diagnostics.extend(diags);
12831283
for operator_function in operator_functions.into_iter(){
12841284
for eval in operator_function.borrow().evaluations().unwrap_or(&vec![]).iter() {
@@ -1582,6 +1582,7 @@ impl Evaluation {
15821582
false,
15831583
true,
15841584
false,
1585+
false,
15851586
false);
15861587
if symbols.is_empty() {
15871588
if let Some(diagnostic_base) = create_diagnostic(session, DiagnosticCode::OLS03011, &[&name, &object.borrow().name()]) {

server/src/core/python_arch_eval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl PythonArchEval {
819819
let symbol_type_rc = symbol_eval[0].upgrade_weak().unwrap();
820820
let symbol_type = symbol_type_rc.borrow();
821821
if symbol_type.typ() == SymType::CLASS {
822-
let (iter, _) = symbol_type.get_member_symbol(session, &S!("__iter__"), None, true, false, false, false);
822+
let (iter, _) = symbol_type.get_member_symbol(session, &S!("__iter__"), None, true, false, false, false, false);
823823
if iter.len() == 1 {
824824
SyncOdoo::build_now(session, &iter[0], BuildSteps::ARCH_EVAL);
825825
SyncOdoo::build_now(session, &iter[0], BuildSteps::VALIDATION);
@@ -1049,6 +1049,7 @@ impl PythonArchEval {
10491049
from_module.clone(),
10501050
false,
10511051
true,
1052+
false,
10521053
true,
10531054
false);
10541055
if ix == split_expr.len() - 1 {

server/src/core/python_arch_eval_hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ impl PythonArchEvalHooks {
11551155
for arg in arguments.args.iter() {
11561156
let Expr::StringLiteral(expr) = arg else {return diagnostics};
11571157
let field_name = expr.value.to_string();
1158-
let (syms, _) = class_sym.borrow().get_member_symbol(session, &field_name, from_module.clone(), false, false, true, false);
1158+
let (syms, _) = class_sym.borrow().get_member_symbol(session, &field_name, from_module.clone(), false, true, false, true, false);
11591159
if syms.is_empty(){
11601160
if let Some(diagnostic) = create_diagnostic(session, DiagnosticCode::OLS03014, &[&field_name, &model_name]) {
11611161
diagnostics.push(Diagnostic {

server/src/core/python_odoo_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl PythonOdooBuilder {
203203
}
204204

205205
fn _get_attribute(session: &mut SessionInfo, loc_sym: &mut Symbol, attr: &String, diagnostics: &mut Vec<Diagnostic>) -> Option<EvaluationValue> {
206-
let (attr_sym, _) = loc_sym.get_member_symbol(session, attr, None, true, false, false, false);
206+
let (attr_sym, _) = loc_sym.get_member_symbol(session, attr, None, true, false, false, false, false);
207207
if attr_sym.len() == 0 {
208208
return None;
209209
}

server/src/core/python_validator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,10 @@ impl PythonValidator {
521521
false,
522522
false,
523523
true,
524+
true,
524525
false
525526
);
526-
let method_found = symbols.iter().any(|symbol| symbol.borrow().typ() == SymType::FUNCTION);
527+
let method_found = !symbols.is_empty();
527528
if !method_found{
528529
let Some(arg_range) = eval_weak.as_weak().context.get(&format!("{special_fn_field_name}_arg_range")).map(|ctx_val| ctx_val.as_text_range()) else {
529530
continue;

server/src/core/symbols/symbol.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ impl Symbol {
21292129
if let Some(base_attr) = base_attr {
21302130
let attribute_type_sym = symbol;
21312131
//TODO shouldn't we set the from_module in the call to get_member_symbol?
2132-
let get_method = attribute_type_sym.get_member_symbol(session, &S!("__get__"), None, true, false, true, false).0.first().cloned();
2132+
let get_method = attribute_type_sym.get_member_symbol(session, &S!("__get__"), None, true, false, false, true, false).0.first().cloned();
21332133
match get_method {
21342134
Some(get_method) if (base_attr.borrow().typ() == SymType::CLASS) => {
21352135
let get_method = get_method.borrow();
@@ -2757,12 +2757,33 @@ impl Symbol {
27572757
if not all, it will return the first found. If all, the all found symbols are returned, but the first one
27582758
is the one that is overriding others.
27592759
:param: from_module: optional, can change the from_module of the given class */
2760-
pub fn get_member_symbol(&self, session: &mut SessionInfo, name: &String, from_module: Option<Rc<RefCell<Symbol>>>, prevent_comodel: bool, only_fields: bool, all: bool, is_super: bool) -> (Vec<Rc<RefCell<Symbol>>>, Vec<Diagnostic>) {
2760+
pub fn get_member_symbol(
2761+
&self,
2762+
session: &mut SessionInfo,
2763+
name: &String,
2764+
from_module: Option<Rc<RefCell<Symbol>>>,
2765+
prevent_comodel: bool,
2766+
only_fields: bool,
2767+
only_methods: bool,
2768+
all: bool,
2769+
is_super: bool
2770+
) -> (Vec<Rc<RefCell<Symbol>>>, Vec<Diagnostic>) {
27612771
let mut visited_classes: PtrWeakHashSet<Weak<RefCell<Symbol>>> = PtrWeakHashSet::new();
2762-
return self._get_member_symbol_helper(session, name, from_module, prevent_comodel, only_fields, all, is_super, &mut visited_classes);
2772+
return self._get_member_symbol_helper(session, name, from_module, prevent_comodel, only_fields, only_methods, all, is_super, &mut visited_classes);
27632773
}
27642774

2765-
fn _get_member_symbol_helper(&self, session: &mut SessionInfo, name: &String, from_module: Option<Rc<RefCell<Symbol>>>, prevent_comodel: bool, only_fields: bool, all: bool, is_super: bool, visited_classes: &mut PtrWeakHashSet<Weak<RefCell<Symbol>>>) -> (Vec<Rc<RefCell<Symbol>>>, Vec<Diagnostic>) {
2775+
fn _get_member_symbol_helper(
2776+
&self,
2777+
session: &mut SessionInfo,
2778+
name: &String,
2779+
from_module: Option<Rc<RefCell<Symbol>>>,
2780+
prevent_comodel: bool,
2781+
only_fields: bool,
2782+
only_methods: bool,
2783+
all: bool,
2784+
is_super: bool,
2785+
visited_classes: &mut PtrWeakHashSet<Weak<RefCell<Symbol>>>
2786+
) -> (Vec<Rc<RefCell<Symbol>>>, Vec<Diagnostic>) {
27662787
let mut result: Vec<Rc<RefCell<Symbol>>> = vec![];
27672788
let mut visited_symbols: PtrWeakHashSet<Weak<RefCell<Symbol>>> = PtrWeakHashSet::new();
27682789
let mut extend_result = |syms: Vec<Rc<RefCell<Symbol>>>| {
@@ -2790,6 +2811,9 @@ impl Symbol {
27902811
if only_fields {
27912812
content_syms = content_syms.iter().filter(|x| x.borrow().is_field(session)).cloned().collect();
27922813
}
2814+
if only_methods {
2815+
content_syms = content_syms.iter().filter(|x| x.borrow().typ() == SymType::FUNCTION).cloned().collect();
2816+
}
27932817
if !content_syms.is_empty() {
27942818
if all {
27952819
extend_result(content_syms);
@@ -2812,7 +2836,7 @@ impl Symbol {
28122836
continue;
28132837
}
28142838
visited_classes.insert(model_symbol.clone());
2815-
let (attributs, att_diagnostic) = model_symbol.borrow()._get_member_symbol_helper(session, name, None, true, only_fields, all, false, visited_classes);
2839+
let (attributs, att_diagnostic) = model_symbol.borrow()._get_member_symbol_helper(session, name, None, true, only_fields, only_methods, all, false, visited_classes);
28162840
diagnostics.extend(att_diagnostic);
28172841
if all {
28182842
extend_result(attributs);
@@ -2830,7 +2854,7 @@ impl Symbol {
28302854
continue;
28312855
}
28322856
visited_classes.insert(model_symbol.clone());
2833-
let (attributs, att_diagnostic) = model_symbol.borrow()._get_member_symbol_helper(session, name, None, true, true, all, false, visited_classes);
2857+
let (attributs, att_diagnostic) = model_symbol.borrow()._get_member_symbol_helper(session, name, None, true, true, only_methods, all, false, visited_classes);
28342858
diagnostics.extend(att_diagnostic);
28352859
if all {
28362860
extend_result(attributs);
@@ -2854,7 +2878,7 @@ impl Symbol {
28542878
continue;
28552879
}
28562880
visited_classes.insert(base.clone());
2857-
let (s, s_diagnostic) = base.borrow().get_member_symbol(session, name, from_module.clone(), prevent_comodel, only_fields, all, false);
2881+
let (s, s_diagnostic) = base.borrow().get_member_symbol(session, name, from_module.clone(), prevent_comodel, only_fields, only_methods, all, false);
28582882
diagnostics.extend(s_diagnostic);
28592883
if !s.is_empty() {
28602884
if all {

server/src/features/completion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ fn add_nested_field_names(
10221022
from_module.clone(),
10231023
false,
10241024
true,
1025+
false,
10251026
true,
10261027
false);
10271028
if symbols.is_empty() {

server/src/features/features_utils.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ impl FeaturesUtils {
5959
let Some(ref arg_id) = keyword.arg else {
6060
return vec![];
6161
};
62+
if arg_id.as_str() == "inverse_name" {
63+
return FeaturesUtils::find_inverse_name_field_symbol(session, from_module, field_value, call_expr);
64+
}
6265
if !["compute", "inverse", "search"].contains(&arg_id.as_str()){
6366
return vec![];
6467
}
@@ -81,7 +84,32 @@ impl FeaturesUtils {
8184
) {
8285
return vec![];
8386
}
84-
parent_class.clone().borrow().get_member_symbol(session, field_value, from_module.clone(), false, false, true, false).0
87+
parent_class.clone().borrow().get_member_symbol(session, field_value, from_module.clone(), false, false, true, true, false).0
88+
}
89+
90+
fn find_inverse_name_field_symbol(
91+
session: &mut SessionInfo,
92+
from_module: Option<Rc<RefCell<Symbol>>>,
93+
field_value: &String,
94+
call_expr: &ExprCall,
95+
) -> Vec<Rc<RefCell<Symbol>>>{
96+
let model_name = if let Some(Expr::StringLiteral(expr)) = call_expr.arguments.args.first() {
97+
expr.value.to_string()
98+
} else {
99+
let Some(model_name) = call_expr.arguments.keywords.iter().find(|kw| kw.arg.as_ref().map(|arg| arg.id == "comodel_name").unwrap_or(false))
100+
.and_then(|kw| match &kw.value {
101+
Expr::StringLiteral(expr) => Some(expr.value.to_string()),
102+
_ => None
103+
}) else {
104+
return vec![];
105+
};
106+
model_name
107+
};
108+
let Some(model) = session.sync_odoo.models.get(&oyarn!("{}", model_name)).cloned() else {
109+
return vec![];
110+
};
111+
let main_syms = model.borrow().get_main_symbols(session, from_module.clone());
112+
main_syms.iter().flat_map(|main_sym| main_sym.clone().borrow().get_member_symbol(session, field_value, from_module.clone(), false, true, false, true, false).0).collect()
85113
}
86114

87115
fn find_simple_decorator_field_symbol(
@@ -96,7 +124,7 @@ impl FeaturesUtils {
96124
if parent_class.borrow().as_class_sym()._model.is_none(){
97125
return vec![];
98126
}
99-
parent_class.clone().borrow().get_member_symbol(session, field_name, from_module.clone(), false, false, true, false).0
127+
parent_class.clone().borrow().get_member_symbol(session, field_name, from_module.clone(), false, true, false, true, false).0
100128
}
101129

102130
fn find_nested_fields(
@@ -119,14 +147,15 @@ impl FeaturesUtils {
119147
let range_end = range_start + TextSize::new((name.len() + 1) as u32);
120148
let cursor_section = TextRange::new(range_start, range_end).contains(TextSize::new(*offset as u32));
121149
if cursor_section {
122-
let fields = parent_object.clone().unwrap().borrow().get_member_symbol(session, &name, from_module.clone(), false, true, true, false).0;
150+
let fields = parent_object.clone().unwrap().borrow().get_member_symbol(session, &name, from_module.clone(), false, true, false,true, false).0;
123151
return fields.into_iter().map(|f| (f, TextRange::new(range_start, range_end - TextSize::new(1)))).collect();
124152
} else {
125153
let (symbols, _diagnostics) = parent_object.clone().unwrap().borrow().get_member_symbol(session,
126154
&name.to_string(),
127155
from_module.clone(),
128156
false,
129157
true,
158+
false,
130159
true,
131160
false);
132161
if symbols.is_empty() {

0 commit comments

Comments
 (0)