Skip to content

Commit 7e4c4d8

Browse files
committed
Move pretty_pat_in_macro to utils
1 parent 1669108 commit 7e4c4d8

3 files changed

Lines changed: 42 additions & 65 deletions

File tree

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

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
use std::collections::BTreeSet;
22

33
use either::Either;
4-
use hir::{
5-
FileRange, PathResolution, Semantics, TypeInfo,
6-
db::{ExpandDatabase, HirDatabase},
7-
sym,
8-
};
4+
use hir::{FileRange, PathResolution, Semantics, TypeInfo, db::HirDatabase, sym};
95
use ide_db::{
106
EditionedFileId, FxHashMap, RootDatabase,
117
base_db::Crate,
128
defs::Definition,
139
imports::insert_use::remove_use_tree_if_simple,
1410
path_transform::PathTransform,
1511
search::{FileReference, FileReferenceNode, SearchScope},
16-
syntax_helpers::{node_ext::expr_as_name_ref, prettify_macro_expansion},
12+
syntax_helpers::node_ext::expr_as_name_ref,
1713
};
1814
use itertools::{Itertools, izip};
1915
use syntax::{
@@ -29,6 +25,7 @@ use syntax::{
2925
use crate::{
3026
AssistId,
3127
assist_context::{AssistContext, Assists},
28+
utils::pretty_node_inside_macro,
3229
};
3330

3431
// Assist: inline_into_callers
@@ -350,15 +347,10 @@ fn inline(
350347
) -> ast::Expr {
351348
let make = file_editor.make();
352349
let file_id = sema.hir_file_for(fn_body.syntax());
353-
let body_to_clone = if let Some(macro_file) = file_id.macro_file() {
350+
if file_id.is_macro() {
354351
cov_mark::hit!(inline_call_defined_in_macro);
355-
let span_map = sema.db.expansion_span_map(macro_file);
356-
let body_prettified =
357-
prettify_macro_expansion(sema.db, fn_body.syntax().clone(), span_map, *krate);
358-
if let Some(body) = ast::BlockExpr::cast(body_prettified) { body } else { fn_body.clone() }
359-
} else {
360-
fn_body.clone()
361-
};
352+
}
353+
let body_to_clone = pretty_node_inside_macro(fn_body.clone(), sema, file_id, *krate);
362354

363355
// Capture before `with_ast_node` re-roots and loses the source-relative position.
364356
let mut original_body_indent = IndentLevel::from_node(body_to_clone.syntax());
@@ -493,21 +485,9 @@ fn inline(
493485
let expr: &ast::Expr = expr;
494486

495487
let mut insert_let_stmt = || {
496-
let param_ty = param_ty.clone().map(|param_ty| {
497-
let file_id = sema.hir_file_for(param_ty.syntax());
498-
if let Some(macro_file) = file_id.macro_file() {
499-
let span_map = sema.db.expansion_span_map(macro_file);
500-
let param_ty_prettified = prettify_macro_expansion(
501-
sema.db,
502-
param_ty.syntax().clone(),
503-
span_map,
504-
*krate,
505-
);
506-
ast::Type::cast(param_ty_prettified).unwrap_or(param_ty)
507-
} else {
508-
param_ty
509-
}
510-
});
488+
let param_ty = param_ty
489+
.clone()
490+
.map(|param_ty| pretty_node_inside_macro(param_ty, sema, file_id, *krate));
511491

512492
let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty);
513493

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use hir::db::ExpandDatabase;
21
use itertools::Itertools;
32
use std::iter::successors;
43

@@ -502,20 +501,11 @@ fn pretty_pat_inside_macro(
502501
pat: Option<ast::Pat>,
503502
sema: &hir::Semantics<'_, RootDatabase>,
504503
) -> Option<ast::Pat> {
505-
let pretty = |pat| {
506-
let db = sema.db;
507-
let scope = sema.scope(&pat)?;
508-
let file_id = scope.file_id().macro_file()?;
509-
// Don't call `prettify_macro_expansion()` outside the actual assist action; see inline_macro assist
510-
let pretty_node = hir::prettify_macro_expansion(
511-
db,
512-
pat,
513-
db.expansion_span_map(file_id),
514-
scope.module().krate(db).into(),
515-
);
516-
ast::Pat::cast(pretty_node)
504+
let pretty = |pat: ast::Pat| {
505+
let scope = sema.scope(pat.syntax())?;
506+
Some(crate::utils::pretty_node_inside_macro(pat, sema, scope.file_id(), scope.krate()))
517507
};
518-
pat.map(|pat| pretty(pat.syntax().clone()).unwrap_or(pat))
508+
pat.map(|pat| pretty(pat.clone()).unwrap_or(pat))
519509
}
520510

521511
#[cfg(test)]

crates/ide-assists/src/utils.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -219,24 +219,7 @@ pub fn add_trait_assoc_items_to_impl(
219219
original_items
220220
.iter()
221221
.map(|InFile { file_id, value: original_item }| {
222-
let mut cloned_item = {
223-
if let Some(macro_file) = file_id.macro_file() {
224-
let span_map = sema.db.expansion_span_map(macro_file);
225-
let item_prettified = prettify_macro_expansion(
226-
sema.db,
227-
original_item.syntax().clone(),
228-
span_map,
229-
target_scope.krate().into(),
230-
);
231-
if let Some(formatted) = ast::AssocItem::cast(item_prettified) {
232-
return formatted;
233-
} else {
234-
stdx::never!("formatted `AssocItem` could not be cast back to `AssocItem`");
235-
}
236-
}
237-
original_item
238-
}
239-
.reset_indent();
222+
let mut cloned_item = original_item.reset_indent();
240223

241224
if let Some(source_scope) = sema.scope(original_item.syntax()) {
242225
// FIXME: Paths in nested macros are not handled well. See
@@ -245,17 +228,16 @@ pub fn add_trait_assoc_items_to_impl(
245228
PathTransform::trait_impl(target_scope, &source_scope, trait_, impl_.clone());
246229
cloned_item = ast::AssocItem::cast(transform.apply(cloned_item.syntax())).unwrap();
247230
}
231+
let cloned_item =
232+
pretty_node_inside_macro(cloned_item, sema, *file_id, target_scope.krate());
248233
let (editor, cloned_item) = SyntaxEditor::with_ast_node(&cloned_item);
249234
cloned_item.remove_attrs_and_docs(&editor);
250235
ast::AssocItem::cast(editor.finish().new_root().clone()).unwrap()
251236
})
252237
.filter_map(|item| match item {
253238
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
254239
let (fn_editor, fn_) = SyntaxEditor::with_ast_node(&fn_);
255-
let fill_expr: ast::Expr = match config.expr_fill_default {
256-
ExprFillDefaultMode::Todo | ExprFillDefaultMode::Default => make.expr_todo(),
257-
ExprFillDefaultMode::Underscore => make.expr_underscore().into(),
258-
};
240+
let fill_expr: ast::Expr = expr_fill_default(config);
259241
let new_body = make.block_expr(None::<ast::Stmt>, Some(fill_expr));
260242
fn_.replace_or_insert_body(&fn_editor, new_body);
261243
let new_fn_ = fn_editor.finish().new_root().clone();
@@ -275,6 +257,31 @@ pub fn add_trait_assoc_items_to_impl(
275257
.collect()
276258
}
277259

260+
pub(crate) fn pretty_node_inside_macro<T: AstNode>(
261+
node: T,
262+
sema: &Semantics<'_, RootDatabase>,
263+
file_id: hir::HirFileId,
264+
target_krate: impl Into<ide_db::base_db::Crate>,
265+
) -> T {
266+
match file_id.macro_file() {
267+
Some(file_id) => {
268+
// Don't call `prettify_macro_expansion()` outside the actual assist action; see inline_macro assist
269+
let pretty_node = prettify_macro_expansion(
270+
sema.db,
271+
node.syntax().clone(),
272+
sema.db.expansion_span_map(file_id),
273+
target_krate.into(),
274+
);
275+
let Some(new_node) = T::cast(pretty_node) else {
276+
stdx::never!("prettify_macro_expansion changes node kind");
277+
return node;
278+
};
279+
new_node
280+
}
281+
None => node,
282+
}
283+
}
284+
278285
pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
279286
node.children_with_tokens()
280287
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))

0 commit comments

Comments
 (0)