@@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, hash::Hash, path::PathBuf, rc::Rc
3
3
use lsp_types:: { Diagnostic , Position , Range } ;
4
4
use tracing:: { info, trace} ;
5
5
6
- use crate :: { constants:: { BuildSteps , SymType , DEBUG_STEPS , EXTENSION_NAME } , core:: { entry_point:: { EntryPoint , EntryPointType } , evaluation:: ContextValue , file_mgr:: FileInfo , model:: Model , odoo:: SyncOdoo , symbols:: symbol:: Symbol , xml_data:: { XmlData , XmlDataActWindow , XmlDataDelete , XmlDataMenuItem , XmlDataRecord , XmlDataReport , XmlDataTemplate } } , threads:: SessionInfo , S } ;
6
+ use crate :: { constants:: { BuildSteps , OYarn , SymType , DEBUG_STEPS , EXTENSION_NAME } , core:: { entry_point:: { EntryPoint , EntryPointType } , evaluation:: ContextValue , file_mgr:: FileInfo , model:: Model , odoo:: SyncOdoo , symbols:: symbol:: Symbol , xml_data:: { XmlData , XmlDataActWindow , XmlDataDelete , XmlDataMenuItem , XmlDataRecord , XmlDataReport , XmlDataTemplate } } , threads:: SessionInfo , Sy , S } ;
7
7
8
8
9
9
@@ -89,33 +89,65 @@ impl XmlValidator {
89
89
dependencies. push ( main_sym. borrow ( ) . get_file ( ) . unwrap ( ) . upgrade ( ) . unwrap ( ) ) ;
90
90
}
91
91
let all_fields = Symbol :: all_fields ( & main_symbols[ 0 ] , session, Some ( module. clone ( ) ) ) ;
92
+ self . validate_fields ( session, xml_data_record, & all_fields, diagnostics) ;
93
+ }
94
+
95
+ fn validate_fields ( & self , session : & mut SessionInfo , xml_data_record : & XmlDataRecord , all_fields : & HashMap < OYarn , Vec < ( Rc < RefCell < Symbol > > , Option < OYarn > ) > > , diagnostics : & mut Vec < Diagnostic > ) {
96
+ //Compute mandatory fields
92
97
let mut mandatory_fields: Vec < String > = vec ! [ ] ;
93
- // for (field_name, field_sym) in all_fields.iter() {
94
- // for (fs, deps) in field_sym.iter() {
95
- // if deps.is_none() {
96
- // let has_required = fs.borrow().evaluations().unwrap_or(&vec![]).iter()
97
- // .any(|eval|
98
- // eval.symbol.get_symbol_as_weak(session, &mut None, diagnostics, None)
99
- // .context.get("required").unwrap_or(&ContextValue::BOOLEAN(false)).as_bool()
100
- // );
101
- // let has_default = fs.borrow().evaluations().unwrap_or(&vec![]).iter()
102
- // .any(|eval|
103
- // eval.symbol.get_symbol_as_weak(session, &mut None, diagnostics, None)
104
- // .context.contains_key("default")
105
- // );
106
- // if has_required && !has_default {
107
- // mandatory_fields.push(field_name.clone());
108
- // }
109
- // }
110
- // }
111
- // }
98
+ for ( field_name, field_sym) in all_fields. iter ( ) {
99
+ for ( fs, deps) in field_sym. iter ( ) {
100
+ if deps. is_none ( ) {
101
+ let has_required = fs. borrow ( ) . evaluations ( ) . unwrap_or ( & vec ! [ ] ) . iter ( )
102
+ . any ( |eval|
103
+ eval. symbol . get_symbol_as_weak ( session, & mut None , diagnostics, None )
104
+ . context . get ( "required" ) . unwrap_or ( & ContextValue :: BOOLEAN ( false ) ) . as_bool ( )
105
+ ) ;
106
+ let has_default = fs. borrow ( ) . evaluations ( ) . unwrap_or ( & vec ! [ ] ) . iter ( )
107
+ . any ( |eval|
108
+ eval. symbol . get_symbol_as_weak ( session, & mut None , diagnostics, None )
109
+ . context . contains_key ( "default" )
110
+ ) ;
111
+ if has_required && !has_default {
112
+ mandatory_fields. push ( field_name. to_string ( ) ) ;
113
+ }
114
+ }
115
+ }
116
+ }
117
+ //check each field in the record
112
118
for field in & xml_data_record. fields {
119
+ //Check that the field belong to the model
113
120
let declared_field = all_fields. get ( & field. name ) ;
114
- if let Some ( declared_field ) = declared_field {
121
+ if let Some ( _declared_field ) = declared_field {
115
122
mandatory_fields. retain ( |f| f != & field. name . as_str ( ) ) ;
116
- //TODO Check type
123
+ //Check specific attributes
124
+ match xml_data_record. model . 0 . as_str ( ) {
125
+ "ir.ui.view" => {
126
+ if field. name == "model" && field. text . is_some ( ) && field. text_range . is_some ( ) {
127
+ //TODO text that field.text is a valid model
128
+ let model = session. sync_odoo . models . get ( & Sy ! ( field. text. as_ref( ) . unwrap( ) ) ) . cloned ( ) ;
129
+ let mut main_sym = vec ! [ ] ;
130
+ if let Some ( model) = model {
131
+ let from_module = self . xml_symbol . borrow ( ) . find_module ( ) ;
132
+ main_sym = model. borrow ( ) . get_main_symbols ( session, from_module) ;
133
+ }
134
+ if main_sym. is_empty ( ) {
135
+ diagnostics. push ( Diagnostic :: new (
136
+ Range :: new ( Position :: new ( field. text_range . as_ref ( ) . unwrap ( ) . start . try_into ( ) . unwrap ( ) , 0 ) , Position :: new ( field. text_range . as_ref ( ) . unwrap ( ) . end . try_into ( ) . unwrap ( ) , 0 ) ) ,
137
+ Some ( lsp_types:: DiagnosticSeverity :: ERROR ) ,
138
+ Some ( lsp_types:: NumberOrString :: String ( S ! ( "OLS30453" ) ) ) ,
139
+ Some ( EXTENSION_NAME . to_string ( ) ) ,
140
+ format ! ( "Model '{}' not found" , field. text. as_ref( ) . unwrap( ) ) ,
141
+ None ,
142
+ None
143
+ ) )
144
+ }
145
+ }
146
+ } ,
147
+ _ => { }
148
+ }
149
+ //TODO check type
117
150
} else {
118
-
119
151
diagnostics. push ( Diagnostic :: new (
120
152
Range :: new ( Position :: new ( field. range . start . try_into ( ) . unwrap ( ) , 0 ) , Position :: new ( field. range . end . try_into ( ) . unwrap ( ) , 0 ) ) ,
121
153
Some ( lsp_types:: DiagnosticSeverity :: ERROR ) ,
@@ -127,16 +159,18 @@ impl XmlValidator {
127
159
) ) ;
128
160
}
129
161
}
130
- // if mandatory_fields.len() > 0 {
131
- // diagnostics.push(Diagnostic::new(
132
- // Range::new(Position::new(xml_data_record.range.start.try_into().unwrap(), 0), Position::new(xml_data_record.range.end.try_into().unwrap(), 0)),
133
- // Some(lsp_types::DiagnosticSeverity::ERROR),
134
- // Some(lsp_types::NumberOrString::String(S!("OLS30452"))),
135
- // Some(EXTENSION_NAME.to_string()),
136
- // format!("Some mandatory fields are not declared in the record: {:?}", mandatory_fields),
137
- // None,
138
- // None
139
- // ));
162
+ //Diagnostic if some mandatory fields are not detected
163
+ // if !mandatory_fields.is_empty() {
164
+ // We have to check that remaining fields are not declared in an inherited record or is automatically field (delegate=True)
165
+ // diagnostics.push(Diagnostic::new(
166
+ // Range::new(Position::new(xml_data_record.range.start.try_into().unwrap(), 0), Position::new(xml_data_record.range.end.try_into().unwrap(), 0)),
167
+ // Some(lsp_types::DiagnosticSeverity::ERROR),
168
+ // Some(lsp_types::NumberOrString::String(S!("OLS30452"))),
169
+ // Some(EXTENSION_NAME.to_string()),
170
+ // format!("Some mandatory fields are not declared in the record: {:?}", mandatory_fields),
171
+ // None,
172
+ // None
173
+ // ));
140
174
// }
141
175
}
142
176
0 commit comments