Skip to content

Commit

Permalink
lsp: Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Aug 23, 2024
1 parent 1a306dd commit 0f0b76e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 73 deletions.
10 changes: 2 additions & 8 deletions tools/lsp/common/component_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}\""))],
Expand All @@ -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" => {
Expand Down
6 changes: 2 additions & 4 deletions tools/lsp/common/document_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 10 additions & 12 deletions tools/lsp/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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,
}));
Expand Down
52 changes: 26 additions & 26 deletions tools/lsp/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
11 changes: 3 additions & 8 deletions tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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)
})
},
)
Expand Down
23 changes: 9 additions & 14 deletions tools/lsp/preview/drop_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
))
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}),
);

Expand All @@ -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 },
))
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
)?);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/lsp/preview/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 0f0b76e

Please sign in to comment.