Skip to content

Commit fe1856e

Browse files
committed
wip
1 parent 45c232e commit fe1856e

File tree

4 files changed

+364
-232
lines changed

4 files changed

+364
-232
lines changed

server/src/core/evaluation.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,13 @@ impl Evaluation {
10871087
if bases.len() != 1 {
10881088
return AnalyzeAstResult::from_only_diagnostics(diagnostics);
10891089
}
1090+
let parent_file_or_func = parent.clone().borrow().parent_file_or_function().as_ref().unwrap().upgrade().unwrap();
1091+
let is_in_validation = match parent_file_or_func.borrow().typ().clone() {
1092+
SymType::FILE | SymType::PACKAGE(_) | SymType::FUNCTION => {
1093+
parent_file_or_func.borrow().build_status(BuildSteps::VALIDATION) == BuildStatus::IN_PROGRESS
1094+
},
1095+
_ => {false}
1096+
};
10901097
let value = Evaluation::expr_to_str(session, &sub.slice, parent.clone(), max_infer, &mut diagnostics);
10911098
diagnostics.extend(value.1);
10921099
if let Some(value) = value.0 {
@@ -1105,6 +1112,7 @@ impl Evaluation {
11051112
context.as_mut().unwrap().insert(S!("args"), ContextValue::STRING(value));
11061113
let old_range = context.as_mut().unwrap().remove(&S!("range"));
11071114
context.as_mut().unwrap().insert(S!("range"), ContextValue::RANGE(sub.slice.range()));
1115+
context.as_mut().unwrap().insert(S!("is_in_validation"), ContextValue::BOOLEAN(is_in_validation));
11081116
let hook_result = hook(session, &get_item_eval.symbol, context, &mut diagnostics, Some(parent.clone()));
11091117
if let Some(hook_result) = hook_result {
11101118
match hook_result {
@@ -1119,6 +1127,7 @@ impl Evaluation {
11191127
}
11201128
}
11211129
context.as_mut().unwrap().remove(&S!("args"));
1130+
context.as_mut().unwrap().remove(&S!("is_in_validation"));
11221131
context.as_mut().unwrap().insert(S!("range"), old_range.unwrap());
11231132
}
11241133
}

server/src/core/python_arch_builder_hooks.rs

Lines changed: 139 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,162 @@
11
use std::path::PathBuf;
22
use std::rc::Rc;
33
use std::cell::RefCell;
4+
use once_cell::sync::Lazy;
45
use ruff_text_size::{TextRange, TextSize};
56
use tracing::warn;
7+
use crate::core::entry_point::EntryPoint;
68
use crate::core::symbols::symbol::Symbol;
79
use crate::threads::SessionInfo;
810
use crate::{Sy, S};
911
use crate::constants::OYarn;
1012

1113
use super::odoo::SyncOdoo;
1214

13-
pub struct PythonArchBuilderHooks {}
15+
type PythonArchClassHookFn = fn (session: &mut SessionInfo, entry: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>);
1416

15-
impl PythonArchBuilderHooks {
17+
pub struct PythonArchClassHook {
18+
pub odoo_entry: bool,
19+
pub trees: Vec<(OYarn, OYarn, (Vec<OYarn>, Vec<OYarn>))>,
20+
pub func: PythonArchClassHookFn
21+
}
1622

17-
pub fn on_class_def(session: &mut SessionInfo, symbol: Rc<RefCell<Symbol>>) {
18-
let mut sym = symbol.borrow_mut();
19-
let name = &sym.name();
20-
match name.as_str() {
21-
"BaseModel" => {
22-
if sym.get_main_entry_tree(session) == (vec![Sy!("odoo"), Sy!("models")], vec![Sy!("BaseModel")]) {
23-
// ----------- env ------------
24-
let env = sym.get_symbol(&(vec![], vec![Sy!("env")]), u32::MAX);
25-
if env.is_empty() {
26-
let mut range = sym.range().clone();
27-
let slots = sym.get_symbol(&(vec![], vec![Sy!("__slots__")]), u32::MAX);
28-
if slots.len() == 1 {
29-
if slots.len() == 1 {
30-
range = slots[0].borrow().range().clone();
31-
}
32-
}
33-
sym.add_new_variable(session, Sy!("env"), &range);
23+
#[allow(non_upper_case_globals)]
24+
static arch_class_hooks: Lazy<Vec<PythonArchClassHook>> = Lazy::new(|| {vec![
25+
PythonArchClassHook {
26+
odoo_entry: true,
27+
trees: vec![
28+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("models")], vec![Sy!("BaseModel")])),
29+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("models")], vec![Sy!("BaseModel")]))
30+
],
31+
func: |session: &mut SessionInfo, entry_point: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>| {
32+
// ----------- env ------------
33+
let env = symbol.borrow().get_symbol(&(vec![], vec![Sy!("env")]), u32::MAX);
34+
if env.is_empty() {
35+
let mut range = symbol.borrow().range().clone();
36+
let slots = symbol.borrow().get_symbol(&(vec![], vec![Sy!("__slots__")]), u32::MAX);
37+
if slots.len() == 1 {
38+
if slots.len() == 1 {
39+
range = slots[0].borrow().range().clone();
3440
}
3541
}
36-
},
37-
"Environment" => {
38-
if sym.get_main_entry_tree(session) == (vec![Sy!("odoo"), Sy!("api")], vec![Sy!("Environment")]) {
39-
let new_sym = sym.get_symbol(&(vec![], vec![Sy!("__new__")]), u32::MAX);
40-
let mut range = sym.range().clone();
41-
if new_sym.len() == 1 {
42-
range = new_sym[0].borrow().range().clone();
43-
}
44-
// ----------- env.cr ------------
45-
sym.add_new_variable(session, Sy!("cr"), &range);
46-
// ----------- env.uid ------------
47-
let uid_sym = sym.add_new_variable(session, Sy!("uid"), &range);
48-
uid_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("The current user id (for access rights checks)"));
49-
// ----------- env.context ------------
50-
let context_sym = sym.add_new_variable(session, Sy!("context"), &range);
51-
context_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("The current context"));
52-
// ----------- env.su ------------
53-
let su_sym = sym.add_new_variable(session, Sy!("su"), &range);
54-
su_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("whether in superuser mode"));
55-
// ----------- env.registry -----------
56-
let _ = sym.add_new_variable(session, Sy!("registry"), &range);
42+
symbol.borrow_mut().add_new_variable(session, Sy!("env"), &range);
43+
}
44+
}
45+
},
46+
PythonArchClassHook {
47+
odoo_entry: true,
48+
trees: vec![
49+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("api")], vec![Sy!("Environment")])),
50+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("environments")], vec![Sy!("Environment")]))
51+
],
52+
func: |session: &mut SessionInfo, entry_point: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>| {
53+
let new_sym = symbol.borrow().get_symbol(&(vec![], vec![Sy!("__new__")]), u32::MAX);
54+
let mut range = symbol.borrow().range().clone();
55+
if new_sym.len() == 1 {
56+
range = new_sym[0].borrow().range().clone();
57+
}
58+
// ----------- env.cr ------------
59+
symbol.borrow_mut().add_new_variable(session, Sy!("cr"), &range);
60+
// ----------- env.uid ------------
61+
let uid_sym = symbol.borrow_mut().add_new_variable(session, Sy!("uid"), &range);
62+
uid_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("The current user id (for access rights checks)"));
63+
// ----------- env.context ------------
64+
let context_sym = symbol.borrow_mut().add_new_variable(session, Sy!("context"), &range);
65+
context_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("The current context"));
66+
// ----------- env.su ------------
67+
let su_sym = symbol.borrow_mut().add_new_variable(session, Sy!("su"), &range);
68+
su_sym.borrow_mut().as_variable_mut().doc_string = Some(S!("whether in superuser mode"));
69+
// ----------- env.registry -----------
70+
let _ = symbol.borrow_mut().add_new_variable(session, Sy!("registry"), &range);
71+
}
72+
},
73+
PythonArchClassHook {
74+
odoo_entry: true,
75+
trees: vec![
76+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Boolean")])),
77+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Integer")])),
78+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Float")])),
79+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Monetary")])),
80+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Char")])),
81+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Text")])),
82+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Html")])),
83+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Date")])),
84+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Datetime")])),
85+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Binary")])),
86+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Image")])),
87+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Selection")])),
88+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Reference")])),
89+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Many2one")])),
90+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Many2oneReference")])),
91+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Json")])),
92+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Properties")])),
93+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("PropertiesDefinition")])),
94+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("One2many")])),
95+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Many2many")])),
96+
(Sy!("0.0"), Sy!("18.0"), (vec![Sy!("odoo"), Sy!("fields")], vec![Sy!("Id")])),
97+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_misc")], vec![Sy!("Boolean")])),
98+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_numeric")], vec![Sy!("Integer")])),
99+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_numeric")], vec![Sy!("Float")])),
100+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_numeric")], vec![Sy!("Monetary")])),
101+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_textual")], vec![Sy!("Char")])),
102+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_textual")], vec![Sy!("Text")])),
103+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_textual")], vec![Sy!("Html")])),
104+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_temporal")], vec![Sy!("Date")])),
105+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_temporal")], vec![Sy!("Datetime")])),
106+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_binary")], vec![Sy!("Binary")])),
107+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_binary")], vec![Sy!("Image")])),
108+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_selection")], vec![Sy!("Selection")])),
109+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_reference")], vec![Sy!("Reference")])),
110+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_relational")], vec![Sy!("Many2one")])),
111+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_relational")], vec![Sy!("Many2oneReference")])),
112+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_misc")], vec![Sy!("Json")])),
113+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_properties")], vec![Sy!("Properties")])),
114+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_properties")], vec![Sy!("PropertiesDefinition")])),
115+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_relational")], vec![Sy!("One2many")])),
116+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_relational")], vec![Sy!("Many2many")])),
117+
(Sy!("18.1"), Sy!("999.0"), (vec![Sy!("odoo"), Sy!("orm"), Sy!("fields_misc")], vec![Sy!("Id")])),
118+
],
119+
func: |session: &mut SessionInfo, entry_point: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>| {
120+
// ----------- __get__ ------------
121+
let get_sym = symbol.borrow().get_symbol(&(vec![], vec![Sy!("__get__")]), u32::MAX);
122+
if get_sym.is_empty() {
123+
let range = symbol.borrow().range().clone();
124+
symbol.borrow_mut().add_new_function(session, &S!("__get__"), &range, &range.end());
125+
} else {
126+
if !["Id", "One2many"].contains(&symbol.borrow().name().as_str()){
127+
warn!("Found __get__ function for field of name ({})", symbol.borrow().name());
57128
}
58-
},
59-
"Boolean" | "Integer" | "Float" | "Monetary" | "Char" | "Text" | "Html" | "Date" | "Datetime" |
60-
"Binary" | "Image" | "Selection" | "Reference" | "Many2one" | "Many2oneReference" | "Json" |
61-
"Properties" | "PropertiesDefinition" | "One2many" | "Many2many" | "Id" => {
62-
if sym.get_main_entry_tree(session).0 == vec![Sy!("odoo"), Sy!("fields")] {
63-
// ----------- __get__ ------------
64-
let get_sym = sym.get_symbol(&(vec![], vec![Sy!("__get__")]), u32::MAX);
65-
if get_sym.is_empty() {
66-
let range = sym.range().clone();
67-
sym.add_new_function(session, &S!("__get__"), &range, &range.end());
68-
} else {
69-
if !["Id", "One2many"].contains(&name.as_str()){
70-
warn!("Found __get__ function for field of name ({})", name);
71-
}
72-
}
73-
// ----------- __init__ ------------
74-
let get_sym = sym.get_symbol(&(vec![], vec![Sy!("__init__")]), u32::MAX);
75-
if get_sym.is_empty() {
76-
let range = sym.range().clone();
77-
sym.add_new_function(session, &S!("__init__"), &range, &range.end());
129+
}
130+
// ----------- __init__ ------------
131+
let get_sym = symbol.borrow().get_symbol(&(vec![], vec![Sy!("__init__")]), u32::MAX);
132+
if get_sym.is_empty() {
133+
let range = symbol.borrow().range().clone();
134+
symbol.borrow_mut().add_new_function(session, &S!("__init__"), &range, &range.end());
135+
}
136+
}
137+
},
138+
]});
139+
140+
pub struct PythonArchBuilderHooks {}
141+
142+
impl PythonArchBuilderHooks {
143+
144+
pub fn on_class_def(session: &mut SessionInfo, entry_point: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>) {
145+
let tree = symbol.borrow().get_tree();
146+
let odoo_tree = symbol.borrow().get_main_entry_tree(session);
147+
let name = symbol.borrow().name().clone();
148+
for hook in arch_class_hooks.iter() {
149+
for hook_tree in hook.trees.iter() {
150+
if hook_tree.0 >= session.sync_odoo.full_version ||
151+
hook_tree.1 <= session.sync_odoo.full_version {
152+
continue; //skip if version not in range
153+
}
154+
if name.eq(hook_tree.2.1.last().unwrap()) {
155+
if (hook.odoo_entry && session.sync_odoo.has_main_entry && odoo_tree == hook_tree.2) || (!hook.odoo_entry && tree == hook_tree.2) {
156+
(hook.func)(session, entry_point, symbol.clone());
78157
}
79158
}
80159
}
81-
_ => {}
82160
}
83161
}
84162

0 commit comments

Comments
 (0)