Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort by crate lexicographical order #513

Merged
merged 15 commits into from
Dec 13, 2023
30 changes: 18 additions & 12 deletions crates/taplo/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken},
util::overlaps,
};
use once_cell::unsync::OnceCell;
use rowan::{GreenNode, NodeOrToken, TextRange};
use std::{
cmp,
Expand Down Expand Up @@ -358,36 +359,40 @@ fn format_impl(node: SyntaxNode, options: Options, context: Context) -> String {
struct FormattedEntry {
syntax: SyntaxElement,
key: String,
_cleaned_key: OnceCell<String>,
panekj marked this conversation as resolved.
Show resolved Hide resolved
value: String,
comment: Option<String>,
}

impl FormattedEntry {
fn cleaned_key(&self) -> &str {
&self
._cleaned_key
.get_or_init(|| self.key.replace('\'', "").replace('"', ""))
bebert64 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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 {}

impl PartialOrd for FormattedEntry {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.key
.replace('\'', "")
.replace('"', "")
.partial_cmp(&other.key.replace('\'', "").replace('"', ""))
self.cleaned_key()
.split('.')
.partial_cmp(other.cleaned_key().split('.'))
}
}

impl Ord for FormattedEntry {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.key
.replace('\'', "")
.replace('"', "")
.cmp(&other.key.replace('\'', "").replace('"', ""))
self.cleaned_key()
.split('.')
.cmp(other.cleaned_key().split('.'))
bebert64 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -764,6 +769,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma

FormattedEntry {
syntax: node.into(),
_cleaned_key: OnceCell::new(),
key,
value,
comment,
Expand Down
Loading