Skip to content

Commit 572c29d

Browse files
mmahroussfda-odoo
authored andcommitted
[IMP] hover and definition for module names in manifest
1 parent f73f012 commit 572c29d

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

server/src/core/symbols/module_symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,10 @@ impl ModuleSymbol {
460460
symbol.borrow().as_module_package().dir_name == *dir_name || symbol.borrow().as_module_package().all_depends.contains(dir_name)
461461
}
462462

463+
pub fn get_all_depends(&self) -> &HashSet<OYarn> {
464+
&self.all_depends
465+
}
466+
463467
pub fn get_dependencies(&self, step: usize, level: usize) -> Option<&PtrWeakHashSet<Weak<RefCell<Symbol>>>>
464468
{
465469
self.dependencies.get(step)?.get(level)?.as_ref()

server/src/features/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ fn build_completion_item_from_symbol(session: &mut SessionInfo, symbols: Vec<Rc<
11121112
documentation: Some(
11131113
lsp_types::Documentation::MarkupContent(MarkupContent {
11141114
kind: lsp_types::MarkupKind::Markdown,
1115-
value: FeaturesUtils::build_markdown_description(session, None, &symbols.iter().map(|symbol|
1115+
value: FeaturesUtils::build_markdown_description(session, None, None, &symbols.iter().map(|symbol|
11161116
Evaluation {
11171117
symbol: EvaluationSymbol::new_with_symbol(Rc::downgrade(symbol), None,
11181118
context_of_symbol.clone(),

server/src/features/definition.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ruff_text_size::TextSize;
44
use std::path::PathBuf;
55
use std::{cell::RefCell, rc::Rc};
66

7-
use crate::constants::SymType;
7+
use crate::constants::{PackageType, SymType};
88
use crate::core::evaluation::{Evaluation, EvaluationValue, ExprOrIdent};
99
use crate::core::file_mgr::{FileInfo, FileMgr};
1010
use crate::core::odoo::SyncOdoo;
@@ -91,6 +91,33 @@ impl DefinitionFeature {
9191
model_found
9292
}
9393

94+
fn check_for_module_string(session: &mut SessionInfo, eval: &Evaluation, file_symbol: &Rc<RefCell<Symbol>>, file_path: &String, links: &mut Vec<LocationLink>) -> bool {
95+
if file_symbol.borrow().typ() != SymType::PACKAGE(PackageType::MODULE) || !file_path.ends_with("__manifest__.py") {
96+
// If not on manifest, we don't check for modules
97+
return false;
98+
};
99+
let value = if let Some(eval_value) = eval.value.as_ref() {
100+
if let EvaluationValue::CONSTANT(Expr::StringLiteral(expr)) = eval_value {
101+
oyarn!("{}", expr.value.to_string())
102+
} else {
103+
return false;
104+
}
105+
} else {
106+
return false;
107+
};
108+
let Some(module) = session.sync_odoo.modules.get(&oyarn!("{}", value)).and_then(|m| m.upgrade()) else {
109+
return false;
110+
};
111+
let path = PathBuf::from(module.borrow().paths()[0].clone()).join("__manifest__.py").sanitize();
112+
links.push(LocationLink{
113+
origin_selection_range: None,
114+
target_uri: FileMgr::pathname2uri(&path),
115+
target_selection_range: Range::default(),
116+
target_range: Range::default(),
117+
});
118+
true
119+
}
120+
94121
fn check_for_xml_id_string(session: &mut SessionInfo, eval: &Evaluation, file_symbol: &Rc<RefCell<Symbol>>, links: &mut Vec<LocationLink>) -> bool {
95122
let value = if let Some(eval_value) = eval.value.as_ref() {
96123
if let EvaluationValue::CONSTANT(Expr::StringLiteral(expr)) = eval_value {
@@ -238,6 +265,7 @@ impl DefinitionFeature {
238265
let eval = evaluations[index].clone();
239266
if DefinitionFeature::check_for_domain_field(session, &eval, file_symbol, &call_expr, offset, &mut links) ||
240267
DefinitionFeature::check_for_compute_string(session, &eval, file_symbol,&call_expr, offset, &mut links) ||
268+
DefinitionFeature::check_for_module_string(session, &eval, file_symbol, &file_info.borrow().uri, &mut links) ||
241269
DefinitionFeature::check_for_model_string(session, &eval, file_symbol, &mut links) ||
242270
DefinitionFeature::check_for_xml_id_string(session, &eval, file_symbol, &mut links) {
243271
index += 1;

server/src/features/features_utils.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::PathBuf;
1111
use std::rc::Weak;
1212
use std::{cell::RefCell, rc::Rc};
1313

14-
use crate::constants::SymType;
14+
use crate::constants::{PackageType, SymType};
1515
use crate::constants::OYarn;
1616
use crate::core::evaluation::{Context, ContextValue, Evaluation, EvaluationSymbolPtr, EvaluationSymbolWeak, EvaluationValue};
1717
use crate::core::symbols::symbol::Symbol;
@@ -320,7 +320,14 @@ impl FeaturesUtils {
320320
vec![]
321321
}
322322

323-
pub fn build_markdown_description(session: &mut SessionInfo, file_symbol: Option<Rc<RefCell<Symbol>>>, evals: &Vec<Evaluation>, call_expr: &Option<ExprCall>, offset: Option<usize>) -> String {
323+
pub fn build_markdown_description(
324+
session: &mut SessionInfo,
325+
file_symbol: Option<Rc<RefCell<Symbol>>>,
326+
file_path: Option<&String>,
327+
evals: &Vec<Evaluation>,
328+
call_expr: &Option<ExprCall>,
329+
offset: Option<usize>
330+
) -> String {
324331
#[derive(Debug, Eq, PartialEq, Hash)]
325332
struct SymbolKey {
326333
name: OYarn,
@@ -339,6 +346,21 @@ impl FeaturesUtils {
339346
if let Some(EvaluationValue::CONSTANT(Expr::StringLiteral(expr))) = eval.value.as_ref() {
340347
let mut block = S!("");
341348
let str = expr.value.to_string();
349+
if let Some(SymType::PACKAGE(PackageType::MODULE)) = file_symbol.as_ref().map(|fs| fs.borrow().typ())
350+
&& file_path.map_or(false, |fp| fp.ends_with("__manifest__.py")) {
351+
// If we are in manifest, we check if the string is a module and list the underlying module dependencies
352+
if let Some(module) = session.sync_odoo.modules.get(&oyarn!("{}", str)).and_then(|m| m.upgrade()) {
353+
block += format!("Module: {}", module.borrow().name()).as_str();
354+
let module_ref = module.borrow();
355+
let dependencies = module_ref.as_module_package().get_all_depends();
356+
if !dependencies.is_empty() {
357+
block += " \n*** \nDependencies: \n";
358+
block += &dependencies.iter().map(|dep| format!("- {}", dep)).join(" \n");
359+
}
360+
}
361+
blocks.push(block);
362+
continue;
363+
}
342364
let from_module = file_symbol.as_ref().and_then(|file_symbol| file_symbol.borrow().find_module());
343365
if let (Some(call_expression), Some(file_sym), Some(offset)) = (call_expr, file_symbol.as_ref(), offset){
344366
let mut special_string_syms = FeaturesUtils::check_for_string_special_syms(session, &str, call_expression, offset, expr.range, file_sym);
@@ -365,7 +387,7 @@ impl FeaturesUtils {
365387
.chain(evals.iter().take(index).cloned())
366388
.chain(evals.iter().skip(index + 1).cloned())
367389
.collect();
368-
let r = FeaturesUtils::build_markdown_description(session, file_symbol, &string_domain_fields_evals, call_expr, Some(offset));
390+
let r = FeaturesUtils::build_markdown_description(session, file_symbol, file_path, &string_domain_fields_evals, call_expr, Some(offset));
369391
// remove the injected `base_attr` context value
370392
special_string_syms.iter_mut().for_each(|sym_rc| {
371393
sym_rc.borrow_mut().evaluations_mut().into_iter().flatten().for_each(|eval| {

server/src/features/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl HoverFeature {
2929
Some(Hover { contents:
3030
HoverContents::Markup(MarkupContent {
3131
kind: lsp_types::MarkupKind::Markdown,
32-
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), &evals, &call_expr, Some(offset))
32+
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), Some(&file_info.borrow().uri), &evals, &call_expr, Some(offset))
3333
}),
3434
range: range
3535
})
@@ -48,7 +48,7 @@ impl HoverFeature {
4848
return Some(Hover { contents:
4949
HoverContents::Markup(MarkupContent {
5050
kind: lsp_types::MarkupKind::Markdown,
51-
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), &evals, &None, Some(offset))
51+
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), Some(&file_info.borrow().uri), &evals, &None, Some(offset))
5252
}),
5353
range: range
5454
})

0 commit comments

Comments
 (0)