|
1 | 1 | //! Completes identifiers in format string literals.
|
2 | 2 |
|
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}; |
4 | 5 | use itertools::Itertools;
|
5 | 6 | use syntax::{ast, AstToken, TextRange, TextSize};
|
6 | 7 |
|
@@ -33,7 +34,23 @@ pub(crate) fn format_string(
|
33 | 34 | ctx.locals.iter().for_each(|(name, _)| {
|
34 | 35 | CompletionItem::new(CompletionItemKind::Binding, source_range, name.to_smol_str())
|
35 | 36 | .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 | + }); |
37 | 54 | }
|
38 | 55 |
|
39 | 56 | #[cfg(test)]
|
@@ -110,6 +127,80 @@ fn main() {
|
110 | 127 | let foobar = 1;
|
111 | 128 | format_args!("{foobar");
|
112 | 129 | }
|
| 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 | +} |
113 | 204 | "#,
|
114 | 205 | );
|
115 | 206 | }
|
|
0 commit comments