Skip to content

Commit 749eeef

Browse files
committed
fix: insert whitespaces into assoc items for assist when macro generated
1 parent 3284195 commit 749eeef

File tree

8 files changed

+84
-26
lines changed

8 files changed

+84
-26
lines changed

crates/hir/src/semantics.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,10 +1163,6 @@ impl<'a> SemanticsScope<'a> {
11631163
Some(Crate { id: self.resolver.krate()? })
11641164
}
11651165

1166-
pub fn in_macro_file(&self) -> bool {
1167-
self.file_id.is_macro()
1168-
}
1169-
11701166
/// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
11711167
pub fn visible_traits(&self) -> FxHashSet<TraitId> {
11721168
let resolver = &self.resolver;

crates/ide/src/expand_macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::Semantics;
22
use ide_db::{
3-
helpers::{pick_best_token, render_macro_node::render_with_ws_inserted},
3+
helpers::{insert_whitespace_into_node::insert_ws_into, pick_best_token},
44
RootDatabase,
55
};
66
use itertools::Itertools;
@@ -50,7 +50,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
5050
let expansions = sema.expand_derive_macro(&attr)?;
5151
Some(ExpandedMacro {
5252
name: tt,
53-
expansion: expansions.into_iter().map(render_with_ws_inserted).join(""),
53+
expansion: expansions.into_iter().map(insert_ws_into).join(""),
5454
})
5555
} else {
5656
None
@@ -83,7 +83,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
8383
// FIXME:
8484
// macro expansion may lose all white space information
8585
// But we hope someday we can use ra_fmt for that
86-
let expansion = render_with_ws_inserted(expanded?).to_string();
86+
let expansion = insert_ws_into(expanded?).to_string();
8787
Some(ExpandedMacro { name: name.unwrap_or_else(|| "???".to_owned()), expansion })
8888
}
8989

crates/ide_assists/src/handlers/add_missing_impl_members.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use hir::HasSource;
2-
use ide_db::traits::resolve_target_trait;
2+
use ide_db::{helpers::insert_whitespace_into_node::insert_ws_into, traits::resolve_target_trait};
33
use syntax::ast::{self, make, AstNode};
44

55
use crate::{
@@ -105,7 +105,7 @@ fn add_missing_impl_members_inner(
105105
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
106106

107107
let missing_items = filter_assoc_items(
108-
ctx.db(),
108+
&ctx.sema,
109109
&ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
110110
mode,
111111
);
@@ -117,16 +117,24 @@ fn add_missing_impl_members_inner(
117117
let target = impl_def.syntax().text_range();
118118
acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
119119
let target_scope = ctx.sema.scope(impl_def.syntax());
120+
let missing_items = missing_items
121+
.into_iter()
122+
.map(|it| {
123+
if ctx.sema.hir_file_for(it.syntax()).is_macro() {
124+
if let Some(it) = ast::AssocItem::cast(insert_ws_into(it.syntax().clone())) {
125+
return it;
126+
}
127+
}
128+
it.clone_for_update()
129+
})
130+
.collect();
120131
let (new_impl_def, first_new_item) = add_trait_assoc_items_to_impl(
121132
&ctx.sema,
122133
missing_items,
123134
trait_,
124135
impl_def.clone(),
125136
target_scope,
126137
);
127-
// if target_scope.in_macro_file() {
128-
129-
// }
130138
match ctx.config.snippet_cap {
131139
None => builder.replace(target, new_impl_def.to_string()),
132140
Some(cap) => {
@@ -893,6 +901,44 @@ impl Default for Foo {
893901
Self(Default::default())
894902
}
895903
}
904+
"#,
905+
)
906+
}
907+
908+
#[test]
909+
fn test_from_macro() {
910+
check_assist(
911+
add_missing_default_members,
912+
r#"
913+
macro_rules! foo {
914+
() => {
915+
trait FooB {
916+
fn foo<'lt>(&'lt self) {}
917+
}
918+
}
919+
}
920+
foo!();
921+
struct Foo(usize);
922+
923+
impl FooB for Foo {
924+
$0
925+
}
926+
"#,
927+
r#"
928+
macro_rules! foo {
929+
() => {
930+
trait FooB {
931+
fn foo<'lt>(&'lt self) {}
932+
}
933+
}
934+
}
935+
foo!();
936+
struct Foo(usize);
937+
938+
impl FooB for Foo {
939+
$0fn foo< 'lt>(& 'lt self){}
940+
941+
}
896942
"#,
897943
)
898944
}

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hir::ModuleDef;
2+
use ide_db::helpers::insert_whitespace_into_node::insert_ws_into;
23
use ide_db::helpers::{
34
get_path_at_cursor_in_tt, import_assets::NameToImport, mod_path_to_ast,
45
parse_tt_as_comma_sep_paths,
@@ -170,7 +171,7 @@ fn impl_def_from_trait(
170171
) -> Option<(ast::Impl, ast::AssocItem)> {
171172
let trait_ = trait_?;
172173
let target_scope = sema.scope(annotated_name.syntax());
173-
let trait_items = filter_assoc_items(sema.db, &trait_.items(sema.db), DefaultMethods::No);
174+
let trait_items = filter_assoc_items(sema, &trait_.items(sema.db), DefaultMethods::No);
174175
if trait_items.is_empty() {
175176
return None;
176177
}
@@ -193,6 +194,17 @@ fn impl_def_from_trait(
193194
node
194195
};
195196

197+
let trait_items = trait_items
198+
.into_iter()
199+
.map(|it| {
200+
if sema.hir_file_for(it.syntax()).is_macro() {
201+
if let Some(it) = ast::AssocItem::cast(insert_ws_into(it.syntax().clone())) {
202+
return it;
203+
}
204+
}
205+
it.clone_for_update()
206+
})
207+
.collect();
196208
let (impl_def, first_assoc_item) =
197209
add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope);
198210

crates/ide_assists/src/utils.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops;
55
use itertools::Itertools;
66

77
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
8-
use hir::{db::HirDatabase, HasSource, HirDisplay};
8+
use hir::{db::HirDatabase, HirDisplay, Semantics};
99
use ide_db::{
1010
helpers::FamousDefs, helpers::SnippetCap, path_transform::PathTransform, RootDatabase,
1111
};
@@ -92,7 +92,7 @@ pub enum DefaultMethods {
9292
}
9393

9494
pub fn filter_assoc_items(
95-
db: &RootDatabase,
95+
sema: &Semantics<RootDatabase>,
9696
items: &[hir::AssocItem],
9797
default_methods: DefaultMethods,
9898
) -> Vec<ast::AssocItem> {
@@ -109,11 +109,11 @@ pub fn filter_assoc_items(
109109
items
110110
.iter()
111111
// Note: This throws away items with no source.
112-
.filter_map(|i| {
112+
.filter_map(|&i| {
113113
let item = match i {
114-
hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(db)?.value),
115-
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(db)?.value),
116-
hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(db)?.value),
114+
hir::AssocItem::Function(i) => ast::AssocItem::Fn(sema.source(i)?.value),
115+
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(sema.source(i)?.value),
116+
hir::AssocItem::Const(i) => ast::AssocItem::Const(sema.source(i)?.value),
117117
};
118118
Some(item)
119119
})
@@ -129,7 +129,7 @@ pub fn filter_assoc_items(
129129
}
130130

131131
pub fn add_trait_assoc_items_to_impl(
132-
sema: &hir::Semantics<ide_db::RootDatabase>,
132+
sema: &Semantics<RootDatabase>,
133133
items: Vec<ast::AssocItem>,
134134
trait_: hir::Trait,
135135
impl_: ast::Impl,
@@ -140,7 +140,6 @@ pub fn add_trait_assoc_items_to_impl(
140140
let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
141141

142142
let items = items.into_iter().map(|assoc_item| {
143-
let assoc_item = assoc_item.clone_for_update();
144143
transform.apply(assoc_item.syntax());
145144
assoc_item.remove_attrs_and_docs();
146145
assoc_item

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
152152
}
153153
}
154154
hir::PathResolution::Def(
155-
def @ (hir::ModuleDef::Adt(_)
155+
def
156+
@
157+
(hir::ModuleDef::Adt(_)
156158
| hir::ModuleDef::TypeAlias(_)
157159
| hir::ModuleDef::BuiltinType(_)),
158160
) => {

crates/ide_db/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod generated_lints;
44
pub mod import_assets;
55
pub mod insert_use;
66
pub mod merge_imports;
7-
pub mod render_macro_node;
7+
pub mod insert_whitespace_into_node;
88
pub mod node_ext;
99
pub mod rust_doc;
1010

crates/ide_db/src/helpers/render_macro_node.rs renamed to crates/ide_db/src/helpers/insert_whitespace_into_node.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Utilities for formatting macro expanded nodes until we get a proper formatter.
12
use syntax::{
23
ast::make,
34
ted::{self, Position},
@@ -9,7 +10,7 @@ use syntax::{
910
// FIXME: It would also be cool to share logic here and in the mbe tests,
1011
// which are pretty unreadable at the moment.
1112
/// Renders a [`SyntaxNode`] with whitespace inserted between tokens that require them.
12-
pub fn render_with_ws_inserted(syn: SyntaxNode) -> SyntaxNode {
13+
pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
1314
let mut indent = 0;
1415
let mut last: Option<SyntaxKind> = None;
1516
let mut mods = Vec::new();
@@ -40,7 +41,9 @@ pub fn render_with_ws_inserted(syn: SyntaxNode) -> SyntaxNode {
4041
make::tokens::whitespace(&" ".repeat(2 * indent)),
4142
));
4243
}
43-
mods.push((Position::after(node), make::tokens::single_newline()));
44+
if node.parent().is_some() {
45+
mods.push((Position::after(node), make::tokens::single_newline()));
46+
}
4447
continue;
4548
}
4649
_ => continue,
@@ -82,7 +85,7 @@ pub fn render_with_ws_inserted(syn: SyntaxNode) -> SyntaxNode {
8285
}
8386
mods.push(do_nl(after, tok));
8487
}
85-
LIFETIME_IDENT if is_next(|it| it == IDENT || it == MUT_KW, true) => {
88+
LIFETIME_IDENT if is_next(|it| is_text(it), true) => {
8689
mods.push(do_ws(after, tok));
8790
}
8891
AS_KW => {

0 commit comments

Comments
 (0)