Skip to content

Commit 4e31135

Browse files
committed
[IMP] csv sub_field in csv headers + tests
1 parent 47b8859 commit 4e31135

File tree

12 files changed

+167
-44
lines changed

12 files changed

+167
-44
lines changed

server/src/core/evaluation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use itertools::Itertools;
22
use itertools::FoldWhile::{Continue, Done};
3-
use ruff_python_ast::{Arguments, Expr, ExprCall, Identifier, Number, Operator, Parameter, UnaryOp};
3+
use ruff_python_ast::{Arguments, Expr, ExprCall, Identifier, Number, Parameter, UnaryOp};
44
use ruff_text_size::{Ranged, TextRange, TextSize};
55
use lsp_types::{Diagnostic, Location, Position, Range};
66
use weak_table::traits::WeakElement;

server/src/features/csv_ast_utils.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use std::{cell::RefCell, rc::Rc};
22

33
use csv::{Reader, StringRecord};
44

5-
use crate::{S, constants::{OYarn, SymType}, core::{file_mgr::FileMgr, odoo::SyncOdoo, symbols::symbol::Symbol, xml_data::{OdooData, OdooDataRecord}}, features::goto_utils::{GotoSource, GotoSourceType}, oyarn, threads::SessionInfo};
5+
use crate::{S, constants::{OYarn, SymType}, core::{odoo::SyncOdoo, symbols::symbol::Symbol, xml_data::{OdooData, OdooDataRecord}}, features::goto_utils::{GotoSource, GotoSourceType}, oyarn, threads::SessionInfo};
66

77

88

99
pub struct CsvAstUtils {}
1010

1111
impl CsvAstUtils {
1212

13-
pub fn get_symbols(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, csv_reader: &mut Reader<&[u8]>, model_name: &OYarn, offset: usize, on_dep_only: bool) -> Vec<GotoSource> {
13+
pub fn get_symbols(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, csv_reader: &mut Reader<&[u8]>, model_name: &OYarn, offset: usize) -> Vec<GotoSource> {
1414
let mut results = vec![];
1515
let module = file_symbol.borrow().find_module();
16-
let Some(model) = session.sync_odoo.models.get(model_name).cloned() else {return vec![];};;
16+
let Some(model) = session.sync_odoo.models.get(model_name).cloned() else {return vec![];};
1717
let model_syms = model.borrow().get_main_symbols(session, module.clone());
1818
let Some(main_symbol) = model_syms.first().cloned() else {return results;};
1919
drop(model_syms);
@@ -23,15 +23,35 @@ impl CsvAstUtils {
2323
let mut h_start = header.position().unwrap().byte() as usize;
2424
for h in header.iter() {
2525
let end = h_start + h.len() as usize;
26+
let has_quotes = h.starts_with('"') && h.ends_with('"') && h.len() >= 2;
2627
let header_txt = CsvAstUtils::remove_quotes(h);
2728
headers.push(oyarn!("{}", header_txt));
2829
if offset >= h_start && offset <= end {
29-
let symbols = main_symbol.borrow().get_member_symbol(session, &header_txt, module.clone(), false, true, false, true, false);
30-
for sym in symbols.0.iter() {
31-
results.push(GotoSource {
32-
source: GotoSourceType::Symbol(sym.clone()),
33-
origin_selection_range: None, //TODO
34-
})
30+
let header_elts = header_txt.splitn(2, [':', '/']).collect::<Vec<_>>();
31+
let symbols = main_symbol.borrow().get_member_symbol(session, &S!(header_elts[0]), module.clone(), false, true, false, true, false);
32+
if offset <= h_start + has_quotes as usize + header_elts[0].len() + 1 {
33+
for sym in symbols.0.iter() {
34+
results.push(GotoSource {
35+
source: GotoSourceType::Symbol(sym.clone()),
36+
origin_selection_range: None, //TODO
37+
})
38+
}
39+
} else {
40+
for sym in symbols.0.iter() {
41+
if sym.borrow().is_specific_field(session, &["Many2one", "One2many", "Many2many"]) && sym.borrow().typ() == SymType::VARIABLE{
42+
let models = sym.borrow().as_variable().get_relational_model(session, module.clone());
43+
if models.len() == 1 {
44+
let model = models[0].clone();
45+
let sub_symbols = model.borrow().get_member_symbol(session, &S!(header_elts[1]), module.clone(), false, true, false, true, false);
46+
for sym in sub_symbols.0.iter() {
47+
results.push(GotoSource {
48+
source: GotoSourceType::Symbol(sym.clone()),
49+
origin_selection_range: None, //TODO
50+
})
51+
}
52+
}
53+
}
54+
}
3555
}
3656
}
3757
h_start = end + 1;

server/src/features/declaration.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
use lsp_types::request::GotoDeclarationResponse;
2-
use lsp_types::{LocationLink, Range};
3-
use std::path::PathBuf;
42
use std::{cell::RefCell, rc::Rc};
53

6-
use crate::constants::SymType;
7-
use crate::core::file_mgr::{AstType, FileInfo, FileMgr};
4+
use crate::core::file_mgr::{AstType, FileInfo};
85
use crate::core::symbols::symbol::Symbol;
96
use crate::features::goto_utils::{GotoRequest, GotoUtils};
10-
use crate::features::xml_ast_utils::{XmlAstResult, XmlAstUtils};
117
use crate::threads::SessionInfo;
12-
use crate::utils::PathSanitizer as _;
138

149
pub struct DeclarationFeature {}
1510

server/src/features/definition.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
use lsp_types::{GotoDefinitionResponse, LocationLink, Range};
2-
use std::path::PathBuf;
1+
use lsp_types::{GotoDefinitionResponse};
32
use std::{cell::RefCell, rc::Rc};
43

5-
use crate::constants::SymType;
6-
use crate::core::file_mgr::{AstType, FileInfo, FileMgr};
4+
use crate::core::file_mgr::{AstType, FileInfo};
75
use crate::core::symbols::symbol::Symbol;
8-
use crate::features::csv_ast_utils::CsvAstUtils;
96
use crate::features::goto_utils::{GotoRequest, GotoUtils};
10-
use crate::features::xml_ast_utils::{XmlAstResult, XmlAstUtils};
117
use crate::threads::SessionInfo;
12-
use crate::utils::PathSanitizer as _;
138

149
pub struct DefinitionFeature {}
1510

server/src/features/goto_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{cell::RefCell, path::PathBuf, rc::Rc};
22

3-
use lsp_types::{LocationLink, Range, request::GotoDeclarationResponse};
3+
use lsp_types::{LocationLink, Range};
44
use ruff_python_ast::{Expr, ExprCall};
55

6-
use crate::{S, Sy, constants::{PackageType, SymType, OYarn}, core::{evaluation::{Evaluation, EvaluationValue, ExprOrIdent}, file_mgr::{FileInfo, FileMgr}, odoo::SyncOdoo, python_odoo_builder::MAGIC_FIELDS, symbols::symbol::Symbol, xml_data::{OdooData, OdooDataRecord}}, features::{ast_utils::AstUtils, csv_ast_utils::CsvAstUtils, features_utils::FeaturesUtils, xml_ast_utils::{XmlAstResult, XmlAstUtils}}, oyarn, threads::SessionInfo, utils::PathSanitizer};
6+
use crate::{S, Sy, constants::{PackageType, SymType, OYarn}, core::{evaluation::{Evaluation, EvaluationValue, ExprOrIdent}, file_mgr::{FileInfo, FileMgr}, odoo::SyncOdoo, python_odoo_builder::MAGIC_FIELDS, symbols::symbol::Symbol, xml_data::OdooData}, features::{ast_utils::AstUtils, csv_ast_utils::CsvAstUtils, features_utils::FeaturesUtils, xml_ast_utils::{XmlAstResult, XmlAstUtils}}, oyarn, threads::SessionInfo, utils::PathSanitizer};
77

88
pub enum GotoRequest {
99
Definition,
@@ -318,7 +318,7 @@ impl GotoUtils {
318318
let offset = file_info.borrow().position_to_offset(line, character, session.sync_odoo.encoding);
319319
let data = file_info.borrow().file_info_ast.borrow().text_document.as_ref().unwrap().contents().to_string();
320320
let mut csv_reader = csv::ReaderBuilder::new().quoting(false).from_reader(data.as_bytes());
321-
let sources = CsvAstUtils::get_symbols(session, file_symbol, &mut csv_reader, &model_name, offset, true);
321+
let sources = CsvAstUtils::get_symbols(session, file_symbol, &mut csv_reader, &model_name, offset);
322322
sources
323323
}
324324

server/src/features/references.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@ use std::rc::Weak;
22
use std::{cell::RefCell, path::PathBuf, rc::Rc};
33

44
use csv::StringRecord;
5-
use lsp_types::{Location, Range, Uri};
5+
use lsp_types::{Location, Uri};
66
use ruff_python_ast::{Alias, Expr, Identifier, Stmt, StmtAnnAssign, StmtAssert, StmtAssign, StmtAugAssign, StmtClassDef, StmtIf, StmtMatch, StmtRaise, StmtReturn, StmtTry, StmtTypeAlias, StmtWith};
77
use ruff_text_size::{Ranged, TextRange, TextSize};
8-
use tracing::{error, warn};
98
use weak_table::PtrWeakHashSet;
109

11-
use crate::core::csv_arch_builder::CsvArchBuilder;
1210
use crate::core::file_mgr::AstType;
1311
use crate::core::symbols::module_symbol::ModuleSymbol;
14-
use crate::core::xml_data::OdooDataRecord;
1512
use crate::features::csv_ast_utils::CsvAstUtils;
1613
use crate::features::references_xml::XmlAstReferenceVisitor;
1714
use crate::{S, Sy, oyarn};
1815
use crate::constants::OYarn;
1916
use crate::core::evaluation::{Evaluation, EvaluationSymbolPtr};
2017
use crate::core::odoo::SyncOdoo;
2118
use crate::features::goto_utils::{GotoRequest, GotoSourceType, GotoUtils};
22-
use crate::{constants::SymType, core::{file_mgr::{FileInfo, FileMgr}, symbols::symbol::Symbol}, features::xml_ast_utils::{XmlAstResult, XmlAstUtils}, threads::SessionInfo, utils::PathSanitizer};
19+
use crate::{constants::SymType, core::{file_mgr::{FileInfo, FileMgr}, symbols::symbol::Symbol}, threads::SessionInfo, utils::PathSanitizer};
2320

2421
#[derive(Debug, Clone)]
2522
pub enum ReferenceTarget {
@@ -178,11 +175,11 @@ impl ReferenceFeature {
178175
}
179176
},
180177
SymType::CSV_FILE => {
181-
let model_name_pb = PathBuf::from(&file_s.borrow().paths()[0]);
182-
let model_name = Sy!(model_name_pb.file_stem().unwrap().to_str().unwrap().to_string());
178+
//let model_name_pb = PathBuf::from(&file_s.borrow().paths()[0]);
179+
//let model_name = Sy!(model_name_pb.file_stem().unwrap().to_str().unwrap().to_string());
183180
let data = file_info.borrow().file_info_ast.borrow().text_document.as_ref().unwrap().contents().to_string();
184181
let mut csv_reader = csv::ReaderBuilder::new().quoting(false).from_reader(data.as_bytes());
185-
locations.extend(ReferenceFeature::reference_in_csv_file(session, &model_name, &file_s, &mut csv_reader, &ReferenceTarget::String(full_xml_id)));
182+
locations.extend(ReferenceFeature::reference_in_csv_file(session, &file_s, &mut csv_reader, &ReferenceTarget::String(full_xml_id)));
186183
},
187184
_ => {}
188185
}
@@ -199,7 +196,7 @@ impl ReferenceFeature {
199196
}
200197
}
201198

202-
fn reference_in_csv_file(session: &mut SessionInfo, model_name: &OYarn, file_symbol: &Rc<RefCell<Symbol>>, csv_reader: &mut csv::Reader<&[u8]>, reference_target: &ReferenceTarget) -> Vec<Location> {
199+
fn reference_in_csv_file(session: &mut SessionInfo, file_symbol: &Rc<RefCell<Symbol>>, csv_reader: &mut csv::Reader<&[u8]>, reference_target: &ReferenceTarget) -> Vec<Location> {
203200
let module = file_symbol.borrow().find_module();
204201
let path = file_symbol.borrow().paths()[0].clone();
205202
let uri = FileMgr::pathname2uri(&path);
@@ -215,15 +212,15 @@ impl ReferenceFeature {
215212
if !headers.is_empty() && headers[0] == "id" {
216213
for result in csv_reader.records() {
217214
if let Ok(result) = result {
218-
let in_record = ReferenceFeature::search_in_record(session, module.clone(), &uri, &path, model_name.clone(), &headers, &result, reference_target);
215+
let in_record = ReferenceFeature::search_in_record(session, module.clone(), &uri, &path, &headers, &result, reference_target);
219216
locations.extend(in_record);
220217
}
221218
}
222219
}
223220
locations
224221
}
225222

226-
fn search_in_record(session: &mut SessionInfo, module: Option<Rc<RefCell<Symbol>>>, uri: &Uri, path: &String, model_name: OYarn, headers: &Vec<OYarn>, record: &StringRecord, reference_target: &ReferenceTarget) -> Vec<Location> {
223+
fn search_in_record(session: &mut SessionInfo, module: Option<Rc<RefCell<Symbol>>>, uri: &Uri, path: &String, headers: &Vec<OYarn>, record: &StringRecord, reference_target: &ReferenceTarget) -> Vec<Location> {
227224
if record.position().is_none() {
228225
return vec![];
229226
}

server/tests/data/addons/module_csv/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
3+
{
4+
'name' : 'Module CSV',
5+
'version' : '1.0',
6+
'summary': 'Test Module CSV',
7+
'sequence': 10,
8+
'description': """
9+
Module CSV
10+
====================
11+
This is the description of the module CSV
12+
""",
13+
'category': 'Accounting/Accounting',
14+
'depends' : [],
15+
'data': [
16+
'data/res.country.state.csv',
17+
],
18+
'installable': True,
19+
'application': True,
20+
'license': 'LGPL-3',
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"id","country_id:id","name","code"
2+
state_au_1000,"base.au","Australian Capital Territories","ACTS"
3+
state_us_1000,base.us,"Alabamaa","ALA"

server/tests/data/addons/module_for_diagnostics/models/bike_parts_wheel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ def _compute_bike_weight(self):
2424
bike.bike_weight = bike.wheel_id.price * 0.5
2525
else:
2626
bike.bike_weight = 0.0
27+
"module_for_diagnostics.bike_wheel_1"
28+
self.env.ref("module_for_diagnostics.bike_wheel_1")
2729
self.env.ref('module_for_diagnostics.bike_wheel_DOES_NOT_EXIST') # TODO: OLS05001
2830
self.env.ref('bike_wheel_DOES_NOT_EXIST') # OLS05002
2931
self.env.ref('module_for_diagnostics.bike_wheel_6') # Ok
3032
self.env.ref('WRONG_MODULE.bike_wheel_6') # OLS05003
3133
self.env.ref('module_for_diagnostics.bike_wheel_6.too.many.dots') # OLS05051
34+
self.env.ref("module_for_diagnostics.bike_wheel_1")

0 commit comments

Comments
 (0)