diff --git a/crates/ide-completion/src/completions/format_string.rs b/crates/ide-completion/src/completions/format_string.rs index cecbe75391d..5512ac21534 100644 --- a/crates/ide-completion/src/completions/format_string.rs +++ b/crates/ide-completion/src/completions/format_string.rs @@ -1,6 +1,7 @@ //! Completes identifiers in format string literals. -use ide_db::syntax_helpers::format_string::is_format_string; +use hir::{ModuleDef, ScopeDef}; +use ide_db::{syntax_helpers::format_string::is_format_string, SymbolKind}; use itertools::Itertools; use syntax::{ast, AstToken, TextRange, TextSize}; @@ -33,7 +34,23 @@ pub(crate) fn format_string( ctx.locals.iter().for_each(|(name, _)| { CompletionItem::new(CompletionItemKind::Binding, source_range, name.to_smol_str()) .add_to(acc, ctx.db); - }) + }); + ctx.scope.process_all_names(&mut |name, scope| { + if let ScopeDef::ModuleDef(module_def) = scope { + let symbol_kind = match module_def { + ModuleDef::Const(..) => SymbolKind::Const, + ModuleDef::Static(..) => SymbolKind::Static, + _ => return, + }; + + CompletionItem::new( + CompletionItemKind::SymbolKind(symbol_kind), + source_range, + name.to_smol_str(), + ) + .add_to(acc, ctx.db); + } + }); } #[cfg(test)] @@ -110,6 +127,80 @@ fn main() { let foobar = 1; format_args!("{foobar"); } +"#, + ); + } + + #[test] + fn completes_constants() { + check_edit( + "FOOBAR", + r#" +//- minicore: fmt +fn main() { + const FOOBAR: usize = 42; + format_args!("{f$0"); +} +"#, + r#" +fn main() { + const FOOBAR: usize = 42; + format_args!("{FOOBAR"); +} +"#, + ); + + check_edit( + "FOOBAR", + r#" +//- minicore: fmt +fn main() { + const FOOBAR: usize = 42; + format_args!("{$0"); +} +"#, + r#" +fn main() { + const FOOBAR: usize = 42; + format_args!("{FOOBAR"); +} +"#, + ); + } + + #[test] + fn completes_static_constants() { + check_edit( + "FOOBAR", + r#" +//- minicore: fmt +fn main() { + static FOOBAR: usize = 42; + format_args!("{f$0"); +} +"#, + r#" +fn main() { + static FOOBAR: usize = 42; + format_args!("{FOOBAR"); +} +"#, + ); + + check_edit( + "FOOBAR", + r#" +//- minicore: fmt +fn main() { + static FOOBAR: usize = 42; + format_args!("{$0"); +} +"#, + r#" +fn main() { + static FOOBAR: usize = 42; + format_args!("{FOOBAR"); +} "#, ); }