Move NumericPrefixWithSuffix into utils

This commit is contained in:
Kirill Bulatov 2024-02-28 11:03:34 +02:00 committed by Kirill Bulatov
parent 96d9df073e
commit ca092fb694
6 changed files with 62 additions and 31 deletions

View file

@ -26,7 +26,7 @@ serde_json.workspace = true
settings.workspace = true
theme.workspace = true
ui.workspace = true
unicase = "2.6"
unicase.workspace = true
util.workspace = true
client.workspace = true
workspace.workspace = true

View file

@ -27,7 +27,7 @@ use std::{cmp::Ordering, ffi::OsStr, ops::Range, path::Path, sync::Arc};
use theme::ThemeSettings;
use ui::{prelude::*, v_flex, ContextMenu, Icon, KeyBinding, Label, ListItem};
use unicase::UniCase;
use util::{maybe, ResultExt, TryFutureExt};
use util::{maybe, NumericPrefixWithSuffix, ResultExt, TryFutureExt};
use workspace::{
dock::{DockPosition, Panel, PanelEvent},
notifications::DetachAndPromptErr,
@ -1498,35 +1498,6 @@ impl ProjectPanel {
}
}
#[derive(Debug, PartialEq)]
struct NumericPrefixWithSuffix<'a>(i32, &'a str);
impl<'a> NumericPrefixWithSuffix<'a> {
fn from_str(str: &'a str) -> Option<Self> {
let mut chars = str.chars();
let prefix: String = chars.by_ref().take_while(|c| c.is_digit(10)).collect();
let remainder = chars.as_str();
match prefix.parse::<i32>() {
Ok(prefix) => Some(NumericPrefixWithSuffix(prefix, remainder)),
Err(_) => None,
}
}
}
impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let NumericPrefixWithSuffix(num_a, remainder_a) = self;
let NumericPrefixWithSuffix(num_b, remainder_b) = other;
Some(
num_a
.cmp(&num_b)
.then_with(|| UniCase::new(remainder_a).cmp(&UniCase::new(remainder_b))),
)
}
}
impl Render for ProjectPanel {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
let has_worktree = self.visible_entries.len() != 0;