Skip to content

Commit e968dd1

Browse files
authored
Merge pull request #20380 from Hmikihiro/add_attr_arg
remove `add_attr()` from edit_in_place.rs because it use `ted`.
2 parents 135f94a + c57a42a commit e968dd1

24 files changed

+185
-116
lines changed

crates/ide-assists/src/handlers/convert_bool_to_enum.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ use ide_db::{
1313
use itertools::Itertools;
1414
use syntax::{
1515
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T,
16-
ast::{
17-
self, HasName,
18-
edit::IndentLevel,
19-
edit_in_place::{AttrsOwnerEdit, Indent},
20-
make,
21-
},
16+
ast::{self, HasName, edit::IndentLevel, edit_in_place::Indent, make},
2217
};
2318

2419
use crate::{
@@ -506,18 +501,6 @@ fn node_to_insert_before(target_node: SyntaxNode) -> SyntaxNode {
506501
}
507502

508503
fn make_bool_enum(make_pub: bool) -> ast::Enum {
509-
let enum_def = make::enum_(
510-
if make_pub { Some(make::visibility_pub()) } else { None },
511-
make::name("Bool"),
512-
None,
513-
None,
514-
make::variant_list(vec![
515-
make::variant(None, make::name("True"), None, None),
516-
make::variant(None, make::name("False"), None, None),
517-
]),
518-
)
519-
.clone_for_update();
520-
521504
let derive_eq = make::attr_outer(make::meta_token_tree(
522505
make::ext::ident_path("derive"),
523506
make::token_tree(
@@ -529,11 +512,19 @@ fn make_bool_enum(make_pub: bool) -> ast::Enum {
529512
NodeOrToken::Token(make::tokens::ident("Eq")),
530513
],
531514
),
532-
))
533-
.clone_for_update();
534-
enum_def.add_attr(derive_eq);
535-
536-
enum_def
515+
));
516+
make::enum_(
517+
[derive_eq],
518+
if make_pub { Some(make::visibility_pub()) } else { None },
519+
make::name("Bool"),
520+
None,
521+
None,
522+
make::variant_list(vec![
523+
make::variant(None, make::name("True"), None, None),
524+
make::variant(None, make::name("False"), None, None),
525+
]),
526+
)
527+
.clone_for_update()
537528
}
538529

539530
#[cfg(test)]

crates/ide-assists/src/handlers/convert_closure_to_fn.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ pub(crate) fn convert_closure_to_fn(acc: &mut Assists, ctx: &AssistContext<'_>)
235235
Some(make::ret_type(make::ty(&ret_ty)))
236236
};
237237
let mut fn_ = make::fn_(
238+
None,
238239
None,
239240
closure_name_or_default.clone(),
240241
closure_type_params,

crates/ide-assists/src/handlers/convert_from_to_tryfrom.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub(crate) fn convert_from_to_tryfrom(acc: &mut Assists, ctx: &AssistContext<'_>
9696
}
9797

9898
let error_type = ast::AssocItem::TypeAlias(make::ty_alias(
99+
None,
99100
"Error",
100101
None,
101102
None,

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,7 @@ fn format_function(
16411641
let (generic_params, where_clause) = make_generic_params_and_where_clause(ctx, fun);
16421642

16431643
make::fn_(
1644+
None,
16441645
None,
16451646
fun_name,
16461647
generic_params,

crates/ide-assists/src/handlers/extract_module.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ impl Module {
585585

586586
if (def_out_sel || !is_item) && use_stmt_not_in_sel {
587587
let use_ = make::use_(
588+
None,
588589
None,
589590
make::use_tree(make::join_paths(use_tree_paths), None, None, false),
590591
);
@@ -599,6 +600,7 @@ impl Module {
599600
let super_path = make::ext::ident_path("super");
600601
let node_path = make::ext::ident_path(&node_syntax.to_string());
601602
let use_ = make::use_(
603+
None,
602604
None,
603605
make::use_tree(make::join_paths(vec![super_path, node_path]), None, None, false),
604606
);

crates/ide-assists/src/handlers/extract_type_alias.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
6969
edit.replace(ty.syntax(), new_ty.syntax());
7070

7171
// Insert new alias
72-
let ty_alias = make::ty_alias("Type", generic_params, None, None, Some((ty, None)))
73-
.clone_for_update();
72+
let ty_alias =
73+
make::ty_alias(None, "Type", generic_params, None, None, Some((ty, None)))
74+
.clone_for_update();
7475

7576
if let Some(cap) = ctx.config.snippet_cap
7677
&& let Some(name) = ty_alias.name()

crates/ide-assists/src/handlers/extract_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
200200
}
201201
ExtractionKind::Constant => {
202202
let ast_ty = make.ty(&ty_string);
203-
ast::Item::Const(make.item_const(None, pat_name, ast_ty, initializer))
203+
ast::Item::Const(make.item_const(None, None, pat_name, ast_ty, initializer))
204204
.into()
205205
}
206206
ExtractionKind::Static => {

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
155155
let ret_type = method_source.ret_type();
156156

157157
let f = make::fn_(
158+
None,
158159
vis,
159160
fn_name,
160161
type_params,
@@ -195,6 +196,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
195196
let assoc_item_list = make::assoc_item_list(Some(vec![item]));
196197

197198
let impl_def = make::impl_(
199+
None,
198200
ty_params,
199201
ty_args,
200202
make::ty_path(make::ext::ident_path(name)),

crates/ide-assists/src/handlers/generate_delegate_trait.rs

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use syntax::{
2020
HasGenericParams, HasName, HasTypeBounds, HasVisibility as astHasVisibility, Path,
2121
WherePred,
2222
edit::{self, AstNodeEdit},
23-
edit_in_place::AttrsOwnerEdit,
2423
make,
2524
},
2625
ted::{self, Position},
@@ -266,6 +265,7 @@ fn generate_impl(
266265
let bound_params = bound_def.generic_param_list();
267266

268267
let delegate = make::impl_trait(
268+
None,
269269
delegee.is_unsafe(db),
270270
bound_params.clone(),
271271
bound_params.map(|params| params.to_generic_args()),
@@ -379,6 +379,7 @@ fn generate_impl(
379379
let path_type = transform_impl(ctx, ast_strukt, &old_impl, &transform_args, path_type)?;
380380
// 3) Generate delegate trait impl
381381
let delegate = make::impl_trait(
382+
None,
382383
trait_.is_unsafe(db),
383384
trait_gen_params,
384385
trait_gen_args,
@@ -652,8 +653,7 @@ fn process_assoc_item(
652653
qual_path_ty: ast::Path,
653654
base_name: &str,
654655
) -> Option<ast::AssocItem> {
655-
let attrs = item.attrs();
656-
let assoc = match item {
656+
match item {
657657
AssocItem::Const(c) => const_assoc_item(c, qual_path_ty),
658658
AssocItem::Fn(f) => func_assoc_item(f, qual_path_ty, base_name),
659659
AssocItem::MacroCall(_) => {
@@ -662,18 +662,7 @@ fn process_assoc_item(
662662
None
663663
}
664664
AssocItem::TypeAlias(ta) => ty_assoc_item(ta, qual_path_ty),
665-
};
666-
if let Some(assoc) = &assoc {
667-
attrs.for_each(|attr| {
668-
assoc.add_attr(attr.clone());
669-
// fix indentations
670-
if let Some(tok) = attr.syntax().next_sibling_or_token() {
671-
let pos = Position::after(tok);
672-
ted::insert(pos, make::tokens::whitespace(" "));
673-
}
674-
})
675665
}
676-
assoc
677666
}
678667

679668
fn const_assoc_item(item: syntax::ast::Const, qual_path_ty: ast::Path) -> Option<AssocItem> {
@@ -687,6 +676,7 @@ fn const_assoc_item(item: syntax::ast::Const, qual_path_ty: ast::Path) -> Option
687676
// make::path_qualified(qual_path_ty, path_expr_segment.as_single_segment().unwrap());
688677
let qualified_path = qualified_path(qual_path_ty, path_expr_segment);
689678
let inner = make::item_const(
679+
item.attrs(),
690680
item.visibility(),
691681
item.name()?,
692682
item.ty()?,
@@ -755,6 +745,7 @@ fn func_assoc_item(
755745

756746
let body = make::block_expr(vec![], Some(call.into())).clone_for_update();
757747
let func = make::fn_(
748+
item.attrs(),
758749
item.visibility(),
759750
item.name()?,
760751
item.generic_param_list(),
@@ -779,13 +770,14 @@ fn ty_assoc_item(item: syntax::ast::TypeAlias, qual_path_ty: Path) -> Option<Ass
779770
let ident = item.name()?.to_string();
780771

781772
let alias = make::ty_alias(
773+
item.attrs(),
782774
ident.as_str(),
783775
item.generic_param_list(),
784776
None,
785777
item.where_clause(),
786778
Some((ty, None)),
787779
)
788-
.clone_for_update();
780+
.indent(edit::IndentLevel(1));
789781

790782
Some(AssocItem::TypeAlias(alias))
791783
}
@@ -1813,6 +1805,63 @@ impl T for B {
18131805
);
18141806
}
18151807

1808+
#[test]
1809+
fn test_ty_alias_attrs() {
1810+
check_assist(
1811+
generate_delegate_trait,
1812+
r#"
1813+
struct A;
1814+
1815+
trait T {
1816+
#[cfg(test)]
1817+
type t;
1818+
#[cfg(not(test))]
1819+
type t;
1820+
}
1821+
1822+
impl T for A {
1823+
#[cfg(test)]
1824+
type t = u32;
1825+
#[cfg(not(test))]
1826+
type t = bool;
1827+
}
1828+
1829+
struct B {
1830+
a$0: A,
1831+
}
1832+
"#,
1833+
r#"
1834+
struct A;
1835+
1836+
trait T {
1837+
#[cfg(test)]
1838+
type t;
1839+
#[cfg(not(test))]
1840+
type t;
1841+
}
1842+
1843+
impl T for A {
1844+
#[cfg(test)]
1845+
type t = u32;
1846+
#[cfg(not(test))]
1847+
type t = bool;
1848+
}
1849+
1850+
struct B {
1851+
a: A,
1852+
}
1853+
1854+
impl T for B {
1855+
#[cfg(test)]
1856+
type t = <A as T>::t;
1857+
1858+
#[cfg(not(test))]
1859+
type t = <A as T>::t;
1860+
}
1861+
"#,
1862+
);
1863+
}
1864+
18161865
#[test]
18171866
fn assoc_items_attributes_mutably_cloned() {
18181867
check_assist(

crates/ide-assists/src/handlers/generate_derive.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use syntax::{
2+
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
23
T,
3-
ast::{self, AstNode, HasAttrs, edit_in_place::AttrsOwnerEdit, make},
4+
ast::{self, AstNode, HasAttrs, edit::IndentLevel, make},
5+
syntax_editor::{Element, Position},
46
};
57

68
use crate::{AssistContext, AssistId, Assists};
@@ -48,8 +50,20 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
4850
))
4951
.clone_for_update();
5052

51-
let nominal = edit.make_mut(nominal);
52-
nominal.add_attr(derive.clone());
53+
let mut editor = edit.make_editor(nominal.syntax());
54+
let indent = IndentLevel::from_node(nominal.syntax());
55+
let after_attrs_and_comments = nominal
56+
.syntax()
57+
.children_with_tokens()
58+
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
59+
.map_or(Position::first_child_of(nominal.syntax()), Position::before);
60+
editor.insert_all(
61+
after_attrs_and_comments,
62+
vec![
63+
derive.syntax().syntax_element(),
64+
make::tokens::whitespace(&format!("\n{indent}")).syntax_element(),
65+
],
66+
);
5367

5468
let delimiter = derive
5569
.meta()
@@ -58,8 +72,9 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
5872
.expect("failed to get token tree out of Meta")
5973
.r_paren_token()
6074
.expect("make::attr_outer was expected to have a R_PAREN");
61-
62-
edit.add_tabstop_before_token(cap, delimiter);
75+
let tabstop_before = edit.make_tabstop_before(cap);
76+
editor.add_annotation(delimiter, tabstop_before);
77+
edit.add_file_edits(ctx.vfs_file_id(), editor);
6378
}
6479
Some(_) => {
6580
// Just move the cursor.

0 commit comments

Comments
 (0)