From e75e2ae5b6b6b1364368ceb3d4081b6508b2f001 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sun, 28 Jun 2020 04:02:03 +0300 Subject: Simlify with matches!() --- crates/ra_assists/src/handlers/change_visibility.rs | 11 +++-------- crates/ra_assists/src/handlers/fix_visibility.rs | 6 +----- crates/ra_assists/src/handlers/merge_match_arms.rs | 5 +---- 3 files changed, 5 insertions(+), 17 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index c21d75be0..18e92e393 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs @@ -30,9 +30,8 @@ pub(crate) fn change_visibility(acc: &mut Assists, ctx: &AssistContext) -> Optio } fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { - T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, - _ => false, + let item_keyword = ctx.token_at_offset().find(|leaf| { + matches!(leaf.kind(), T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait]) }); let (offset, target) = if let Some(keyword) = item_keyword { @@ -73,11 +72,7 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { fn vis_offset(node: &SyntaxNode) -> TextSize { node.children_with_tokens() - .skip_while(|it| match it.kind() { - WHITESPACE | COMMENT | ATTR => true, - _ => false, - }) - .next() + .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) .map(|it| it.text_range().start()) .unwrap_or_else(|| node.text_range().start()) } diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 54601d1f3..02763c5b9 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -179,11 +179,7 @@ fn target_data_for_def( fn vis_offset(node: &SyntaxNode) -> TextSize { node.children_with_tokens() - .skip_while(|it| match it.kind() { - WHITESPACE | COMMENT | ATTR => true, - _ => false, - }) - .next() + .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) .map(|it| it.text_range().start()) .unwrap_or_else(|| node.text_range().start()) } diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index ca04ec671..53c2c130f 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs @@ -81,10 +81,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option } fn contains_placeholder(a: &ast::MatchArm) -> bool { - match a.pat() { - Some(ra_syntax::ast::Pat::PlaceholderPat(..)) => true, - _ => false, - } + matches!(a.pat(), Some(ra_syntax::ast::Pat::PlaceholderPat(..))) } #[cfg(test)] -- cgit v1.2.3 From 503f9498cd469c6467250c02bd8f6dbce973dd57 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 29 Jun 2020 01:18:50 +0300 Subject: Follow matklad suggestions - Move vis_offset() to utils.rs - Shorten explicit ra_syntax::ast -> ast - Undo refactoring exhaustive pattern to non-exhaustive --- crates/ra_assists/src/handlers/change_visibility.rs | 9 +-------- crates/ra_assists/src/handlers/fix_visibility.rs | 9 +-------- crates/ra_assists/src/handlers/merge_match_arms.rs | 2 +- crates/ra_assists/src/utils.rs | 11 ++++++++++- 4 files changed, 13 insertions(+), 18 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 18e92e393..237adbc7b 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs @@ -9,7 +9,7 @@ use ra_syntax::{ }; use test_utils::mark; -use crate::{AssistContext, AssistId, Assists}; +use crate::{utils::vis_offset, AssistContext, AssistId, Assists}; // Assist: change_visibility // @@ -70,13 +70,6 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { }) } -fn vis_offset(node: &SyntaxNode) -> TextSize { - node.children_with_tokens() - .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) - .map(|it| it.text_range().start()) - .unwrap_or_else(|| node.text_range().start()) -} - fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> { if vis.syntax().text() == "pub" { let target = vis.syntax().text_range(); diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 02763c5b9..468f9a77f 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -6,7 +6,7 @@ use ra_syntax::{ SyntaxNode, TextRange, TextSize, }; -use crate::{AssistContext, AssistId, Assists}; +use crate::{utils::vis_offset, AssistContext, AssistId, Assists}; // FIXME: this really should be a fix for diagnostic, rather than an assist. @@ -177,13 +177,6 @@ fn target_data_for_def( Some((offset, target, target_file, target_name)) } -fn vis_offset(node: &SyntaxNode) -> TextSize { - node.children_with_tokens() - .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) - .map(|it| it.text_range().start()) - .unwrap_or_else(|| node.text_range().start()) -} - #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable}; diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index 53c2c130f..90ce66378 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs @@ -81,7 +81,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option } fn contains_placeholder(a: &ast::MatchArm) -> bool { - matches!(a.pat(), Some(ra_syntax::ast::Pat::PlaceholderPat(..))) + matches!(a.pat(), Some(ast::Pat::PlaceholderPat(..))) } #[cfg(test)] diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index b7c45a619..02de902d6 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -7,7 +7,9 @@ use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; use ra_ide_db::RootDatabase; use ra_syntax::{ ast::{self, make, NameOwner}, - AstNode, SyntaxNode, T, + AstNode, + SyntaxKind::*, + SyntaxNode, TextSize, T, }; use rustc_hash::FxHashSet; @@ -120,6 +122,13 @@ pub(crate) fn resolve_target_trait( } } +pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize { + node.children_with_tokens() + .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR)) + .map(|it| it.text_range().start()) + .unwrap_or_else(|| node.text_range().start()) +} + pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { if let Some(expr) = invert_special_case(&expr) { return expr; -- cgit v1.2.3 From 36128c170d9d938cfcc52bc851aaed1fab6a2ad9 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 29 Jun 2020 01:23:39 +0300 Subject: Get rid of unused imports --- crates/ra_assists/src/handlers/change_visibility.rs | 7 ++----- crates/ra_assists/src/handlers/fix_visibility.rs | 6 +----- 2 files changed, 3 insertions(+), 10 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 237adbc7b..157c7b665 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs @@ -1,11 +1,8 @@ use ra_syntax::{ ast::{self, NameOwner, VisibilityOwner}, AstNode, - SyntaxKind::{ - ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY, - WHITESPACE, - }, - SyntaxNode, TextSize, T, + SyntaxKind::{CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY}, + T, }; use test_utils::mark; diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 468f9a77f..19d4dac5e 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -1,10 +1,6 @@ use hir::{db::HirDatabase, HasSource, HasVisibility, PathResolution}; use ra_db::FileId; -use ra_syntax::{ - ast, AstNode, - SyntaxKind::{ATTR, COMMENT, WHITESPACE}, - SyntaxNode, TextRange, TextSize, -}; +use ra_syntax::{ast, AstNode, TextRange, TextSize}; use crate::{utils::vis_offset, AssistContext, AssistId, Assists}; -- cgit v1.2.3