forked from jmacdonald/amp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.rs
248 lines (212 loc) · 7.5 KB
/
selection.rs
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
use crate::models::application::{Application, ClipboardContent, Mode};
use scribe::buffer::{LineRange, Range};
use super::application;
use crate::errors::*;
use crate::commands::{self, Result};
use crate::util;
use crate::util::reflow::Reflow;
pub fn delete(app: &mut Application) -> Result {
let rng = sel_to_range(app)?;
let buf = app.workspace.current_buffer().unwrap();
buf.delete_range(rng.clone());
buf.cursor.move_to(rng.start());
Ok(())
}
pub fn copy_and_delete(app: &mut Application) -> Result {
let _ = copy_to_clipboard(app);
delete(app)
}
pub fn change(app: &mut Application) -> Result {
let _ = copy_to_clipboard(app);
delete(app)?;
application::switch_to_insert_mode(app)?;
commands::view::scroll_to_cursor(app)
}
pub fn copy(app: &mut Application) -> Result {
copy_to_clipboard(app)?;
application::switch_to_normal_mode(app)
}
pub fn select_all(app: &mut Application) -> Result {
app.workspace
.current_buffer()
.ok_or(BUFFER_MISSING)?
.cursor
.move_to_first_line();
application::switch_to_select_line_mode(app)?;
app.workspace
.current_buffer()
.ok_or(BUFFER_MISSING)?
.cursor
.move_to_last_line();
Ok(())
}
fn copy_to_clipboard(app: &mut Application) -> Result {
let buffer = app.workspace.current_buffer().ok_or(BUFFER_MISSING)?;
match app.mode {
Mode::Select(ref select_mode) => {
let cursor_position = *buffer.cursor.clone();
let selected_range = Range::new(cursor_position, select_mode.anchor);
let data = buffer.read(&selected_range.clone())
.ok_or("Couldn't read selected data from buffer")?;
app.clipboard.set_content(ClipboardContent::Inline(data))?;
}
Mode::SelectLine(ref mode) => {
let selected_range = util::inclusive_range(
&LineRange::new(
mode.anchor,
buffer.cursor
.line
),
buffer
);
let data = buffer.read(&selected_range.clone())
.ok_or("Couldn't read selected data from buffer")?;
app.clipboard.set_content(ClipboardContent::Block(data))?;
}
_ => bail!("Can't copy data to clipboard outside of select modes"),
};
Ok(())
}
pub fn justify(app: &mut Application) -> Result {
let rng = sel_to_range(app)?;
let mut buf = app.workspace.current_buffer().unwrap();
let tar = match app.preferences.borrow().line_length_guide() {
Some(n) => n,
None => bail!("Justification requires a line_length_guide."),
};
let rfl = Reflow::new(&mut buf, rng, tar)?;
rfl.apply()?;
Ok(())
}
fn sel_to_range(app: &mut Application) -> std::result::Result<Range, Error> {
let buf = app.workspace.current_buffer().ok_or(BUFFER_MISSING)?;
match app.mode {
Mode::Select(ref sel) => {
let cursor_position = *buf.cursor.clone();
Ok(Range::new(cursor_position, sel.anchor))
},
Mode::SelectLine(ref sel) => {
Ok(util::inclusive_range(
&LineRange::new(
sel.anchor,
buf.cursor.line
),
buf
))
},
Mode::Search(ref search) => {
Ok(search
.results
.as_ref()
.and_then(|r| r.selection())
.ok_or("A selection is required.")
.unwrap()
.clone()
)
}
_ => bail!("A selection is required."),
}
}
#[cfg(test)]
mod tests {
use crate::commands;
use crate::models::application::{Application, Mode};
use scribe::Buffer;
use scribe::buffer::Position;
#[test]
fn select_all_selects_the_entire_buffer() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
// Insert data with indentation and move to the end of the line.
buffer.insert("amp\neditor\nbuffer");
let position = Position {
line: 1,
offset: 3,
};
buffer.cursor.move_to(position);
// Now that we've set up the buffer, add it
// to the application and call the command.
app.workspace.add_buffer(buffer);
super::select_all(&mut app).unwrap();
// Ensure that the application is in select line mode,
// and that its anchor position is on the first line
// of the buffer.
match app.mode {
Mode::SelectLine(ref mode) => {
assert_eq!(mode.anchor, 0);
},
_ => panic!("Application isn't in select line mode.")
}
// Ensure that the cursor is moved to the last line of the buffer.
assert_eq!(app.workspace.current_buffer().unwrap().cursor.line, 2);
}
#[test]
fn delete_removes_the_selection_in_select_mode() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
// Insert data with indentation and move to the end of the line.
buffer.insert("amp\neditor\nbuffer");
let position = Position {
line: 1,
offset: 0,
};
buffer.cursor.move_to(position);
// Now that we've set up the buffer, add it
// to the application and call the command.
app.workspace.add_buffer(buffer);
commands::application::switch_to_select_mode(&mut app).unwrap();
commands::cursor::move_right(&mut app).unwrap();
commands::selection::delete(&mut app).unwrap();
// Ensure that the cursor is moved to the last line of the buffer.
assert_eq!(
app.workspace.current_buffer().unwrap().data(),
String::from("amp\nditor\nbuffer")
)
}
#[test]
fn delete_removes_the_selected_line_in_select_line_mode() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
// Insert data with indentation and move to the end of the line.
buffer.insert("amp\neditor\nbuffer");
let position = Position {
line: 1,
offset: 0,
};
buffer.cursor.move_to(position);
// Now that we've set up the buffer, add it
// to the application and call the command.
app.workspace.add_buffer(buffer);
commands::application::switch_to_select_line_mode(&mut app).unwrap();
commands::selection::delete(&mut app).unwrap();
// Ensure that the cursor is moved to the last line of the buffer.
assert_eq!(
app.workspace.current_buffer().unwrap().data(),
String::from("amp\nbuffer")
)
}
#[test]
fn delete_removes_the_current_result_in_search_mode() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
// Insert data with indentation and move to the end of the line.
buffer.insert("amp\neditor\nbuffer");
let position = Position {
line: 1,
offset: 0,
};
buffer.cursor.move_to(position);
// Now that we've set up the buffer, add it
// to the application and call the command.
app.workspace.add_buffer(buffer);
app.search_query = Some(String::from("ed"));
commands::application::switch_to_search_mode(&mut app).unwrap();
commands::search::accept_query(&mut app).unwrap();
commands::selection::delete(&mut app).unwrap();
// Ensure that the cursor is moved to the last line of the buffer.
assert_eq!(
app.workspace.current_buffer().unwrap().data(),
String::from("amp\nitor\nbuffer")
)
}
}