]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/format.rs
Merge #6172
[rust.git] / crates / ide / src / syntax_highlighting / format.rs
1 //! Syntax highlighting for format macro strings.
2 use syntax::{
3     ast::{self, FormatSpecifier, HasFormatSpecifier},
4     AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
5 };
6
7 use crate::{syntax_highlighting::HighlightedRangeStack, HighlightTag, HighlightedRange};
8
9 #[derive(Default)]
10 pub(super) struct FormatStringHighlighter {
11     format_string: Option<SyntaxElement>,
12 }
13
14 impl FormatStringHighlighter {
15     pub(super) fn check_for_format_string(&mut self, parent: &SyntaxNode) {
16         // Check if macro takes a format string and remember it for highlighting later.
17         // The macros that accept a format string expand to a compiler builtin macros
18         // `format_args` and `format_args_nl`.
19         if let Some(name) = parent
20             .parent()
21             .and_then(ast::MacroCall::cast)
22             .and_then(|mc| mc.path())
23             .and_then(|p| p.segment())
24             .and_then(|s| s.name_ref())
25         {
26             match name.text().as_str() {
27                 "format_args" | "format_args_nl" => {
28                     self.format_string = parent
29                         .children_with_tokens()
30                         .filter(|t| t.kind() != SyntaxKind::WHITESPACE)
31                         .nth(1)
32                         .filter(|e| {
33                             ast::String::can_cast(e.kind()) || ast::RawString::can_cast(e.kind())
34                         })
35                 }
36                 _ => {}
37             }
38         }
39     }
40     pub(super) fn highlight_format_string(
41         &self,
42         range_stack: &mut HighlightedRangeStack,
43         string: &impl HasFormatSpecifier,
44         range: TextRange,
45     ) {
46         if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) {
47             range_stack.push();
48             string.lex_format_specifier(|piece_range, kind| {
49                 if let Some(highlight) = highlight_format_specifier(kind) {
50                     range_stack.add(HighlightedRange {
51                         range: piece_range + range.start(),
52                         highlight: highlight.into(),
53                         binding_hash: None,
54                     });
55                 }
56             });
57             range_stack.pop();
58         }
59     }
60 }
61
62 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
63     Some(match kind {
64         FormatSpecifier::Open
65         | FormatSpecifier::Close
66         | FormatSpecifier::Colon
67         | FormatSpecifier::Fill
68         | FormatSpecifier::Align
69         | FormatSpecifier::Sign
70         | FormatSpecifier::NumberSign
71         | FormatSpecifier::DollarSign
72         | FormatSpecifier::Dot
73         | FormatSpecifier::Asterisk
74         | FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier,
75         FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
76         FormatSpecifier::Identifier => HighlightTag::Local,
77     })
78 }