Skip to content

Commit

Permalink
live-preview: Use PathBuf in DeclarationInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Aug 23, 2024
1 parent 0f0b76e commit 6d81096
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
13 changes: 6 additions & 7 deletions tools/lsp/preview/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +27,7 @@ pub struct DefinitionInformation {

#[derive(Clone, Debug, PartialEq)]
pub struct DeclarationInformation {
pub uri: Url,
pub path: PathBuf,
pub start_position: TextSize,
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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<int> foo = 42`, not to the `<`
Expand Down Expand Up @@ -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);
Expand All @@ -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, "");

Expand Down
11 changes: 3 additions & 8 deletions tools/lsp/preview/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,11 @@ fn map_property_declaration(
declared_at: &Option<properties::DeclarationInformation>,
) -> Option<PropertyDeclaration> {
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)?,
})
Expand Down Expand Up @@ -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 },
},
Expand Down
2 changes: 0 additions & 2 deletions tools/lsp/preview/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ impl PreviewConnector {
) -> Result<PreviewConnectorPromise, JsValue> {
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| {
Expand Down
2 changes: 1 addition & 1 deletion tools/lsp/ui/api.slint
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export struct PropertyDefinition {

/// The Property Declaration
export struct PropertyDeclaration {
source-uri: string,
source-path: string,
source-version: int,
range: Range,
}
Expand Down

0 comments on commit 6d81096

Please sign in to comment.