mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Fix indent for unresolved_field fixes
Examples --- ```rust mod indent { struct Foo {} fn foo() { let foo = Foo{}; foo.bar$0; } } ``` **Before this PR**: ```rust mod indent { struct Foo { bar: () } fn foo() { let foo = Foo{}; foo.bar; } } ``` **After this PR**: ```rust mod indent { struct Foo { bar: () } fn foo() { let foo = Foo{}; foo.bar; } } ``` --- New field list add newline ```rust mod indent { struct Foo; fn foo() { Foo.bar$0; } } ``` **Before this PR**: ```rust mod indent { struct Foo{ bar: () } fn foo() { Foo.bar; } } ``` **After this PR**: ```rust mod indent { struct Foo { bar: (), } fn foo() { Foo.bar; } } ```
This commit is contained in:
parent
1f4e5e82ff
commit
db027e057d
@ -1,5 +1,3 @@
|
|||||||
use std::iter;
|
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{Adt, FileRange, HasSource, HirDisplay, InFile, Struct, Union, db::ExpandDatabase};
|
use hir::{Adt, FileRange, HasSource, HirDisplay, InFile, Struct, Union, db::ExpandDatabase};
|
||||||
use ide_db::text_edit::TextEdit;
|
use ide_db::text_edit::TextEdit;
|
||||||
@ -194,17 +192,20 @@ fn add_field_to_struct_fix(
|
|||||||
Some(make::visibility_pub_crate())
|
Some(make::visibility_pub_crate())
|
||||||
};
|
};
|
||||||
// FIXME: Allow for choosing a visibility modifier see https://github.com/rust-lang/rust-analyzer/issues/11563
|
// FIXME: Allow for choosing a visibility modifier see https://github.com/rust-lang/rust-analyzer/issues/11563
|
||||||
let indent = IndentLevel::from_node(struct_syntax.value) + 1;
|
let indent = IndentLevel::from_node(struct_syntax.value);
|
||||||
|
|
||||||
let field = make::record_field(visibility, field_name, suggested_type).indent(indent);
|
let field =
|
||||||
let record_field_list = make::record_field_list(iter::once(field));
|
make::record_field(visibility, field_name, suggested_type).indent(indent + 1);
|
||||||
// A Unit Struct with no `;` is invalid syntax. We should not suggest this fix.
|
// A Unit Struct with no `;` is invalid syntax. We should not suggest this fix.
|
||||||
let semi_colon =
|
let semi_colon =
|
||||||
algo::skip_trivia_token(struct_syntax.value.last_token()?, Direction::Prev)?;
|
algo::skip_trivia_token(struct_syntax.value.last_token()?, Direction::Prev)?;
|
||||||
if semi_colon.kind() != SyntaxKind::SEMICOLON {
|
if semi_colon.kind() != SyntaxKind::SEMICOLON {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
src_change_builder.replace(semi_colon.text_range(), record_field_list.to_string());
|
src_change_builder.replace(
|
||||||
|
semi_colon.text_range(),
|
||||||
|
format!(" {{\n{}{field},\n{indent}}}", indent + 1),
|
||||||
|
);
|
||||||
|
|
||||||
Some(Assist {
|
Some(Assist {
|
||||||
id: AssistId::quick_fix("convert-unit-struct-to-record-struct"),
|
id: AssistId::quick_fix("convert-unit-struct-to-record-struct"),
|
||||||
@ -230,7 +231,7 @@ fn record_field_layout(
|
|||||||
field_list: ast::RecordFieldList,
|
field_list: ast::RecordFieldList,
|
||||||
struct_syntax: &SyntaxNode,
|
struct_syntax: &SyntaxNode,
|
||||||
) -> Option<(TextSize, String)> {
|
) -> Option<(TextSize, String)> {
|
||||||
let (offset, needs_comma, trailing_new_line, indent) = match field_list.fields().last() {
|
let (offset, needs_comma, indent) = match field_list.fields().last() {
|
||||||
Some(record_field) => {
|
Some(record_field) => {
|
||||||
let syntax = algo::skip_trivia_token(field_list.r_curly_token()?, Direction::Prev)?;
|
let syntax = algo::skip_trivia_token(field_list.r_curly_token()?, Direction::Prev)?;
|
||||||
|
|
||||||
@ -239,19 +240,22 @@ fn record_field_layout(
|
|||||||
(
|
(
|
||||||
last_field_syntax.text_range().end(),
|
last_field_syntax.text_range().end(),
|
||||||
syntax.kind() != SyntaxKind::COMMA,
|
syntax.kind() != SyntaxKind::COMMA,
|
||||||
false,
|
|
||||||
last_field_indent,
|
last_field_indent,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Empty Struct. Add a field right before the closing brace
|
// Empty Struct. Add a field right before the closing brace
|
||||||
None => {
|
None => {
|
||||||
let indent = IndentLevel::from_node(struct_syntax) + 1;
|
let indent = IndentLevel::from_node(struct_syntax) + 1;
|
||||||
let offset = field_list.r_curly_token()?.text_range().start();
|
let offset = field_list.l_curly_token()?.text_range().end();
|
||||||
(offset, false, true, indent)
|
(offset, false, indent)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let comma = if needs_comma { ",\n" } else { "" };
|
let trailing_new_line = if !field_list.syntax().text().contains_char('\n') {
|
||||||
let trailing_new_line = if trailing_new_line { "\n" } else { "" };
|
format!("\n{}", field_list.indent_level())
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
let comma = if needs_comma { ",\n" } else { "\n" };
|
||||||
let record_field = make::record_field(visibility, name, suggested_type);
|
let record_field = make::record_field(visibility, name, suggested_type);
|
||||||
|
|
||||||
Some((offset, format!("{comma}{indent}{record_field}{trailing_new_line}")))
|
Some((offset, format!("{comma}{indent}{record_field}{trailing_new_line}")))
|
||||||
@ -377,18 +381,24 @@ fn foo() {
|
|||||||
fn unresolved_field_fix_on_unit() {
|
fn unresolved_field_fix_on_unit() {
|
||||||
check_fix(
|
check_fix(
|
||||||
r#"
|
r#"
|
||||||
|
mod indent {
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
Foo.bar$0;
|
Foo.bar$0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
struct Foo{ bar: () }
|
mod indent {
|
||||||
|
struct Foo {
|
||||||
|
bar: (),
|
||||||
|
}
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
Foo.bar;
|
Foo.bar;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -396,6 +406,7 @@ fn foo() {
|
|||||||
fn unresolved_field_fix_on_empty() {
|
fn unresolved_field_fix_on_empty() {
|
||||||
check_fix(
|
check_fix(
|
||||||
r#"
|
r#"
|
||||||
|
mod indent {
|
||||||
struct Foo{
|
struct Foo{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,8 +414,10 @@ fn foo() {
|
|||||||
let foo = Foo{};
|
let foo = Foo{};
|
||||||
foo.bar$0;
|
foo.bar$0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
|
mod indent {
|
||||||
struct Foo{
|
struct Foo{
|
||||||
bar: ()
|
bar: ()
|
||||||
}
|
}
|
||||||
@ -413,6 +426,32 @@ fn foo() {
|
|||||||
let foo = Foo{};
|
let foo = Foo{};
|
||||||
foo.bar;
|
foo.bar;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
check_fix(
|
||||||
|
r#"
|
||||||
|
mod indent {
|
||||||
|
struct Foo {}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
let foo = Foo{};
|
||||||
|
foo.bar$0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
mod indent {
|
||||||
|
struct Foo {
|
||||||
|
bar: ()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
let foo = Foo{};
|
||||||
|
foo.bar;
|
||||||
|
}
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user