Skip to content

Commit d0b2827

Browse files
committed
[IMP] server: xml_ids location and xml_ids on packages + gotodef on ref
1 parent 9a8e314 commit d0b2827

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
@@ -1097,7 +1097,7 @@ impl PythonArchEvalHooks {
10971097
return None;
10981098
};
10991099
let module_rc_bw = module_rc.borrow();
1100-
let Some(symbol) = module_rc_bw.as_module_package().xml_ids.get(xml_id.as_str()) else {
1100+
let Some(symbol) = module_rc_bw.as_module_package().xml_id_locations.get(xml_id.as_str()) else {
11011101
if in_validation {
11021102
/*diagnostics.push(Diagnostic::new(
11031103
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
@@ -49,33 +49,32 @@ impl PythonOdooBuilder {
4949
self._load_class_attributes(session, &mut diagnostics);
5050
self._add_magic_fields(session);
5151
let model_name = sym.borrow().as_class_sym()._model.as_ref().unwrap().name.clone();
52+
if let Some(module) = sym.borrow().find_module() {
53+
let file = self.symbol.borrow().get_file().unwrap().upgrade().unwrap();
54+
let xml_id_model_name = oyarn!("model_{}", model_name.replace(".", "_").as_str());
55+
let mut module = module.borrow_mut();
56+
let set = module.as_module_package_mut().xml_id_locations.entry(xml_id_model_name.clone()).or_insert(PtrWeakHashSet::new());
57+
set.insert(file.clone());
58+
drop(module); //in case of file being same than module
59+
let mut file = file.borrow_mut();
60+
file.insert_xml_id(xml_id_model_name.clone(), XmlData::RECORD(XmlDataRecord {
61+
file_symbol: Rc::downgrade(&sym),
62+
model: (Sy!("ir.model"), std::ops::Range::<usize> {
63+
start: 0,
64+
end: 1,
65+
}),
66+
xml_id: Some(xml_id_model_name),
67+
fields: vec![],
68+
range: std::ops::Range::<usize> {
69+
start: self.symbol.borrow().range().start().to_usize(),
70+
end: self.symbol.borrow().range().end().to_usize(),
71+
}
72+
}));
73+
}
5274
match session.sync_odoo.models.get(&model_name).cloned(){
5375
Some(model) => model.borrow_mut().add_symbol(session, sym.clone()),
5476
None => {
5577
let model = Model::new(model_name.clone(), sym.clone());
56-
session.sync_odoo.modules.get("base").map(|module| {
57-
let file = self.symbol.borrow().get_file().unwrap().upgrade().unwrap();
58-
let xml_id_model_name = oyarn!("model_{}", model_name.replace(".", "_").as_str());
59-
let module = module.upgrade().unwrap();
60-
let mut module = module.borrow_mut();
61-
let set = module.as_module_package_mut().xml_ids.entry(xml_id_model_name.clone()).or_insert(PtrWeakHashSet::new());
62-
set.insert(file.clone());
63-
let mut file = file.borrow_mut();
64-
let file = file.as_file_mut();
65-
file.xml_ids.entry(xml_id_model_name.clone()).or_insert(vec![]).push(XmlData::RECORD(XmlDataRecord {
66-
file_symbol: Rc::downgrade(&sym),
67-
model: (Sy!("ir.model"), std::ops::Range::<usize> {
68-
start: 0,
69-
end: 1,
70-
}),
71-
xml_id: Some(xml_id_model_name),
72-
fields: vec![],
73-
range: std::ops::Range::<usize> {
74-
start: self.symbol.borrow().range().start().to_usize(),
75-
end: self.symbol.borrow().range().end().to_usize(),
76-
}
77-
}));
78-
});
7978
session.sync_odoo.models.insert(model_name.clone(), Rc::new(RefCell::new(model)));
8079
}
8180
}

server/src/core/symbols/module_symbol.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub struct ModuleSymbol {
4242
all_depends: HashSet<OYarn>, //computed all depends to avoid too many recomputations
4343
data: Vec<(String, TextRange)>, // TODO
4444
pub module_symbols: HashMap<OYarn, Rc<RefCell<Symbol>>>,
45-
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.
45+
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.
46+
pub xml_ids: HashMap<OYarn, Vec<XmlData>>, //used for dynamic XML_ID records, like ir.models. normal ids are in their XmlFile
4647
pub arch_status: BuildStatus,
4748
pub arch_eval_status: BuildStatus,
4849
pub odoo_status: BuildStatus,
@@ -81,6 +82,7 @@ impl ModuleSymbol {
8182
root_path: dir_path.sanitize(),
8283
loaded: false,
8384
module_name: OYarn::from(""),
85+
xml_id_locations: HashMap::new(),
8486
xml_ids: HashMap::new(),
8587
dir_name: OYarn::from(""),
8688
depends: vec!((OYarn::from("base"), TextRange::default())),
@@ -518,9 +520,9 @@ impl ModuleSymbol {
518520
//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"
519521
pub fn get_xml_id(&self, xml_id: &OYarn) -> Vec<XmlData> {
520522
let mut res = vec![];
521-
if let Some(xml_file_set) = self.xml_ids.get(xml_id) {
523+
if let Some(xml_file_set) = self.xml_id_locations.get(xml_id) {
522524
for xml_file in xml_file_set.iter() {
523-
if let Some(xml_data) = xml_file.borrow().as_xml_file_sym().xml_ids.get(xml_id) {
525+
if let Some(xml_data) = xml_file.borrow().get_xml_id(xml_id) {
524526
res.extend(xml_data.iter().cloned());
525527
}
526528
}

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
@@ -4,6 +4,7 @@ use tracing::{info, trace};
44
use weak_table::traits::WeakElement;
55

66
use crate::core::file_mgr::{add_diagnostic, NoqaInfo};
7+
use crate::core::xml_data::XmlData;
78
use crate::{constants::*, oyarn, Sy};
89
use crate::core::entry_point::EntryPoint;
910
use crate::core::evaluation::{Context, ContextValue, Evaluation, EvaluationSymbolPtr, EvaluationSymbolWeak};
@@ -2817,6 +2818,31 @@ impl Symbol {
28172818
res
28182819
}
28192820

2821+
pub fn get_xml_id(&self, xml_id: &OYarn) -> Option<Vec<XmlData>> {
2822+
match self {
2823+
Symbol::XmlFileSymbol(xml_file) => xml_file.xml_ids.get(xml_id).cloned(),
2824+
Symbol::Package(PackageSymbol::Module(module)) => module.xml_ids.get(xml_id).cloned(),
2825+
Symbol::Package(PackageSymbol::PythonPackage(package)) => package.xml_ids.get(xml_id).cloned(),
2826+
Symbol::File(file) => file.xml_ids.get(xml_id).cloned(),
2827+
_ => None,
2828+
}
2829+
}
2830+
2831+
pub fn insert_xml_id(&mut self, xml_id: OYarn, xml_data: XmlData) {
2832+
match self {
2833+
Symbol::File(file) => {
2834+
file.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2835+
},
2836+
Symbol::Package(PackageSymbol::Module(module)) => {
2837+
module.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2838+
},
2839+
Symbol::Package(PackageSymbol::PythonPackage(package)) => {
2840+
package.xml_ids.entry(xml_id).or_insert(vec![]).push(xml_data);
2841+
},
2842+
_ => {}
2843+
}
2844+
}
2845+
28202846
pub fn print_dependencies(&self) {
28212847
/*println!("------- Output dependencies of {} -------", self.name());
28222848
println!("--- ARCH");

server/src/core/xml_arch_builder.rs

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

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)