@@ -7,6 +7,7 @@ use crate::common::{
7
7
use crate :: lsp_ext:: Health ;
8
8
use crate :: preview:: element_selection:: ElementSelection ;
9
9
use crate :: util;
10
+ use i_slint_compiler:: diagnostics;
10
11
use i_slint_compiler:: object_tree:: ElementRc ;
11
12
use i_slint_compiler:: parser:: { syntax_nodes, SyntaxKind } ;
12
13
use i_slint_core:: component_factory:: FactoryContext ;
@@ -51,9 +52,11 @@ enum PreviewFutureState {
51
52
NeedsReload ,
52
53
}
53
54
55
+ type SourceCodeCache = HashMap < Url , ( common:: UrlVersion , String ) > ;
56
+
54
57
#[ derive( Default ) ]
55
58
struct ContentCache {
56
- source_code : HashMap < Url , ( common :: UrlVersion , String ) > ,
59
+ source_code : SourceCodeCache ,
57
60
dependency : HashSet < Url > ,
58
61
current : Option < PreviewComponent > ,
59
62
config : PreviewConfig ,
@@ -68,6 +71,7 @@ static CONTENT_CACHE: std::sync::OnceLock<Mutex<ContentCache>> = std::sync::Once
68
71
struct PreviewState {
69
72
ui : Option < ui:: PreviewUi > ,
70
73
handle : Rc < RefCell < Option < slint_interpreter:: ComponentInstance > > > ,
74
+ document_cache : Rc < RefCell < Option < Rc < common:: DocumentCache > > > > ,
71
75
selected : Option < element_selection:: ElementSelection > ,
72
76
notify_editor_about_selection_after_update : bool ,
73
77
known_components : Vec < ComponentInformation > ,
@@ -572,7 +576,7 @@ async fn parse_source(
572
576
) -> core:: pin:: Pin <
573
577
Box < dyn core:: future:: Future < Output = Option < std:: io:: Result < String > > > > ,
574
578
> + ' static ,
575
- ) -> ( Vec < i_slint_compiler :: diagnostics:: Diagnostic > , Option < ComponentDefinition > ) {
579
+ ) -> ( Vec < diagnostics:: Diagnostic > , Option < ComponentDefinition > ) {
576
580
let mut builder = slint_interpreter:: ComponentCompiler :: default ( ) ;
577
581
578
582
#[ cfg( target_arch = "wasm32" ) ]
@@ -818,17 +822,12 @@ fn component_instance() -> Option<ComponentInstance> {
818
822
} )
819
823
}
820
824
821
- fn document_cache ( ) -> Option < common:: DocumentCache > {
822
- let component_instance = component_instance ( ) ?;
823
- Some ( common:: DocumentCache :: new_from_type_loader (
824
- component_instance. definition ( ) . raw_type_loader ( ) ?,
825
- ) )
826
- }
827
-
828
- fn document_cache_from ( component_instance : & ComponentInstance ) -> Option < common:: DocumentCache > {
829
- Some ( common:: DocumentCache :: new_from_type_loader (
830
- component_instance. definition ( ) . raw_type_loader ( ) ?,
831
- ) )
825
+ /// This is a *read-only* snapshot of the raw type loader, use this when you
826
+ /// need to know the exact state the compiled resources were in.
827
+ fn document_cache ( ) -> Option < Rc < common:: DocumentCache > > {
828
+ PREVIEW_STATE . with ( move |preview_state| {
829
+ preview_state. borrow ( ) . document_cache . borrow ( ) . as_ref ( ) . map ( |dc| dc. clone ( ) )
830
+ } )
832
831
}
833
832
834
833
fn set_show_preview_ui ( show_preview_ui : bool ) {
@@ -902,12 +901,18 @@ fn update_preview_area(compiled: Option<ComponentDefinition>) {
902
901
903
902
let ui = preview_state. ui . as_ref ( ) . unwrap ( ) ;
904
903
let shared_handle = preview_state. handle . clone ( ) ;
904
+ let shared_document_cache = preview_state. document_cache . clone ( ) ;
905
905
906
906
if let Some ( compiled) = compiled {
907
907
set_preview_factory (
908
908
ui,
909
909
compiled,
910
910
Box :: new ( move |instance| {
911
+ if let Some ( rtl) = instance. definition ( ) . raw_type_loader ( ) {
912
+ shared_document_cache. replace ( Some ( Rc :: new (
913
+ common:: DocumentCache :: new_from_type_loader ( rtl) ,
914
+ ) ) ) ;
915
+ }
911
916
shared_handle. replace ( Some ( instance) ) ;
912
917
} ) ,
913
918
) ;
0 commit comments