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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
2.1 KiB
Rust
Raw Normal View History

2020-10-13 22:56:41 +00:00
//! Syntax highlighting for format macro strings.
use ide_db::{
defs::Definition,
2022-03-06 18:01:30 +00:00
syntax_helpers::format_string::{is_format_string, lex_format_specifiers, FormatSpecifier},
SymbolKind,
2020-10-13 22:56:41 +00:00
};
use syntax::{ast, TextRange};
2020-10-13 22:56:41 +00:00
use crate::{
syntax_highlighting::{highlight::highlight_def, highlights::Highlights},
HlRange, HlTag,
};
2021-01-10 08:57:17 +00:00
pub(super) fn highlight_format_string(
stack: &mut Highlights,
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
krate: hir::Crate,
2021-01-10 08:57:17 +00:00
string: &ast::String,
expanded_string: &ast::String,
2021-01-10 08:57:17 +00:00
range: TextRange,
) {
if !is_format_string(expanded_string) {
2021-01-10 08:57:17 +00:00
return;
}
2020-10-13 22:56:41 +00:00
// FIXME: Replace this with the HIR info we have now.
lex_format_specifiers(string, &mut |piece_range, kind| {
2021-01-10 08:57:17 +00:00
if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HlRange {
range: piece_range + range.start(),
highlight: highlight.into(),
binding_hash: None,
});
}
});
if let Some(parts) = sema.as_format_args_parts(string) {
parts.into_iter().for_each(|(range, res)| {
if let Some(res) = res {
stack.add(HlRange {
range,
highlight: highlight_def(sema, krate, Definition::from(res)),
binding_hash: None,
})
}
})
}
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),
FormatSpecifier::Escape => HlTag::EscapeSequence,
2020-10-13 22:56:41 +00:00
})
}