Skip to content

Commit 518cfe8

Browse files
committed
Auto merge of rust-lang#16723 - norskeld:constants-autocomplete, r=Veykril
fix: autocomplete constants inside format strings Hi! This PR adds autocompletion for constants (including statics) inside format strings and closes rust-lang#16608. I'm not sure about adding the `constants` field to the `CompletionContext`. It kinda makes sense, since it's in line with the `locals` field, and this way everything looks a bit cleaner, but at the same time does it really need to be there? Anyway, let me know if anything should/can be changed. :)
2 parents d444acd + 1d28aec commit 518cfe8

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

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

+93-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Completes identifiers in format string literals.
22
3-
use ide_db::syntax_helpers::format_string::is_format_string;
3+
use hir::{ModuleDef, ScopeDef};
4+
use ide_db::{syntax_helpers::format_string::is_format_string, SymbolKind};
45
use itertools::Itertools;
56
use syntax::{ast, AstToken, TextRange, TextSize};
67

@@ -33,7 +34,23 @@ pub(crate) fn format_string(
3334
ctx.locals.iter().for_each(|(name, _)| {
3435
CompletionItem::new(CompletionItemKind::Binding, source_range, name.to_smol_str())
3536
.add_to(acc, ctx.db);
36-
})
37+
});
38+
ctx.scope.process_all_names(&mut |name, scope| {
39+
if let ScopeDef::ModuleDef(module_def) = scope {
40+
let symbol_kind = match module_def {
41+
ModuleDef::Const(..) => SymbolKind::Const,
42+
ModuleDef::Static(..) => SymbolKind::Static,
43+
_ => return,
44+
};
45+
46+
CompletionItem::new(
47+
CompletionItemKind::SymbolKind(symbol_kind),
48+
source_range,
49+
name.to_smol_str(),
50+
)
51+
.add_to(acc, ctx.db);
52+
}
53+
});
3754
}
3855

3956
#[cfg(test)]
@@ -110,6 +127,80 @@ fn main() {
110127
let foobar = 1;
111128
format_args!("{foobar");
112129
}
130+
"#,
131+
);
132+
}
133+
134+
#[test]
135+
fn completes_constants() {
136+
check_edit(
137+
"FOOBAR",
138+
r#"
139+
//- minicore: fmt
140+
fn main() {
141+
const FOOBAR: usize = 42;
142+
format_args!("{f$0");
143+
}
144+
"#,
145+
r#"
146+
fn main() {
147+
const FOOBAR: usize = 42;
148+
format_args!("{FOOBAR");
149+
}
150+
"#,
151+
);
152+
153+
check_edit(
154+
"FOOBAR",
155+
r#"
156+
//- minicore: fmt
157+
fn main() {
158+
const FOOBAR: usize = 42;
159+
format_args!("{$0");
160+
}
161+
"#,
162+
r#"
163+
fn main() {
164+
const FOOBAR: usize = 42;
165+
format_args!("{FOOBAR");
166+
}
167+
"#,
168+
);
169+
}
170+
171+
#[test]
172+
fn completes_static_constants() {
173+
check_edit(
174+
"FOOBAR",
175+
r#"
176+
//- minicore: fmt
177+
fn main() {
178+
static FOOBAR: usize = 42;
179+
format_args!("{f$0");
180+
}
181+
"#,
182+
r#"
183+
fn main() {
184+
static FOOBAR: usize = 42;
185+
format_args!("{FOOBAR");
186+
}
187+
"#,
188+
);
189+
190+
check_edit(
191+
"FOOBAR",
192+
r#"
193+
//- minicore: fmt
194+
fn main() {
195+
static FOOBAR: usize = 42;
196+
format_args!("{$0");
197+
}
198+
"#,
199+
r#"
200+
fn main() {
201+
static FOOBAR: usize = 42;
202+
format_args!("{FOOBAR");
203+
}
113204
"#,
114205
);
115206
}

0 commit comments

Comments
 (0)