Skip to content

Commit e533a76

Browse files
committed
[IMP] server: xml_ids location and xml_ids on packages + gotodef on ref
1 parent d4295f2 commit e533a76

File tree

8 files changed

+79
-43
lines changed

8 files changed

+79
-43
lines changed

server/src/core/python_arch_eval_hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ impl PythonArchEvalHooks {
10731073
return None;
10741074
};
10751075
let module_rc_bw = module_rc.borrow();
1076-
let Some(symbol) = module_rc_bw.as_module_package().xml_ids.get(xml_id.as_str()) else {
1076+
let Some(symbol) = module_rc_bw.as_module_package().xml_id_locations.get(xml_id.as_str()) else {
10771077
if in_validation {
10781078
/*diagnostics.push(Diagnostic::new(
10791079
FileMgr::textRange_to_temporary_Range(&xml_id_expr.range()),

server/src/core/python_odoo_builder.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ impl PythonOdooBuilder {
5050
self._load_class_attributes(session, &mut diagnostics);
5151
self._add_magic_fields(session);
5252
let model_name = sym.borrow().as_class_sym()._model.as_ref().unwrap().name.clone();
53+
if let Some(module) = sym.borrow().find_module() {
54+
let file = self.symbol.borrow().get_file().unwrap().upgrade().unwrap();
55+
let xml_id_model_name = oyarn!("model_{}", model_name.replace(".", "_").as_str());
56+
let mut module = module.borrow_mut();
57+
let set = module.as_module_package_mut().xml_id_locations.entry(xml_id_model_name.clone()).or_insert(PtrWeakHashSet::new());
58+
set.insert(file.clone());
59+
drop(module); //in case of file being same than module
60+
let mut file = file.borrow_mut();
61+
file.insert_xml_id(xml_id_model_name.clone(), XmlData::RECORD(XmlDataRecord {
62+
file_symbol: Rc::downgrade(&sym),
63+
model: (Sy!("ir.model"), std::ops::Range::<usize> {
64+
start: 0,
65+
end: 1,
66+
}),
67+
xml_id: Some(xml_id_model_name),
68+
fields: vec![],
69+
range: std::ops::Range::<usize> {
70+
start: self.symbol.borrow().range().start().to_usize(),
71+
end: self.symbol.borrow().range().end().to_usize(),
72+
}
73+
}));
74+
}
5375
match session.sync_odoo.models.get(&model_name).cloned(){
5476
Some(model) => {
5577
let inherited_model_names = sym.borrow().as_class_sym()._model.as_ref().unwrap().inherit.clone();
@@ -81,29 +103,6 @@ impl PythonOdooBuilder {
81103
},
82104
None => {
83105
let model = Model::new(model_name.clone(), sym.clone());
84-
session.sync_odoo.modules.get("base").map(|module| {
85-
let file = self.symbol.borrow().get_file().unwrap().upgrade().unwrap();
86-
let xml_id_model_name = oyarn!("model_{}", model_name.replace(".", "_").as_str());
87-
let module = module.upgrade().unwrap();
88-
let mut module = module.borrow_mut();
89-
let set = module.as_module_package_mut().xml_ids.entry(xml_id_model_name.clone()).or_insert(PtrWeakHashSet::new());
90-
set.insert(file.clone());
91-
let mut file = file.borrow_mut();
92-
let file = file.as_file_mut();
93-
file.xml_ids.entry(xml_id_model_name.clone()).or_insert(vec![]).push(XmlData::RECORD(XmlDataRecord {
94-
file_symbol: Rc::downgrade(&sym),
95-
model: (Sy!("ir.model"), std::ops::Range::<usize> {
96-
start: 0,
97-
end: 1,
98-
}),
99-
xml_id: Some(xml_id_model_name),
100-
fields: vec![],
101-
range: std::ops::Range::<usize> {
102-
start: self.symbol.borrow().range().start().to_usize(),
103-
end: self.symbol.borrow().range().end().to_usize(),
104-
}
105-
}));
106-
});
107106
session.sync_odoo.models.insert(model_name.clone(), Rc::new(RefCell::new(model)));
108107
}
109108
}

server/src/core/symbols/module_symbol.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pub struct ModuleSymbol {
4141
all_depends: HashSet<OYarn>, //computed all depends to avoid too many recomputations
4242
data: Vec<(String, TextRange)>, // TODO
4343
pub module_symbols: HashMap<OYarn, Rc<RefCell<Symbol>>>,
44-
pub xml_ids: HashMap<OYarn, PtrWeakHashSet<Weak<RefCell<Symbol>>>>, //contains all xml_file_symbols that contains the xml_id. Needed because it can be in another module.
44+
pub xml_id_locations: HashMap<OYarn, PtrWeakHashSet<Weak<RefCell<Symbol>>>>, //contains all xml_file_symbols that contains the xml_id. Needed because it can be in another module.
45+
pub xml_ids: HashMap<OYarn, Vec<XmlData>>, //used for dynamic XML_ID records, like ir.models. normal ids are in their XmlFile
4546
pub arch_status: BuildStatus,
4647
pub arch_eval_status: BuildStatus,
4748
pub odoo_status: BuildStatus,
@@ -80,6 +81,7 @@ impl ModuleSymbol {
8081
root_path: dir_path.sanitize(),
8182
loaded: false,
8283
module_name: OYarn::from(""),
84+
xml_id_locations: HashMap::new(),
8385
xml_ids: HashMap::new(),
8486
dir_name: OYarn::from(""),
8587
depends: vec!((OYarn::from("base"), TextRange::default())),
@@ -530,9 +532,9 @@ impl ModuleSymbol {
530532
//For example, stock could create an xml_id called "account.my_xml_id", and so be returned by this function called on "account" module with xml_id "my_xml_id"
531533
pub fn get_xml_id(&self, xml_id: &OYarn) -> Vec<XmlData> {
532534
let mut res = vec![];
533-
if let Some(xml_file_set) = self.xml_ids.get(xml_id) {
535+
if let Some(xml_file_set) = self.xml_id_locations.get(xml_id) {
534536
for xml_file in xml_file_set.iter() {
535-
if let Some(xml_data) = xml_file.borrow().as_xml_file_sym().xml_ids.get(xml_id) {
537+
if let Some(xml_data) = xml_file.borrow().get_xml_id(xml_id) {
536538
res.extend(xml_data.iter().cloned());
537539
}
538540
}

server/src/core/symbols/package_symbol.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use weak_table::{PtrWeakHashSet, PtrWeakKeyHashMap};
22

3-
use crate::{constants::{BuildStatus, BuildSteps, OYarn}, core::{file_mgr::NoqaInfo, model::Model}, oyarn, threads::SessionInfo, S};
3+
use crate::{constants::{BuildStatus, BuildSteps, OYarn}, core::{file_mgr::NoqaInfo, model::Model, xml_data::XmlData}, oyarn, threads::SessionInfo, S};
44
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::{Rc, Weak}};
55

66
use super::{module_symbol::ModuleSymbol, symbol::Symbol, symbol_mgr::{SectionRange, SymbolMgr}};
@@ -111,6 +111,7 @@ pub struct PythonPackageSymbol {
111111
pub not_found_paths: Vec<(BuildSteps, Vec<OYarn>)>,
112112
pub in_workspace: bool,
113113
pub self_import: bool,
114+
pub xml_ids: HashMap<OYarn, Vec<XmlData>>, //used for dynamic XML_ID records, like ir.models
114115
pub module_symbols: HashMap<OYarn, Rc<RefCell<Symbol>>>,
115116
pub model_dependencies: PtrWeakHashSet<Weak<RefCell<Model>>>, //always on validation level, as odoo step is always required
116117
pub dependencies: Vec<Vec<Option<PtrWeakHashSet<Weak<RefCell<Symbol>>>>>>,
@@ -142,6 +143,7 @@ impl PythonPackageSymbol {
142143
validation_status: BuildStatus::PENDING,
143144
not_found_paths: vec![],
144145
in_workspace: false,
146+
xml_ids: HashMap::new(),
145147
self_import: false, //indicates that if unloaded, the symbol should be added in the rebuild automatically as nothing depends on it (used for root packages)
146148
module_symbols: HashMap::new(),
147149
sections: vec![],

server/src/core/symbols/symbol.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use weak_table::traits::WeakElement;
55

66
use crate::core::diagnostics::{create_diagnostic, DiagnosticCode};
77
use crate::core::file_mgr::NoqaInfo;
8+
use crate::core::xml_data::XmlData;
89
use crate::{constants::*, oyarn, Sy};
910
use crate::core::entry_point::EntryPoint;
1011
use crate::core::evaluation::{Context, ContextValue, Evaluation, EvaluationSymbolPtr, EvaluationSymbolWeak};
@@ -2855,6 +2856,31 @@ impl Symbol {
28552856
res
28562857
}
28572858

2859+
pub fn get_xml_id(&self, xml_id: &OYarn) -> Option<Vec<XmlData>> {
2860+
match self {
2861+
Symbol::XmlFileSymbol(xml_file) => xml_file.xml_ids.get(xml_id).cloned(),
2862+
Symbol::Package(PackageSymbol::Module(module)) => module.xml_ids.get(xml_id).cloned(),
2863+
Symbol::Package(PackageSymbol::PythonPackage(package)) => package.xml_ids.get(xml_id).cloned(),
2864+
Symbol::File(file) => file.xml_ids.get(xml_id).cloned(),
2865+
_ => None,
2866+
}
2867+
}
2868+
2869+
pub fn insert_xml_id(&mut self, xml_id: OYarn, xml_data: XmlData) {
2870+
match self {
2871+
Symbol::File(file) => {
2872+
file.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2873+
},
2874+
Symbol::Package(PackageSymbol::Module(module)) => {
2875+
module.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2876+
},
2877+
Symbol::Package(PackageSymbol::PythonPackage(package)) => {
2878+
package.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2879+
},
2880+
_ => {}
2881+
}
2882+
}
2883+
28582884
pub fn print_dependencies(&self) {
28592885
/*println!("------- Output dependencies of {} -------", self.name());
28602886
println!("--- ARCH");

server/src/core/xml_arch_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl XmlArchBuilder {
8181
}
8282
}
8383
xml_data.set_file_symbol(&self.xml_symbol);
84-
xml_module.borrow_mut().as_module_package_mut().xml_ids.entry(Sy!(id.clone())).or_insert(PtrWeakHashSet::new()).insert(self.xml_symbol.clone());
84+
xml_module.borrow_mut().as_module_package_mut().xml_id_locations.entry(Sy!(id.clone())).or_insert(PtrWeakHashSet::new()).insert(self.xml_symbol.clone());
8585
self.xml_symbol.borrow_mut().as_xml_file_sym_mut().xml_ids.entry(Sy!(id.clone())).or_insert(vec![]).push(xml_data);
8686
}
8787
}

server/src/features/definition.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,21 @@ impl DefinitionFeature {
184184
}
185185
},
186186
XmlAstResult::XML_DATA(xml_file_symbol, range) => {
187-
for path in xml_file_symbol.borrow().paths().iter() {
188-
let full_path = match xml_file_symbol.borrow().typ() {
189-
SymType::PACKAGE(_) => PathBuf::from(path).join(format!("__init__.py{}", xml_file_symbol.borrow().as_package().i_ext())).sanitize(),
190-
_ => path.clone()
191-
};
192-
let range = match xml_file_symbol.borrow().typ() {
193-
SymType::PACKAGE(_) | SymType::FILE | SymType::NAMESPACE | SymType::DISK_DIR => Range::default(),
194-
_ => session.sync_odoo.get_file_mgr().borrow().std_range_to_range(session, &full_path, &range),
195-
};
196-
links.push(Location{uri: FileMgr::pathname2uri(&full_path), range: range});
187+
let file = xml_file_symbol.borrow().get_file(); //in case of XML_DATA coming from a python class
188+
if let Some(file) = file {
189+
if let Some(file) = file.upgrade() {
190+
for path in file.borrow().paths().iter() {
191+
let full_path = match file.borrow().typ() {
192+
SymType::PACKAGE(_) => PathBuf::from(path).join(format!("__init__.py{}", file.borrow().as_package().i_ext())).sanitize(),
193+
_ => path.clone()
194+
};
195+
let range = match file.borrow().typ() {
196+
SymType::PACKAGE(_) | SymType::FILE | SymType::NAMESPACE | SymType::DISK_DIR => Range::default(),
197+
_ => session.sync_odoo.get_file_mgr().borrow().std_range_to_range(session, &full_path, &range),
198+
};
199+
links.push(Location{uri: FileMgr::pathname2uri(&full_path), range: range});
200+
}
201+
}
197202
}
198203
}
199204
}

server/src/features/xml_ast_utils.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ impl XmlAstUtils {
198198
if on_dep_only {
199199
xml_ids = xml_ids.into_iter().filter(|x|
200200
{
201-
let file = x.get_xml_file_symbol();
201+
let file = x.get_file_symbol();
202202
if let Some(file) = file {
203-
let module = file.borrow().find_module();
204-
if let Some(module) = module {
205-
return ModuleSymbol::is_in_deps(session, &file_symbol.borrow().find_module().unwrap(), module.borrow().name());
203+
if let Some(file) = file.upgrade() {
204+
let module = file.borrow().find_module();
205+
if let Some(module) = module {
206+
return ModuleSymbol::is_in_deps(session, &file_symbol.borrow().find_module().unwrap(), module.borrow().name());
207+
}
206208
}
207209
}
208210
return false;

0 commit comments

Comments
 (0)