]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/format.rs
feat: Add very simplistic ident completion for format_args! macro input
[rust.git] / crates / ide / src / syntax_highlighting / format.rs
1 //! Syntax highlighting for format macro strings.
2 use ide_db::{helpers::format_string::is_format_string, SymbolKind};
3 use syntax::{
4     ast::{self, FormatSpecifier, HasFormatSpecifier},
5     TextRange,
6 };
7
8 use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag};
9
10 pub(super) fn highlight_format_string(
11     stack: &mut Highlights,
12     string: &ast::String,
13     expanded_string: &ast::String,
14     range: TextRange,
15 ) {
16     if !is_format_string(expanded_string) {
17         return;
18     }
19
20     string.lex_format_specifier(|piece_range, kind| {
21         if let Some(highlight) = highlight_format_specifier(kind) {
22             stack.add(HlRange {
23                 range: piece_range + range.start(),
24                 highlight: highlight.into(),
25                 binding_hash: None,
26             });
27         }
28     });
29 }
30
31 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
32     Some(match kind {
33         FormatSpecifier::Open
34         | FormatSpecifier::Close
35         | FormatSpecifier::Colon
36         | FormatSpecifier::Fill
37         | FormatSpecifier::Align
38         | FormatSpecifier::Sign
39         | FormatSpecifier::NumberSign
40         | FormatSpecifier::DollarSign
41         | FormatSpecifier::Dot
42         | FormatSpecifier::Asterisk
43         | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
44
45         FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
46
47         FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
48     })
49 }