Don't spam loop-rewriting assist

The more focused the assist, the better!
This commit is contained in:
Aleksey Kladov 2021-02-28 21:08:51 +03:00
parent 358b9a50f7
commit aa04e3bbb2
2 changed files with 22 additions and 2 deletions

View file

@ -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(

View file

@ -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
//