@@ -9,7 +9,6 @@ use lsp_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Position, Range}
9
9
use tracing:: { debug, trace, warn} ;
10
10
11
11
use crate :: constants:: * ;
12
- use crate :: core:: import_resolver:: resolve_import_stmt;
13
12
use crate :: core:: odoo:: SyncOdoo ;
14
13
use crate :: core:: symbols:: symbol:: Symbol ;
15
14
use crate :: core:: evaluation:: Evaluation ;
@@ -22,9 +21,9 @@ use super::config::DiagMissingImportsMode;
22
21
use super :: entry_point:: EntryPoint ;
23
22
use super :: evaluation:: { ContextValue , EvaluationSymbolPtr , EvaluationSymbolWeak } ;
24
23
use super :: file_mgr:: FileMgr ;
25
- use super :: import_resolver:: ImportResult ;
26
24
use super :: python_arch_eval_hooks:: PythonArchEvalHooks ;
27
25
use super :: symbols:: function_symbol:: FunctionSymbol ;
26
+ use super :: symbols:: variable_symbol:: { ImportInformation , VariableSymbol } ;
28
27
29
28
30
29
#[ derive( Debug , Clone ) ]
@@ -179,122 +178,25 @@ impl PythonArchEval {
179
178
}
180
179
}
181
180
182
- fn _match_diag_config ( & self , odoo : & mut SyncOdoo , symbol : & Rc < RefCell < Symbol > > ) -> bool {
183
- let import_diag_level = & odoo. config . diag_missing_imports ;
184
- if * import_diag_level == DiagMissingImportsMode :: None {
185
- return false
186
- }
187
- if * import_diag_level == DiagMissingImportsMode :: All {
188
- return true
189
- }
190
- if * import_diag_level == DiagMissingImportsMode :: OnlyOdoo {
191
- let tree = symbol. borrow ( ) . get_tree ( ) ;
192
- if tree. 0 . len ( ) > 0 && tree. 0 [ 0 ] == "odoo" {
193
- return true ;
194
- }
195
- }
196
- false
197
- }
198
-
199
- ///Follow the evaluations of sym_ref, evaluate files if needed, and return true if the end evaluation contains from_sym
200
- fn check_for_loop_evaluation ( & mut self , session : & mut SessionInfo , sym_ref : Rc < RefCell < Symbol > > , from_sym : & Rc < RefCell < Symbol > > ) -> bool {
201
- let sym_ref_cl = sym_ref. clone ( ) ;
202
- let syms_followed = Symbol :: follow_ref ( & EvaluationSymbolPtr :: WEAK ( EvaluationSymbolWeak :: new (
203
- Rc :: downgrade ( & sym_ref_cl) , None , false
204
- ) ) , session, & mut None , false , false , None , & mut self . diagnostics ) ;
205
- for sym in syms_followed. iter ( ) {
206
- let sym = sym. upgrade_weak ( ) ;
207
- if let Some ( sym) = sym {
208
- if sym. borrow ( ) . evaluations ( ) . is_some ( ) && sym. borrow ( ) . evaluations ( ) . unwrap ( ) . is_empty ( ) {
209
- let file_sym = sym_ref. borrow ( ) . get_file ( ) ;
210
- if file_sym. is_some ( ) {
211
- let rc_file_sym = file_sym. as_ref ( ) . unwrap ( ) . upgrade ( ) . unwrap ( ) ;
212
- if rc_file_sym. borrow_mut ( ) . build_status ( BuildSteps :: ARCH_EVAL ) == BuildStatus :: PENDING && session. sync_odoo . is_in_rebuild ( & rc_file_sym, BuildSteps :: ARCH_EVAL ) {
213
- session. sync_odoo . remove_from_rebuild_arch_eval ( & rc_file_sym) ;
214
- let mut builder = PythonArchEval :: new ( self . entry_point . clone ( ) , rc_file_sym) ;
215
- builder. eval_arch ( session) ;
216
- if self . check_for_loop_evaluation ( session, sym_ref. clone ( ) , from_sym) {
217
- return true ;
218
- }
219
- }
220
- }
221
- }
222
- if Rc :: ptr_eq ( & sym, & from_sym) {
223
- return true ;
224
- }
225
- }
226
- }
227
- false
228
- }
229
-
230
181
fn eval_symbols_from_import_stmt ( & mut self , session : & mut SessionInfo , from_stmt : Option < & Identifier > , name_aliases : & [ Alias ] , level : Option < u32 > , range : & TextRange ) {
231
182
if name_aliases. len ( ) == 1 && name_aliases[ 0 ] . name . to_string ( ) == "*" {
232
183
return ;
233
184
}
234
- let import_results: Vec < ImportResult > = resolve_import_stmt (
235
- session,
236
- & self . file ,
237
- from_stmt,
238
- name_aliases,
239
- level,
240
- & mut Some ( & mut self . diagnostics ) ) ;
241
-
242
- for _import_result in import_results. iter ( ) {
243
- let variable = self . sym_stack . last ( ) . unwrap ( ) . borrow_mut ( ) . get_positioned_symbol ( & _import_result. name , & _import_result. range ) ;
185
+ for alias in name_aliases {
186
+ let var_name = alias. asname . as_ref ( ) . unwrap_or ( & alias. name ) . to_string ( ) . clone ( ) ;
187
+ let variable = self . sym_stack . last ( ) . unwrap ( ) . borrow_mut ( ) . get_positioned_symbol ( & var_name, & alias. range ) ;
244
188
let Some ( variable) = variable. clone ( ) else {
245
189
continue ;
246
190
} ;
247
- if _import_result. found {
248
- let import_sym_ref = _import_result. symbol . clone ( ) ;
249
- let has_loop = self . check_for_loop_evaluation ( session, import_sym_ref, & variable) ;
250
- if !has_loop { //anti-loop. We want to be sure we are not evaluating to the same sym
251
- variable. borrow_mut ( ) . set_evaluations ( vec ! [ Evaluation :: eval_from_symbol( & Rc :: downgrade( & _import_result. symbol) , None ) ] ) ;
252
- let file_of_import_symbol = _import_result. symbol . borrow ( ) . get_file ( ) ;
253
- if let Some ( import_file) = file_of_import_symbol {
254
- let import_file = import_file. upgrade ( ) . unwrap ( ) ;
255
- if !Rc :: ptr_eq (& self . file , & import_file) {
256
- self . file . borrow_mut ( ) . add_dependency ( & mut import_file. borrow_mut ( ) , self . current_step , BuildSteps :: ARCH ) ;
257
- }
258
- }
259
- } else {
260
- let mut file_tree = [ _import_result. file_tree . 0 . clone ( ) , _import_result. file_tree . 1 . clone ( ) ] . concat ( ) ;
261
- file_tree. extend ( _import_result. name . split ( "." ) . map ( str:: to_string) ) ;
262
- self . file . borrow_mut ( ) . not_found_paths_mut ( ) . push ( ( self . current_step , file_tree. clone ( ) ) ) ;
263
- self . entry_point . borrow_mut ( ) . not_found_symbols . insert ( self . file . clone ( ) ) ;
264
- if self . _match_diag_config ( session. sync_odoo , & _import_result. symbol ) {
265
- self . diagnostics . push ( Diagnostic :: new (
266
- Range :: new ( Position :: new ( _import_result. range . start ( ) . to_u32 ( ) , 0 ) , Position :: new ( _import_result. range . end ( ) . to_u32 ( ) , 0 ) ) ,
267
- Some ( DiagnosticSeverity :: WARNING ) ,
268
- Some ( NumberOrString :: String ( S ! ( "OLS20004" ) ) ) ,
269
- Some ( EXTENSION_NAME . to_string ( ) ) ,
270
- format ! ( "Failed to evaluate import {}" , file_tree. clone( ) . join( "." ) ) ,
271
- None ,
272
- None ,
273
- ) ) ;
274
- }
275
- }
276
-
277
- } else {
278
- let mut file_tree = [ _import_result. file_tree . 0 . clone ( ) , _import_result. file_tree . 1 . clone ( ) ] . concat ( ) ;
279
- file_tree. extend ( _import_result. name . split ( "." ) . map ( str:: to_string) ) ;
280
- if BUILT_IN_LIBS . contains ( & file_tree[ 0 ] . as_str ( ) ) {
281
- continue ;
282
- }
283
- if !self . safe_import . last ( ) . unwrap ( ) {
284
- self . file . borrow_mut ( ) . not_found_paths_mut ( ) . push ( ( self . current_step , file_tree. clone ( ) ) ) ;
285
- self . entry_point . borrow_mut ( ) . not_found_symbols . insert ( self . file . clone ( ) ) ;
286
- if self . _match_diag_config ( session. sync_odoo , & _import_result. symbol ) {
287
- self . diagnostics . push ( Diagnostic :: new (
288
- Range :: new ( Position :: new ( _import_result. range . start ( ) . to_u32 ( ) , 0 ) , Position :: new ( _import_result. range . end ( ) . to_u32 ( ) , 0 ) ) ,
289
- Some ( DiagnosticSeverity :: WARNING ) ,
290
- Some ( NumberOrString :: String ( S ! ( "OLS20001" ) ) ) ,
291
- Some ( EXTENSION_NAME . to_string ( ) ) ,
292
- format ! ( "{} not found" , file_tree. clone( ) . join( "." ) ) ,
293
- None ,
294
- None ,
295
- ) ) ;
296
- }
297
- }
191
+ variable. borrow_mut ( ) . as_variable_mut ( ) . import_information = Some ( ImportInformation {
192
+ from : from_stmt. cloned ( ) ,
193
+ level : level,
194
+ alias : alias. clone ( ) ,
195
+ import_step : self . current_step . clone ( )
196
+ } ) ;
197
+ //In workspace we evaluate imports as we want to raise diagnostics if not found. If outside of the workspace, let's keep only information to lazy load them
198
+ if self . file . borrow ( ) . in_workspace ( ) || !self . file . borrow ( ) . is_external ( ) {
199
+ VariableSymbol :: load_from_import_information ( session, variable, & self . file , & self . entry_point , & mut self . diagnostics ) ;
298
200
}
299
201
}
300
202
}
0 commit comments