Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 46d2271

Browse files
committed
Remove pattern rendering hack
1 parent 06ee4d6 commit 46d2271

File tree

12 files changed

+235
-122
lines changed

12 files changed

+235
-122
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use crate::{
3636
const_::render_const,
3737
function::{render_fn, render_method},
3838
literal::{render_struct_literal, render_variant_lit},
39-
macro_::render_macro,
39+
macro_::{render_macro, render_macro_pat},
4040
pattern::{render_struct_pat, render_variant_pat},
41-
render_field, render_path_resolution, render_resolution_simple, render_tuple_field,
41+
render_field, render_path_resolution, render_pattern_resolution, render_tuple_field,
4242
type_alias::{render_type_alias, render_type_alias_with_eq},
4343
union_literal::render_union_literal,
4444
RenderContext,
@@ -134,10 +134,14 @@ impl Completions {
134134
item.add_to(self);
135135
}
136136

137-
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
137+
pub(crate) fn add_crate_roots(
138+
&mut self,
139+
ctx: &CompletionContext,
140+
path_ctx: &PathCompletionCtx,
141+
) {
138142
ctx.process_all_names(&mut |name, res| match res {
139143
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
140-
self.add_module(ctx, m, name);
144+
self.add_module(ctx, path_ctx, m, name);
141145
}
142146
_ => (),
143147
});
@@ -160,25 +164,36 @@ impl Completions {
160164
);
161165
}
162166

163-
pub(crate) fn add_resolution_simple(
167+
pub(crate) fn add_pattern_resolution(
164168
&mut self,
165169
ctx: &CompletionContext,
170+
pattern_ctx: &PatternContext,
166171
local_name: hir::Name,
167172
resolution: hir::ScopeDef,
168173
) {
169174
if ctx.is_scope_def_hidden(resolution) {
175+
cov_mark::hit!(qualified_path_doc_hidden);
170176
return;
171177
}
172-
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build());
178+
self.add(
179+
render_pattern_resolution(RenderContext::new(ctx), pattern_ctx, local_name, resolution)
180+
.build(),
181+
);
173182
}
174183

175184
pub(crate) fn add_module(
176185
&mut self,
177186
ctx: &CompletionContext,
187+
path_ctx: &PathCompletionCtx,
178188
module: hir::Module,
179189
local_name: hir::Name,
180190
) {
181-
self.add_resolution_simple(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
191+
self.add_path_resolution(
192+
ctx,
193+
path_ctx,
194+
local_name,
195+
hir::ScopeDef::ModuleDef(module.into()),
196+
);
182197
}
183198

184199
pub(crate) fn add_macro(
@@ -204,6 +219,29 @@ impl Completions {
204219
);
205220
}
206221

222+
pub(crate) fn add_macro_pat(
223+
&mut self,
224+
ctx: &CompletionContext,
225+
pattern_ctx: &PatternContext,
226+
mac: hir::Macro,
227+
local_name: hir::Name,
228+
) {
229+
let is_private_editable = match ctx.is_visible(&mac) {
230+
Visible::Yes => false,
231+
Visible::Editable => true,
232+
Visible::No => return,
233+
};
234+
self.add(
235+
render_macro_pat(
236+
RenderContext::new(ctx).private_editable(is_private_editable),
237+
pattern_ctx,
238+
local_name,
239+
mac,
240+
)
241+
.build(),
242+
);
243+
}
244+
207245
pub(crate) fn add_function(
208246
&mut self,
209247
ctx: &CompletionContext,

crates/ide-completion/src/completions/attribute.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,24 @@ pub(crate) fn complete_attribute_path(
9595
acc.add_macro(ctx, path_ctx, m, name)
9696
}
9797
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
98-
acc.add_module(ctx, m, name)
98+
acc.add_module(ctx, path_ctx, m, name)
9999
}
100100
_ => (),
101101
}
102102
}
103103
return;
104104
}
105105
// fresh use tree with leading colon2, only show crate roots
106-
Qualified::Absolute => acc.add_crate_roots(ctx),
106+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
107107
// only show modules in a fresh UseTree
108108
Qualified::No => {
109109
ctx.process_all_names(&mut |name, def| match def {
110110
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
111111
acc.add_macro(ctx, path_ctx, m, name)
112112
}
113-
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
113+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
114+
acc.add_module(ctx, path_ctx, m, name)
115+
}
114116
_ => (),
115117
});
116118
acc.add_nameref_keywords_with_colon(ctx);

crates/ide-completion/src/completions/attribute/derive.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ pub(crate) fn complete_derive_path(
3535
{
3636
acc.add_macro(ctx, path_ctx, mac, name)
3737
}
38-
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
38+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
39+
acc.add_module(ctx, path_ctx, m, name)
40+
}
3941
_ => (),
4042
}
4143
}
4244
}
43-
Qualified::Absolute => acc.add_crate_roots(ctx),
45+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
4446
// only show modules in a fresh UseTree
4547
Qualified::No => {
4648
ctx.process_all_names(&mut |name, def| {
@@ -51,7 +53,7 @@ pub(crate) fn complete_derive_path(
5153
mac
5254
}
5355
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
54-
return acc.add_module(ctx, m, name);
56+
return acc.add_module(ctx, path_ctx, m, name);
5557
}
5658
_ => return,
5759
};

crates/ide-completion/src/completions/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub(crate) fn complete_expr_path(
152152
_ => (),
153153
}
154154
}
155-
Qualified::Absolute => acc.add_crate_roots(ctx),
155+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
156156
Qualified::No => {
157157
acc.add_nameref_keywords_with_colon(ctx);
158158
if let Some(adt) =

crates/ide-completion/src/completions/flyimport.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
CompletionContext, DotAccess, PathCompletionCtx, PathKind, PatternContext, Qualified,
1313
TypeLocation,
1414
},
15-
render::{render_resolution_with_import, RenderContext},
15+
render::{render_resolution_with_import, render_resolution_with_import_pat, RenderContext},
1616
};
1717

1818
use super::Completions;
@@ -149,32 +149,22 @@ pub(crate) fn import_on_the_fly_path(
149149
pub(crate) fn import_on_the_fly_pat(
150150
acc: &mut Completions,
151151
ctx: &CompletionContext,
152-
pat_ctx: &PatternContext,
152+
pattern_ctx: &PatternContext,
153153
) -> Option<()> {
154154
if !ctx.config.enable_imports_on_the_fly {
155155
return None;
156156
}
157-
if let PatternContext { record_pat: Some(_), .. } = pat_ctx {
157+
if let PatternContext { record_pat: Some(_), .. } = pattern_ctx {
158158
return None;
159159
}
160160

161161
let potential_import_name = import_name(ctx);
162162
let import_assets = import_assets_for_path(ctx, &potential_import_name, None)?;
163163

164-
import_on_the_fly(
164+
import_on_the_fly_pat2(
165165
acc,
166166
ctx,
167-
&PathCompletionCtx {
168-
has_call_parens: false,
169-
has_macro_bang: false,
170-
qualified: Qualified::No,
171-
parent: None,
172-
// FIXME
173-
path: syntax::ast::make::ext::ident_path("dummy__"),
174-
kind: crate::context::PathKind::Pat { pat_ctx: pat_ctx.clone() },
175-
has_type_args: false,
176-
use_tree_parent: false,
177-
},
167+
pattern_ctx,
178168
import_assets,
179169
ctx.original_token.parent()?,
180170
potential_import_name,
@@ -289,6 +279,50 @@ fn import_on_the_fly(
289279
Some(())
290280
}
291281

282+
fn import_on_the_fly_pat2(
283+
acc: &mut Completions,
284+
ctx: &CompletionContext,
285+
pattern_ctx: &PatternContext,
286+
import_assets: ImportAssets,
287+
position: SyntaxNode,
288+
potential_import_name: String,
289+
) -> Option<()> {
290+
let _p = profile::span("import_on_the_fly_pat").detail(|| potential_import_name.clone());
291+
292+
if ImportScope::find_insert_use_container(&position, &ctx.sema).is_none() {
293+
return None;
294+
}
295+
296+
let ns_filter = |import: &LocatedImport| match import.original_item {
297+
ItemInNs::Macros(mac) => mac.is_fn_like(ctx.db),
298+
ItemInNs::Types(_) => true,
299+
ItemInNs::Values(def) => matches!(def, hir::ModuleDef::Const(_)),
300+
};
301+
let user_input_lowercased = potential_import_name.to_lowercase();
302+
303+
acc.add_all(
304+
import_assets
305+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
306+
.into_iter()
307+
.filter(ns_filter)
308+
.filter(|import| {
309+
!ctx.is_item_hidden(&import.item_to_import)
310+
&& !ctx.is_item_hidden(&import.original_item)
311+
})
312+
.sorted_by_key(|located_import| {
313+
compute_fuzzy_completion_order_key(
314+
&located_import.import_path,
315+
&user_input_lowercased,
316+
)
317+
})
318+
.filter_map(|import| {
319+
render_resolution_with_import_pat(RenderContext::new(ctx), pattern_ctx, import)
320+
})
321+
.map(|builder| builder.build()),
322+
);
323+
Some(())
324+
}
325+
292326
fn import_on_the_fly_method(
293327
acc: &mut Completions,
294328
ctx: &CompletionContext,
@@ -297,7 +331,7 @@ fn import_on_the_fly_method(
297331
position: SyntaxNode,
298332
potential_import_name: String,
299333
) -> Option<()> {
300-
let _p = profile::span("import_on_the_fly").detail(|| potential_import_name.clone());
334+
let _p = profile::span("import_on_the_fly_method").detail(|| potential_import_name.clone());
301335

302336
if ImportScope::find_insert_use_container(&position, &ctx.sema).is_none() {
303337
return None;

crates/ide-completion/src/completions/item_list.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) fn complete_item_list(
4545
acc.add_macro(ctx, path_ctx, m, name)
4646
}
4747
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
48-
acc.add_module(ctx, m, name)
48+
acc.add_module(ctx, path_ctx, m, name)
4949
}
5050
_ => (),
5151
}
@@ -55,13 +55,15 @@ pub(crate) fn complete_item_list(
5555
acc.add_keyword(ctx, "super::");
5656
}
5757
}
58-
Qualified::Absolute => acc.add_crate_roots(ctx),
58+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
5959
Qualified::No if ctx.qualifier_ctx.none() => {
6060
ctx.process_all_names(&mut |name, def| match def {
6161
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(ctx.db) => {
6262
acc.add_macro(ctx, path_ctx, m, name)
6363
}
64-
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
64+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
65+
acc.add_module(ctx, path_ctx, m, name)
66+
}
6567
_ => (),
6668
});
6769
acc.add_nameref_keywords_with_colon(ctx);

crates/ide-completion/src/completions/pattern.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,7 @@ pub(crate) fn complete_pattern(
8282
hir::ModuleDef::Const(..) => refutable,
8383
hir::ModuleDef::Module(..) => true,
8484
hir::ModuleDef::Macro(mac) if mac.is_fn_like(ctx.db) => {
85-
return acc.add_macro(
86-
ctx,
87-
&PathCompletionCtx {
88-
has_call_parens: false,
89-
has_macro_bang: false,
90-
qualified: Qualified::No,
91-
// FIXME
92-
path: syntax::ast::make::ext::ident_path("dummy__"),
93-
parent: None,
94-
kind: crate::context::PathKind::Pat { pat_ctx: pattern_ctx.clone() },
95-
has_type_args: false,
96-
use_tree_parent: false,
97-
},
98-
mac,
99-
name,
100-
);
85+
return acc.add_macro_pat(ctx, pattern_ctx, mac, name);
10186
}
10287
_ => false,
10388
},
@@ -118,7 +103,7 @@ pub(crate) fn complete_pattern(
118103
| ScopeDef::Unknown => false,
119104
};
120105
if add_simple_path {
121-
acc.add_resolution_simple(ctx, name, res);
106+
acc.add_pattern_resolution(ctx, pattern_ctx, name, res);
122107
}
123108
});
124109
}
@@ -207,7 +192,7 @@ pub(crate) fn complete_pattern_path(
207192
}
208193
}
209194
// qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path
210-
Qualified::Absolute => acc.add_crate_roots(ctx),
195+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
211196
Qualified::No => {
212197
ctx.process_all_names(&mut |name, res| {
213198
// FIXME: properly filter here

crates/ide-completion/src/completions/type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(crate) fn complete_type_path(
141141
_ => (),
142142
}
143143
}
144-
Qualified::Absolute => acc.add_crate_roots(ctx),
144+
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
145145
Qualified::No => {
146146
acc.add_nameref_keywords_with_colon(ctx);
147147
if let TypeLocation::TypeBound = location {

crates/ide-completion/src/completions/use_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ pub(crate) fn complete_use_path(
8989
// fresh use tree with leading colon2, only show crate roots
9090
Qualified::Absolute => {
9191
cov_mark::hit!(use_tree_crate_roots_only);
92-
acc.add_crate_roots(ctx);
92+
acc.add_crate_roots(ctx, path_ctx);
9393
}
9494
// only show modules and non-std enum in a fresh UseTree
9595
Qualified::No => {
9696
cov_mark::hit!(unqualified_path_selected_only);
9797
ctx.process_all_names(&mut |name, res| {
9898
match res {
9999
ScopeDef::ModuleDef(hir::ModuleDef::Module(module)) => {
100-
acc.add_module(ctx, module, name);
100+
acc.add_module(ctx, path_ctx, module, name);
101101
}
102102
ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
103103
// exclude prelude enum

crates/ide-completion/src/completions/vis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
pub(crate) fn complete_vis_path(
99
acc: &mut Completions,
1010
ctx: &CompletionContext,
11-
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
11+
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
1212
&has_in_token: &bool,
1313
) {
1414
match qualified {
@@ -23,7 +23,7 @@ pub(crate) fn complete_vis_path(
2323
if let Some(next) = next_towards_current {
2424
if let Some(name) = next.name(ctx.db) {
2525
cov_mark::hit!(visibility_qualified);
26-
acc.add_module(ctx, next, name);
26+
acc.add_module(ctx, path_ctx, next, name);
2727
}
2828
}
2929

0 commit comments

Comments
 (0)