1
1
use std:: cmp:: Ordering ;
2
2
use std:: collections:: HashMap ;
3
- use std:: ops:: Index ;
4
- use std:: path:: PathBuf ;
5
3
use std:: rc:: Rc ;
6
4
use std:: rc:: Weak ;
7
5
use std:: cell:: RefCell ;
8
6
use lsp_types:: Diagnostic ;
9
- use lsp_types:: DiagnosticSeverity ;
10
- use lsp_types:: NumberOrString ;
11
7
use once_cell:: sync:: Lazy ;
12
8
use ruff_python_ast:: Arguments ;
13
9
use ruff_python_ast:: Expr ;
@@ -23,16 +19,13 @@ use crate::constants::*;
23
19
use crate :: oyarn;
24
20
use crate :: threads:: SessionInfo ;
25
21
use crate :: utils:: compare_semver;
26
- use crate :: utils:: is_file_cs;
27
- use crate :: utils:: PathSanitizer ;
28
22
use crate :: Sy ;
29
23
use crate :: S ;
30
24
31
25
use super :: entry_point:: EntryPoint ;
32
26
use super :: evaluation:: { ContextValue , Evaluation , EvaluationSymbolPtr , EvaluationSymbol , EvaluationSymbolWeak } ;
33
27
use super :: file_mgr:: FileMgr ;
34
28
use super :: python_arch_eval:: PythonArchEval ;
35
- use super :: symbols:: module_symbol:: ModuleSymbol ;
36
29
37
30
type PythonArchEvalHookFile = fn ( odoo : & mut SessionInfo , entry : & Rc < RefCell < EntryPoint > > , file_symbol : Rc < RefCell < Symbol > > , symbol : Rc < RefCell < Symbol > > ) ;
38
31
@@ -704,100 +697,86 @@ impl PythonArchEvalHooks {
704
697
diagnostics
705
698
}
706
699
707
- pub fn eval_env_get_item ( session : & mut SessionInfo , evaluation_sym : & EvaluationSymbol , context : & mut Option < Context > , diagnostics : & mut Vec < Diagnostic > , scope : Option < Rc < RefCell < Symbol > > > ) -> Option < EvaluationSymbolPtr >
700
+ pub fn eval_env_get_item ( session : & mut SessionInfo , _evaluation_sym : & EvaluationSymbol , context : & mut Option < Context > , diagnostics : & mut Vec < Diagnostic > , scope : Option < Rc < RefCell < Symbol > > > ) -> Option < EvaluationSymbolPtr >
708
701
{
709
- if let Some ( context) = context {
710
- let in_validation = context. get ( & S ! ( "is_in_validation" ) ) . unwrap_or ( & ContextValue :: BOOLEAN ( false ) ) . as_bool ( ) ;
711
- let arg = context. get ( & S ! ( "args" ) ) ;
712
- if let Some ( arg) = arg {
713
- match arg {
714
- ContextValue :: STRING ( s) => {
715
- let model = session. sync_odoo . models . get ( & oyarn ! ( "{}" , s) ) ;
716
- let mut has_class_in_parents = false ;
717
- if let Some ( scope) = scope. as_ref ( ) {
718
- has_class_in_parents = scope. borrow ( ) . get_in_parents ( & vec ! [ SymType :: CLASS ] , true ) . is_some ( ) ;
702
+ let res = Some ( EvaluationSymbolPtr :: WEAK ( EvaluationSymbolWeak :: new ( Weak :: new ( ) , Some ( true ) , false ) ) ) ;
703
+ let Some ( context) = context else {
704
+ return res
705
+ } ;
706
+ let in_validation = context. get ( & S ! ( "is_in_validation" ) ) . unwrap_or ( & ContextValue :: BOOLEAN ( false ) ) . as_bool ( ) ;
707
+ let Some ( ContextValue :: STRING ( s) ) = context. get ( & S ! ( "args" ) ) else {
708
+ return res
709
+ } ;
710
+ let maybe_model = session. sync_odoo . models . get ( & oyarn ! ( "{}" , s) ) ;
711
+ let has_class_in_parents = scope. as_ref ( ) . map ( |scope| scope. borrow ( ) . get_in_parents ( & vec ! [ SymType :: CLASS ] , true ) . is_some ( ) ) . unwrap_or ( false ) ;
712
+ if maybe_model. map ( |m| m. borrow_mut ( ) . has_symbols ( ) ) . unwrap_or ( false ) {
713
+ let Some ( model) = maybe_model else { unreachable ! ( ) } ;
714
+ let module = context. get ( & S ! ( "module" ) ) ;
715
+ let from_module = if let Some ( ContextValue :: MODULE ( m) ) = module {
716
+ m. upgrade ( ) . clone ( )
717
+ } else {
718
+ None
719
+ } ;
720
+ if let Some ( scope) = scope {
721
+ let mut f = scope. borrow_mut ( ) ;
722
+ f. add_model_dependencies ( model) ;
723
+ }
724
+ let model = model. clone ( ) ;
725
+ let model = model. borrow ( ) ;
726
+ let symbols = model. get_main_symbols ( session, from_module. clone ( ) ) ;
727
+ if let Some ( first_symbol) = symbols. first ( ) {
728
+ return Some ( EvaluationSymbolPtr :: WEAK ( EvaluationSymbolWeak :: new ( Rc :: downgrade ( first_symbol) , Some ( true ) , false ) ) ) ;
729
+ }
730
+ if in_validation && has_class_in_parents { //we don't want to show error for functions outside of a model body
731
+ if from_module. is_some ( ) {
732
+ //retry without from_module to see if model exists elsewhere
733
+ let symbols = model. get_main_symbols ( session, None ) ;
734
+ if symbols. is_empty ( ) {
735
+ // Model exists, but has no main symbols
736
+ if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03005 , & [ ] ) { // Is this error code correct?
737
+ diagnostics. push ( Diagnostic {
738
+ range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
739
+ ..diagnostic_base. clone ( )
740
+ } ) ;
719
741
}
720
- if let Some ( model) = model {
721
- let module = context. get ( & S ! ( "module" ) ) ;
722
- let from_module;
723
- if let Some ( ContextValue :: MODULE ( m) ) = module {
724
- if let Some ( m) = m. upgrade ( ) {
725
- from_module = Some ( m. clone ( ) ) ;
726
- } else {
727
- from_module = None ;
728
- }
729
- } else {
730
- from_module = None ;
731
- }
732
- if let Some ( scope) = scope {
733
- let mut f = scope. borrow_mut ( ) ;
734
- f. add_model_dependencies ( model) ;
735
- }
736
- let model = model. clone ( ) ;
737
- let model = model. borrow ( ) ;
738
- let symbols = model. get_main_symbols ( session, from_module. clone ( ) ) ;
739
- if !symbols. is_empty ( ) {
740
- for s in symbols. iter ( ) {
741
- if from_module. is_none ( ) || ModuleSymbol :: is_in_deps ( session, & from_module. as_ref ( ) . unwrap ( ) , & s. borrow ( ) . find_module ( ) . unwrap ( ) . borrow ( ) . as_module_package ( ) . dir_name ) {
742
- return Some ( EvaluationSymbolPtr :: WEAK ( EvaluationSymbolWeak :: new ( Rc :: downgrade ( s) , Some ( true ) , false ) ) ) ;
743
- }
744
- }
745
- } else {
746
- if from_module. is_some ( ) && has_class_in_parents { //we don't want to show error for functions outside of a model body
747
- //retry without from_module to see if model exists elsewhere
748
- let symbols = model. get_main_symbols ( session, None ) ;
749
- if symbols. is_empty ( ) {
750
- if in_validation {
751
- if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03005 , & [ ] ) {
752
- diagnostics. push ( Diagnostic {
753
- range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
754
- ..diagnostic_base. clone ( )
755
- } ) ;
756
- }
757
- }
758
- } else {
759
- if in_validation {
760
- let valid_modules: Vec < OYarn > = symbols. iter ( ) . map ( |s| match s. borrow ( ) . find_module ( ) {
761
- Some ( sym) => sym. borrow ( ) . name ( ) . clone ( ) ,
762
- None => Sy ! ( "Unknown" ) . clone ( )
763
- } ) . collect ( ) ;
764
- if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03001 , & [ & format ! ( "{:?}" , valid_modules) ] ) {
765
- diagnostics. push ( Diagnostic {
766
- range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
767
- ..diagnostic_base. clone ( )
768
- } ) ;
769
- }
770
- }
771
- }
772
- } else if has_class_in_parents {
773
- if in_validation {
774
- if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03002 , & [ ] ) {
775
- diagnostics. push ( Diagnostic {
776
- range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
777
- ..diagnostic_base
778
- } ) ;
779
- } ;
780
- }
781
- }
782
- }
783
- } else if has_class_in_parents {
784
- if in_validation {
785
- if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03002 , & [ ] ) {
786
- diagnostics. push ( Diagnostic {
787
- range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
788
- ..diagnostic_base
789
- } ) ;
790
- } ;
791
- }
742
+ } else {
743
+ // Model exists but not in dependencies
744
+ let valid_modules: Vec < OYarn > = symbols. iter ( ) . map ( |s| match s. borrow ( ) . find_module ( ) {
745
+ Some ( sym) => sym. borrow ( ) . name ( ) . clone ( ) ,
746
+ None => Sy ! ( "Unknown" ) . clone ( )
747
+ } ) . collect ( ) ;
748
+ if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03001 , & [ & format ! ( "{:?}" , valid_modules) ] ) {
749
+ diagnostics. push ( Diagnostic {
750
+ range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
751
+ ..diagnostic_base. clone ( )
752
+ } ) ;
792
753
}
793
754
}
794
- _ => {
795
- //NOT A STRING
755
+ } else {
756
+ // Model exists, but has no main symbols
757
+ if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03005 , & [ ] ) {
758
+ diagnostics. push ( Diagnostic {
759
+ range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
760
+ ..diagnostic_base
761
+ } ) ;
796
762
}
797
763
}
798
764
}
765
+ } else if in_validation && has_class_in_parents {
766
+ // Model Unknown
767
+ if let Some ( diagnostic_base) = create_diagnostic ( & session, DiagnosticCode :: OLS03002 , & [ ] ) {
768
+ diagnostics. push ( Diagnostic {
769
+ range : FileMgr :: textRange_to_temporary_Range ( & context. get ( & S ! ( "range" ) ) . unwrap ( ) . as_text_range ( ) ) ,
770
+ ..diagnostic_base
771
+ } ) ;
772
+ }
773
+ let Some ( file_symbol) = scope. and_then ( |scope| scope. borrow ( ) . get_file ( ) ) . and_then ( |file| file. upgrade ( ) ) else {
774
+ return res
775
+ } ;
776
+ file_symbol. borrow_mut ( ) . as_file_mut ( ) . not_found_models . insert ( Sy ! ( s. clone( ) ) , BuildSteps :: VALIDATION ) ;
777
+ session. sync_odoo . get_main_entry ( ) . borrow_mut ( ) . not_found_symbols_for_models . insert ( file_symbol. clone ( ) ) ;
799
778
}
800
- Some ( EvaluationSymbolPtr :: WEAK ( EvaluationSymbolWeak :: new ( Weak :: new ( ) , Some ( true ) , false ) ) )
779
+ res
801
780
}
802
781
803
782
pub fn eval_registry_get_item ( session : & mut SessionInfo , evaluation_sym : & EvaluationSymbol , context : & mut Option < Context > , diagnostics : & mut Vec < Diagnostic > , scope : Option < Rc < RefCell < Symbol > > > ) -> Option < EvaluationSymbolPtr >
0 commit comments