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
32 changes: 20 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,42 @@ 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.
private_cleaned_key: OnceCell<String>,
value: String,
comment: Option<String>,
}

impl FormattedEntry {
fn cleaned_key(&self) -> &str {
&self
.private_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()
.split('.')
.eq(other.cleaned_key().split('.'))
bebert64 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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('"', ""))
Some(self.cmp(other))
}
}

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 @@ -765,6 +772,7 @@ fn format_entry(node: SyntaxNode, options: &Options, context: &Context) -> Forma
FormattedEntry {
syntax: node.into(),
key,
private_cleaned_key: OnceCell::new(),
value,
comment,
}
Expand Down
Loading