Migrate add_braces assist, because edit_in_place uses ted
- And fix indent
Example
---
```rust
fn foo() {
{
match n {
Some(n) $0=> foo(
29,
30,
),
_ => ()
};
}
}
```
**Before this PR**:
```rust
fn main() {
{
match n {
Some(n) => {
foo(
29,
30,
)
},
_ => ()
};
}
}
```
**After this PR**:
```rust
fn foo() {
{
match n {
Some(n) => {
foo(
29,
30,
)
},
_ => ()
};
}
}
```
This commit is contained in:
parent
63f364601a
commit
b2566ff07b
1 changed files with 39 additions and 4 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use either::Either;
|
||||
use syntax::{
|
||||
AstNode,
|
||||
ast::{self, edit_in_place::Indent, syntax_factory::SyntaxFactory},
|
||||
ast::{self, edit::AstNodeEdit, syntax_factory::SyntaxFactory},
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, Assists};
|
||||
|
|
@ -43,10 +43,10 @@ pub(crate) fn add_braces(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
|
|||
let make = SyntaxFactory::with_mappings();
|
||||
let mut editor = builder.make_editor(expr.syntax());
|
||||
|
||||
let block_expr = make.block_expr(None, Some(expr.clone()));
|
||||
block_expr.indent(expr.indent_level());
|
||||
let new_expr = expr.reset_indent().indent(1.into());
|
||||
let block_expr = make.block_expr(None, Some(new_expr));
|
||||
|
||||
editor.replace(expr.syntax(), block_expr.syntax());
|
||||
editor.replace(expr.syntax(), block_expr.indent(expr.indent_level()).syntax());
|
||||
|
||||
editor.add_mappings(make.finish_with_mappings());
|
||||
builder.add_file_edits(ctx.vfs_file_id(), editor);
|
||||
|
|
@ -171,6 +171,41 @@ fn foo() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_indent() {
|
||||
check_assist(
|
||||
add_braces,
|
||||
r#"
|
||||
fn foo() {
|
||||
{
|
||||
match n {
|
||||
Some(n) $0=> foo(
|
||||
29,
|
||||
30,
|
||||
),
|
||||
_ => ()
|
||||
};
|
||||
}
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
fn foo() {
|
||||
{
|
||||
match n {
|
||||
Some(n) => {
|
||||
foo(
|
||||
29,
|
||||
30,
|
||||
)
|
||||
},
|
||||
_ => ()
|
||||
};
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_assist_for_match_with_braces() {
|
||||
check_assist_not_applicable(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue