From c8b4a0cc3cf3c003dadabe8358878df5898964c1 Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 07:20:04 +0100 Subject: [PATCH 01/15] unclean implementation --- crates/taplo/src/formatter/mod.rs | 46 ++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 096032885..aadea6bd5 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,17 +3,19 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, -}; -use rowan::{GreenNode, NodeOrToken, TextRange}; -use std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, +use { + crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, + }, + rowan::{GreenNode, NodeOrToken, TextRange}, + std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, + }, }; #[cfg(feature = "serde")] @@ -378,7 +380,16 @@ impl PartialOrd for FormattedEntry { self.key .replace('\'', "") .replace('"', "") - .partial_cmp(&other.key.replace('\'', "").replace('"', "")) + .split('.') + .into_iter() + .partial_cmp( + &mut other + .key + .replace('\'', "") + .replace('"', "") + .split('.') + .into_iter(), + ) } } @@ -387,7 +398,16 @@ impl Ord for FormattedEntry { self.key .replace('\'', "") .replace('"', "") - .cmp(&other.key.replace('\'', "").replace('"', "")) + .split('.') + .into_iter() + .cmp( + &mut other + .key + .replace('\'', "") + .replace('"', "") + .split('.') + .into_iter(), + ) } } From 7afbc3c9b682c38e2a91633de159e14368fd31da Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 08:18:46 +0100 Subject: [PATCH 02/15] a bit of factorization --- crates/taplo/src/formatter/mod.rs | 35 ++++++++----------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index aadea6bd5..b4c2eb2d9 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -375,39 +375,22 @@ impl PartialEq for FormattedEntry { impl Eq for FormattedEntry {} +fn cleaned_key(key: &str) -> String { + key.replace('\'', "").replace('"', "") +} +fn split_key<'a>(key: &'a str) -> std::str::Split<'a, char> { + key.split('.').into_iter() +} + impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { - self.key - .replace('\'', "") - .replace('"', "") - .split('.') - .into_iter() - .partial_cmp( - &mut other - .key - .replace('\'', "") - .replace('"', "") - .split('.') - .into_iter(), - ) + split_key(&cleaned_key(&self.key)).partial_cmp(&mut split_key(&cleaned_key(&other.key))) } } impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { - self.key - .replace('\'', "") - .replace('"', "") - .split('.') - .into_iter() - .cmp( - &mut other - .key - .replace('\'', "") - .replace('"', "") - .split('.') - .into_iter(), - ) + split_key(&cleaned_key(&self.key)).cmp(&mut split_key(&cleaned_key(&other.key))) } } From f02373136f7a5e9281fcd052dc7a52e39bda6c57 Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 08:51:01 +0100 Subject: [PATCH 03/15] always clean keys ? --- crates/taplo/src/formatter/mod.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index b4c2eb2d9..60ecf9e23 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -360,37 +360,32 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String { struct FormattedEntry { syntax: SyntaxElement, key: String, + cleaned_key: String, value: String, comment: Option, } impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { - self.key - .replace('\'', "") - .replace('"', "") - .eq(&other.key.replace('\'', "").replace('"', "")) + self.cleaned_key.eq(&other.cleaned_key) } } impl Eq for FormattedEntry {} -fn cleaned_key(key: &str) -> String { - key.replace('\'', "").replace('"', "") -} -fn split_key<'a>(key: &'a str) -> std::str::Split<'a, char> { - key.split('.').into_iter() -} - impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { - split_key(&cleaned_key(&self.key)).partial_cmp(&mut split_key(&cleaned_key(&other.key))) + self.cleaned_key + .split('.') + .partial_cmp(&mut other.cleaned_key.split('.')) } } impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { - split_key(&cleaned_key(&self.key)).cmp(&mut split_key(&cleaned_key(&other.key))) + self.cleaned_key + .split('.') + .cmp(&mut other.cleaned_key.split('.')) } } @@ -767,6 +762,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma FormattedEntry { syntax: node.into(), + cleaned_key: key.replace('\'', "").replace('"', ""), key, value, comment, From e75fcbb3f0bce17eba87b802ce85a2495f87b36c Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 09:17:08 +0100 Subject: [PATCH 04/15] refcell --- crates/taplo/src/formatter/mod.rs | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 60ecf9e23..51755fcd0 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -11,6 +11,7 @@ use { }, rowan::{GreenNode, NodeOrToken, TextRange}, std::{ + cell::RefCell, cmp, iter::{repeat, FromIterator}, ops::Range, @@ -360,32 +361,57 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String { struct FormattedEntry { syntax: SyntaxElement, key: String, - cleaned_key: String, + _cleaned_key: RefCell>, value: String, comment: Option, } impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { - self.cleaned_key.eq(&other.cleaned_key) + self.cleaned_key().eq(&other.cleaned_key()) } } impl Eq for FormattedEntry {} +impl FormattedEntry { + fn cleaned_key<'a>(&'a self) -> std::cell::Ref<'a, Option> { + self._cleaned_key + .borrow_mut() + .get_or_insert_with(|| self.key.replace('\'', "").replace('"', "")); + self._cleaned_key.borrow() + } +} + impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { - self.cleaned_key + self.cleaned_key() + .as_deref() + .expect("Has been initialized") .split('.') - .partial_cmp(&mut other.cleaned_key.split('.')) + .partial_cmp( + &mut other + .cleaned_key() + .as_deref() + .expect("Has been initialized") + .split('.'), + ) } } impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { - self.cleaned_key + self.cleaned_key() + .as_deref() + .expect("Has been initialized") .split('.') - .cmp(&mut other.cleaned_key.split('.')) + .cmp( + &mut other + .cleaned_key() + .as_deref() + .expect("Has been initialized") + .split('.'), + ) } } @@ -762,7 +788,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma FormattedEntry { syntax: node.into(), - cleaned_key: key.replace('\'', "").replace('"', ""), + _cleaned_key: RefCell::new(None), key, value, comment, From b1611cf4c1330b281d77a96cbcd8b7d239239069 Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 15:15:52 +0100 Subject: [PATCH 05/15] cleaner with OnceCell --- crates/taplo/src/formatter/mod.rs | 37 +++++++++---------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 51755fcd0..84cb76c28 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -9,9 +9,9 @@ use { syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, util::overlaps, }, + once_cell::unsync::OnceCell, rowan::{GreenNode, NodeOrToken, TextRange}, std::{ - cell::RefCell, cmp, iter::{repeat, FromIterator}, ops::Range, @@ -361,57 +361,40 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String { struct FormattedEntry { syntax: SyntaxElement, key: String, - _cleaned_key: RefCell>, + _cleaned_key: OnceCell, value: String, comment: Option, } impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { - self.cleaned_key().eq(&other.cleaned_key()) + self.cleaned_key().eq(other.cleaned_key()) } } impl Eq for FormattedEntry {} impl FormattedEntry { - fn cleaned_key<'a>(&'a self) -> std::cell::Ref<'a, Option> { - self._cleaned_key - .borrow_mut() - .get_or_insert_with(|| self.key.replace('\'', "").replace('"', "")); - self._cleaned_key.borrow() + fn cleaned_key(&self) -> &str { + &self + ._cleaned_key + .get_or_init(|| self.key.replace('\'', "").replace('"', "")) } } impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { self.cleaned_key() - .as_deref() - .expect("Has been initialized") .split('.') - .partial_cmp( - &mut other - .cleaned_key() - .as_deref() - .expect("Has been initialized") - .split('.'), - ) + .partial_cmp(&mut other.cleaned_key().split('.')) } } impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { self.cleaned_key() - .as_deref() - .expect("Has been initialized") .split('.') - .cmp( - &mut other - .cleaned_key() - .as_deref() - .expect("Has been initialized") - .split('.'), - ) + .cmp(&mut other.cleaned_key().split('.')) } } @@ -788,7 +771,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma FormattedEntry { syntax: node.into(), - _cleaned_key: RefCell::new(None), + _cleaned_key: OnceCell::new(), key, value, comment, From 7c0dac9b19cf725cfcf118f2c092894dcd24e34f Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 16:40:18 +0100 Subject: [PATCH 06/15] remove unnecessary &mut + fix imports --- crates/taplo/src/formatter/mod.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 84cb76c28..f3b990ddd 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,20 +3,18 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use { - crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, - }, - once_cell::unsync::OnceCell, - rowan::{GreenNode, NodeOrToken, TextRange}, - std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, - }, +use crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, +}; +use once_cell::unsync::OnceCell; +use rowan::{GreenNode, NodeOrToken, TextRange}; +use std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, }; #[cfg(feature = "serde")] @@ -386,7 +384,7 @@ impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { self.cleaned_key() .split('.') - .partial_cmp(&mut other.cleaned_key().split('.')) + .partial_cmp(other.cleaned_key().split('.')) } } @@ -394,7 +392,7 @@ impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { self.cleaned_key() .split('.') - .cmp(&mut other.cleaned_key().split('.')) + .cmp(other.cleaned_key().split('.')) } } From 0b8483aa958d92685a12f4ba5549072e7d0225e4 Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 16:41:46 +0100 Subject: [PATCH 07/15] move impl FormattedEntry up --- crates/taplo/src/formatter/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index f3b990ddd..2db38255b 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -364,14 +364,6 @@ struct FormattedEntry { comment: Option, } -impl PartialEq for FormattedEntry { - fn eq(&self, other: &Self) -> bool { - self.cleaned_key().eq(other.cleaned_key()) - } -} - -impl Eq for FormattedEntry {} - impl FormattedEntry { fn cleaned_key(&self) -> &str { &self @@ -380,6 +372,14 @@ impl FormattedEntry { } } +impl PartialEq for FormattedEntry { + fn eq(&self, other: &Self) -> bool { + self.cleaned_key().eq(other.cleaned_key()) + } +} + +impl Eq for FormattedEntry {} + impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { self.cleaned_key() From 08a48f9a37eea5f321e25c0ad2337125d520775d Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 17:56:13 +0100 Subject: [PATCH 08/15] private cleaned_key --- crates/taplo/src/formatter/mod.rs | 81 ++++++++++++++++++------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 2db38255b..21dc5ffa8 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,18 +3,20 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, -}; -use once_cell::unsync::OnceCell; -use rowan::{GreenNode, NodeOrToken, TextRange}; -use std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, +use { + crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, + }, + once_cell::unsync::OnceCell, + rowan::{GreenNode, NodeOrToken, TextRange}, + std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, + }, }; #[cfg(feature = "serde")] @@ -356,21 +358,40 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String { formatted } -struct FormattedEntry { - syntax: SyntaxElement, - key: String, - _cleaned_key: OnceCell, - value: String, - comment: Option, -} +mod formatted_entry { + use super::{OnceCell, SyntaxElement}; + + pub(super) struct FormattedEntry { + pub(super) syntax: SyntaxElement, + pub(super) key: String, + cleaned_key: OnceCell, + pub(super) value: String, + pub(super) comment: Option, + } -impl FormattedEntry { - fn cleaned_key(&self) -> &str { - &self - ._cleaned_key - .get_or_init(|| self.key.replace('\'', "").replace('"', "")) + impl FormattedEntry { + pub(super) fn new( + syntax: SyntaxElement, + key: String, + value: String, + comment: Option, + ) -> 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('"', "")) + } } } +use formatted_entry::FormattedEntry; impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { @@ -382,9 +403,7 @@ impl Eq for FormattedEntry {} impl PartialOrd for FormattedEntry { fn partial_cmp(&self, other: &Self) -> Option { - self.cleaned_key() - .split('.') - .partial_cmp(other.cleaned_key().split('.')) + Some(self.cmp(other)) } } @@ -767,13 +786,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma } } - FormattedEntry { - syntax: node.into(), - _cleaned_key: OnceCell::new(), - key, - value, - comment, - } + FormattedEntry::new(node.into(), key, value, comment) } fn format_key(node: SyntaxNode, formatted: &mut String, _options: &Options, _context: &Context) { From 698c8346e71d45877b2801f48d1c4705a18f128b Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 17:56:58 +0100 Subject: [PATCH 09/15] Eq using split --- crates/taplo/src/formatter/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 21dc5ffa8..19f7fbe8d 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -395,7 +395,9 @@ use formatted_entry::FormattedEntry; impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { - self.cleaned_key().eq(other.cleaned_key()) + self.cleaned_key() + .split('.') + .eq(other.cleaned_key().split('.')) } } From 41e38bbee0001b72b1784428c1627bbadfa0590e Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 18:39:40 +0100 Subject: [PATCH 10/15] simplify --- crates/taplo/src/formatter/mod.rs | 53 ++++++++++++------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 19f7fbe8d..7aab88986 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -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, - pub(super) value: String, - pub(super) comment: Option, - } +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, + value: String, + comment: Option, +} - impl FormattedEntry { - pub(super) fn new( - syntax: SyntaxElement, - key: String, - value: String, - comment: Option, - ) -> 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 { @@ -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) { From a1d832e84682c3e93f620f2e428e50cd7074c2c1 Mon Sep 17 00:00:00 2001 From: RomainC Date: Mon, 11 Dec 2023 18:41:29 +0100 Subject: [PATCH 11/15] fix imports --- crates/taplo/src/formatter/mod.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 7aab88986..ae2fc894e 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,20 +3,18 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use { - crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, - }, - once_cell::unsync::OnceCell, - rowan::{GreenNode, NodeOrToken, TextRange}, - std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, - }, +use crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, +}; +use once_cell::unsync::OnceCell; +use rowan::{GreenNode, NodeOrToken, TextRange}; +use std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, }; #[cfg(feature = "serde")] From d9cf95c178c18b20c205c085511e9d5741328734 Mon Sep 17 00:00:00 2001 From: RomainC Date: Tue, 12 Dec 2023 16:29:44 +0100 Subject: [PATCH 12/15] cleaned_key as Vec --- crates/taplo/src/formatter/mod.rs | 48 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index ae2fc894e..03255b16f 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,18 +3,20 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, -}; -use once_cell::unsync::OnceCell; -use rowan::{GreenNode, NodeOrToken, TextRange}; -use std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, +use { + crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, + }, + once_cell::unsync::OnceCell, + rowan::{GreenNode, NodeOrToken, TextRange}, + std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, + }, }; #[cfg(feature = "serde")] @@ -361,24 +363,26 @@ struct FormattedEntry { 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, + private_cleaned_key: OnceCell>, value: String, comment: Option, } impl FormattedEntry { - fn cleaned_key(&self) -> &str { - &self - .private_cleaned_key - .get_or_init(|| self.key.replace('\'', "").replace('"', "")) + fn cleaned_key(&self) -> &Vec { + self.private_cleaned_key.get_or_init(|| { + self.key + .replace(['\'', '"'], "") + .split('.') + .map(ToOwned::to_owned) + .collect() + }) } } impl PartialEq for FormattedEntry { fn eq(&self, other: &Self) -> bool { - self.cleaned_key() - .split('.') - .eq(other.cleaned_key().split('.')) + self.cleaned_key().eq(other.cleaned_key()) } } @@ -392,9 +396,7 @@ impl PartialOrd for FormattedEntry { impl Ord for FormattedEntry { fn cmp(&self, other: &Self) -> cmp::Ordering { - self.cleaned_key() - .split('.') - .cmp(other.cleaned_key().split('.')) + self.cleaned_key().cmp(other.cleaned_key()) } } From 14b52d4be079ea482fb7db3361b822a9a04f0b62 Mon Sep 17 00:00:00 2001 From: RomainC Date: Tue, 12 Dec 2023 16:47:13 +0100 Subject: [PATCH 13/15] fix imports formating --- crates/taplo/src/formatter/mod.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 03255b16f..1a403f90b 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,20 +3,18 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use { - crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, - }, - once_cell::unsync::OnceCell, - rowan::{GreenNode, NodeOrToken, TextRange}, - std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, - }, +use crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, +}; +use once_cell::unsync::OnceCell; +use rowan::{GreenNode, NodeOrToken, TextRange}; +use std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, }; #[cfg(feature = "serde")] From 0e1ceb8efcb6717d4118ef6f7b20707c921b1019 Mon Sep 17 00:00:00 2001 From: RomainC Date: Wed, 13 Dec 2023 11:17:15 +0100 Subject: [PATCH 14/15] rename field --- crates/taplo/src/formatter/mod.rs | 32 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 1a403f90b..9dcddb02c 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,18 +3,20 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, -}; -use once_cell::unsync::OnceCell; -use rowan::{GreenNode, NodeOrToken, TextRange}; -use std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, +use { + crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, + }, + once_cell::unsync::OnceCell, + rowan::{GreenNode, NodeOrToken, TextRange}, + std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, + }, }; #[cfg(feature = "serde")] @@ -361,14 +363,14 @@ struct FormattedEntry { 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>, + cleaned_key: OnceCell>, value: String, comment: Option, } impl FormattedEntry { fn cleaned_key(&self) -> &Vec { - self.private_cleaned_key.get_or_init(|| { + self.cleaned_key.get_or_init(|| { self.key .replace(['\'', '"'], "") .split('.') @@ -772,7 +774,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma FormattedEntry { syntax: node.into(), key, - private_cleaned_key: OnceCell::new(), + cleaned_key: OnceCell::new(), value, comment, } From 0f920ebced7810d403599924a058679d42b21241 Mon Sep 17 00:00:00 2001 From: RomainC Date: Wed, 13 Dec 2023 14:27:26 +0100 Subject: [PATCH 15/15] documentation --- crates/taplo/src/formatter/mod.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/taplo/src/formatter/mod.rs b/crates/taplo/src/formatter/mod.rs index 9dcddb02c..338c019fb 100644 --- a/crates/taplo/src/formatter/mod.rs +++ b/crates/taplo/src/formatter/mod.rs @@ -3,20 +3,18 @@ //! The formatting can be done on documents that might //! contain invalid syntax. In that case the invalid part is skipped. -use { - crate::{ - dom::{self, node::DomNode, FromSyntax, Keys, Node}, - syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, - util::overlaps, - }, - once_cell::unsync::OnceCell, - rowan::{GreenNode, NodeOrToken, TextRange}, - std::{ - cmp, - iter::{repeat, FromIterator}, - ops::Range, - rc::Rc, - }, +use crate::{ + dom::{self, node::DomNode, FromSyntax, Keys, Node}, + syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken}, + util::overlaps, +}; +use once_cell::unsync::OnceCell; +use rowan::{GreenNode, NodeOrToken, TextRange}; +use std::{ + cmp, + iter::{repeat, FromIterator}, + ops::Range, + rc::Rc, }; #[cfg(feature = "serde")] @@ -361,8 +359,8 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> 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. + /// This field is used to cache the "cleaned" version of the key and should only + /// be accessed through the `cleaned_key` helpers method. cleaned_key: OnceCell>, value: String, comment: Option,