Skip to content

Commit 5c4e239

Browse files
committed
[IMP] hover and definition for module names in manifest
1 parent 4449f9e commit 5c4e239

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

server/src/core/symbols/module_symbol.rs

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

418+
pub fn get_all_depends(&self) -> &HashSet<OYarn> {
419+
&self.all_depends
420+
}
421+
418422
pub fn get_dependencies(&self, step: usize, level: usize) -> Option<&PtrWeakHashSet<Weak<RefCell<Symbol>>>>
419423
{
420424
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
@@ -1089,7 +1089,7 @@ fn build_completion_item_from_symbol(session: &mut SessionInfo, symbols: Vec<Rc<
10891089
documentation: Some(
10901090
lsp_types::Documentation::MarkupContent(MarkupContent {
10911091
kind: lsp_types::MarkupKind::Markdown,
1092-
value: FeaturesUtils::build_markdown_description(session, None, &symbols.iter().map(|symbol|
1092+
value: FeaturesUtils::build_markdown_description(session, None, None, &symbols.iter().map(|symbol|
10931093
Evaluation {
10941094
symbol: EvaluationSymbol::new_with_symbol(Rc::downgrade(symbol), None,
10951095
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};
99
use crate::core::file_mgr::{FileInfo, FileMgr};
1010
use crate::core::odoo::SyncOdoo;
@@ -90,6 +90,33 @@ impl DefinitionFeature {
9090
model_found
9191
}
9292

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

server/src/features/features_utils.rs

Lines changed: 27 additions & 5 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,
@@ -336,9 +343,24 @@ impl FeaturesUtils {
336343
let mut aggregator: HashMap<SymbolKey, Vec<SymbolInfo>> = HashMap::new();
337344
for (index, eval) in evals.iter().enumerate() {
338345
//search for a constant evaluation like a model name or domain field
339-
if let Some(EvaluationValue::CONSTANT(Expr::StringLiteral(expr))) = eval.value.as_ref() {
346+
if let Some(EvaluationValue::CONSTANT(Expr::StringLiteral(expr))) = eval.value.as_ref()
347+
&& file_path.map_or(false, |fp| fp.ends_with("__manifest__.py")) {
340348
let mut block = S!("");
341349
let str = expr.value.to_string();
350+
if let Some(SymType::PACKAGE(PackageType::MODULE)) = file_symbol.as_ref().map(|fs| fs.borrow().typ()) {
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| {
@@ -622,7 +644,7 @@ impl FeaturesUtils {
622644
}
623645

624646
/// Finds and returns useful links for an evaluation
625-
fn get_useful_link(session: &mut SessionInfo, typ: &EvaluationSymbolPtr) -> String {
647+
fn get_useful_link(_session: &mut SessionInfo, typ: &EvaluationSymbolPtr) -> String {
626648
// Possibly add more links in the future
627649
let Some(typ) = typ.upgrade_weak() else {
628650
return S!("")

server/src/features/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl HoverFeature {
2525
Some(Hover { contents:
2626
HoverContents::Markup(MarkupContent {
2727
kind: lsp_types::MarkupKind::Markdown,
28-
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), &evals, &call_expr, Some(offset))
28+
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), Some(&file_info.borrow().uri), &evals, &call_expr, Some(offset))
2929
}),
3030
range: range
3131
})
@@ -44,7 +44,7 @@ impl HoverFeature {
4444
return Some(Hover { contents:
4545
HoverContents::Markup(MarkupContent {
4646
kind: lsp_types::MarkupKind::Markdown,
47-
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), &evals, &None, Some(offset))
47+
value: FeaturesUtils::build_markdown_description(session, Some(file_symbol.clone()), Some(&file_info.borrow().uri), &evals, &None, Some(offset))
4848
}),
4949
range: range
5050
})

0 commit comments

Comments
 (0)