Skip to content

Commit 14cc41f

Browse files
authored
Merge pull request #20438 from A4-Tacks/fix-guess-renamed-macro-braces
Fix guess renamed macro braces
2 parents 47037ad + cfa0893 commit 14cc41f

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

crates/ide-completion/src/render/macro_.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ fn render(
4646
ctx.source_range()
4747
};
4848

49-
let (name, escaped_name) =
50-
(name.as_str(), name.display(ctx.db(), completion.edition).to_smolstr());
49+
let orig_name = macro_.name(ctx.db());
50+
let (name, orig_name, escaped_name) = (
51+
name.as_str(),
52+
orig_name.as_str(),
53+
name.display(ctx.db(), completion.edition).to_smolstr(),
54+
);
5155
let docs = ctx.docs(macro_);
5256
let docs_str = docs.as_ref().map(Documentation::as_str).unwrap_or_default();
5357
let is_fn_like = macro_.is_fn_like(completion.db);
54-
let (bra, ket) = if is_fn_like { guess_macro_braces(name, docs_str) } else { ("", "") };
58+
let (bra, ket) =
59+
if is_fn_like { guess_macro_braces(name, orig_name, docs_str) } else { ("", "") };
5560

5661
let needs_bang = is_fn_like && !is_use_path && !has_macro_bang;
5762

@@ -109,9 +114,13 @@ fn banged_name(name: &str) -> SmolStr {
109114
SmolStr::from_iter([name, "!"])
110115
}
111116

112-
fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) {
117+
fn guess_macro_braces(
118+
macro_name: &str,
119+
orig_name: &str,
120+
docs: &str,
121+
) -> (&'static str, &'static str) {
113122
let mut votes = [0, 0, 0];
114-
for (idx, s) in docs.match_indices(&macro_name) {
123+
for (idx, s) in docs.match_indices(macro_name).chain(docs.match_indices(orig_name)) {
115124
let (before, after) = (&docs[..idx], &docs[idx + s.len()..]);
116125
// Ensure to match the full word
117126
if after.starts_with('!')
@@ -240,7 +249,25 @@ fn main() { $0 }
240249
macro_rules! foo { () => {} }
241250
fn main() { foo! {$0} }
242251
"#,
243-
)
252+
);
253+
254+
check_edit(
255+
"bar!",
256+
r#"
257+
/// `foo![]`
258+
#[macro_export]
259+
macro_rules! foo { () => {} }
260+
pub use crate::foo as bar;
261+
fn main() { $0 }
262+
"#,
263+
r#"
264+
/// `foo![]`
265+
#[macro_export]
266+
macro_rules! foo { () => {} }
267+
pub use crate::foo as bar;
268+
fn main() { bar![$0] }
269+
"#,
270+
);
244271
}
245272

246273
#[test]

0 commit comments

Comments
 (0)