mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Replace state with function
This commit is contained in:
parent
bcb9ae18de
commit
083986030e
2 changed files with 55 additions and 69 deletions
|
@ -1,60 +1,48 @@
|
|||
//! Syntax highlighting for format macro strings.
|
||||
use syntax::{
|
||||
ast::{self, FormatSpecifier, HasFormatSpecifier},
|
||||
AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
|
||||
AstNode, AstToken, TextRange,
|
||||
};
|
||||
|
||||
use crate::{HlRange, HlTag, SymbolKind};
|
||||
use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag, SymbolKind};
|
||||
|
||||
use super::highlights::Highlights;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct FormatStringHighlighter {
|
||||
format_string: Option<SyntaxElement>,
|
||||
}
|
||||
|
||||
impl FormatStringHighlighter {
|
||||
pub(super) fn check_for_format_string(&mut self, parent: &SyntaxNode) {
|
||||
// Check if macro takes a format string and remember it for highlighting later.
|
||||
// The macros that accept a format string expand to a compiler builtin macros
|
||||
// `format_args` and `format_args_nl`.
|
||||
if let Some(name) = parent
|
||||
.parent()
|
||||
.and_then(ast::MacroCall::cast)
|
||||
.and_then(|mc| mc.path())
|
||||
.and_then(|p| p.segment())
|
||||
.and_then(|s| s.name_ref())
|
||||
{
|
||||
match name.text().as_str() {
|
||||
"format_args" | "format_args_nl" => {
|
||||
self.format_string = parent
|
||||
.children_with_tokens()
|
||||
.filter(|t| t.kind() != SyntaxKind::WHITESPACE)
|
||||
.nth(1)
|
||||
.filter(|e| ast::String::can_cast(e.kind()))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
pub(super) fn highlight_format_string(
|
||||
stack: &mut Highlights,
|
||||
string: &ast::String,
|
||||
range: TextRange,
|
||||
) {
|
||||
if is_format_string(string).is_none() {
|
||||
return;
|
||||
}
|
||||
pub(super) fn highlight_format_string(
|
||||
&self,
|
||||
stack: &mut Highlights,
|
||||
string: &impl HasFormatSpecifier,
|
||||
range: TextRange,
|
||||
) {
|
||||
if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) {
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn is_format_string(string: &ast::String) -> Option<()> {
|
||||
let parent = string.syntax().parent();
|
||||
|
||||
let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?;
|
||||
if !matches!(name.text().as_str(), "format_args" | "format_args_nl") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let first_literal = parent
|
||||
.children_with_tokens()
|
||||
.filter_map(|it| it.as_token().cloned().and_then(ast::String::cast))
|
||||
.next()?;
|
||||
if &first_literal != string {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
|
||||
|
@ -70,7 +58,9 @@ fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
|
|||
| FormatSpecifier::Dot
|
||||
| FormatSpecifier::Asterisk
|
||||
| FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
|
||||
|
||||
FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
|
||||
|
||||
FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue