use crate::{theme::color, tokens::ColorRole};
use gpui_kit::{
App, FontWeight, HighlightStyle, IntoElement, RenderOnce, SharedString, StyledText, Window,
};
use std::ops::Range;
pub fn fuzzy_match(text: &str, query: &str) -> Option<Vec<usize>> {
if query.is_empty() {
return Some(Vec::new());
}
let query: Vec<char> = query.chars().flat_map(char::to_lowercase).collect();
let mut hits = Vec::with_capacity(query.len());
let mut j = 0;
for (i, ch) in text.chars().enumerate() {
if j == query.len() {
break;
}
if ch.to_lowercase().next() == Some(query[j]) {
hits.push(i);
j += 1;
}
}
(j == query.len()).then_some(hits)
}
pub fn highlight_style() -> HighlightStyle {
HighlightStyle {
color: Some(color(ColorRole::TextPrimary)),
font_weight: Some(FontWeight::SEMIBOLD),
..Default::default()
}
}
pub fn highlight_runs(text: &str, indices: &[usize]) -> Vec<(Range<usize>, HighlightStyle)> {
let style = highlight_style();
match_ranges(text, indices)
.into_iter()
.map(|range| (range, style))
.collect()
}
fn match_ranges(text: &str, indices: &[usize]) -> Vec<Range<usize>> {
let mut hit = indices.to_vec();
hit.sort_unstable();
hit.dedup();
let mut ranges: Vec<Range<usize>> = Vec::new();
let mut want = hit.into_iter().peekable();
for (i, (byte, ch)) in text.char_indices().enumerate() {
while want.peek().is_some_and(|&w| w < i) {
want.next();
}
if want.peek() != Some(&i) {
continue;
}
want.next();
let end = byte + ch.len_utf8();
match ranges.last_mut() {
Some(last) if last.end == byte => last.end = end,
_ => ranges.push(byte..end),
}
}
ranges
}
#[derive(IntoElement)]
pub struct HighlightedText {
text: SharedString,
query: Option<SharedString>,
indices: Option<Vec<usize>>,
}
impl HighlightedText {
pub fn new(text: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
query: None,
indices: None,
}
}
pub fn query(mut self, query: impl Into<SharedString>) -> Self {
self.query = Some(query.into());
self
}
pub fn indices(mut self, indices: Vec<usize>) -> Self {
self.indices = Some(indices);
self
}
pub fn hits(&self) -> Vec<usize> {
self.indices.clone().unwrap_or_else(|| {
self.query
.as_ref()
.and_then(|q| fuzzy_match(&self.text, q))
.unwrap_or_default()
})
}
}
impl RenderOnce for HighlightedText {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let runs = highlight_runs(&self.text, &self.hits());
StyledText::new(self.text).with_highlights(runs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn fuzzy_match_follows_the_reference() {
assert_eq!(
fuzzy_match("SidebarThreadRow.tsx", "stro"),
Some(vec![0, 7, 9, 14])
);
assert_eq!(
fuzzy_match("Open settings", "opset"),
Some(vec![0, 1, 5, 6, 7])
);
assert_eq!(
fuzzy_match("packages/web/src/components", "pwsc"),
Some(vec![0, 9, 13, 15])
);
assert_eq!(fuzzy_match("abc", ""), Some(vec![]));
assert_eq!(fuzzy_match("abc", "abd"), None);
assert_eq!(fuzzy_match("abc", "abcd"), None);
assert_eq!(fuzzy_match("ÄbC", "äc"), Some(vec![0, 2]));
assert_eq!(fuzzy_match("设置 · 通用", "通"), Some(vec![5]));
}
#[test]
fn runs_merge_consecutive_hits_into_byte_ranges() {
assert_eq!(
match_ranges("Open settings", &[0, 1, 5, 6, 7]),
vec![0..2, 5..8]
);
assert_eq!(match_ranges("abc", &[2, 0, 0, 9]), vec![0..1, 2..3]);
assert_eq!(match_ranges("设置通用", &[1, 2]), vec![3..9]);
assert!(match_ranges("abc", &[]).is_empty());
}
#[test]
fn highlight_is_weight_and_ink_without_underline() {
crate::theme::tests_set(CarbonTheme::G100);
{
let style = highlight_style();
assert_eq!(style.font_weight, Some(FontWeight::SEMIBOLD));
assert_eq!(
style.color,
Some(color_in(CarbonTheme::G100, ColorRole::TextPrimary))
);
assert!(style.underline.is_none());
assert!(style.background_color.is_none());
}
}
#[test]
fn indices_win_over_query() {
let text = HighlightedText::new("Open settings")
.query("opset")
.indices(vec![3]);
assert_eq!(text.hits(), vec![3]);
let text = HighlightedText::new("Open settings").query("zz");
assert!(text.hits().is_empty());
}
}