From aa04e3bbb2fab8ee7f7aa8eb406943d314976a0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 28 Feb 2021 21:08:51 +0300 Subject: Don't spam loop-rewriting assist The more focused the assist, the better! --- .../src/handlers/convert_for_to_iter_for_each.rs | 22 +++++++++++++++++++++- .../src/handlers/replace_let_with_if_let.rs | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs b/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs index 9fddf889c..011df1c18 100644 --- a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs +++ b/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs @@ -3,6 +3,7 @@ use hir::known; use ide_db::helpers::FamousDefs; use stdx::format_to; use syntax::{ast, AstNode}; +use test_utils::mark; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -13,7 +14,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` // fn main() { // let x = vec![1, 2, 3]; -// for $0v in x { +// for$0 v in x { // let y = v * 2; // } // } @@ -32,6 +33,10 @@ pub(crate) fn convert_for_to_iter_for_each(acc: &mut Assists, ctx: &AssistContex let iterable = for_loop.iterable()?; let pat = for_loop.pat()?; let body = for_loop.loop_body()?; + if body.syntax().text_range().start() < ctx.offset() { + mark::hit!(not_available_in_body); + return None; + } acc.add( AssistId("convert_for_to_iter_for_each", AssistKind::RefactorRewrite), @@ -180,6 +185,21 @@ fn main() { ) } + #[test] + fn not_available_in_body() { + mark::check!(not_available_in_body); + check_assist_not_applicable( + convert_for_to_iter_for_each, + r" +fn main() { + let x = vec![1, 2, 3]; + for v in x { + $0v *= 2; + } +}", + ) + } + #[test] fn test_for_borrowed() { check_assist_with_fixtures( diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs index 5a27ada6b..be7e724b5 100644 --- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs @@ -1,5 +1,6 @@ use std::iter::once; +use ide_db::ty_filter::TryEnum; use syntax::{ ast::{ self, @@ -10,7 +11,6 @@ use syntax::{ }; use crate::{AssistContext, AssistId, AssistKind, Assists}; -use ide_db::ty_filter::TryEnum; // Assist: replace_let_with_if_let // -- cgit v1.2.3