From 1d28aecd13a3a19c3a9fdce81e0c50f0b74caf6b Mon Sep 17 00:00:00 2001 From: Vladislav Mamon Date: Thu, 29 Feb 2024 20:10:30 +0300 Subject: [PATCH] fix: autocomplete constants inside format strings --- .../src/completions/format_string.rs | 95 ++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) 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"); +} "#, ); }