BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / input / base / selection.rs

selection.rs

eef6c0a4 History
selection.rs 5.2 KiB Download Raw
1use crate::input::InputModeKind;
2use std::ops::Range;
3
4use gpui::{Context, Window};
5use ropey::Rope;
6use sum_tree::Bias;
7
8use super::{InputBaseState, RopeExt as _};
9use crate::text_boundary::word_range_from_chars;
10
11/// Unique identifier for a cursor/selection.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
13pub(super) struct CursorId(usize);
14
15impl CursorId {
16 pub(super) fn new(id: usize) -> Self {
17 Self(id)
18 }
19}
20
21impl<M: InputModeKind> InputBaseState<M> {
22 /// Select the word at the given offset on double-click.
23 ///
24 /// The offset is the UTF-8 offset.
25 pub(super) fn select_word(&mut self, offset: usize, _: &mut Window, cx: &mut Context<Self>) {
26 // A masked value renders as one unbroken run of mask characters, so it
27 // has no word boundaries to select by. Take all of it instead, rather
28 // than let the selection highlight reveal where the words are.
29 let range = if self.masked {
30 0..self.text.len()
31 } else {
32 let Some(range) = TextSelector::word_range(&self.text, offset) else {
33 return;
34 };
35 range
36 };
37
38 self.undo_manager.break_transaction_coalescing();
39 self.selections.remove_all_but_active();
40 self.set_selection(range.start, range.end);
41 self.selected_word_range = Some(*self.active_selection());
42 cx.notify()
43 }
44
45 /// Select the line at the given offset on triple-click.
46 ///
47 /// The offset is the UTF-8 offset.
48 pub(super) fn select_line(&mut self, offset: usize, _: &mut Window, cx: &mut Context<Self>) {
49 let range = TextSelector::line_range(&self.text, offset);
50 self.undo_manager.break_transaction_coalescing();
51 self.selections.remove_all_but_active();
52 self.set_selection(range.start, range.end);
53 self.selected_word_range = None;
54 cx.notify()
55 }
56}
57
58struct TextSelector;
59impl TextSelector {
60 /// Select a line in the given text at the specified offset.
61 ///
62 /// The offset is the UTF-8 offset.
63 ///
64 /// Returns the start and end offsets of the selected line.
65 pub(crate) fn line_range(text: &Rope, offset: usize) -> Range<usize> {
66 let offset = text.clip_offset(offset, Bias::Left);
67 let row = text.offset_to_point(offset).row;
68 let start = text.line_start_offset(row);
69 let end = text.line_end_offset(row);
70
71 start..end
72 }
73
74 /// Select a word in the given text at the specified offset.
75 ///
76 /// The offset is the UTF-8 offset.
77 ///
78 /// Returns the start and end offsets of the selected word.
79 pub(crate) fn word_range(text: &Rope, offset: usize) -> Option<Range<usize>> {
80 let offset = text.clip_offset(offset, Bias::Left);
81 let Some(char) = text.char_at(offset) else {
82 return None;
83 };
84
85 let end = offset + char.len_utf8();
86 let prev_chars = text.chars_at(offset).reversed().take(128);
87 let next_chars = text.chars_at(end).take(128);
88 Some(word_range_from_chars(offset, char, prev_chars, next_chars))
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95 use ropey::Rope;
96
97 #[test]
98 fn test_word_range() {
99 use indoc::indoc;
100
101 let rope = Rope::from(indoc! {
102 r#"
103 test text:
104 abcde 中文🎉 test
105 hello[()]
106 test_connector ____
107 Rope
108 rök
109 grande île
110 "#
111 });
112
113 let tests = vec![
114 (0, 0, Some("test")),
115 (0, 4, Some(" ")),
116 (1, 0, Some("abcde")),
117 (1, 4, Some("abcde")),
118 (1, 5, Some(" ")),
119 (1, 6, Some("中")),
120 (1, 9, Some("文")),
121 (1, 13, Some("🎉")),
122 (1, 20, Some("test")),
123 (2, 5, Some("[")),
124 (2, 6, Some("(")),
125 (2, 7, Some(")")),
126 (2, 8, Some("]")),
127 (3, 5, Some("test_connector")),
128 (3, 14, Some(" ")),
129 (3, 16, Some("____")),
130 (4, 0, Some("Rope")),
131 (5, 0, Some("rök")),
132 (6, 8, Some("île")),
133 ];
134
135 for (line, column, expected) in tests {
136 let line_start_offset = rope.line_start_offset(line);
137 let offset = line_start_offset + column;
138 let range = TextSelector::word_range(&rope, offset);
139
140 let actual = range.map(|r| rope.slice(r).to_string());
141 let expect = expected.map(|s| s.to_string());
142 assert_eq!(actual, expect, "line {}, column {}", line, column);
143 }
144 }
145
146 #[test]
147 fn test_line_range() {
148 let rope = Rope::from("first line\nsecond line\nthird");
149 let tests = vec![
150 (0, 0, "first line"),
151 (0, 5, "first line"),
152 (1, 3, "second line"),
153 (2, 1, "third"),
154 ];
155
156 for (line, column, expected) in tests {
157 let line_start_offset = rope.line_start_offset(line);
158 let offset = line_start_offset + column;
159 let range = TextSelector::line_range(&rope, offset);
160
161 let actual = rope.slice(range).to_string();
162 assert_eq!(actual, expected, "line {}, column {}", line, column);
163 }
164 }
165}