From 0f0b76e95263b476a4d36f29cb4a8607bbdea3c0 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 23 Aug 2024 11:16:07 +0000 Subject: [PATCH] lsp: Clippy fixes --- tools/lsp/common/component_catalog.rs | 10 ++---- tools/lsp/common/document_cache.rs | 6 ++-- tools/lsp/language.rs | 22 ++++++------ tools/lsp/main.rs | 52 +++++++++++++-------------- tools/lsp/preview.rs | 11 ++---- tools/lsp/preview/drop_location.rs | 23 +++++------- tools/lsp/preview/ui.rs | 2 +- 7 files changed, 53 insertions(+), 73 deletions(-) diff --git a/tools/lsp/common/component_catalog.rs b/tools/lsp/common/component_catalog.rs index 49a47a8a117..ca7d202467e 100644 --- a/tools/lsp/common/component_catalog.rs +++ b/tools/lsp/common/component_catalog.rs @@ -12,10 +12,7 @@ use i_slint_compiler::langtype::{DefaultSizeBinding, ElementType}; #[cfg(feature = "preview-engine")] fn builtin_component_info(name: &str, fills_parent: bool) -> ComponentInformation { - let is_layout = match name { - "GridLayout" | "HorizontalLayout" | "VerticalLayout" => true, - _ => false, - }; + let is_layout = matches!(name, "GridLayout" | "HorizontalLayout" | "VerticalLayout"); let default_properties = match name { "Text" | "TextInput" => vec![PropertyChange::new("text", format!("\"{name}\""))], @@ -38,10 +35,7 @@ fn builtin_component_info(name: &str, fills_parent: bool) -> ComponentInformatio } fn std_widgets_info(name: &str, is_global: bool) -> ComponentInformation { - let is_layout = match name { - "GridBox" | "HorizontalBox" | "VerticalBox" => true, - _ => false, - }; + let is_layout = matches!(name, "GridBox" | "HorizontalBox" | "VerticalBox"); let default_properties = match name { "Button" | "CheckBox" | "LineEdit" | "Switch" | "TextEdit" => { diff --git a/tools/lsp/common/document_cache.rs b/tools/lsp/common/document_cache.rs index 3f18ff8255c..b1de26a609b 100644 --- a/tools/lsp/common/document_cache.rs +++ b/tools/lsp/common/document_cache.rs @@ -64,9 +64,7 @@ impl Default for CompilerConfiguration { } impl CompilerConfiguration { - fn to_compiler_configuration( - mut self, - ) -> (i_slint_compiler::CompilerConfiguration, OpenImportFallback) { + fn build(mut self) -> (i_slint_compiler::CompilerConfiguration, OpenImportFallback) { let mut result = default_cc(); result.include_paths = std::mem::take(&mut self.include_paths); result.library_paths = std::mem::take(&mut self.library_paths); @@ -131,7 +129,7 @@ impl DocumentCache { } pub fn new(config: CompilerConfiguration) -> Self { - let (mut compiler_config, open_import_fallback) = config.to_compiler_configuration(); + let (mut compiler_config, open_import_fallback) = config.build(); let (open_import_fallback, source_file_versions) = Self::wire_up_import_fallback( &mut compiler_config, diff --git a/tools/lsp/language.rs b/tools/lsp/language.rs index e5beb71f92b..4c57dc7c0c1 100644 --- a/tools/lsp/language.rs +++ b/tools/lsp/language.rs @@ -717,7 +717,7 @@ fn get_code_actions( kind: Some(lsp_types::CodeActionKind::REFACTOR), edit: common::create_workspace_edit_from_path( document_cache, - &token.source_file.path(), + token.source_file.path(), edits, ), ..Default::default() @@ -785,7 +785,7 @@ fn get_code_actions( kind: Some(lsp_types::CodeActionKind::REFACTOR), edit: common::create_workspace_edit_from_path( document_cache, - &token.source_file.path(), + token.source_file.path(), edits, ), ..Default::default() @@ -812,7 +812,7 @@ fn get_code_actions( kind: Some(lsp_types::CodeActionKind::REFACTOR), edit: common::create_workspace_edit_from_path( document_cache, - &token.source_file.path(), + token.source_file.path(), edits, ), ..Default::default() @@ -827,7 +827,7 @@ fn get_code_actions( kind: Some(lsp_types::CodeActionKind::REFACTOR), edit: common::create_workspace_edit_from_path( document_cache, - &token.source_file.path(), + token.source_file.path(), edits, ), ..Default::default() @@ -925,14 +925,12 @@ fn get_document_symbols( kind: lsp_types::SymbolKind::STRUCT, ..ds.clone() }), - Type::Enumeration(enumeration) => enumeration.node.as_ref().and_then(|node| { - Some(DocumentSymbol { - range: util::node_to_lsp_range(node), - selection_range: util::node_to_lsp_range(&node.DeclaredIdentifier()), - name: enumeration.name.clone(), - kind: lsp_types::SymbolKind::ENUM, - ..ds.clone() - }) + Type::Enumeration(enumeration) => enumeration.node.as_ref().map(|node| DocumentSymbol { + range: util::node_to_lsp_range(node), + selection_range: util::node_to_lsp_range(&node.DeclaredIdentifier()), + name: enumeration.name.clone(), + kind: lsp_types::SymbolKind::ENUM, + ..ds.clone() }), _ => None, })); diff --git a/tools/lsp/main.rs b/tools/lsp/main.rs index 2af35f1ec87..31bda41cc83 100644 --- a/tools/lsp/main.rs +++ b/tools/lsp/main.rs @@ -288,34 +288,34 @@ fn main_loop(connection: Connection, init_param: InitializeParams, cli_args: Cli #[cfg(feature = "preview-builtin")] preview::set_server_notifier(server_notifier.clone()); - let mut compiler_config = CompilerConfiguration::default(); - - compiler_config.style = - Some(if cli_args.style.is_empty() { "native".into() } else { cli_args.style }); - compiler_config.include_paths = cli_args.include_paths; - compiler_config.library_paths = cli_args - .library_paths - .iter() - .filter_map(|entry| entry.split('=').collect_tuple().map(|(k, v)| (k.into(), v.into()))) - .collect(); let server_notifier_ = server_notifier.clone(); - compiler_config.open_import_fallback = Some(Rc::new(move |path| { - let server_notifier = server_notifier_.clone(); - Box::pin(async move { - let contents = std::fs::read_to_string(&path); - if let Ok(contents) = &contents { - if let Ok(url) = Url::from_file_path(&path) { - server_notifier.send_message_to_preview( - common::LspToPreviewMessage::SetContents { - url: common::VersionedUrl::new(url, None), - contents: contents.clone(), - }, - ) + let compiler_config = CompilerConfiguration { + style: Some(if cli_args.style.is_empty() { "native".into() } else { cli_args.style }), + include_paths: cli_args.include_paths, + library_paths: cli_args + .library_paths + .iter() + .filter_map(|entry| entry.split('=').collect_tuple().map(|(k, v)| (k.into(), v.into()))) + .collect(), + open_import_fallback: Some(Rc::new(move |path| { + let server_notifier = server_notifier_.clone(); + Box::pin(async move { + let contents = std::fs::read_to_string(&path); + if let Ok(contents) = &contents { + if let Ok(url) = Url::from_file_path(&path) { + server_notifier.send_message_to_preview( + common::LspToPreviewMessage::SetContents { + url: common::VersionedUrl::new(url, None), + contents: contents.clone(), + }, + ) + } } - } - Some(contents.map(|c| (None, c))) - }) - })); + Some(contents.map(|c| (None, c))) + }) + })), + ..Default::default() + }; let ctx = Rc::new(Context { document_cache: RefCell::new(crate::common::DocumentCache::new(compiler_config)), diff --git a/tools/lsp/preview.rs b/tools/lsp/preview.rs index 5d758471899..321bf7a102a 100644 --- a/tools/lsp/preview.rs +++ b/tools/lsp/preview.rs @@ -743,7 +743,7 @@ fn test_workspace_edit(edit: &lsp_types::WorkspaceEdit, test_edit: bool) -> bool let Some(document_cache) = document_cache() else { return false; }; - drop_location::workspace_edit_compiles(&document_cache, &edit) + drop_location::workspace_edit_compiles(&document_cache, edit) } else { true } @@ -812,12 +812,7 @@ fn finish_parsing(ok: bool) { for (url, (version, contents)) in &source_code { let mut diag = diagnostics::BuildDiagnostics::default(); if document_cache.get_document(url).is_none() { - poll_once(document_cache.load_url( - url, - version.clone(), - contents.clone(), - &mut diag, - )); + poll_once(document_cache.load_url(url, *version, contents.clone(), &mut diag)); } } @@ -1104,7 +1099,7 @@ async fn reload_preview_impl( let path = path.to_owned(); Box::pin(async move { let path = PathBuf::from(&path); - get_path_from_cache(&path).map(|r| Result::Ok(r)) + get_path_from_cache(&path).map(Result::Ok) }) }, ) diff --git a/tools/lsp/preview/drop_location.rs b/tools/lsp/preview/drop_location.rs index 61dd25c54b2..f0139a72b42 100644 --- a/tools/lsp/preview/drop_location.rs +++ b/tools/lsp/preview/drop_location.rs @@ -580,7 +580,7 @@ pub fn add_new_component( let edit = lsp_types::TextEdit { range: lsp_types::Range::new(start_pos, end_pos), new_text }; Some(( - common::create_workspace_edit_from_path(&document_cache, &source_file.path(), vec![edit])?, + common::create_workspace_edit_from_path(document_cache, source_file.path(), vec![edit])?, DropData { selection_offset, path }, )) } @@ -957,7 +957,7 @@ pub fn drop_at( if let Some((child_insertion_parent, _, _)) = &*component.child_insertion_point.borrow() { - element_base_type_is_layout(&child_insertion_parent) + element_base_type_is_layout(child_insertion_parent) } else { is_layout } @@ -1027,11 +1027,9 @@ pub fn drop_at( edits.extend( drop_ignored_elements_from_node(&drop_info.target_element_node, &source_file) .drain(..) - .map(|te| { - // Abuse map somewhat... - selection_offset = text_edit::TextOffsetAdjustment::new(&te, &source_file) - .adjust(selection_offset); - te + .inspect(|te| { + selection_offset = + text_edit::TextOffsetAdjustment::new(te, &source_file).adjust(selection_offset); }), ); @@ -1048,7 +1046,7 @@ pub fn drop_at( edits.push(lsp_types::TextEdit { range: lsp_types::Range::new(start_pos, end_pos), new_text }); Some(( - common::create_workspace_edit_from_path(&document_cache, &source_file.path(), edits)?, + common::create_workspace_edit_from_path(&document_cache, source_file.path(), edits)?, DropData { selection_offset, path }, )) } @@ -1138,11 +1136,8 @@ pub fn create_move_element_workspace_edit( let size = element.geometries(component_instance).first().map(|g| g.size)?; if drop_info.target_element_node.layout_kind() == ui::LayoutKind::None { - let Some((edit, _)) = - preview::resize_selected_element_impl(LogicalRect::new(position, size)) - else { - return None; - }; + let (edit, _) = + preview::resize_selected_element_impl(LogicalRect::new(position, size))?; let (path, selection_offset) = element.path_and_offset(); return Some((edit, DropData { selection_offset, path })); } else { @@ -1231,7 +1226,7 @@ pub fn create_move_element_workspace_edit( } edits.push(common::SingleTextEdit::from_path( &document_cache, - &source_file.path(), + source_file.path(), edit, )?); } diff --git a/tools/lsp/preview/ui.rs b/tools/lsp/preview/ui.rs index 85735fa8b67..b4852993c53 100644 --- a/tools/lsp/preview/ui.rs +++ b/tools/lsp/preview/ui.rs @@ -134,7 +134,7 @@ pub fn ui_set_known_components( if let Some(position) = &ci.defined_at { if let Some(library) = position.url.path().strip_prefix("/@") { - library_map.entry(format!("@{library}")).or_insert(Vec::new()).push(item); + library_map.entry(format!("@{library}")).or_default().push(item); } else { let path = i_slint_compiler::pathutils::clean_path( &(position.url.to_file_path().unwrap_or_default()),