From 6d81096ac61ae16cc3faebf5ab5a7a3f761fe7fb Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 23 Aug 2024 12:04:27 +0000 Subject: [PATCH] live-preview: Use PathBuf in DeclarationInformation --- tools/lsp/preview/properties.rs | 13 ++++++------- tools/lsp/preview/ui.rs | 11 +++-------- tools/lsp/preview/wasm.rs | 2 -- tools/lsp/ui/api.slint | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/tools/lsp/preview/properties.rs b/tools/lsp/preview/properties.rs index 7e662629516..ce70a681fb8 100644 --- a/tools/lsp/preview/properties.rs +++ b/tools/lsp/preview/properties.rs @@ -15,6 +15,7 @@ use i_slint_compiler::parser::{syntax_nodes, Language, SyntaxKind, TextRange, Te use lsp_types::Url; use std::collections::HashSet; +use std::path::PathBuf; #[derive(Clone, Debug, PartialEq)] pub struct DefinitionInformation { @@ -26,7 +27,7 @@ pub struct DefinitionInformation { #[derive(Clone, Debug, PartialEq)] pub struct DeclarationInformation { - pub uri: Url, + pub path: PathBuf, pub start_position: TextSize, } @@ -98,8 +99,7 @@ fn add_element_properties( } let declared_at = value.type_node().as_ref().map(|n| DeclarationInformation { - uri: Url::from_file_path(n.source_file.path()) - .unwrap_or_else(|_| Url::parse("file:///unnamed").unwrap()), + path: n.source_file.path().to_path_buf(), start_position: n.text_range().start(), }); Some(PropertyInformation { @@ -1299,7 +1299,6 @@ component MainWindow inherits Window { } } "#.to_string()); - let file_url = url.clone(); let doc = dc.get_document(&url).unwrap(); let source = &doc.node.as_ref().unwrap().source_file; @@ -1312,7 +1311,7 @@ component MainWindow inherits Window { let declaration = foo_property.declared_at.as_ref().unwrap(); let start_position = util::text_size_to_lsp_position(&source, declaration.start_position); - assert_eq!(declaration.uri, file_url); + assert_eq!(declaration.path, source.path()); assert_eq!(start_position.line, 3); assert_eq!(start_position.character, 20); // This should probably point to the start of // `property foo = 42`, not to the `<` @@ -1344,7 +1343,7 @@ component SomeRect inherits Rectangle { assert_eq!(glob_property.type_name, "int"); let declaration = glob_property.declared_at.as_ref().unwrap(); let start_position = util::text_size_to_lsp_position(&source, declaration.start_position); - assert_eq!(declaration.uri, url); + assert_eq!(declaration.path, source.path()); assert_eq!(start_position.line, 2); assert_eq!(glob_property.group, ""); assert_eq!(find_property(&result, "width"), None); @@ -1354,7 +1353,7 @@ component SomeRect inherits Rectangle { assert_eq!(abcd_property.type_name, "int"); let declaration = abcd_property.declared_at.as_ref().unwrap(); let start_position = util::text_size_to_lsp_position(&source, declaration.start_position); - assert_eq!(declaration.uri, url); + assert_eq!(declaration.path, source.path()); assert_eq!(start_position.line, 7); assert_eq!(abcd_property.group, ""); diff --git a/tools/lsp/preview/ui.rs b/tools/lsp/preview/ui.rs index b4852993c53..977603aa302 100644 --- a/tools/lsp/preview/ui.rs +++ b/tools/lsp/preview/ui.rs @@ -224,16 +224,11 @@ fn map_property_declaration( declared_at: &Option, ) -> Option { let da = declared_at.as_ref()?; - - let doc = document_cache.get_document(&da.uri)?; - let doc_node = doc.node.as_ref()?; - let source_version = - document_cache.document_version_by_path(doc_node.source_file.path()).unwrap_or(-1); - + let source_version = document_cache.document_version_by_path(&da.path).unwrap_or(-1); let pos = TextRange::new(da.start_position, da.start_position); Some(PropertyDeclaration { - source_uri: da.uri.to_string().into(), + source_path: da.path.to_string_lossy().to_string().into(), source_version, range: to_ui_range(pos)?, }) @@ -414,7 +409,7 @@ fn map_properties_to_ui( for pi in &properties.properties { let declared_at = map_property_declaration(document_cache, &pi.declared_at).unwrap_or( PropertyDeclaration { - source_uri: String::new().into(), + source_path: String::new().into(), source_version: -1, range: Range { start: 0, end: 0 }, }, diff --git a/tools/lsp/preview/wasm.rs b/tools/lsp/preview/wasm.rs index 700880b4b25..258c84cf76d 100644 --- a/tools/lsp/preview/wasm.rs +++ b/tools/lsp/preview/wasm.rs @@ -62,8 +62,6 @@ impl PreviewConnector { ) -> Result { console_error_panic_hook::set_once(); - i_slint_core::debug_log!("PreviewConnector: Enable experimental? {experimental}"); - WASM_CALLBACKS.set(Some(WasmCallbacks { lsp_notifier, resource_url_mapper })); Ok(JsValue::from(js_sys::Promise::new(&mut move |resolve, reject| { diff --git a/tools/lsp/ui/api.slint b/tools/lsp/ui/api.slint index ec3bb1f1b0e..7ed14139c91 100644 --- a/tools/lsp/ui/api.slint +++ b/tools/lsp/ui/api.slint @@ -88,7 +88,7 @@ export struct PropertyDefinition { /// The Property Declaration export struct PropertyDeclaration { - source-uri: string, + source-path: string, source-version: int, range: Range, }