Skip to content

Commit a979378

Browse files
bors[bot]Veykril
andauthored
Merge #11089
11089: internal: Render more completions from hir instead of ast r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 22adcfd + 60dfe8c commit a979378

File tree

11 files changed

+54
-96
lines changed

11 files changed

+54
-96
lines changed

crates/ide/src/hover/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ where
420420
E: Fn(&D) -> Option<V>,
421421
V: Display,
422422
{
423-
let label = if let Some(value) = (value_extractor)(&def) {
423+
let label = if let Some(value) = value_extractor(&def) {
424424
format!("{} = {}", def.display(db), value)
425425
} else {
426426
def.display(db).to_string()

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ fn foo() { let _ = lib::S::$0 }
287287
"#,
288288
expect![[r#"
289289
fn public_method() fn()
290-
ct PUBLIC_CONST pub const PUBLIC_CONST: u32;
291-
ta PublicType pub type PublicType;
290+
ct PUBLIC_CONST pub const PUBLIC_CONST: u32
291+
ta PublicType pub type PublicType = u32
292292
"#]],
293293
);
294294
}
@@ -377,12 +377,12 @@ trait Sub: Super {
377377
fn foo<T: Sub>() { T::$0 }
378378
"#,
379379
expect![[r#"
380-
ta SubTy (as Sub) type SubTy;
381-
ta Ty (as Super) type Ty;
382-
ct C2 (as Sub) const C2: ();
380+
ta SubTy (as Sub) type SubTy
381+
ta Ty (as Super) type Ty
382+
ct C2 (as Sub) const C2: ()
383383
fn subfunc() (as Sub) fn()
384384
me submethod(…) (as Sub) fn(&self)
385-
ct CONST (as Super) const CONST: u8;
385+
ct CONST (as Super) const CONST: u8
386386
fn func() (as Super) fn()
387387
me method(…) (as Super) fn(&self)
388388
"#]],
@@ -417,12 +417,12 @@ impl<T> Sub for Wrap<T> {
417417
}
418418
"#,
419419
expect![[r#"
420-
ta SubTy (as Sub) type SubTy;
421-
ta Ty (as Super) type Ty;
422-
ct CONST (as Super) const CONST: u8;
420+
ta SubTy (as Sub) type SubTy
421+
ta Ty (as Super) type Ty
422+
ct CONST (as Super) const CONST: u8
423423
fn func() (as Super) fn()
424424
me method(…) (as Super) fn(&self)
425-
ct C2 (as Sub) const C2: ();
425+
ct C2 (as Sub) const C2: ()
426426
fn subfunc() (as Sub) fn()
427427
me submethod(…) (as Sub) fn(&self)
428428
"#]],
@@ -653,7 +653,7 @@ impl u8 {
653653
}
654654
"#,
655655
expect![[r#"
656-
ct MAX pub const MAX: Self;
656+
ct MAX pub const MAX: Self
657657
me func(…) fn(self)
658658
"#]],
659659
);

crates/ide_completion/src/render/builder_ext.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Extensions for `Builder` structure required for item rendering.
22
33
use itertools::Itertools;
4+
use syntax::SmolStr;
45

56
use crate::{context::PathKind, item::Builder, patterns::ImmediateLocation, CompletionContext};
67

@@ -56,7 +57,7 @@ impl Builder {
5657
pub(super) fn add_call_parens(
5758
&mut self,
5859
ctx: &CompletionContext,
59-
name: String,
60+
name: SmolStr,
6061
params: Params,
6162
) -> &mut Builder {
6263
if !self.should_add_parens(ctx) {

crates/ide_completion/src/render/const_.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Renderer for `const` fields.
22
3-
use hir::{AsAssocItem, HasSource};
3+
use hir::{AsAssocItem, HirDisplay};
44
use ide_db::SymbolKind;
5-
use syntax::display::const_label;
65

76
use crate::{item::CompletionItem, render::RenderContext};
87

@@ -14,8 +13,7 @@ pub(crate) fn render_const(ctx: RenderContext<'_>, const_: hir::Const) -> Option
1413
fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
1514
let db = ctx.db();
1615
let name = const_.name(db)?.to_smol_str();
17-
// FIXME: This is parsing files!
18-
let detail = const_label(&const_.source(db)?.value);
16+
let detail = const_.display(db).to_string();
1917

2018
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), name.clone());
2119
item.set_documentation(ctx.docs(const_))

crates/ide_completion/src/render/enum_variant.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::iter;
55
use hir::{db::HirDatabase, HasAttrs, HirDisplay, StructKind};
66
use ide_db::SymbolKind;
77
use itertools::Itertools;
8+
use syntax::SmolStr;
89

910
use crate::{
1011
item::{CompletionItem, ImportEdit},
@@ -48,10 +49,10 @@ fn render(
4849
false,
4950
),
5051
};
52+
let qualified_name = qualified_name.to_string();
53+
let short_qualified_name: SmolStr = short_qualified_name.to_string().into();
5154

52-
// FIXME: ModPath::to_smol_str()?
53-
let mut item =
54-
CompletionItem::new(SymbolKind::Variant, ctx.source_range(), qualified_name.to_string());
55+
let mut item = CompletionItem::new(SymbolKind::Variant, ctx.source_range(), qualified_name);
5556
item.set_documentation(variant.docs(db))
5657
.set_deprecated(ctx.is_deprecated(variant))
5758
.detail(detail(db, variant, variant_kind));
@@ -60,8 +61,6 @@ fn render(
6061
item.add_import(import_to_add);
6162
}
6263

63-
// FIXME: ModPath::to_smol_str()?
64-
let short_qualified_name = short_qualified_name.to_string();
6564
if variant_kind == hir::StructKind::Tuple {
6665
cov_mark::hit!(inserts_parens_for_tuple_enums);
6766
let params = Params::Anonymous(variant.fields(db).len());

crates/ide_completion/src/render/function.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ fn render(
5252
let name = local_name.unwrap_or_else(|| func.name(db));
5353
let params = params(completion, func, &func_type);
5454

55-
// FIXME: SmolStr?
5655
let call = match &func_type {
57-
FuncType::Method(Some(receiver)) => format!("{}.{}", receiver, &name),
58-
_ => name.to_string(),
56+
FuncType::Method(Some(receiver)) => format!("{}.{}", receiver, &name).into(),
57+
_ => name.to_smol_str(),
5958
};
6059
let mut item = CompletionItem::new(
6160
if func.self_param(db).is_some() {
@@ -66,23 +65,6 @@ fn render(
6665
ctx.source_range(),
6766
call.clone(),
6867
);
69-
item.set_documentation(ctx.docs(func))
70-
.set_deprecated(ctx.is_deprecated(func) || ctx.is_deprecated_assoc_item(func))
71-
.detail(detail(db, func))
72-
.add_call_parens(completion, call.clone(), params);
73-
74-
if import_to_add.is_none() {
75-
if let Some(actm) = func.as_assoc_item(db) {
76-
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
77-
item.trait_name(trt.name(db).to_smol_str());
78-
}
79-
}
80-
}
81-
82-
if let Some(import_to_add) = import_to_add {
83-
item.add_import(import_to_add);
84-
}
85-
item.lookup_by(name.to_smol_str());
8668

8769
let ret_type = func.ret_type(db);
8870
item.set_relevance(CompletionRelevance {
@@ -100,6 +82,24 @@ fn render(
10082
}
10183
}
10284

85+
item.set_documentation(ctx.docs(func))
86+
.set_deprecated(ctx.is_deprecated(func) || ctx.is_deprecated_assoc_item(func))
87+
.detail(detail(db, func))
88+
.add_call_parens(completion, call, params);
89+
90+
if import_to_add.is_none() {
91+
if let Some(actm) = func.as_assoc_item(db) {
92+
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
93+
item.trait_name(trt.name(db).to_smol_str());
94+
}
95+
}
96+
}
97+
98+
if let Some(import_to_add) = import_to_add {
99+
item.add_import(import_to_add);
100+
}
101+
item.lookup_by(name.to_smol_str());
102+
103103
item.build()
104104
}
105105

crates/ide_completion/src/render/type_alias.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Renderer for type aliases.
22
3-
use hir::{AsAssocItem, HasSource};
3+
use hir::{AsAssocItem, HirDisplay};
44
use ide_db::SymbolKind;
5-
use syntax::{ast::HasName, display::type_label};
5+
use syntax::SmolStr;
66

77
use crate::{item::CompletionItem, render::RenderContext};
88

@@ -29,16 +29,12 @@ fn render(
2929
) -> Option<CompletionItem> {
3030
let db = ctx.db();
3131

32-
// FIXME: This parses the file!
33-
let ast_node = type_alias.source(db)?.value;
34-
let name = ast_node.name().map(|name| {
35-
if with_eq {
36-
format!("{} = ", name.text())
37-
} else {
38-
name.text().to_string()
39-
}
40-
})?;
41-
let detail = type_label(&ast_node);
32+
let name = if with_eq {
33+
SmolStr::from_iter([&*type_alias.name(db).to_smol_str(), " = "])
34+
} else {
35+
type_alias.name(db).to_smol_str()
36+
};
37+
let detail = type_alias.display(db).to_string();
4238

4339
let mut item = CompletionItem::new(SymbolKind::TypeAlias, ctx.source_range(), name.clone());
4440
item.set_documentation(ctx.docs(type_alias))

crates/ide_completion/src/tests/expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ fn func() {
546546
ev TupleV(…) (u32)
547547
ev RecordV {field: u32}
548548
ev UnitV ()
549-
ct ASSOC_CONST const ASSOC_CONST: ();
549+
ct ASSOC_CONST const ASSOC_CONST: ()
550550
fn assoc_fn() fn()
551-
ta AssocType type AssocType;
551+
ta AssocType type AssocType = ()
552552
"#]],
553553
);
554554
}

crates/ide_completion/src/tests/pattern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ fn func() {
294294
ev TupleV(…) (u32)
295295
ev RecordV {field: u32}
296296
ev UnitV ()
297-
ct ASSOC_CONST const ASSOC_CONST: ();
297+
ct ASSOC_CONST const ASSOC_CONST: ()
298298
fn assoc_fn() fn()
299-
ta AssocType type AssocType;
299+
ta AssocType type AssocType = ()
300300
"#]],
301301
);
302302
}

crates/ide_completion/src/tests/type_pos.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {}
148148
kw self
149149
kw super
150150
kw crate
151-
ta Foo = (as Trait2) type Foo;
151+
ta Foo = (as Trait2) type Foo
152152
tp T
153153
cp CONST_PARAM
154154
tt Trait
@@ -199,7 +199,7 @@ impl Enum {
199199
fn func(_: Enum::$0) {}
200200
"#,
201201
expect![[r#"
202-
ta AssocType type AssocType;
202+
ta AssocType type AssocType = ()
203203
"#]],
204204
);
205205
}

0 commit comments

Comments
 (0)