zed/crates/language/src/outline.rs

103 lines
3.3 KiB
Rust
Raw Normal View History

2022-01-13 14:10:29 +00:00
use fuzzy::{StringMatch, StringMatchCandidate};
2022-01-13 22:46:15 +00:00
use gpui::{executor::Background, fonts::HighlightStyle};
use std::{ops::Range, sync::Arc};
2022-01-13 14:10:29 +00:00
#[derive(Debug)]
pub struct Outline<T> {
pub items: Vec<OutlineItem<T>>,
2022-01-13 14:10:29 +00:00
candidates: Vec<StringMatchCandidate>,
}
2022-01-13 11:01:11 +00:00
#[derive(Clone, Debug)]
pub struct OutlineItem<T> {
pub depth: usize,
pub range: Range<T>,
pub text: String,
pub name_ranges: Vec<Range<u32>>,
pub highlight_ranges: Vec<(Range<usize>, HighlightStyle)>,
}
2022-01-13 14:10:29 +00:00
impl<T> Outline<T> {
pub fn new(items: Vec<OutlineItem<T>>) -> Self {
2022-01-13 14:10:29 +00:00
Self {
candidates: items
.iter()
.map(|item| {
let text = item
.name_ranges
.iter()
.map(|range| &item.text[range.start as usize..range.end as usize])
.collect::<String>();
2022-01-13 14:10:29 +00:00
StringMatchCandidate {
char_bag: text.as_str().into(),
string: text,
2022-01-13 14:10:29 +00:00
}
})
.collect(),
items,
}
}
pub async fn search(&self, query: &str, executor: Arc<Background>) -> Vec<StringMatch> {
let mut matches = fuzzy::match_strings(
2022-01-13 14:10:29 +00:00
&self.candidates,
query,
true,
100,
&Default::default(),
executor,
)
.await;
2022-01-13 14:10:29 +00:00
matches.sort_unstable_by_key(|m| m.candidate_index);
let mut tree_matches = Vec::new();
let mut prev_item_ix = 0;
for mut string_match in matches {
let outline_match = &self.items[string_match.candidate_index];
let mut name_ranges = outline_match.name_ranges.iter();
let mut name_range = name_ranges.next().unwrap();
let mut preceding_ranges_len = 0;
2022-01-13 14:10:29 +00:00
for position in &mut string_match.positions {
while *position >= preceding_ranges_len + name_range.len() as usize {
preceding_ranges_len += name_range.len();
name_range = name_ranges.next().unwrap();
}
*position = name_range.start as usize + (*position - preceding_ranges_len);
2022-01-13 14:10:29 +00:00
}
let insertion_ix = tree_matches.len();
let mut cur_depth = outline_match.depth;
2022-01-13 14:10:29 +00:00
for (ix, item) in self.items[prev_item_ix..string_match.candidate_index]
.iter()
.enumerate()
.rev()
2022-01-13 14:10:29 +00:00
{
if cur_depth == 0 {
break;
}
2022-01-13 14:10:29 +00:00
let candidate_index = ix + prev_item_ix;
if item.depth == cur_depth - 1 {
tree_matches.insert(
insertion_ix,
StringMatch {
candidate_index,
score: Default::default(),
positions: Default::default(),
string: Default::default(),
},
);
cur_depth -= 1;
2022-01-13 14:10:29 +00:00
}
}
prev_item_ix = string_match.candidate_index + 1;
2022-01-13 14:10:29 +00:00
tree_matches.push(string_match);
}
tree_matches
}
}