Skip to content

Commit 8d9ea63

Browse files
committed
live-preview: Keep a document cache handy
1 parent f358454 commit 8d9ea63

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

tools/lsp/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl DocumentCache {
6363
}
6464

6565
pub fn snapshot(&self) -> Option<Self> {
66-
i_slint_compiler::typeloader::snapshot(&self.0).map(|tl| Self::new_from_type_loader(tl))
66+
i_slint_compiler::typeloader::snapshot(&self.0).map(Self::new_from_type_loader)
6767
}
6868

6969
pub fn resolve_import_path(

tools/lsp/preview.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::common::{
77
use crate::lsp_ext::Health;
88
use crate::preview::element_selection::ElementSelection;
99
use crate::util;
10+
use i_slint_compiler::diagnostics;
1011
use i_slint_compiler::object_tree::ElementRc;
1112
use i_slint_compiler::parser::{syntax_nodes, SyntaxKind};
1213
use i_slint_core::component_factory::FactoryContext;
@@ -51,9 +52,11 @@ enum PreviewFutureState {
5152
NeedsReload,
5253
}
5354

55+
type SourceCodeCache = HashMap<Url, (common::UrlVersion, String)>;
56+
5457
#[derive(Default)]
5558
struct ContentCache {
56-
source_code: HashMap<Url, (common::UrlVersion, String)>,
59+
source_code: SourceCodeCache,
5760
dependency: HashSet<Url>,
5861
current: Option<PreviewComponent>,
5962
config: PreviewConfig,
@@ -68,6 +71,7 @@ static CONTENT_CACHE: std::sync::OnceLock<Mutex<ContentCache>> = std::sync::Once
6871
struct PreviewState {
6972
ui: Option<ui::PreviewUi>,
7073
handle: Rc<RefCell<Option<slint_interpreter::ComponentInstance>>>,
74+
document_cache: Rc<RefCell<Option<Rc<common::DocumentCache>>>>,
7175
selected: Option<element_selection::ElementSelection>,
7276
notify_editor_about_selection_after_update: bool,
7377
known_components: Vec<ComponentInformation>,
@@ -572,7 +576,7 @@ async fn parse_source(
572576
) -> core::pin::Pin<
573577
Box<dyn core::future::Future<Output = Option<std::io::Result<String>>>>,
574578
> + 'static,
575-
) -> (Vec<i_slint_compiler::diagnostics::Diagnostic>, Option<ComponentDefinition>) {
579+
) -> (Vec<diagnostics::Diagnostic>, Option<ComponentDefinition>) {
576580
let mut builder = slint_interpreter::ComponentCompiler::default();
577581

578582
#[cfg(target_arch = "wasm32")]
@@ -818,17 +822,12 @@ fn component_instance() -> Option<ComponentInstance> {
818822
})
819823
}
820824

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+
})
832831
}
833832

834833
fn set_show_preview_ui(show_preview_ui: bool) {
@@ -902,12 +901,18 @@ fn update_preview_area(compiled: Option<ComponentDefinition>) {
902901

903902
let ui = preview_state.ui.as_ref().unwrap();
904903
let shared_handle = preview_state.handle.clone();
904+
let shared_document_cache = preview_state.document_cache.clone();
905905

906906
if let Some(compiled) = compiled {
907907
set_preview_factory(
908908
ui,
909909
compiled,
910910
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+
}
911916
shared_handle.replace(Some(instance));
912917
}),
913918
);

tools/lsp/preview/drop_location.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub fn drop_at(
757757
) -> Option<(lsp_types::WorkspaceEdit, DropData)> {
758758
let component_type = &component.name;
759759
let component_instance = preview::component_instance()?;
760-
let document_cache = preview::document_cache_from(&component_instance)?;
760+
let document_cache = preview::document_cache()?;
761761

762762
let drop_info = find_drop_location(&component_instance, position, component_type)?;
763763

@@ -989,7 +989,7 @@ pub fn create_move_element_workspace_edit(
989989

990990
let (path, _) = drop_info.target_element_node.path_and_offset();
991991

992-
let document_cache = preview::document_cache_from(component_instance)?;
992+
let document_cache = preview::document_cache()?;
993993
let doc = document_cache.get_document_by_path(&path)?;
994994
let source_file = doc.node.as_ref().unwrap().source_file.clone();
995995

0 commit comments

Comments
 (0)