-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Expand file tree
/
Copy pathduplicate.rs
More file actions
356 lines (306 loc) · 9.9 KB
/
Copy pathduplicate.rs
File metadata and controls
356 lines (306 loc) · 9.9 KB
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
use std::ops::Range;
use editor::{DisplayPoint, MultiBufferOffset, display_map::DisplaySnapshot};
use gpui::Context;
use language::PointUtf16;
use multi_buffer::{MultiBufferPoint, MultiBufferRow};
use text::Bias;
use ui::Window;
use crate::Vim;
#[derive(Copy, Clone)]
enum Direction {
Above,
Below,
}
impl Vim {
/// Creates a duplicate of every selection below it in the first place that has both its start
/// and end
pub(super) fn helix_duplicate_selections_below(
&mut self,
times: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.duplicate_selections(times, window, cx, Direction::Below);
}
/// Creates a duplicate of every selection above it in the first place that has both its start
/// and end
pub(super) fn helix_duplicate_selections_above(
&mut self,
times: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.duplicate_selections(times, window, cx, Direction::Above);
}
fn duplicate_selections(
&mut self,
times: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
direction: Direction,
) {
let times = times.unwrap_or(1);
self.update_editor(cx, |_, editor, cx| {
let mut selections = Vec::new();
let map = editor.display_snapshot(cx);
let mut original_selections = editor.selections.all_display(&map);
// The order matters, because it is recorded when the selections are added.
if matches!(direction, Direction::Above) {
original_selections.reverse();
}
for origin in original_selections {
let origin = origin.tail()..origin.head();
selections.push(display_point_range_to_offset_range(&origin, &map));
let mut last_origin = origin;
for _ in 1..=times {
if let Some(duplicate) =
find_next_valid_duplicate_space(last_origin.clone(), &map, direction)
{
selections.push(display_point_range_to_offset_range(&duplicate, &map));
last_origin = duplicate;
} else {
break;
}
}
}
editor.change_selections(Default::default(), window, cx, |s| {
s.select_ranges(selections);
});
});
}
}
fn find_next_valid_duplicate_space(
origin: Range<DisplayPoint>,
map: &DisplaySnapshot,
direction: Direction,
) -> Option<Range<DisplayPoint>> {
let buffer = map.buffer_snapshot();
let start_col_utf16 = buffer
.point_to_point_utf16(origin.start.to_point(map))
.column;
let end_col_utf16 = buffer.point_to_point_utf16(origin.end.to_point(map)).column;
let mut candidate = origin;
loop {
match direction {
Direction::Below => {
if candidate.end.row() >= map.max_point().row() {
return None;
}
*candidate.start.row_mut() += 1;
*candidate.end.row_mut() += 1;
}
Direction::Above => {
if candidate.start.row() == DisplayPoint::zero().row() {
return None;
}
*candidate.start.row_mut() = candidate.start.row().0.saturating_sub(1);
*candidate.end.row_mut() = candidate.end.row().0.saturating_sub(1);
}
}
let start_row = DisplayPoint::new(candidate.start.row(), 0)
.to_point(map)
.row;
let end_row = DisplayPoint::new(candidate.end.row(), 0).to_point(map).row;
if start_col_utf16 > buffer.line_len_utf16(MultiBufferRow(start_row))
|| end_col_utf16 > buffer.line_len_utf16(MultiBufferRow(end_row))
{
continue;
}
let start_col = buffer
.point_utf16_to_point(PointUtf16::new(start_row, start_col_utf16))
.column;
let end_col = buffer
.point_utf16_to_point(PointUtf16::new(end_row, end_col_utf16))
.column;
let candidate_start =
map.point_to_display_point(MultiBufferPoint::new(start_row, start_col), Bias::Left);
let candidate_end =
map.point_to_display_point(MultiBufferPoint::new(end_row, end_col), Bias::Right);
if map.clip_point(candidate_start, Bias::Left) == candidate_start
&& map.clip_point(candidate_end, Bias::Right) == candidate_end
{
return Some(candidate_start..candidate_end);
}
}
}
fn display_point_range_to_offset_range(
range: &Range<DisplayPoint>,
map: &DisplaySnapshot,
) -> Range<MultiBufferOffset> {
range.start.to_offset(map, Bias::Left)..range.end.to_offset(map, Bias::Right)
}
#[cfg(test)]
mod tests {
use db::indoc;
use editor::{Inlay, MultiBufferOffset};
use crate::{state::Mode, test::VimTestContext};
#[gpui::test]
async fn test_selection_duplication(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state(
indoc! {"
The quick brown
fox «jumpsˇ»
over the
lazy dog."},
Mode::HelixNormal,
);
cx.simulate_keystrokes("C");
cx.assert_state(
indoc! {"
The quick brown
fox «jumpsˇ»
over the
lazy« dog.ˇ»"},
Mode::HelixNormal,
);
cx.simulate_keystrokes("C");
cx.assert_state(
indoc! {"
The quick brown
fox «jumpsˇ»
over the
lazy« dog.ˇ»"},
Mode::HelixNormal,
);
cx.simulate_keystrokes("alt-C");
cx.assert_state(
indoc! {"
The «quickˇ» brown
fox «jumpsˇ»
over the
lazy« dog.ˇ»"},
Mode::HelixNormal,
);
cx.simulate_keystrokes(",");
cx.assert_state(
indoc! {"
The «quickˇ» brown
fox jumps
over the
lazy dog."},
Mode::HelixNormal,
);
}
#[gpui::test]
async fn test_selection_duplication_backwards(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state(
indoc! {"
The quick brown
«ˇfox» jumps
over the
lazy dog."},
Mode::HelixNormal,
);
cx.simulate_keystrokes("C C alt-C");
cx.assert_state(
indoc! {"
«ˇThe» quick brown
«ˇfox» jumps
«ˇove»r the
«ˇlaz»y dog."},
Mode::HelixNormal,
);
}
#[gpui::test]
async fn test_selection_duplication_count(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state(
indoc! {"
The «qˇ»uick brown
fox jumps
over the
lazy dog."},
Mode::HelixNormal,
);
cx.simulate_keystrokes("9 C");
cx.assert_state(
indoc! {"
The «qˇ»uick brown
fox «jˇ»umps
over« ˇ»the
lazy« ˇ»dog."},
Mode::HelixNormal,
);
}
#[gpui::test]
async fn test_selection_duplication_multiline_multibyte(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
// Multiline selection on rows with multibyte chars should preserve
// the visual column on both start and end rows.
cx.set_state(
indoc! {"
«H䡻llo
Hëllo
Hallo"},
Mode::HelixNormal,
);
cx.simulate_keystrokes("C");
cx.assert_state(
indoc! {"
«H䡻llo
«H롻llo
Hallo"},
Mode::HelixNormal,
);
}
#[gpui::test]
async fn test_selection_duplication_multibyte(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
// Selection on a line with multibyte chars should duplicate to the
// same character column on the next line, not skip it.
cx.set_state(
indoc! {"
H«äˇ»llo
Hallo"},
Mode::HelixNormal,
);
cx.simulate_keystrokes("C");
cx.assert_state(
indoc! {"
H«äˇ»llo
H«aˇ»llo"},
Mode::HelixNormal,
);
}
#[gpui::test]
async fn test_selection_duplication_with_inlay_hints(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.enable_helix();
cx.set_state(
indoc! {"
let x = «1ˇ»;
let y = 2;"},
Mode::HelixNormal,
);
cx.update_editor(|editor, window, cx| {
let buffer = &editor.snapshot(window, cx).buffer;
editor.splice_inlays(
&[],
vec![
Inlay::mock_hint(0, buffer.anchor_after(MultiBufferOffset(5)), ": i32"),
Inlay::mock_hint(1, buffer.anchor_after(MultiBufferOffset(16)), ": i32"),
],
cx,
);
});
cx.simulate_keystrokes("C");
assert_eq!(
cx.display_text(),
"let x: i32 = 1;
let y: i32 = 2;",
);
cx.assert_state(
indoc! {"
let x = «1ˇ»;
let y = «2ˇ»;"},
Mode::HelixNormal,
);
}
}