Skip to content

Commit 6c8c02f

Browse files
committed
Don't parse source files to generate macro completion details
1 parent b1ab577 commit 6c8c02f

File tree

15 files changed

+157
-204
lines changed

15 files changed

+157
-204
lines changed

crates/hir/src/display.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use syntax::SmolStr;
1818

1919
use crate::{
2020
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasCrate, HasVisibility,
21-
LifetimeParam, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeOrConstParam,
22-
TypeParam, Union, Variant,
21+
LifetimeParam, Macro, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias,
22+
TypeOrConstParam, TypeParam, Union, Variant,
2323
};
2424

2525
impl HirDisplay for Function {
@@ -509,3 +509,14 @@ impl HirDisplay for Module {
509509
}
510510
}
511511
}
512+
513+
impl HirDisplay for Macro {
514+
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
515+
match self.id {
516+
hir_def::MacroId::Macro2Id(_) => write!(f, "macro"),
517+
hir_def::MacroId::MacroRulesId(_) => write!(f, "macro_rules!"),
518+
hir_def::MacroId::ProcMacroId(_) => write!(f, "proc_macro"),
519+
}?;
520+
write!(f, " {}", self.name(f.db))
521+
}
522+
}

crates/ide/src/hover/render.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::fmt::Display;
33

44
use either::Either;
5-
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
5+
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HirDisplay, Semantics, TypeInfo};
66
use ide_db::{
77
base_db::SourceDatabase,
88
defs::Definition,
@@ -13,9 +13,7 @@ use ide_db::{
1313
use itertools::Itertools;
1414
use stdx::format_to;
1515
use syntax::{
16-
algo, ast,
17-
display::{fn_as_proc_macro_label, macro_label},
18-
match_ast, AstNode, Direction,
16+
algo, ast, match_ast, AstNode, Direction,
1917
SyntaxKind::{LET_EXPR, LET_STMT},
2018
SyntaxToken, T,
2119
};
@@ -342,14 +340,8 @@ pub(super) fn definition(
342340
) -> Option<Markup> {
343341
let mod_path = definition_mod_path(db, &def);
344342
let (label, docs) = match def {
345-
Definition::Macro(it) => (
346-
match &it.source(db)?.value {
347-
Either::Left(mac) => macro_label(mac),
348-
Either::Right(mac_fn) => fn_as_proc_macro_label(mac_fn),
349-
},
350-
it.attrs(db).docs(),
351-
),
352-
Definition::Field(def) => label_and_docs(db, def),
343+
Definition::Macro(it) => label_and_docs(db, it),
344+
Definition::Field(it) => label_and_docs(db, it),
353345
Definition::Module(it) => label_and_docs(db, it),
354346
Definition::Function(it) => label_and_docs(db, it),
355347
Definition::Adt(it) => label_and_docs(db, it),

crates/ide/src/hover/tests.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,16 +4102,16 @@ identity!{
41024102
}
41034103
"#,
41044104
expect![[r#"
4105-
*Copy*
4105+
*Copy*
41064106
4107-
```rust
4108-
test
4109-
```
4107+
```rust
4108+
test
4109+
```
41104110
4111-
```rust
4112-
pub macro Copy
4113-
```
4114-
"#]],
4111+
```rust
4112+
macro Copy
4113+
```
4114+
"#]],
41154115
);
41164116
}
41174117

@@ -4126,16 +4126,16 @@ pub macro Copy {}
41264126
struct Foo;
41274127
"#,
41284128
expect![[r#"
4129-
*Copy*
4129+
*Copy*
41304130
4131-
```rust
4132-
test
4133-
```
4131+
```rust
4132+
test
4133+
```
41344134
4135-
```rust
4136-
pub macro Copy
4137-
```
4138-
"#]],
4135+
```rust
4136+
macro Copy
4137+
```
4138+
"#]],
41394139
);
41404140
check(
41414141
r#"
@@ -4148,16 +4148,16 @@ mod foo {
41484148
struct Foo;
41494149
"#,
41504150
expect![[r#"
4151-
*Copy*
4151+
*Copy*
41524152
4153-
```rust
4154-
test::foo
4155-
```
4153+
```rust
4154+
test::foo
4155+
```
41564156
4157-
```rust
4158-
pub macro Copy
4159-
```
4160-
"#]],
4157+
```rust
4158+
macro Copy
4159+
```
4160+
"#]],
41614161
);
41624162
}
41634163

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ macro_rules! foo { () => {} }
421421
422422
fn main() { let _ = crate::$0 }
423423
"#,
424-
expect![[r##"
424+
expect![[r#"
425425
fn main() fn()
426-
ma foo!(…) #[macro_export] macro_rules! foo
427-
"##]],
426+
ma foo!(…) macro_rules! foo
427+
"#]],
428428
);
429429
}
430430

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ mod macros {
208208
macro_rules! concat { }
209209
}
210210
"#,
211-
expect![[r##"
211+
expect![[r#"
212212
fn f() fn()
213-
ma concat!(…) #[macro_export] macro_rules! concat
213+
ma concat!(…) macro_rules! concat
214214
md std
215215
bt u32
216-
"##]],
216+
"#]],
217217
);
218218
}
219219

crates/ide_completion/src/render/macro_.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
//! Renderer for macro invocations.
22
3-
use either::Either;
4-
use hir::{Documentation, HasSource, InFile, Semantics};
5-
use ide_db::{RootDatabase, SymbolKind};
6-
use syntax::{
7-
display::{fn_as_proc_macro_label, macro_label},
8-
SmolStr,
9-
};
3+
use hir::{Documentation, HirDisplay};
4+
use ide_db::SymbolKind;
5+
use syntax::SmolStr;
106

117
use crate::{
128
context::PathKind,
@@ -52,7 +48,7 @@ fn render(
5248
label(&ctx, needs_bang, bra, ket, &name),
5349
);
5450
item.set_deprecated(ctx.is_deprecated(macro_))
55-
.set_detail(detail(&completion.sema, macro_))
51+
.detail(macro_.display(completion.db).to_string())
5652
.set_documentation(docs)
5753
.set_relevance(ctx.completion_relevance());
5854

@@ -103,18 +99,6 @@ fn banged_name(name: &str) -> SmolStr {
10399
SmolStr::from_iter([name, "!"])
104100
}
105101

106-
fn detail(sema: &Semantics<RootDatabase>, macro_: hir::Macro) -> Option<String> {
107-
// FIXME: This is parsing the file!
108-
let InFile { file_id, value } = macro_.source(sema.db)?;
109-
let _ = sema.parse_or_expand(file_id);
110-
let detail = match value {
111-
Either::Left(node) => macro_label(&node),
112-
// FIXME: this should render with the derive name, not the function name
113-
Either::Right(node) => fn_as_proc_macro_label(&node),
114-
};
115-
Some(detail)
116-
}
117-
118102
fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) {
119103
let mut votes = [0, 0, 0];
120104
for (idx, s) in docs.match_indices(&macro_name) {

crates/ide_completion/src/tests/attribute.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn proc_macros_qualified() {
6262
struct Foo;
6363
"#,
6464
expect![[r#"
65-
at identity pub macro identity
65+
at identity proc_macro identity
6666
"#]],
6767
)
6868
}
@@ -302,7 +302,7 @@ struct Foo;
302302
"#,
303303
expect![[r#"
304304
md core
305-
at derive pub macro derive
305+
at derive macro derive
306306
kw self::
307307
kw super::
308308
kw crate::
@@ -689,12 +689,12 @@ mod derive {
689689
"#,
690690
expect![[r#"
691691
md core
692-
de Default pub macro Default
692+
de Default macro Default
693693
de Clone, Copy
694-
de PartialEq pub macro PartialEq
694+
de PartialEq macro PartialEq
695695
de PartialEq, Eq
696696
de PartialEq, Eq, PartialOrd, Ord
697-
de Clone pub macro Clone
697+
de Clone macro Clone
698698
de PartialEq, PartialOrd
699699
kw self::
700700
kw super::
@@ -712,11 +712,11 @@ mod derive {
712712
"#,
713713
expect![[r#"
714714
md core
715-
de Default pub macro Default
715+
de Default macro Default
716716
de Clone, Copy
717717
de Eq
718718
de Eq, PartialOrd, Ord
719-
de Clone pub macro Clone
719+
de Clone macro Clone
720720
de PartialOrd
721721
kw self::
722722
kw super::
@@ -734,17 +734,17 @@ mod derive {
734734
"#,
735735
expect![[r#"
736736
md core
737-
de Default pub macro Default
737+
de Default macro Default
738738
de Clone, Copy
739739
de Eq
740740
de Eq, PartialOrd, Ord
741-
de Clone pub macro Clone
741+
de Clone macro Clone
742742
de PartialOrd
743743
kw self::
744744
kw super::
745745
kw crate::
746746
"#]],
747-
)
747+
);
748748
}
749749

750750
#[test]
@@ -761,7 +761,7 @@ mod derive {
761761
kw self::
762762
kw super::
763763
kw crate::
764-
de DeriveIdentity (use proc_macros::DeriveIdentity) pub macro derive_identity
764+
de DeriveIdentity (use proc_macros::DeriveIdentity) proc_macro DeriveIdentity
765765
"#]],
766766
);
767767
check_derive(
@@ -772,7 +772,7 @@ use proc_macros::DeriveIdentity;
772772
#[derive(der$0)] struct Test;
773773
"#,
774774
expect![[r#"
775-
de DeriveIdentity pub macro derive_identity
775+
de DeriveIdentity proc_macro DeriveIdentity
776776
md proc_macros
777777
md core
778778
kw self::
@@ -808,7 +808,7 @@ use proc_macros::DeriveIdentity;
808808
#[derive(proc_macros::$0)] struct Test;
809809
"#,
810810
expect![[r#"
811-
de DeriveIdentity pub macro derive_identity
811+
de DeriveIdentity proc_macro DeriveIdentity
812812
"#]],
813813
);
814814
check_derive(
@@ -818,7 +818,7 @@ use proc_macros::DeriveIdentity;
818818
#[derive(proc_macros::C$0)] struct Test;
819819
"#,
820820
expect![[r#"
821-
de DeriveIdentity pub macro derive_identity
821+
de DeriveIdentity proc_macro DeriveIdentity
822822
"#]],
823823
);
824824
}

crates/ide_completion/src/tests/expression.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn baz() {
3030
}
3131
"#,
3232
// This should not contain `FooDesc {…}`.
33-
expect![[r##"
33+
expect![[r#"
3434
kw unsafe
3535
kw match
3636
kw while
@@ -57,13 +57,13 @@ fn baz() {
5757
fn baz() fn()
5858
st Unit
5959
md _69latrick
60-
ma makro!(…) #[macro_export] macro_rules! makro
60+
ma makro!(…) macro_rules! makro
6161
fn function() fn()
6262
sc STATIC
6363
un Union
6464
ev TupleV(…) (u32)
6565
ct CONST
66-
"##]],
66+
"#]],
6767
)
6868
}
6969

@@ -125,7 +125,7 @@ impl Unit {
125125
}
126126
"#,
127127
// `self` is in here twice, once as the module, once as the local
128-
expect![[r##"
128+
expect![[r#"
129129
me self.foo() fn(self)
130130
kw unsafe
131131
kw fn
@@ -166,14 +166,14 @@ impl Unit {
166166
md module
167167
st Unit
168168
md qualified
169-
ma makro!(…) #[macro_export] macro_rules! makro
169+
ma makro!(…) macro_rules! makro
170170
?? Unresolved
171171
fn function() fn()
172172
sc STATIC
173173
un Union
174174
ev TupleV(…) (u32)
175175
ct CONST
176-
"##]],
176+
"#]],
177177
);
178178
check(
179179
r#"
@@ -187,22 +187,22 @@ impl Unit {
187187
}
188188
}
189189
"#,
190-
expect![[r##"
190+
expect![[r#"
191191
tt Trait
192192
en Enum
193193
st Record
194194
st Tuple
195195
md module
196196
st Unit
197197
md qualified
198-
ma makro!(…) #[macro_export] macro_rules! makro
198+
ma makro!(…) macro_rules! makro
199199
?? Unresolved
200200
fn function() fn()
201201
sc STATIC
202202
un Union
203203
ev TupleV(…) (u32)
204204
ct CONST
205-
"##]],
205+
"#]],
206206
);
207207
}
208208

0 commit comments

Comments
 (0)