Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
bebert64 committed Dec 11, 2023
1 parent 698c834 commit 41e38bb
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions crates/taplo/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,40 +358,23 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String {
formatted
}

mod formatted_entry {
use super::{OnceCell, SyntaxElement};

pub(super) struct FormattedEntry {
pub(super) syntax: SyntaxElement,
pub(super) key: String,
cleaned_key: OnceCell<String>,
pub(super) value: String,
pub(super) comment: Option<String>,
}
struct FormattedEntry {
syntax: SyntaxElement,
key: String,
// This field is used to cache the "cleaned" version of the key and should only
// be accessed through the `cleaned_key` helpers method.
private_cleaned_key: OnceCell<String>,
value: String,
comment: Option<String>,
}

impl FormattedEntry {
pub(super) fn new(
syntax: SyntaxElement,
key: String,
value: String,
comment: Option<String>,
) -> Self {
Self {
syntax,
key,
value,
comment,
cleaned_key: OnceCell::new(),
}
}
pub(super) fn cleaned_key(&self) -> &str {
&self
.cleaned_key
.get_or_init(|| self.key.replace('\'', "").replace('"', ""))
}
impl FormattedEntry {
fn cleaned_key(&self) -> &str {
&self
.private_cleaned_key
.get_or_init(|| self.key.replace('\'', "").replace('"', ""))
}
}
use formatted_entry::FormattedEntry;

impl PartialEq for FormattedEntry {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -788,7 +771,13 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma
}
}

FormattedEntry::new(node.into(), key, value, comment)
FormattedEntry {
syntax: node.into(),
key,
private_cleaned_key: OnceCell::new(),
value,
comment,
}
}

fn format_key(node: SyntaxNode, formatted: &mut String, _options: &Options, _context: &Context) {
Expand Down

0 comments on commit 41e38bb

Please sign in to comment.