Skip to content

Commit 74786b2

Browse files
committed
wip references
1 parent 69cf49c commit 74786b2

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

server/src/features/references.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ impl ReferenceFeature {
5757
/// TODO: All files within workspace
5858
/// TODO: Odoo specific (XML field refs, string-based model refs)
5959
pub fn get_references(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, file_info: &Rc<RefCell<FileInfo>>, line: u32, character: u32) -> Option<Vec<Location>> {
60-
let offset = file_info.borrow().position_to_offset(line, character);
60+
let offset = file_info.borrow().position_to_offset(line, character, session.sync_odoo.encoding);
6161

62-
let (analyse_ast_result, _range, _call_expr) = AstUtils::get_symbols(session, file_symbol, file_info, offset as u32);
62+
let file_info_ast_clone = file_info.borrow().file_info_ast.clone();
63+
let file_info_ast_ref = file_info_ast_clone.borrow();
64+
let (analyse_ast_result, _range, _expr, _call_expr) = AstUtils::get_symbols(session, &file_info_ast_ref, file_symbol, offset as u32);
6365

64-
if analyse_ast_result.evaluations.is_empty() {
66+
if analyse_ast_result.evaluations.len() != 1 {
6567
return None;
6668
}
6769

@@ -71,15 +73,45 @@ impl ReferenceFeature {
7173

7274
let symbol_name = target_symbol_rc.borrow().name().to_string();
7375

76+
let mut locations = Vec::new();
77+
locations.extend(ReferenceFeature::references_in_files(session, file_symbol, file_info, &symbol_name, &target_symbol_rc));
78+
79+
//take arch and arch_eval dependents
80+
for dep in file_symbol.borrow().dependents()[0].iter().take(2) {
81+
if let Some(dep_set) = dep {
82+
for dep_symbol_rc in dep_set.iter() {
83+
let Some(Some(file)) = dep_symbol_rc.borrow().get_file().map(|x| x.upgrade()) else { //to be sure we are on a file
84+
continue;
85+
};
86+
let Some(dep_file_info) = session.sync_odoo.get_file_mgr().borrow().get_file_info(&file.borrow().paths()[0]) else {
87+
continue;
88+
};
89+
locations.extend(ReferenceFeature::references_in_files(session, &dep_symbol_rc, &dep_file_info, &symbol_name, &target_symbol_rc));
90+
}
91+
}
92+
}
93+
94+
if locations.is_empty() {
95+
None
96+
} else {
97+
Some(locations)
98+
}
99+
}
100+
101+
pub fn references_in_files(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, file_info: &Rc<RefCell<FileInfo>>, symbol_name: &String, target_symbol_rc: &Rc<RefCell<Symbol>>) -> Vec<Location> {
74102
// find all Name expressions with the same name
75103
let file_info_ast = file_info.borrow().file_info_ast.clone();
76104
let file_info_ast_borrow = file_info_ast.borrow();
77-
let stmts = file_info_ast_borrow.get_stmts()?;
105+
let stmts = file_info_ast_borrow.get_stmts();
106+
let stmts = match stmts {
107+
Some(s) => s,
108+
None => return Vec::new(),
109+
};
78110

79-
let name_matches = NameFinderVisitor::find_all_names(stmts, &symbol_name);
111+
let name_matches = NameFinderVisitor::find_all_names(stmts, symbol_name);
80112

81113
if name_matches.is_empty() {
82-
return None;
114+
return Vec::new();
83115
}
84116

85117
let file_path = file_symbol.borrow().paths()[0].clone();
@@ -106,12 +138,7 @@ impl ReferenceFeature {
106138
});
107139
}
108140
}
109-
110-
if locations.is_empty() {
111-
None
112-
} else {
113-
Some(locations)
114-
}
141+
locations
115142
}
116143

117144
pub fn get_references_xml(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, file_info: &Rc<RefCell<FileInfo>>, line: u32, character: u32) -> Option<Vec<Location>> {

server/tests/test_get_symbol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ fn test_definition() {
248248
#[test]
249249
fn test_references() {
250250
// setup
251-
let mut odoo = setup::setup::setup_server(true);
251+
let (mut odoo, config) = setup::setup::setup_server(true);
252252
let test_addons_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("data").join("addons");
253253
let module1_test_file = test_addons_path.join("module_1").join("models").join("base_test_models.py").sanitize();
254254

255255
// test file exists
256256
assert!(PathBuf::from(&module1_test_file).exists(), "Test file does not exist: {}", module1_test_file);
257-
let mut session = setup::setup::create_session(&mut odoo);
257+
let mut session = setup::setup::create_init_session(&mut odoo, config);
258258

259259
let file_mgr = session.sync_odoo.get_file_mgr();
260260
let m1_tf_file_info = file_mgr.borrow().get_file_info(&module1_test_file).unwrap();

0 commit comments

Comments
 (0)