highlighted_workspace_location.rs

  1use std::path::Path;
  2
  3use fuzzy::StringMatch;
  4use ui::{prelude::*, HighlightedLabel};
  5use util::paths::PathExt;
  6use workspace::WorkspaceLocation;
  7
  8#[derive(IntoElement)]
  9pub struct HighlightedText {
 10    pub text: String,
 11    pub highlight_positions: Vec<usize>,
 12    char_count: usize,
 13}
 14
 15impl HighlightedText {
 16    fn join(components: impl Iterator<Item = Self>, separator: &str) -> Self {
 17        let mut char_count = 0;
 18        let separator_char_count = separator.chars().count();
 19        let mut text = String::new();
 20        let mut highlight_positions = Vec::new();
 21        for component in components {
 22            if char_count != 0 {
 23                text.push_str(separator);
 24                char_count += separator_char_count;
 25            }
 26
 27            highlight_positions.extend(
 28                component
 29                    .highlight_positions
 30                    .iter()
 31                    .map(|position| position + char_count),
 32            );
 33            text.push_str(&component.text);
 34            char_count += component.text.chars().count();
 35        }
 36
 37        Self {
 38            text,
 39            highlight_positions,
 40            char_count,
 41        }
 42    }
 43}
 44
 45impl RenderOnce for HighlightedText {
 46    type Rendered = HighlightedLabel;
 47
 48    fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
 49        HighlightedLabel::new(self.text, self.highlight_positions)
 50    }
 51}
 52
 53pub struct HighlightedWorkspaceLocation {
 54    pub names: HighlightedText,
 55    pub paths: Vec<HighlightedText>,
 56}
 57
 58impl HighlightedWorkspaceLocation {
 59    pub fn new(string_match: &StringMatch, location: &WorkspaceLocation) -> Self {
 60        let mut path_start_offset = 0;
 61        let (names, paths): (Vec<_>, Vec<_>) = location
 62            .paths()
 63            .iter()
 64            .map(|path| {
 65                let path = path.compact();
 66                let highlighted_text = Self::highlights_for_path(
 67                    path.as_ref(),
 68                    &string_match.positions,
 69                    path_start_offset,
 70                );
 71
 72                path_start_offset += highlighted_text.1.char_count;
 73
 74                highlighted_text
 75            })
 76            .unzip();
 77
 78        Self {
 79            names: HighlightedText::join(names.into_iter().filter_map(|name| name), ", "),
 80            paths,
 81        }
 82    }
 83
 84    // Compute the highlighted text for the name and path
 85    fn highlights_for_path(
 86        path: &Path,
 87        match_positions: &Vec<usize>,
 88        path_start_offset: usize,
 89    ) -> (Option<HighlightedText>, HighlightedText) {
 90        let path_string = path.to_string_lossy();
 91        let path_char_count = path_string.chars().count();
 92        // Get the subset of match highlight positions that line up with the given path.
 93        // Also adjusts them to start at the path start
 94        let path_positions = match_positions
 95            .iter()
 96            .copied()
 97            .skip_while(|position| *position < path_start_offset)
 98            .take_while(|position| *position < path_start_offset + path_char_count)
 99            .map(|position| position - path_start_offset)
100            .collect::<Vec<_>>();
101
102        // Again subset the highlight positions to just those that line up with the file_name
103        // again adjusted to the start of the file_name
104        let file_name_text_and_positions = path.file_name().map(|file_name| {
105            let text = file_name.to_string_lossy();
106            let char_count = text.chars().count();
107            let file_name_start = path_char_count - char_count;
108            let highlight_positions = path_positions
109                .iter()
110                .copied()
111                .skip_while(|position| *position < file_name_start)
112                .take_while(|position| *position < file_name_start + char_count)
113                .map(|position| position - file_name_start)
114                .collect::<Vec<_>>();
115            HighlightedText {
116                text: text.to_string(),
117                highlight_positions,
118                char_count,
119            }
120        });
121
122        (
123            file_name_text_and_positions,
124            HighlightedText {
125                text: path_string.to_string(),
126                highlight_positions: path_positions,
127                char_count: path_char_count,
128            },
129        )
130    }
131}