aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/panes/selection.rs
blob: 22c10e180c2bbeae14f18014e903a352d685a58c (plain) (blame)
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
use std::{collections::HashSet, ops::Range};

use zellij_utils::position::Position;

// The selection is empty when start == end
// it includes the character at start, and everything before end.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Selection {
    pub start: Position,
    pub end: Position,
    active: bool, // used to handle moving the selection up and down
    last_added_word_position: Option<(Position, Position)>, // (start / end)
    last_added_line_index: Option<isize>,
}

impl Default for Selection {
    fn default() -> Self {
        Self {
            start: Position::new(0, 0),
            end: Position::new(0, 0),
            active: false,
            last_added_word_position: None,
            last_added_line_index: None,
        }
    }
}

impl Selection {
    pub fn start(&mut self, start: Position) {
        self.active = true;
        self.start = start;
        self.end = start;
    }

    pub fn to(&mut self, to: Position) {
        self.end = to
    }

    pub fn end(&mut self, end: Position) {
        self.active = false;
        self.end = end;
    }

    pub fn finalize(&mut self) {
        self.active = false;
    }

    pub fn set_start_and_end_positions(&mut self, start: Position, end: Position) {
        self.active = true;
        self.start = start;
        self.end = end;
        self.last_added_word_position = Some((start, end));
        self.last_added_line_index = Some(start.line.0);
    }
    pub fn add_word_to_position(&mut self, word_start: Position, word_end: Position) {
        // here we assume word_start is smaller or equal to word_end
        let already_added = self
            .last_added_word_position
            .map(|(last_word_start, last_word_end)| {
                last_word_start == word_start && last_word_end == word_end
            })
            .unwrap_or(false);
        if already_added {
            return;
        }
        let word_is_above_last_added_word = self
            .last_added_word_position
            .map(|(l_start, _l_end)| word_start.line < l_start.line)
            .unwrap_or(false);
        let word_is_below_last_added_word = self
            .last_added_word_position
            .map(|(_l_start, l_end)| word_end.line > l_end.line)
            .unwrap_or(false);
        if word_is_above_last_added_word && word_start.line < self.start.line {
            // extend line above
            self.start = word_start;
        } else if word_is_below_last_added_word && word_end.line > self.end.line {
            // extend line below
            self.end = word_end;
        } else if word_is_below_last_added_word && word_start.line > self.start.line {
            // reduce from above
            self.start = word_start;
        } else if word_is_above_last_added_word && word_end.line < self.end.line {
            // reduce from below
            self.end = word_end;
        } else {
            let word_end_is_to_the_left_of_last_word_start = self
                .last_added_word_position
                .map(|(l_start, _l_end)| word_end.column <= l_start.column)
                .unwrap_or(false);
            let word_start_is_to_the_right_of_last_word_end = self
                .last_added_word_position
                .map(|(_l_start, l_end)| word_start.column >= l_end.column)
                .unwrap_or(false);
            let last_word_start_equals_word_end = self
                .last_added_word_position
                .map(|(l_start, _l_end)| l_start.column == word_end.column)
                .unwrap_or(false);
            let last_word_end_equals_word_start = self
                .last_added_word_position
                .map(|(_l_start, l_end)| l_end.column == word_start.column)
                .unwrap_or(false);
            let selection_start_column_is_to_the_right_of_word_start =
                self.start.column > word_start.column;
            let selection_start_is_on_same_line_as_word_start = self.start.line == word_start.line;
            let selection_end_is_to_the_left_of_word_end = self.end.column < word_end.column;
            let selection_end_is_on_same_line_as_word_end = self.end.line == word_end.line;
            if word_end_is_to_the_left_of_last_word_start
                && selection_start_column_is_to_the_right_of_word_start
                && selection_start_is_on_same_line_as_word_start
            {
                // extend selection left
                self.start.column = word_start.column;
            } else if word_start_is_to_the_right_of_last_word_end
                && selection_end_is_to_the_left_of_word_end
                && selection_end_is_on_same_line_as_word_end
            {
                // extend selection right
                self.end.column = word_end.column;
            } else if last_word_start_equals_word_end {
                // reduce selection from the right
                self.end.column = word_end.column;
            } else if last_word_end_equals_word_start {
                // reduce selection from the left
                self.start.column = word_start.column;
            }
        }
        self.last_added_word_position = Some((word_start, word_end));
    }
    pub fn add_line_to_position(&mut self, line_index: isize, last_index_in_line: usize) {
        let already_added = self
            .last_added_line_index
            .map(|last_added_line_index| last_added_line_index == line_index)
            .unwrap_or(false);
        if already_added {
            return;
        }
        let line_index_is_smaller_than_last_added_line_index = self
            .last_added_line_index
            .map(|last| line_index < last)
            .unwrap_or(false);
        let line_index_is_larger_than_last_added_line_index = self
            .last_added_line_index
            .map(|last| line_index > last)
            .unwrap_or(false);

        if line_index_is_smaller_than_last_added_line_index && self.start.line.0 > line_index {
            // extend selection one line upwards
            self.start = Position::new(line_index as i32, 0);
        } else if line_index_is_larger_than_last_added_line_index && self.end.line.0 < line_index {
            // extend selection one line downwards
            self.end = Position::new(line_index as i32, last_index_in_line as u16);
        } else if line_index_is_smaller_than_last_added_line_index && self.end.line.0 > line_index {
            // reduce selection one line from below
            self.end = Position::new(line_index as i32, last_index_in_line as u16);
        } else if line_index_is_larger_than_last_added_line_index && self.start.line.0 < line_index
        {
            // reduce selection one line from above
            self.start = Position::new(line_index as i32, 0);
        }

        self.last_added_line_index = Some(line_index);
    }

    pub fn contains(&self, row: usize, col: usize) -> bool {
        let row = row as isize;
        let (start, end) = if self.start <= self.end {
            (self.start, self.end)
        } else {
            (self.end, self.start)
        };

        if (start.line.0) < row && row < end.line.0 {
            return true;
        }
        if start.line == end.line {
            return row == start.line.0 && start.column.0 <= col && col < end.column.0;
        }
        if start.line.0 == row && col >= start.column.0 {
            return true;
        }
        end.line.0 == row && col < end.column.0
    }

    pub fn contains_row(&self, row: usize) -> bool {
        let row = row as isize;
        let (start, end) = if self.start <= self.end {
            (self.start, self.end)
        } else {
            (self.end, self.start)
        };
        start.line.0 <= row && end.line.0 >= row
    }

    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    pub fn reset(&mut self) {
        self.start = Position::new(0, 0);
        self.end = self.start;
    }

    pub fn sorted(&self) -> Self {
        let (start, end) = if self.start <= self.end {
            (self.start, self.end)
        } else {
            (self.end, self.start)
        };
        Self {
            start,
            end,
            active: self.active,
            last_added_word_position: self.last_added_word_position,
            last_added_line_index: self.last_added_line_index,
        }
    }

    pub fn line_indices(&self) -> std::ops::RangeInclusive<isize> {
        let sorted = self.sorted();
        sorted.start.line.0..=sorted.end.line.0
    }

    pub fn move_up(&mut self, lines: usize) {
        self.start.line.0 -= lines as isize;
        if !self.active {
            self.end.line.0 -= lines as isize;
        }
    }

    pub fn move_down(&mut self, lines: usize) {
        self.start.line.0 += lines as isize;
        if !self.active {
            self.end.line.0 += lines as isize;
        }
    }
    pub fn offset(mut self, offset_x: usize, offset_y: usize) -> Self {
        self.start.line.0 += offset_y as isize;
        self.end.line.0 += offset_y as isize;
        self.start.column.0 += offset_x;
        self.end.column.0 += offset_x;
        self
    }

    /// Return an iterator over the line indices, up to max, that are not present in both self and other,
    /// except for the indices of the first and last line of both self and s2, that are always included.
    pub fn diff(&self, other: &Self, max: usize) -> impl Iterator<Item = isize> {
        let mut lines_to_update = HashSet::new();

        lines_to_update.insert(self.start.line.0);
        lines_to_update.insert(self.end.line.0);
        lines_to_update.insert(other.start.line.0);
        lines_to_update.insert(other.end.line.0);

        let old_lines: HashSet<isize> = self.get_visible_indices(max).collect();
        let new_lines: HashSet<isize> = other.get_visible_indices(max).collect();

        old_lines.symmetric_difference(&new_lines).for_each(|&l| {
            let _ = lines_to_update.insert(l);
        });

        lines_to_update
            .into_iter()
            .filter(move |&l| l >= 0 && l < max as isize)
    }

    fn get_visible_indices(&self, max: usize) -> Range<isize> {
        let Selection { start, end, .. } = self.sorted();
        let start = start.line.0.max(0);
        let end = end.line.0.min(max as isize);
        start..end
    }
}

#[cfg(test)]
#[path = "./unit/selection_tests.rs"]
mod selection_tests;