mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Merge #3786
3786: When adding match arm, don't let the floating comma r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
2cdeb7363a
2 changed files with 45 additions and 14 deletions
|
@ -1,15 +1,11 @@
|
||||||
//! FIXME: write short doc here
|
|
||||||
|
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use hir::{Adt, HasSource, ModuleDef, Semantics};
|
use hir::{Adt, HasSource, ModuleDef, Semantics};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
|
use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
use ra_syntax::ast::{self, make, AstNode, NameOwner};
|
|
||||||
|
|
||||||
use ast::{MatchArm, Pat};
|
|
||||||
|
|
||||||
// Assist: fill_match_arms
|
// Assist: fill_match_arms
|
||||||
//
|
//
|
||||||
|
@ -717,4 +713,28 @@ mod tests {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fill_match_arms_placeholder() {
|
||||||
|
check_assist(
|
||||||
|
fill_match_arms,
|
||||||
|
r#"
|
||||||
|
enum A { One, Two, }
|
||||||
|
fn foo(a: A) {
|
||||||
|
match a<|> {
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One, Two, }
|
||||||
|
fn foo(a: A) {
|
||||||
|
match <|>a {
|
||||||
|
A::One => {}
|
||||||
|
A::Two => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -369,21 +369,32 @@ impl ast::MatchArmList {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn remove_placeholder(&self) -> ast::MatchArmList {
|
pub fn remove_placeholder(&self) -> ast::MatchArmList {
|
||||||
let placeholder = self.arms().find(|arm| {
|
let placeholder =
|
||||||
if let Some(ast::Pat::PlaceholderPat(_)) = arm.pat() {
|
self.arms().find(|arm| matches!(arm.pat(), Some(ast::Pat::PlaceholderPat(_))));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
false
|
|
||||||
});
|
|
||||||
if let Some(placeholder) = placeholder {
|
if let Some(placeholder) = placeholder {
|
||||||
let s: SyntaxElement = placeholder.syntax().clone().into();
|
self.remove_arm(&placeholder)
|
||||||
let e = s.clone();
|
|
||||||
self.replace_children(s..=e, &mut iter::empty())
|
|
||||||
} else {
|
} else {
|
||||||
self.clone()
|
self.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
fn remove_arm(&self, arm: &ast::MatchArm) -> ast::MatchArmList {
|
||||||
|
let start = arm.syntax().clone();
|
||||||
|
let end = if let Some(comma) = start
|
||||||
|
.siblings_with_tokens(Direction::Next)
|
||||||
|
.skip(1)
|
||||||
|
.skip_while(|it| it.kind().is_trivia())
|
||||||
|
.next()
|
||||||
|
.filter(|it| it.kind() == T![,])
|
||||||
|
{
|
||||||
|
comma
|
||||||
|
} else {
|
||||||
|
start.clone().into()
|
||||||
|
};
|
||||||
|
self.replace_children(start.into()..=end, None)
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
|
pub fn append_arm(&self, item: ast::MatchArm) -> ast::MatchArmList {
|
||||||
let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {
|
let r_curly = match self.syntax().children_with_tokens().find(|it| it.kind() == T!['}']) {
|
||||||
|
|
Loading…
Reference in a new issue