Default::default the highlighters

This commit is contained in:
Lukas Wirth 2020-10-14 22:21:58 +02:00
parent 8c6dc5f28a
commit bab29e65eb
3 changed files with 7 additions and 18 deletions

View file

@ -74,7 +74,7 @@ pub(crate) fn highlight(
let mut current_macro_call: Option<ast::MacroCall> = None; let mut current_macro_call: Option<ast::MacroCall> = None;
let mut format_string_highlighter = FormatStringHighlighter::default(); let mut format_string_highlighter = FormatStringHighlighter::default();
let mut macro_rules_highlighter = MacroRulesHighlighter::new(); let mut macro_rules_highlighter = MacroRulesHighlighter::default();
// Walk all nodes, keeping track of whether we are inside a macro or not. // Walk all nodes, keeping track of whether we are inside a macro or not.
// If in macro, expand it first and highlight the expanded code. // If in macro, expand it first and highlight the expanded code.
@ -125,8 +125,8 @@ pub(crate) fn highlight(
WalkEvent::Leave(Some(mc)) => { WalkEvent::Leave(Some(mc)) => {
assert!(current_macro_call == Some(mc)); assert!(current_macro_call == Some(mc));
current_macro_call = None; current_macro_call = None;
format_string_highlighter.reset(); format_string_highlighter = FormatStringHighlighter::default();
macro_rules_highlighter.reset(); macro_rules_highlighter = MacroRulesHighlighter::default();
} }
_ => (), _ => (),
} }

View file

@ -12,10 +12,6 @@ pub(super) struct FormatStringHighlighter {
} }
impl FormatStringHighlighter { impl FormatStringHighlighter {
pub(super) fn reset(&mut self) {
self.format_string = None;
}
pub(super) fn check_for_format_string(&mut self, parent: &SyntaxNode) { pub(super) fn check_for_format_string(&mut self, parent: &SyntaxNode) {
// Check if macro takes a format string and remember it for highlighting later. // 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 // The macros that accept a format string expand to a compiler builtin macros

View file

@ -3,21 +3,14 @@ use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
use crate::{HighlightTag, HighlightedRange}; use crate::{HighlightTag, HighlightedRange};
#[derive(Default)]
pub(super) struct MacroRulesHighlighter { pub(super) struct MacroRulesHighlighter {
state: Option<MacroMatcherParseState>, state: Option<MacroMatcherParseState>,
} }
impl MacroRulesHighlighter { impl MacroRulesHighlighter {
pub(super) fn new() -> Self {
MacroRulesHighlighter { state: None }
}
pub(super) fn init(&mut self) { pub(super) fn init(&mut self) {
self.state = Some(MacroMatcherParseState::new()); self.state = Some(MacroMatcherParseState::default());
}
pub(super) fn reset(&mut self) {
self.state = None;
} }
pub(super) fn advance(&mut self, token: &SyntaxToken) { pub(super) fn advance(&mut self, token: &SyntaxToken) {
@ -51,8 +44,8 @@ struct MacroMatcherParseState {
in_invoc_body: bool, in_invoc_body: bool,
} }
impl MacroMatcherParseState { impl Default for MacroMatcherParseState {
fn new() -> Self { fn default() -> Self {
MacroMatcherParseState { MacroMatcherParseState {
paren_ty: None, paren_ty: None,
paren_level: 0, paren_level: 0,