rust-analyzer/crates/ide/src/syntax_highlighting/format.rs

75 lines
2.5 KiB
Rust
Raw Normal View History

2020-10-13 22:56:41 +00:00
//! Syntax highlighting for format macro strings.
2021-01-20 14:25:34 +00:00
use ide_db::SymbolKind;
2020-10-13 22:56:41 +00:00
use syntax::{
ast::{self, FormatSpecifier, HasFormatSpecifier},
2021-01-10 08:57:17 +00:00
AstNode, AstToken, TextRange,
2020-10-13 22:56:41 +00:00
};
2021-01-20 14:25:34 +00:00
use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag};
2021-01-10 08:57:17 +00:00
pub(super) fn highlight_format_string(
stack: &mut Highlights,
string: &ast::String,
range: TextRange,
) {
if is_format_string(string).is_none() {
return;
}
2020-10-13 22:56:41 +00:00
2021-01-10 08:57:17 +00:00
string.lex_format_specifier(|piece_range, kind| {
if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HlRange {
range: piece_range + range.start(),
highlight: highlight.into(),
binding_hash: None,
});
}
});
2020-10-13 22:56:41 +00:00
}
2021-01-10 08:57:17 +00:00
fn is_format_string(string: &ast::String) -> Option<()> {
2021-10-01 11:28:05 +00:00
// Check if `string` is a format string argument of a macro invocation.
// `string` is a string literal, mapped down into the innermost macro expansion.
// Since `format_args!` etc. remove the format string when expanding, but place all arguments
// in the expanded output, we know that the string token is (part of) the format string if it
// appears in `format_args!` (otherwise it would have been mapped down further).
//
// This setup lets us correctly highlight the components of `concat!("{}", "bla")` format
// strings. It still fails for `concat!("{", "}")`, but that is rare.
let macro_call = string.syntax().ancestors().find_map(ast::MacroCall::cast)?;
let name = macro_call.path()?.segment()?.name_ref()?;
2021-01-10 08:57:17 +00:00
if !matches!(
name.text().as_str(),
"format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
) {
2021-01-10 08:57:17 +00:00
return None;
2020-10-13 22:56:41 +00:00
}
2021-01-10 08:57:17 +00:00
// NB: we match against `panic_2015`/`panic_2021` here because they have a special-cased arm for
// `"{}"`, which otherwise wouldn't get highlighted.
2021-01-10 08:57:17 +00:00
Some(())
2020-10-13 22:56:41 +00:00
}
2021-01-09 11:44:01 +00:00
fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
2020-10-13 22:56:41 +00:00
Some(match kind {
FormatSpecifier::Open
| FormatSpecifier::Close
| FormatSpecifier::Colon
| FormatSpecifier::Fill
| FormatSpecifier::Align
| FormatSpecifier::Sign
| FormatSpecifier::NumberSign
| FormatSpecifier::DollarSign
| FormatSpecifier::Dot
| FormatSpecifier::Asterisk
2021-01-09 11:44:01 +00:00
| FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
2021-01-10 08:57:17 +00:00
2021-01-09 11:44:01 +00:00
FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
2021-01-10 08:57:17 +00:00
2021-01-09 11:44:01 +00:00
FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
2020-10-13 22:56:41 +00:00
})
}