gpui-component 0.7.3

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI components for GPUI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Presentation prepared on a background thread: theme-independent syntax
//! captures and inline changes between paired lines.
use std::{collections::HashMap, ops::Range};

use gpui::{HighlightStyle, SharedString};
use gpui_base::input::HighlightStyleResolver;
use similar::{ChangeTag, TextDiff};

use super::{DiffFile, DiffSide, document::SourceLine};
use crate::{Rope, highlighter::SyntaxHighlighter};

/// The unit inline changes are compared and emphasized in.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DiffInlineUnit {
    /// Words and punctuation; changed words separated only by whitespace are
    /// emphasized as one run.
    #[default]
    Word,
    /// Grapheme clusters.
    Character,
}

/// Paired lines sharing less than this proportion of content are emphasized as
/// whole lines only; marking nearly every character adds noise, not signal.
const MIN_INLINE_SIMILARITY: f32 = 0.25;

/// Settings that determine prepared presentation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PresentationOptions {
    pub inline_unit: Option<DiffInlineUnit>,
    pub inline_max_line_length: usize,
    pub syntax_max_line_length: usize,
}

/// Highlighters by language, shared by the files of one preparation pass
/// because building one compiles its language's queries.
pub(crate) type SyntaxHighlighters = HashMap<SharedString, SyntaxHighlighter>;

/// Prepared presentation for one file.
#[derive(Default)]
pub(crate) struct FilePresentation {
    names: Vec<SharedString>,
    syntax: [LineSpans<u16>; 2],
    inline: [LineSpans<()>; 2],
}

/// Byte ranges relative to each line's source start, with a payload.
struct LineSpans<T> {
    /// `starts[ix]..starts[ix + 1]` index the spans of line `ix`.
    starts: Vec<u32>,
    spans: Vec<(Range<u32>, T)>,
}

impl<T> Default for LineSpans<T> {
    fn default() -> Self {
        Self {
            starts: Vec::new(),
            spans: Vec::new(),
        }
    }
}

impl<T> LineSpans<T> {
    fn from_lines(per_line: Vec<Vec<(Range<u32>, T)>>) -> Self {
        let mut spans = Self::default();
        spans.starts.reserve(per_line.len() + 1);
        spans.starts.push(0);
        for line in per_line {
            spans.spans.extend(line);
            spans.starts.push(spans.spans.len() as u32);
        }
        spans
    }

    fn line(&self, ix: usize) -> &[(Range<u32>, T)] {
        match (self.starts.get(ix), self.starts.get(ix + 1)) {
            (Some(start), Some(end)) => &self.spans[*start as usize..*end as usize],
            _ => &[],
        }
    }
}

impl FilePresentation {
    /// Blocking; run it on a background thread.
    pub(crate) fn prepare(
        file: &DiffFile,
        options: PresentationOptions,
        highlighters: &mut SyntaxHighlighters,
    ) -> Self {
        let mut presentation = Self::default();
        let mut name_ixs = HashMap::<SharedString, u16>::new();
        for side in [DiffSide::Original, DiffSide::Modified] {
            let per_line = syntax_spans(
                file,
                side,
                options.syntax_max_line_length,
                highlighters,
                &mut |name| {
                    let next = name_ixs.len().min(u16::MAX as usize) as u16;
                    *name_ixs.entry(name.clone()).or_insert_with(|| {
                        presentation.names.push(name.clone());
                        next
                    })
                },
            );
            presentation.syntax[side as usize] = LineSpans::from_lines(per_line);
        }
        if let Some(unit) = options.inline_unit {
            presentation.inline = inline_changes(file, unit, options.inline_max_line_length);
        }
        presentation
    }

    /// Theme styles for one line's syntax, as display ranges.
    pub(crate) fn syntax_highlights(
        &self,
        line: &SourceLine,
        side: DiffSide,
        ix: usize,
        theme: &dyn HighlightStyleResolver,
    ) -> Vec<(Range<usize>, HighlightStyle)> {
        self.syntax[side as usize]
            .line(ix)
            .iter()
            .filter_map(|(range, name)| {
                let style = theme.style(&self.names[*name as usize])?;
                Some((
                    line.display_range(range.start as usize..range.end as usize),
                    style,
                ))
            })
            .collect()
    }

    /// Changed runs within one line, as display ranges.
    pub(crate) fn inline_changes<'a>(
        &'a self,
        line: &'a SourceLine,
        side: DiffSide,
        ix: usize,
    ) -> impl Iterator<Item = Range<usize>> + 'a {
        self.inline[side as usize]
            .line(ix)
            .iter()
            .map(|(range, _)| line.display_range(range.start as usize..range.end as usize))
    }
}

/// Parses each hunk of one side independently, so an unterminated comment or
/// string in one hunk cannot affect the next.
fn syntax_spans(
    file: &DiffFile,
    side: DiffSide,
    max_line_length: usize,
    highlighters: &mut SyntaxHighlighters,
    name_ix: &mut dyn FnMut(&SharedString) -> u16,
) -> Vec<Vec<(Range<u32>, u16)>> {
    let lines = file.lines(side);
    let mut per_line: Vec<Vec<(Range<u32>, u16)>> = (0..lines.len()).map(|_| Vec::new()).collect();
    let language = match (file.language_override(), file.side_path(side)) {
        (Some(language), _) => language.clone(),
        (None, Some(path)) => detected_language(path),
        (None, None) => return per_line,
    };
    if language.as_str() == "text" || lines.is_empty() {
        return per_line;
    }
    let highlighter = highlighters
        .entry(language.clone())
        .or_insert_with(|| SyntaxHighlighter::new(&language));
    let ranges = if file.hunks().is_empty() {
        vec![0..lines.len()]
    } else {
        file.hunks().iter().map(|hunk| hunk.lines(side)).collect()
    };
    let source = file.source(side);
    for range in ranges.into_iter().filter(|range| !range.is_empty()) {
        let base = lines[range.start].source().start;
        let text = &source[base..lines[range.end - 1].source().end];
        let hunk_lines = &lines[range.clone()];
        for (capture, name) in highlighter.capture_names(&Rope::from_str(text)) {
            let capture = capture.start + base..capture.end + base;
            let first = hunk_lines.partition_point(|line| line.source().end <= capture.start);
            for (offset, line) in hunk_lines[first..].iter().enumerate() {
                let line_start = line.source().start;
                if line_start >= capture.end {
                    break;
                }
                if line.content_end() - line_start > max_line_length {
                    continue;
                }
                let start = capture.start.max(line_start);
                let end = capture.end.min(line.content_end());
                if start < end {
                    per_line[range.start + first + offset].push((
                        (start - line_start) as u32..(end - line_start) as u32,
                        name_ix(&name),
                    ));
                }
            }
        }
    }
    per_line
}

/// Compares each deleted line with the added line paired with it.
fn inline_changes(
    file: &DiffFile,
    unit: DiffInlineUnit,
    max_line_length: usize,
) -> [LineSpans<()>; 2] {
    let mut sides: [Vec<Vec<(Range<u32>, ())>>; 2] = [
        (0..file.lines_count(DiffSide::Original))
            .map(|_| Vec::new())
            .collect(),
        (0..file.lines_count(DiffSide::Modified))
            .map(|_| Vec::new())
            .collect(),
    ];
    for pair in file.pairs() {
        let (true, Some(old_ix), Some(new_ix)) =
            (pair.is_changed(), pair.original(), pair.modified())
        else {
            continue;
        };
        let old = file.lines(DiffSide::Original)[old_ix].text();
        let new = file.lines(DiffSide::Modified)[new_ix].text();
        if old.len() > max_line_length || new.len() > max_line_length || old == new {
            continue;
        }
        let (old_runs, new_runs) = changed_runs(old, new, unit);
        sides[0][old_ix] = old_runs.into_iter().map(|range| (range, ())).collect();
        sides[1][new_ix] = new_runs.into_iter().map(|range| (range, ())).collect();
    }
    let [original, modified] = sides;
    [
        LineSpans::from_lines(original),
        LineSpans::from_lines(modified),
    ]
}

/// Byte ranges that differ between two lines, on each side.
pub(crate) fn changed_runs(
    old: &str,
    new: &str,
    unit: DiffInlineUnit,
) -> (Vec<Range<u32>>, Vec<Range<u32>>) {
    let diff = match unit {
        DiffInlineUnit::Word => TextDiff::from_unicode_words(old, new),
        DiffInlineUnit::Character => TextDiff::from_graphemes(old, new),
    };
    if diff.ratio() < MIN_INLINE_SIMILARITY {
        return (Vec::new(), Vec::new());
    }
    let (mut old_runs, mut new_runs) = (Vec::new(), Vec::new());
    let (mut old_offset, mut new_offset) = (0usize, 0usize);
    for change in diff.iter_all_changes() {
        let len = change.value().len();
        match change.tag() {
            ChangeTag::Equal => {
                old_offset += len;
                new_offset += len;
            }
            ChangeTag::Delete => {
                push_run(&mut old_runs, old_offset..old_offset + len, old, unit);
                old_offset += len;
            }
            ChangeTag::Insert => {
                push_run(&mut new_runs, new_offset..new_offset + len, new, unit);
                new_offset += len;
            }
        }
    }
    (old_runs, new_runs)
}

/// Extends the previous run when only whitespace separates it from `range`.
fn push_run(runs: &mut Vec<Range<u32>>, range: Range<usize>, text: &str, unit: DiffInlineUnit) {
    if let Some(last) = runs.last_mut() {
        let gap = &text[last.end as usize..range.start];
        if gap.is_empty() || unit == DiffInlineUnit::Word && gap.trim().is_empty() {
            last.end = range.end as u32;
            return;
        }
    }
    runs.push(range.start as u32..range.end as u32);
}

/// File paths may come from another platform. Only the final component
/// determines the language; dots in a directory are not extensions.
pub(crate) fn detected_language(path: &str) -> SharedString {
    let name = path.rsplit(['/', '\\']).next().unwrap_or("").to_lowercase();
    let language = match name.as_str() {
        "makefile" | "gnumakefile" => "make",
        "cmakelists.txt" => "cmake",
        _ => match name.rsplit_once('.').map(|(_, extension)| extension) {
            Some("jsx" | "mjs" | "cjs") => "javascript",
            Some("mts" | "cts") => "typescript",
            Some("cc" | "cxx" | "hpp" | "hxx") => "cpp",
            Some("h") => "c",
            Some("htm") => "html",
            Some("gql") => "graphql",
            Some("exs") => "elixir",
            Some(
                extension @ ("astro" | "sh" | "bash" | "c" | "cmake" | "cs" | "cpp" | "css"
                | "scss" | "diff" | "ejs" | "ex" | "erb" | "go" | "graphql" | "html"
                | "java" | "js" | "json" | "jsonc" | "kt" | "kts" | "lua" | "md"
                | "mdx" | "php" | "php3" | "php4" | "php5" | "phtml" | "proto" | "py"
                | "pyi" | "rb" | "rs" | "scala" | "sql" | "svelte" | "swift" | "toml"
                | "tsx" | "ts" | "yaml" | "yml" | "zig"),
            ) => extension,
            _ => "text",
        },
    };
    crate::highlighter::language_name(language)
}

#[cfg(test)]
mod tests {
    use super::*;

    const OPTIONS: PresentationOptions = PresentationOptions {
        inline_unit: Some(DiffInlineUnit::Word),
        inline_max_line_length: 1000,
        syntax_max_line_length: 1000,
    };

    #[test]
    fn language_detection_uses_filename() {
        for (name, language) in [
            ("src/main.RS", "rust"),
            ("src/components/App.jsx", "javascript"),
            ("C:\\src\\lib.hpp", "cpp"),
            ("build/CMakeLists.txt", "cmake"),
            ("build/Makefile", "make"),
            ("directory.rs/README", "text"),
            ("ts", "text"),
            ("notes.unknown", "text"),
        ] {
            assert_eq!(detected_language(name).as_str(), language);
        }
        let file = DiffFile::parse("--- a/notes\n+++ b/notes\n@@ -1 +1 @@\n-a\n+b\n")
            .unwrap()
            .remove(0);
        assert_eq!(file.language().as_str(), "text");
        assert_eq!(file.with_language("rust").language().as_str(), "rust");
    }

    #[test]
    fn inline_changes_merge_words_across_whitespace_and_skip_unrelated_lines() {
        let (old, new) = changed_runs(
            "let total = price * count;",
            "let total = cost * quantity;",
            DiffInlineUnit::Word,
        );
        assert_eq!(old, [12..17, 20..25]);
        assert_eq!(new, [12..16, 19..27]);
        let (old, new) = changed_runs("old value here", "new value here", DiffInlineUnit::Word);
        assert_eq!((old, new), (vec![0..3], vec![0..3]));
        let (old, new) = changed_runs("a b c", "a x y c", DiffInlineUnit::Word);
        assert_eq!(new, [2..5]);
        assert!(old.is_empty() || old == [2..3]);
        let (old, new) = changed_runs("colour", "color", DiffInlineUnit::Character);
        assert_eq!((old, new), (vec![4..5], vec![]));
        let (old, new) = changed_runs("alpha", "zzzzzzzzz", DiffInlineUnit::Word);
        assert!(old.is_empty() && new.is_empty());
    }

    #[test]
    fn inline_changes_cover_paired_lines_only() {
        let file = DiffFile::parse(
            "--- a/a.txt\n+++ b/a.txt\n@@ -1,2 +1,3 @@\n-let total = price * count;\n same\n+let total = cost * count;\n+added\n",
        )
        .unwrap()
        .remove(0);
        let presentation =
            FilePresentation::prepare(&file, OPTIONS, &mut SyntaxHighlighters::default());
        let ranges = |side, ix| {
            let line = &file.lines(side)[ix];
            presentation
                .inline_changes(line, side, ix)
                .collect::<Vec<_>>()
        };
        // The deletion and the first addition are in different change runs,
        // so they are not paired.
        assert!(ranges(DiffSide::Original, 0).is_empty());
        let file = DiffFile::parse(
            "--- a/a.txt\n+++ b/a.txt\n@@ -1 +1,2 @@\n-let total = price * count;\n+let total = cost * count;\n+added\n",
        )
        .unwrap()
        .remove(0);
        let presentation =
            FilePresentation::prepare(&file, OPTIONS, &mut SyntaxHighlighters::default());
        let ranges = |side, ix| {
            let line = &file.lines(side)[ix];
            presentation
                .inline_changes(line, side, ix)
                .collect::<Vec<_>>()
        };
        assert_eq!(ranges(DiffSide::Original, 0), [12..17]);
        assert_eq!(ranges(DiffSide::Modified, 0), [12..16]);
        assert!(ranges(DiffSide::Modified, 1).is_empty());
        let off = PresentationOptions {
            inline_unit: None,
            ..OPTIONS
        };
        let presentation =
            FilePresentation::prepare(&file, off, &mut SyntaxHighlighters::default());
        assert_eq!(
            presentation
                .inline_changes(&file.lines(DiffSide::Original)[0], DiffSide::Original, 0)
                .count(),
            0
        );
    }

    #[cfg(feature = "tree-sitter-rust")]
    #[test]
    fn syntax_is_parsed_per_hunk_and_respects_language_and_line_limit() {
        // Hunks are separated by unavailable source. Parsing their fragments
        // together would join `/* open` and `close */` into one comment.
        let file = DiffFile::parse(
            "--- a/a.rs\n+++ b/a.rs\n@@ -1,2 +1,2 @@\n-let a = 1;\n+let a = 2;\n /* open\n@@ -10,2 +10,2 @@\n close */\n-fn old() {}\n+fn new() {}\n",
        )
        .unwrap()
        .remove(0);
        let presentation =
            FilePresentation::prepare(&file, OPTIONS, &mut SyntaxHighlighters::default());
        let names = |presentation: &FilePresentation, ix: usize| {
            presentation.syntax[DiffSide::Modified as usize]
                .line(ix)
                .iter()
                .map(|(_, name)| presentation.names[*name as usize].to_string())
                .collect::<Vec<_>>()
        };
        assert!(names(&presentation, 0).iter().any(|name| name == "keyword"));
        assert!(
            !names(&presentation, 2)
                .iter()
                .any(|name| name.starts_with("comment"))
        );
        assert!(
            names(&presentation, 3)
                .iter()
                .any(|name| name == "function")
        );

        let short = PresentationOptions {
            syntax_max_line_length: 5,
            ..OPTIONS
        };
        let presentation =
            FilePresentation::prepare(&file, short, &mut SyntaxHighlighters::default());
        assert!(names(&presentation, 0).is_empty());

        let as_text = file.clone().with_language("text");
        let presentation =
            FilePresentation::prepare(&as_text, OPTIONS, &mut SyntaxHighlighters::default());
        assert!(names(&presentation, 0).is_empty());
    }
}