@@ -19,7 +19,7 @@ use std::path::{Path, PathBuf};
19use helix_view::{
19use helix_view::{
20 document::Mode,
20 document::Mode,
21 view::{View, PADDING},
21 view::{View, PADDING},
22− Document, DocumentId, Editor,
22+ Document, DocumentId, Editor, ViewId,
25use crossterm::event::{KeyCode, KeyEvent};
25use crossterm::event::{KeyCode, KeyEvent};
@@ -31,6 +31,7 @@ use crate::application::{LspCallbackWrapper, LspCallbacks};
31pub struct Context<'a> {
31pub struct Context<'a> {
32 pub count: usize,
32 pub count: usize,
33 pub editor: &'a mut Editor,
33 pub editor: &'a mut Editor,
35 pub callback: Option<crate::compositor::Callback>,
36 pub callback: Option<crate::compositor::Callback>,
36 pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
37 pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
@@ -101,9 +102,10 @@ pub type Command = fn(cx: &mut Context);
102pub fn move_char_left(cx: &mut Context) {
103pub fn move_char_left(cx: &mut Context) {
103 let count = cx.count;
104 let count = cx.count;
105+ let view_id = cx.view_id;
104 let doc = cx.doc();
106 let doc = cx.doc();
105 let text = doc.text().slice(..);
107 let text = doc.text().slice(..);
106− let selection = doc.selection().transform(|range| {
108+ let selection = doc.selection(view_id).transform(|range| {
107 movement::move_horizontally(
109 movement::move_horizontally(
@@ -112,14 +114,15 @@ pub fn move_char_left(cx: &mut Context) {
112 false, /* extend */
114 false, /* extend */
115− doc.set_selection(selection);
117+ doc.set_selection(view_id, selection);
118pub fn move_char_right(cx: &mut Context) {
120pub fn move_char_right(cx: &mut Context) {
119 let count = cx.count;
121 let count = cx.count;
122+ let view_id = cx.view_id;
120 let doc = cx.doc();
123 let doc = cx.doc();
121 let text = doc.text().slice(..);
124 let text = doc.text().slice(..);
122− let selection = doc.selection().transform(|range| {
125+ let selection = doc.selection(view_id).transform(|range| {
123 movement::move_horizontally(
126 movement::move_horizontally(
@@ -128,14 +131,15 @@ pub fn move_char_right(cx: &mut Context) {
128 false, /* extend */
131 false, /* extend */
131− doc.set_selection(selection);
134+ doc.set_selection(view_id, selection);
134pub fn move_line_up(cx: &mut Context) {
137pub fn move_line_up(cx: &mut Context) {
135 let count = cx.count;
138 let count = cx.count;
139+ let view_id = cx.view_id;
136 let doc = cx.doc();
140 let doc = cx.doc();
137 let text = doc.text().slice(..);
141 let text = doc.text().slice(..);
138− let selection = doc.selection().transform(|range| {
142+ let selection = doc.selection(view_id).transform(|range| {
139 movement::move_vertically(
143 movement::move_vertically(
@@ -144,14 +148,15 @@ pub fn move_line_up(cx: &mut Context) {
144 false, /* extend */
148 false, /* extend */
147− doc.set_selection(selection);
151+ doc.set_selection(view_id, selection);
150pub fn move_line_down(cx: &mut Context) {
154pub fn move_line_down(cx: &mut Context) {
151 let count = cx.count;
155 let count = cx.count;
156+ let view_id = cx.view_id;
152 let doc = cx.doc();
157 let doc = cx.doc();
153 let text = doc.text().slice(..);
158 let text = doc.text().slice(..);
154− let selection = doc.selection().transform(|range| {
159+ let selection = doc.selection(view_id).transform(|range| {
155 movement::move_vertically(
160 movement::move_vertically(
@@ -160,12 +165,13 @@ pub fn move_line_down(cx: &mut Context) {
160 false, /* extend */
165 false, /* extend */
163− doc.set_selection(selection);
168+ doc.set_selection(view_id, selection);
166pub fn move_line_end(cx: &mut Context) {
171pub fn move_line_end(cx: &mut Context) {
172+ let view_id = cx.view_id;
167 let doc = cx.doc();
173 let doc = cx.doc();
168− let lines = selection_lines(doc.text(), doc.selection());
174+ let lines = selection_lines(doc.text(), doc.selection(view_id));
170 let positions = lines
176 let positions = lines
171 .into_iter()
177 .into_iter()
@@ -180,12 +186,13 @@ pub fn move_line_end(cx: &mut Context) {
181 let selection = Selection::new(positions.collect(), 0);
187 let selection = Selection::new(positions.collect(), 0);
183− doc.set_selection(selection);
189+ doc.set_selection(view_id, selection);
186pub fn move_line_start(cx: &mut Context) {
192pub fn move_line_start(cx: &mut Context) {
193+ let view_id = cx.view_id;
187 let doc = cx.doc();
194 let doc = cx.doc();
188− let lines = selection_lines(doc.text(), doc.selection());
195+ let lines = selection_lines(doc.text(), doc.selection(view_id));
190 let positions = lines
197 let positions = lines
191 .into_iter()
198 .into_iter()
@@ -197,7 +204,7 @@ pub fn move_line_start(cx: &mut Context) {
198 let selection = Selection::new(positions.collect(), 0);
205 let selection = Selection::new(positions.collect(), 0);
200− doc.set_selection(selection);
207+ doc.set_selection(view_id, selection);
203// TODO: move vs extend could take an extra type Extend/Move that would
210// TODO: move vs extend could take an extra type Extend/Move that would
@@ -206,93 +213,101 @@ pub fn move_line_start(cx: &mut Context) {
207pub fn move_next_word_start(cx: &mut Context) {
214pub fn move_next_word_start(cx: &mut Context) {
208 let count = cx.count;
215 let count = cx.count;
216+ let view_id = cx.view_id;
209 let doc = cx.doc();
217 let doc = cx.doc();
210 let text = doc.text().slice(..);
218 let text = doc.text().slice(..);
212− let selection = doc.selection().transform(|range| {
220+ let selection = doc.selection(view_id).transform(|range| {
213 let pos = movement::move_next_word_start(text, range.head, count);
221 let pos = movement::move_next_word_start(text, range.head, count);
214 Range::new(pos, pos)
222 Range::new(pos, pos)
217− doc.set_selection(selection);
225+ doc.set_selection(view_id, selection);
220pub fn move_prev_word_start(cx: &mut Context) {
228pub fn move_prev_word_start(cx: &mut Context) {
221 let count = cx.count;
229 let count = cx.count;
230+ let view_id = cx.view_id;
222 let doc = cx.doc();
231 let doc = cx.doc();
223 let text = doc.text().slice(..);
232 let text = doc.text().slice(..);
225− let selection = doc.selection().transform(|range| {
234+ let selection = doc.selection(view_id).transform(|range| {
226 let pos = movement::move_prev_word_start(text, range.head, count);
235 let pos = movement::move_prev_word_start(text, range.head, count);
227 Range::new(pos, pos)
236 Range::new(pos, pos)
230− doc.set_selection(selection);
239+ doc.set_selection(view_id, selection);
233pub fn move_next_word_end(cx: &mut Context) {
242pub fn move_next_word_end(cx: &mut Context) {
234 let count = cx.count;
243 let count = cx.count;
244+ let view_id = cx.view_id;
235 let doc = cx.doc();
245 let doc = cx.doc();
236 let text = doc.text().slice(..);
246 let text = doc.text().slice(..);
238− let selection = doc.selection().transform(|range| {
248+ let selection = doc.selection(view_id).transform(|range| {
239 let pos = movement::move_next_word_end(text, range.head, count);
249 let pos = movement::move_next_word_end(text, range.head, count);
240 Range::new(pos, pos)
250 Range::new(pos, pos)
243− doc.set_selection(selection);
253+ doc.set_selection(view_id, selection);
246pub fn move_file_start(cx: &mut Context) {
256pub fn move_file_start(cx: &mut Context) {
247 push_jump(cx);
257 push_jump(cx);
258+ let view_id = cx.view_id;
248 let doc = cx.doc();
259 let doc = cx.doc();
249− doc.set_selection(Selection::point(0));
260+ doc.set_selection(view_id, Selection::point(0));
252pub fn move_file_end(cx: &mut Context) {
263pub fn move_file_end(cx: &mut Context) {
253 push_jump(cx);
264 push_jump(cx);
265+ let view_id = cx.view_id;
254 let doc = cx.doc();
266 let doc = cx.doc();
255 let text = doc.text();
267 let text = doc.text();
256 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
268 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
257− doc.set_selection(Selection::point(last_line));
269+ doc.set_selection(view_id, Selection::point(last_line));
260pub fn extend_next_word_start(cx: &mut Context) {
272pub fn extend_next_word_start(cx: &mut Context) {
261 let count = cx.count;
273 let count = cx.count;
274+ let view_id = cx.view_id;
262 let doc = cx.doc();
275 let doc = cx.doc();
263 let text = doc.text().slice(..);
276 let text = doc.text().slice(..);
265− let selection = doc.selection().transform(|mut range| {
278+ let selection = doc.selection(view_id).transform(|mut range| {
266 let pos = movement::move_next_word_start(text, range.head, count);
279 let pos = movement::move_next_word_start(text, range.head, count);
267 Range::new(range.anchor, pos)
280 Range::new(range.anchor, pos)
270− doc.set_selection(selection);
283+ doc.set_selection(view_id, selection);
273pub fn extend_prev_word_start(cx: &mut Context) {
286pub fn extend_prev_word_start(cx: &mut Context) {
274 let count = cx.count;
287 let count = cx.count;
288+ let view_id = cx.view_id;
275 let doc = cx.doc();
289 let doc = cx.doc();
276 let text = doc.text().slice(..);
290 let text = doc.text().slice(..);
278− let selection = doc.selection().transform(|mut range| {
292+ let selection = doc.selection(view_id).transform(|mut range| {
279 let pos = movement::move_prev_word_start(text, range.head, count);
293 let pos = movement::move_prev_word_start(text, range.head, count);
280 Range::new(range.anchor, pos)
294 Range::new(range.anchor, pos)
282− doc.set_selection(selection);
296+ doc.set_selection(view_id, selection);
285pub fn extend_next_word_end(cx: &mut Context) {
299pub fn extend_next_word_end(cx: &mut Context) {
286 let count = cx.count;
300 let count = cx.count;
301+ let view_id = cx.view_id;
287 let doc = cx.doc();
302 let doc = cx.doc();
288 let text = doc.text().slice(..);
303 let text = doc.text().slice(..);
290− let selection = doc.selection().transform(|mut range| {
305+ let selection = doc.selection(view_id).transform(|mut range| {
291 let pos = movement::move_next_word_end(text, range.head, count);
306 let pos = movement::move_next_word_end(text, range.head, count);
292 Range::new(range.anchor, pos)
307 Range::new(range.anchor, pos)
295− doc.set_selection(selection);
310+ doc.set_selection(view_id, selection);
@@ -313,10 +328,11 @@ where
314 } = event
329 } = event
331+ let view_id = cx.view_id;
316 let doc = cx.doc();
332 let doc = cx.doc();
317 let text = doc.text().slice(..);
333 let text = doc.text().slice(..);
319− let selection = doc.selection().transform(|mut range| {
335+ let selection = doc.selection(view_id).transform(|mut range| {
320 if let Some(pos) = search::find_nth_next(text, ch, range.head, count, inclusive) {
336 if let Some(pos) = search::find_nth_next(text, ch, range.head, count, inclusive) {
321 if extend {
337 if extend {
322 Range::new(range.anchor, pos)
338 Range::new(range.anchor, pos)
@@ -330,7 +346,7 @@ where
333− doc.set_selection(selection);
349+ doc.set_selection(view_id, selection);
@@ -417,15 +433,16 @@ pub fn replace(cx: &mut Context) {
418 let text = Tendril::from_char(ch);
434 let text = Tendril::from_char(ch);
436+ let view_id = cx.view_id;
420 let doc = cx.doc();
437 let doc = cx.doc();
422 let transaction =
439 let transaction =
423− Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
440+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
424 (range.from(), range.to() + 1, Some(text.clone()))
441 (range.from(), range.to() + 1, Some(text.clone()))
427− doc.apply(&transaction);
444+ doc.apply(&transaction, view_id);
428− doc.append_changes_to_history();
445+ doc.append_changes_to_history(view_id);
@@ -434,7 +451,7 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
434 use Direction::*;
451 use Direction::*;
435 let view = cx.editor.view();
452 let view = cx.editor.view();
436 let doc = cx.editor.document(view.doc).unwrap();
453 let doc = cx.editor.document(view.doc).unwrap();
437− let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
454+ let cursor = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor());
438 let doc_last_line = doc.text().len_lines() - 1;
455 let doc_last_line = doc.text().len_lines() - 1;
440 let last_line = view.last_line(doc);
457 let last_line = view.last_line(doc);
@@ -465,6 +482,8 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
465 last_line.saturating_sub(scrolloff),
482 last_line.saturating_sub(scrolloff),
485+ let view_id = view.id;
468 // view drops here
487 // view drops here
470 // upgrade to mut reference
489 // upgrade to mut reference
@@ -473,7 +492,7 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
473 let text = doc.text().slice(..);
492 let text = doc.text().slice(..);
474 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
493 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
476− doc.set_selection(Selection::point(pos));
495+ doc.set_selection(view_id, Selection::point(pos));
479pub fn page_up(cx: &mut Context) {
498pub fn page_up(cx: &mut Context) {
@@ -502,9 +521,10 @@ pub fn half_page_down(cx: &mut Context) {
503pub fn extend_char_left(cx: &mut Context) {
522pub fn extend_char_left(cx: &mut Context) {
504 let count = cx.count;
523 let count = cx.count;
524+ let view_id = cx.view_id;
505 let doc = cx.doc();
525 let doc = cx.doc();
506 let text = doc.text().slice(..);
526 let text = doc.text().slice(..);
507− let selection = doc.selection().transform(|range| {
527+ let selection = doc.selection(view_id).transform(|range| {
508 movement::move_horizontally(
528 movement::move_horizontally(
@@ -513,14 +533,15 @@ pub fn extend_char_left(cx: &mut Context) {
513 true, /* extend */
533 true, /* extend */
516− doc.set_selection(selection);
536+ doc.set_selection(view_id, selection);
519pub fn extend_char_right(cx: &mut Context) {
539pub fn extend_char_right(cx: &mut Context) {
520 let count = cx.count;
540 let count = cx.count;
541+ let view_id = cx.view_id;
521 let doc = cx.doc();
542 let doc = cx.doc();
522 let text = doc.text().slice(..);
543 let text = doc.text().slice(..);
523− let selection = doc.selection().transform(|range| {
544+ let selection = doc.selection(view_id).transform(|range| {
524 movement::move_horizontally(
545 movement::move_horizontally(
@@ -529,14 +550,15 @@ pub fn extend_char_right(cx: &mut Context) {
529 true, /* extend */
550 true, /* extend */
532− doc.set_selection(selection);
553+ doc.set_selection(view_id, selection);
535pub fn extend_line_up(cx: &mut Context) {
556pub fn extend_line_up(cx: &mut Context) {
536 let count = cx.count;
557 let count = cx.count;
558+ let view_id = cx.view_id;
537 let doc = cx.doc();
559 let doc = cx.doc();
538 let text = doc.text().slice(..);
560 let text = doc.text().slice(..);
539− let selection = doc.selection().transform(|range| {
561+ let selection = doc.selection(view_id).transform(|range| {
540 movement::move_vertically(
562 movement::move_vertically(
@@ -545,14 +567,15 @@ pub fn extend_line_up(cx: &mut Context) {
545 true, /* extend */
567 true, /* extend */
548− doc.set_selection(selection);
570+ doc.set_selection(view_id, selection);
551pub fn extend_line_down(cx: &mut Context) {
573pub fn extend_line_down(cx: &mut Context) {
552 let count = cx.count;
574 let count = cx.count;
575+ let view_id = cx.view_id;
553 let doc = cx.doc();
576 let doc = cx.doc();
554 let text = doc.text().slice(..);
577 let text = doc.text().slice(..);
555− let selection = doc.selection().transform(|range| {
578+ let selection = doc.selection(view_id).transform(|range| {
556 movement::move_vertically(
579 movement::move_vertically(
@@ -561,21 +584,24 @@ pub fn extend_line_down(cx: &mut Context) {
561 true, /* extend */
584 true, /* extend */
564− doc.set_selection(selection);
587+ doc.set_selection(view_id, selection);
567pub fn select_all(cx: &mut Context) {
590pub fn select_all(cx: &mut Context) {
591+ let view_id = cx.view_id;
568 let doc = cx.doc();
592 let doc = cx.doc();
570 let end = doc.text().len_chars().saturating_sub(1);
594 let end = doc.text().len_chars().saturating_sub(1);
571− doc.set_selection(Selection::single(0, end))
595+ doc.set_selection(view_id, Selection::single(0, end))
574pub fn select_regex(cx: &mut Context) {
598pub fn select_regex(cx: &mut Context) {
599+ let view_id = cx.view_id;
575− let prompt = ui::regex_prompt(cx, "select:".to_string(), |doc, regex| {
600+ let prompt = ui::regex_prompt(cx, "select:".to_string(), move |doc, regex| {
576 let text = doc.text().slice(..);
601 let text = doc.text().slice(..);
577− if let Some(selection) = selection::select_on_matches(text, doc.selection(), ®ex) {
602+ if let Some(selection) = selection::select_on_matches(text, doc.selection(view_id), ®ex)
578− doc.set_selection(selection);
604+ doc.set_selection(view_id, selection);
@@ -583,23 +609,25 @@ pub fn select_regex(cx: &mut Context) {
585pub fn split_selection(cx: &mut Context) {
611pub fn split_selection(cx: &mut Context) {
612+ let view_id = cx.view_id;
586− let prompt = ui::regex_prompt(cx, "split:".to_string(), |doc, regex| {
613+ let prompt = ui::regex_prompt(cx, "split:".to_string(), move |doc, regex| {
587 let text = doc.text().slice(..);
614 let text = doc.text().slice(..);
588− let selection = selection::split_on_matches(text, doc.selection(), ®ex);
615+ let selection = selection::split_on_matches(text, doc.selection(view_id), ®ex);
589− doc.set_selection(selection);
616+ doc.set_selection(view_id, selection);
592 cx.push_layer(Box::new(prompt));
619 cx.push_layer(Box::new(prompt));
595pub fn split_selection_on_newline(cx: &mut Context) {
622pub fn split_selection_on_newline(cx: &mut Context) {
623+ let view_id = cx.view_id;
596 let doc = cx.doc();
624 let doc = cx.doc();
597 let text = doc.text().slice(..);
625 let text = doc.text().slice(..);
598 // only compile the regex once
626 // only compile the regex once
599 #[allow(clippy::trivial_regex)]
627 #[allow(clippy::trivial_regex)]
600 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
628 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n").unwrap());
601− let selection = selection::split_on_matches(text, doc.selection(), ®EX);
629+ let selection = selection::split_on_matches(text, doc.selection(view_id), ®EX);
602− doc.set_selection(selection);
630+ doc.set_selection(view_id, selection);
605// search: searches for the first occurence in file, provides a prompt
633// search: searches for the first occurence in file, provides a prompt
@@ -609,9 +637,9 @@ pub fn split_selection_on_newline(cx: &mut Context) {
609// I'd probably collect all the matches right now and store the current index. The cache needs
637// I'd probably collect all the matches right now and store the current index. The cache needs
610// wiping if input happens.
638// wiping if input happens.
612−fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
640+fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex) {
613 let text = doc.text();
641 let text = doc.text();
614− let start = doc.selection().cursor();
642+ let start = doc.selection(view_id).cursor();
616 // TODO: use find_at to find the next match after the cursor, loop around the end
644 // TODO: use find_at to find the next match after the cursor, loop around the end
617 if let Some(mat) = regex.find_at(&contents, start) {
645 if let Some(mat) = regex.find_at(&contents, start) {
@@ -619,7 +647,7 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
619 let end = text.byte_to_char(mat.end());
647 let end = text.byte_to_char(mat.end());
620 let selection = Selection::single(start, end - 1);
648 let selection = Selection::single(start, end - 1);
621 // TODO: (first_match, regex) stuff in register?
649 // TODO: (first_match, regex) stuff in register?
622− doc.set_selection(selection);
650+ doc.set_selection(view_id, selection);
@@ -633,10 +661,11 @@ pub fn search(cx: &mut Context) {
633 // feed chunks into the regex yet
661 // feed chunks into the regex yet
634 let contents = doc.text().slice(..).to_string();
662 let contents = doc.text().slice(..).to_string();
664+ let view_id = cx.view_id;
636 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
665 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
637 let text = doc.text();
666 let text = doc.text();
638− let start = doc.selection().cursor();
667+ let start = doc.selection(view_id).cursor();
639− _search(doc, &contents, ®ex);
668+ _search(doc, view_id, &contents, ®ex);
641 // TODO: only store on enter (accept), not update
670 // TODO: only store on enter (accept), not update
642 register::set('\\', vec![regex.as_str().to_string()]);
671 register::set('\\', vec![regex.as_str().to_string()]);
@@ -648,17 +677,19 @@ pub fn search(cx: &mut Context) {
648pub fn search_next(cx: &mut Context) {
677pub fn search_next(cx: &mut Context) {
649 if let Some(query) = register::get('\\') {
678 if let Some(query) = register::get('\\') {
650 let query = query.first().unwrap();
679 let query = query.first().unwrap();
680+ let view_id = cx.view_id;
651 let doc = cx.doc();
681 let doc = cx.doc();
652 let contents = doc.text().slice(..).to_string();
682 let contents = doc.text().slice(..).to_string();
653 let regex = Regex::new(&query).unwrap();
683 let regex = Regex::new(&query).unwrap();
654− _search(doc, &contents, ®ex);
684+ _search(doc, view_id, &contents, ®ex);
658pub fn search_selection(cx: &mut Context) {
688pub fn search_selection(cx: &mut Context) {
689+ let view_id = cx.view_id;
659 let doc = cx.doc();
690 let doc = cx.doc();
660 let contents = doc.text().slice(..);
691 let contents = doc.text().slice(..);
661− let query = doc.selection().primary().fragment(contents);
692+ let query = doc.selection(view_id).primary().fragment(contents);
662 let regex = regex::escape(&query);
693 let regex = regex::escape(&query);
663 register::set('\\', vec![regex]);
694 register::set('\\', vec![regex]);
664 search_next(cx);
695 search_next(cx);
@@ -672,22 +703,24 @@ pub fn search_selection(cx: &mut Context) {
673pub fn select_line(cx: &mut Context) {
704pub fn select_line(cx: &mut Context) {
674 let count = cx.count;
705 let count = cx.count;
706+ let view_id = cx.view_id;
675 let doc = cx.doc();
707 let doc = cx.doc();
677− let pos = doc.selection().primary();
709+ let pos = doc.selection(view_id).primary();
678 let text = doc.text();
710 let text = doc.text();
680 let line = text.char_to_line(pos.head);
712 let line = text.char_to_line(pos.head);
681 let start = text.line_to_char(line);
713 let start = text.line_to_char(line);
682 let end = text.line_to_char(line + count).saturating_sub(1);
714 let end = text.line_to_char(line + count).saturating_sub(1);
684− doc.set_selection(Selection::single(start, end));
716+ doc.set_selection(view_id, Selection::single(start, end));
686pub fn extend_line(cx: &mut Context) {
718pub fn extend_line(cx: &mut Context) {
719+ let view_id = cx.view_id;
687 let count = cx.count;
720 let count = cx.count;
688 let doc = cx.doc();
721 let doc = cx.doc();
690− let pos = doc.selection().primary();
723+ let pos = doc.selection(view_id).primary();
691 let text = doc.text();
724 let text = doc.text();
693 let line_start = text.char_to_line(pos.anchor);
726 let line_start = text.char_to_line(pos.anchor);
@@ -700,47 +733,52 @@ pub fn extend_line(cx: &mut Context) {
700 let start = text.line_to_char(line_start);
733 let start = text.line_to_char(line_start);
701 let end = text.line_to_char(line + 1).saturating_sub(1);
734 let end = text.line_to_char(line + 1).saturating_sub(1);
703− doc.set_selection(Selection::single(start, end));
736+ doc.set_selection(view_id, Selection::single(start, end));
706// heuristic: append changes to history after each command, unless we're in insert mode
739// heuristic: append changes to history after each command, unless we're in insert mode
708−fn _delete_selection(doc: &mut Document) {
741+fn _delete_selection(doc: &mut Document, view_id: ViewId) {
709− let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
743+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
710− (range.from(), range.to() + 1, None)
744+ (range.from(), range.to() + 1, None)
712− doc.apply(&transaction);
746+ doc.apply(&transaction, view_id);
715pub fn delete_selection(cx: &mut Context) {
749pub fn delete_selection(cx: &mut Context) {
750+ let view_id = cx.view_id;
716 let doc = cx.doc();
751 let doc = cx.doc();
717− _delete_selection(doc);
752+ _delete_selection(doc, view_id);
719− doc.append_changes_to_history();
754+ doc.append_changes_to_history(view_id);
722pub fn change_selection(cx: &mut Context) {
757pub fn change_selection(cx: &mut Context) {
758+ let view_id = cx.view_id;
723 let doc = cx.doc();
759 let doc = cx.doc();
724− _delete_selection(doc);
760+ _delete_selection(doc, view_id);
725 enter_insert_mode(doc);
761 enter_insert_mode(doc);
728pub fn collapse_selection(cx: &mut Context) {
764pub fn collapse_selection(cx: &mut Context) {
765+ let view_id = cx.view_id;
729 let doc = cx.doc();
766 let doc = cx.doc();
730 let selection = doc
767 let selection = doc
731− .selection()
768+ .selection(view_id)
732 .transform(|range| Range::new(range.head, range.head));
769 .transform(|range| Range::new(range.head, range.head));
734− doc.set_selection(selection);
771+ doc.set_selection(view_id, selection);
737pub fn flip_selections(cx: &mut Context) {
774pub fn flip_selections(cx: &mut Context) {
775+ let view_id = cx.view_id;
738 let doc = cx.doc();
776 let doc = cx.doc();
739 let selection = doc
777 let selection = doc
740− .selection()
778+ .selection(view_id)
741 .transform(|range| Range::new(range.head, range.anchor));
779 .transform(|range| Range::new(range.head, range.anchor));
743− doc.set_selection(selection);
781+ doc.set_selection(view_id, selection);
746fn enter_insert_mode(doc: &mut Document) {
784fn enter_insert_mode(doc: &mut Document) {
@@ -749,29 +787,31 @@ fn enter_insert_mode(doc: &mut Document) {
750// inserts at the start of each selection
788// inserts at the start of each selection
751pub fn insert_mode(cx: &mut Context) {
789pub fn insert_mode(cx: &mut Context) {
790+ let view_id = cx.view_id;
752 let doc = cx.doc();
791 let doc = cx.doc();
753 enter_insert_mode(doc);
792 enter_insert_mode(doc);
755 let selection = doc
794 let selection = doc
756− .selection()
795+ .selection(view_id)
757 .transform(|range| Range::new(range.to(), range.from()));
796 .transform(|range| Range::new(range.to(), range.from()));
758− doc.set_selection(selection);
797+ doc.set_selection(view_id, selection);
761// inserts at the end of each selection
800// inserts at the end of each selection
762pub fn append_mode(cx: &mut Context) {
801pub fn append_mode(cx: &mut Context) {
802+ let view_id = cx.view_id;
763 let doc = cx.doc();
803 let doc = cx.doc();
764 enter_insert_mode(doc);
804 enter_insert_mode(doc);
765 doc.restore_cursor = true;
805 doc.restore_cursor = true;
767 let text = doc.text().slice(..);
807 let text = doc.text().slice(..);
768− let selection = doc.selection().transform(|range| {
808+ let selection = doc.selection(view_id).transform(|range| {
769 Range::new(
809 Range::new(
770 range.from(),
810 range.from(),
771 graphemes::next_grapheme_boundary(text, range.to()), // to() + next char
811 graphemes::next_grapheme_boundary(text, range.to()), // to() + next char
774− doc.set_selection(selection);
814+ doc.set_selection(view_id, selection);
777const COMMAND_LIST: &[&str] = &["write", "open", "quit"];
817const COMMAND_LIST: &[&str] = &["write", "open", "quit"];
@@ -931,25 +971,27 @@ pub fn prepend_to_line(cx: &mut Context) {
931pub fn append_to_line(cx: &mut Context) {
971pub fn append_to_line(cx: &mut Context) {
932 move_line_end(cx);
972 move_line_end(cx);
974+ let view_id = cx.view_id;
934 let doc = cx.doc();
975 let doc = cx.doc();
935 enter_insert_mode(doc);
976 enter_insert_mode(doc);
937 // offset by another 1 char since move_line_end will position on the last char, we want to
978 // offset by another 1 char since move_line_end will position on the last char, we want to
938 // append past that
979 // append past that
939− let selection = doc.selection().transform(|range| {
980+ let selection = doc.selection(view_id).transform(|range| {
940 let pos = range.head + 1;
981 let pos = range.head + 1;
941 Range::new(pos, pos)
982 Range::new(pos, pos)
943− doc.set_selection(selection);
984+ doc.set_selection(view_id, selection);
946// o inserts a new line after each line with a selection
987// o inserts a new line after each line with a selection
947pub fn open_below(cx: &mut Context) {
988pub fn open_below(cx: &mut Context) {
989+ let view_id = cx.view_id;
948 let count = cx.count;
990 let count = cx.count;
949 let doc = cx.doc();
991 let doc = cx.doc();
950 enter_insert_mode(doc);
992 enter_insert_mode(doc);
952− let lines = selection_lines(doc.text(), doc.selection());
994+ let lines = selection_lines(doc.text(), doc.selection(view_id));
954 let positions = lines.into_iter().map(|index| {
996 let positions = lines.into_iter().map(|index| {
955 // adjust all positions to the end of the line (next line minus one)
997 // adjust all positions to the end of the line (next line minus one)
@@ -991,16 +1033,17 @@ pub fn open_below(cx: &mut Context) {
991 let transaction =
1033 let transaction =
992 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1034 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
994− doc.apply(&transaction);
1036+ doc.apply(&transaction, view_id);
997// O inserts a new line before each line with a selection
1039// O inserts a new line before each line with a selection
998pub fn open_above(cx: &mut Context) {
1040pub fn open_above(cx: &mut Context) {
1041+ let view_id = cx.view_id;
999 let count = cx.count;
1042 let count = cx.count;
1000 let doc = cx.doc();
1043 let doc = cx.doc();
1001 enter_insert_mode(doc);
1044 enter_insert_mode(doc);
1003− let lines = selection_lines(doc.text(), doc.selection());
1046+ let lines = selection_lines(doc.text(), doc.selection(view_id));
1005 let positions = lines.into_iter().map(|index| {
1048 let positions = lines.into_iter().map(|index| {
1006 // adjust all positions to the end of the previous line
1049 // adjust all positions to the end of the previous line
@@ -1043,26 +1086,27 @@ pub fn open_above(cx: &mut Context) {
1043 let transaction =
1086 let transaction =
1044 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1087 Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1046− doc.apply(&transaction);
1089+ doc.apply(&transaction, view_id);
1049pub fn normal_mode(cx: &mut Context) {
1092pub fn normal_mode(cx: &mut Context) {
1093+ let view_id = cx.view_id;
1050 let doc = cx.doc();
1094 let doc = cx.doc();
1052 doc.mode = Mode::Normal;
1096 doc.mode = Mode::Normal;
1054− doc.append_changes_to_history();
1098+ doc.append_changes_to_history(view_id);
1056 // if leaving append mode, move cursor back by 1
1100 // if leaving append mode, move cursor back by 1
1057 if doc.restore_cursor {
1101 if doc.restore_cursor {
1058 let text = doc.text().slice(..);
1102 let text = doc.text().slice(..);
1059− let selection = doc.selection().transform(|range| {
1103+ let selection = doc.selection(view_id).transform(|range| {
1060 Range::new(
1104 Range::new(
1061 range.from(),
1105 range.from(),
1062 graphemes::prev_grapheme_boundary(text, range.to()),
1106 graphemes::prev_grapheme_boundary(text, range.to()),
1065− doc.set_selection(selection);
1109+ doc.set_selection(view_id, selection);
1067 doc.restore_cursor = false;
1111 doc.restore_cursor = false;
@@ -1071,13 +1115,15 @@ pub fn normal_mode(cx: &mut Context) {
1071// Store a jump on the jumplist.
1115// Store a jump on the jumplist.
1072fn push_jump(cx: &mut Context) {
1116fn push_jump(cx: &mut Context) {
1073 let jump = {
1117 let jump = {
1118+ let view_id = cx.view_id;
1074 let doc = cx.doc();
1119 let doc = cx.doc();
1075− (doc.id(), doc.selection().clone())
1120+ (doc.id(), doc.selection(view_id).clone())
1077 cx.view().jumps.push(jump);
1122 cx.view().jumps.push(jump);
1080pub fn goto_mode(cx: &mut Context) {
1125pub fn goto_mode(cx: &mut Context) {
1126+ let view_id = cx.view_id;
1081 let count = cx.count;
1127 let count = cx.count;
1083 if count > 1 {
1129 if count > 1 {
@@ -1087,7 +1133,7 @@ pub fn goto_mode(cx: &mut Context) {
1087 // to 1g
1133 // to 1g
1088 let doc = cx.doc();
1134 let doc = cx.doc();
1089 let pos = doc.text().line_to_char(count - 1);
1135 let pos = doc.text().line_to_char(count - 1);
1090− doc.set_selection(Selection::point(pos));
1136+ doc.set_selection(view_id, Selection::point(pos));
@@ -1128,10 +1174,11 @@ fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
1128 let id = editor
1174 let id = editor
1129 .open(PathBuf::from(location.uri.path()), action)
1175 .open(PathBuf::from(location.uri.path()), action)
1130 .expect("editor.open failed");
1176 .expect("editor.open failed");
1177+ let view_id = editor.view().id;
1131 let doc = &mut editor.documents[id];
1178 let doc = &mut editor.documents[id];
1132 let definition_pos = location.range.start;
1179 let definition_pos = location.range.start;
1133 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1180 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1134− doc.set_selection(Selection::point(new_pos));
1181+ doc.set_selection(view_id, Selection::point(new_pos));
1137 match locations.as_slice() {
1184 match locations.as_slice() {
@@ -1155,6 +1202,7 @@ fn _goto(cx: &mut Context, locations: Vec<lsp::Location>) {
1157pub fn goto_definition(cx: &mut Context) {
1204pub fn goto_definition(cx: &mut Context) {
1205+ let view_id = cx.view_id;
1158 let doc = cx.doc();
1206 let doc = cx.doc();
1159 let language_server = match doc.language_server() {
1207 let language_server = match doc.language_server() {
1160 Some(language_server) => language_server,
1208 Some(language_server) => language_server,
@@ -1162,7 +1210,7 @@ pub fn goto_definition(cx: &mut Context) {
1164 // TODO: blocking here is not ideal
1212 // TODO: blocking here is not ideal
1165− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1213+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1167 // TODO: handle fails
1215 // TODO: handle fails
1168 let res =
1216 let res =
@@ -1171,6 +1219,7 @@ pub fn goto_definition(cx: &mut Context) {
1173pub fn goto_type_definition(cx: &mut Context) {
1221pub fn goto_type_definition(cx: &mut Context) {
1222+ let view_id = cx.view_id;
1174 let doc = cx.doc();
1223 let doc = cx.doc();
1175 let language_server = match doc.language_server() {
1224 let language_server = match doc.language_server() {
1176 Some(language_server) => language_server,
1225 Some(language_server) => language_server,
@@ -1178,7 +1227,7 @@ pub fn goto_type_definition(cx: &mut Context) {
1180 // TODO: blocking here is not ideal
1229 // TODO: blocking here is not ideal
1181− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1230+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1183 // TODO: handle fails
1232 // TODO: handle fails
1184 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
1233 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
@@ -1187,6 +1236,7 @@ pub fn goto_type_definition(cx: &mut Context) {
1189pub fn goto_implementation(cx: &mut Context) {
1238pub fn goto_implementation(cx: &mut Context) {
1239+ let view_id = cx.view_id;
1190 let doc = cx.doc();
1240 let doc = cx.doc();
1191 let language_server = match doc.language_server() {
1241 let language_server = match doc.language_server() {
1192 Some(language_server) => language_server,
1242 Some(language_server) => language_server,
@@ -1194,7 +1244,7 @@ pub fn goto_implementation(cx: &mut Context) {
1196 // TODO: blocking here is not ideal
1246 // TODO: blocking here is not ideal
1197− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1247+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1199 // TODO: handle fails
1249 // TODO: handle fails
1200 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
1250 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
@@ -1203,6 +1253,7 @@ pub fn goto_implementation(cx: &mut Context) {
1205pub fn goto_reference(cx: &mut Context) {
1255pub fn goto_reference(cx: &mut Context) {
1256+ let view_id = cx.view_id;
1206 let doc = cx.doc();
1257 let doc = cx.doc();
1207 let language_server = match doc.language_server() {
1258 let language_server = match doc.language_server() {
1208 Some(language_server) => language_server,
1259 Some(language_server) => language_server,
@@ -1210,7 +1261,7 @@ pub fn goto_reference(cx: &mut Context) {
1212 // TODO: blocking here is not ideal
1263 // TODO: blocking here is not ideal
1213− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1264+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1215 // TODO: handle fails
1266 // TODO: handle fails
1216 let res =
1267 let res =
@@ -1219,6 +1270,7 @@ pub fn goto_reference(cx: &mut Context) {
1221pub fn signature_help(cx: &mut Context) {
1272pub fn signature_help(cx: &mut Context) {
1273+ let view_id = cx.view_id;
1222 let doc = cx.doc();
1274 let doc = cx.doc();
1224 let language_server = match doc.language_server() {
1276 let language_server = match doc.language_server() {
@@ -1227,7 +1279,7 @@ pub fn signature_help(cx: &mut Context) {
1229 // TODO: blocking here is not ideal
1281 // TODO: blocking here is not ideal
1230− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1282+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1232 // TODO: handle fails
1284 // TODO: handle fails
@@ -1320,20 +1372,21 @@ pub mod insert {
1321 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1373 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1322 pub fn insert_char(cx: &mut Context, c: char) {
1374 pub fn insert_char(cx: &mut Context, c: char) {
1375+ let view_id = cx.view_id;
1323 let doc = cx.doc();
1376 let doc = cx.doc();
1325 // run through insert hooks, stopping on the first one that returns Some(t)
1378 // run through insert hooks, stopping on the first one that returns Some(t)
1326 for hook in HOOKS {
1379 for hook in HOOKS {
1327− if let Some(transaction) = hook(doc.text(), doc.selection(), c) {
1380+ if let Some(transaction) = hook(doc.text(), doc.selection(view_id), c) {
1328− doc.apply(&transaction);
1381+ doc.apply(&transaction, view_id);
1333 let t = Tendril::from_char(c);
1386 let t = Tendril::from_char(c);
1334− let transaction = Transaction::insert(doc.text(), doc.selection(), t);
1387+ let transaction = Transaction::insert(doc.text(), doc.selection(view_id), t);
1336− doc.apply(&transaction);
1389+ doc.apply(&transaction, view_id);
1338 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
1391 // TODO: need a post insert hook too for certain triggers (autocomplete, signature help, etc)
1339 // this could also generically look at Transaction, but it's a bit annoying to look at
1392 // this could also generically look at Transaction, but it's a bit annoying to look at
@@ -1344,58 +1397,69 @@ pub mod insert {
1346 pub fn insert_tab(cx: &mut Context) {
1399 pub fn insert_tab(cx: &mut Context) {
1400+ let view_id = cx.view_id;
1347 let doc = cx.doc();
1401 let doc = cx.doc();
1348 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1402 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1349 // indent by one to reach 4 spaces).
1403 // indent by one to reach 4 spaces).
1351 let indent = Tendril::from(doc.indent_unit());
1405 let indent = Tendril::from(doc.indent_unit());
1352− let transaction = Transaction::insert(doc.text(), doc.selection(), indent);
1406+ let transaction = Transaction::insert(doc.text(), doc.selection(view_id), indent);
1353− doc.apply(&transaction);
1407+ doc.apply(&transaction, view_id);
1356 pub fn insert_newline(cx: &mut Context) {
1410 pub fn insert_newline(cx: &mut Context) {
1411+ let view_id = cx.view_id;
1357 let doc = cx.doc();
1412 let doc = cx.doc();
1358 let text = doc.text().slice(..);
1413 let text = doc.text().slice(..);
1359− let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1415+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1360− // TODO: offset range.head by 1? when calculating?
1416+ // TODO: offset range.head by 1? when calculating?
1362− helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
1417+ let indent_level = helix_core::indent::suggested_indent_for_pos(
1363− let indent = doc.indent_unit().repeat(indent_level);
1423+ let indent = doc.indent_unit().repeat(indent_level);
1364− let mut text = String::with_capacity(1 + indent.len());
1424+ let mut text = String::with_capacity(1 + indent.len());
1365− text.push('\n');
1425+ text.push('\n');
1366− text.push_str(&indent);
1426+ text.push_str(&indent);
1367− (range.head, range.head, Some(text.into()))
1427+ (range.head, range.head, Some(text.into()))
1369− doc.apply(&transaction);
1429+ doc.apply(&transaction, view_id);
1372 // TODO: handle indent-aware delete
1432 // TODO: handle indent-aware delete
1373 pub fn delete_char_backward(cx: &mut Context) {
1433 pub fn delete_char_backward(cx: &mut Context) {
1434+ let view_id = cx.view_id;
1374 let count = cx.count;
1435 let count = cx.count;
1375 let doc = cx.doc();
1436 let doc = cx.doc();
1376 let text = doc.text().slice(..);
1437 let text = doc.text().slice(..);
1377− let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1439+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1379− graphemes::nth_prev_grapheme_boundary(text, range.head, count),
1441+ graphemes::nth_prev_grapheme_boundary(text, range.head, count),
1380− range.head,
1442+ range.head,
1384− doc.apply(&transaction);
1446+ doc.apply(&transaction, view_id);
1387 pub fn delete_char_forward(cx: &mut Context) {
1449 pub fn delete_char_forward(cx: &mut Context) {
1450+ let view_id = cx.view_id;
1388 let count = cx.count;
1451 let count = cx.count;
1389 let doc = cx.doc();
1452 let doc = cx.doc();
1390 let text = doc.text().slice(..);
1453 let text = doc.text().slice(..);
1391− let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1455+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1393− range.head,
1457+ range.head,
1394− graphemes::nth_next_grapheme_boundary(text, range.head, count),
1458+ graphemes::nth_next_grapheme_boundary(text, range.head, count),
1398− doc.apply(&transaction);
1462+ doc.apply(&transaction, view_id);
@@ -1405,20 +1469,23 @@ pub mod insert {
1405// storing it?
1469// storing it?
1407pub fn undo(cx: &mut Context) {
1471pub fn undo(cx: &mut Context) {
1472+ let view_id = cx.view_id;
1408− cx.doc().undo();
1473+ cx.doc().undo(view_id);
1411pub fn redo(cx: &mut Context) {
1476pub fn redo(cx: &mut Context) {
1477+ let view_id = cx.view_id;
1412− cx.doc().redo();
1478+ cx.doc().redo(view_id);
1415// Yank / Paste
1481// Yank / Paste
1417pub fn yank(cx: &mut Context) {
1483pub fn yank(cx: &mut Context) {
1418 // TODO: should selections be made end inclusive?
1484 // TODO: should selections be made end inclusive?
1485+ let view_id = cx.view_id;
1419 let doc = cx.doc();
1486 let doc = cx.doc();
1420 let values: Vec<String> = doc
1487 let values: Vec<String> = doc
1421− .selection()
1488+ .selection(view_id)
1422 .fragments(doc.text().slice(..))
1489 .fragments(doc.text().slice(..))
1423 .map(|cow| cow.into_owned())
1490 .map(|cow| cow.into_owned())
1424 .collect();
1491 .collect();
@@ -1461,32 +1528,33 @@ pub fn paste(cx: &mut Context) {
1462 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1529 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1531+ let view_id = cx.view_id;
1464 let doc = cx.doc();
1532 let doc = cx.doc();
1466 let transaction = if linewise {
1534 let transaction = if linewise {
1467 // paste on the next line
1535 // paste on the next line
1468 // TODO: can simply take a range + modifier and compute the right pos without ifs
1536 // TODO: can simply take a range + modifier and compute the right pos without ifs
1469 let text = doc.text();
1537 let text = doc.text();
1470− Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1538+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1471 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
1539 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
1472 (line_end, line_end, Some(values.next().unwrap()))
1540 (line_end, line_end, Some(values.next().unwrap()))
1474 } else {
1542 } else {
1475− Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1543+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
1476 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
1544 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
1480− doc.apply(&transaction);
1548+ doc.apply(&transaction, view_id);
1481− doc.append_changes_to_history();
1549+ doc.append_changes_to_history(view_id);
1485−fn get_lines(doc: &Document) -> Vec<usize> {
1553+fn get_lines(doc: &Document, view_id: ViewId) -> Vec<usize> {
1486 let mut lines = Vec::new();
1554 let mut lines = Vec::new();
1488 // Get all line numbers
1556 // Get all line numbers
1489− for range in doc.selection() {
1557+ for range in doc.selection(view_id) {
1490 let start = doc.text().char_to_line(range.from());
1558 let start = doc.text().char_to_line(range.from());
1491 let end = doc.text().char_to_line(range.to());
1559 let end = doc.text().char_to_line(range.to());
@@ -1500,8 +1568,9 @@ fn get_lines(doc: &Document) -> Vec<usize> {
1502pub fn indent(cx: &mut Context) {
1570pub fn indent(cx: &mut Context) {
1571+ let view_id = cx.view_id;
1503 let doc = cx.doc();
1572 let doc = cx.doc();
1504− let lines = get_lines(doc);
1573+ let lines = get_lines(doc, view_id);
1506 // Indent by one level
1575 // Indent by one level
1507 let indent = Tendril::from(doc.indent_unit());
1576 let indent = Tendril::from(doc.indent_unit());
@@ -1513,13 +1582,14 @@ pub fn indent(cx: &mut Context) {
1513 (pos, pos, Some(indent.clone()))
1582 (pos, pos, Some(indent.clone()))
1516− doc.apply(&transaction);
1585+ doc.apply(&transaction, view_id);
1517− doc.append_changes_to_history();
1586+ doc.append_changes_to_history(view_id);
1520pub fn unindent(cx: &mut Context) {
1589pub fn unindent(cx: &mut Context) {
1590+ let view_id = cx.view_id;
1521 let doc = cx.doc();
1591 let doc = cx.doc();
1522− let lines = get_lines(doc);
1592+ let lines = get_lines(doc, view_id);
1523 let mut changes = Vec::with_capacity(lines.len());
1593 let mut changes = Vec::with_capacity(lines.len());
1524 let tab_width = doc.tab_width();
1594 let tab_width = doc.tab_width();
@@ -1547,12 +1617,13 @@ pub fn unindent(cx: &mut Context) {
1548 let transaction = Transaction::change(doc.text(), changes.into_iter());
1618 let transaction = Transaction::change(doc.text(), changes.into_iter());
1550− doc.apply(&transaction);
1620+ doc.apply(&transaction, view_id);
1551− doc.append_changes_to_history();
1621+ doc.append_changes_to_history(view_id);
1554pub fn format_selections(cx: &mut Context) {
1624pub fn format_selections(cx: &mut Context) {
1555 use helix_lsp::lsp;
1625 use helix_lsp::lsp;
1626+ let view_id = cx.view_id;
1556 let doc = cx.doc();
1627 let doc = cx.doc();
1558 // via lsp if available
1629 // via lsp if available
@@ -1561,7 +1632,7 @@ pub fn format_selections(cx: &mut Context) {
1561 // TODO: blocking here is not ideal
1632 // TODO: blocking here is not ideal
1563 let ranges: Vec<lsp::Range> = doc
1634 let ranges: Vec<lsp::Range> = doc
1564− .selection()
1635+ .selection(view_id)
1566 .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
1637 .map(|range| helix_lsp::util::range_to_lsp_range(doc.text(), *range))
1567 .collect();
1638 .collect();
@@ -1583,14 +1654,15 @@ pub fn format_selections(cx: &mut Context) {
1584 let transaction = helix_lsp::util::generate_transaction_from_edits(doc.text(), edits);
1655 let transaction = helix_lsp::util::generate_transaction_from_edits(doc.text(), edits);
1586− doc.apply(&transaction);
1657+ doc.apply(&transaction, view_id);
1589− doc.append_changes_to_history();
1660+ doc.append_changes_to_history(view_id);
1592pub fn join_selections(cx: &mut Context) {
1663pub fn join_selections(cx: &mut Context) {
1593 use movement::skip_over_next;
1664 use movement::skip_over_next;
1665+ let view_id = cx.view_id;
1594 let doc = cx.doc();
1666 let doc = cx.doc();
1595 let text = doc.text();
1667 let text = doc.text();
1596 let slice = doc.text().slice(..);
1668 let slice = doc.text().slice(..);
@@ -1598,7 +1670,7 @@ pub fn join_selections(cx: &mut Context) {
1598 let mut changes = Vec::new();
1670 let mut changes = Vec::new();
1599 let fragment = Tendril::from(" ");
1671 let fragment = Tendril::from(" ");
1601− for selection in doc.selection() {
1673+ for selection in doc.selection(view_id) {
1602 let start = text.char_to_line(selection.from());
1674 let start = text.char_to_line(selection.from());
1603 let mut end = text.char_to_line(selection.to());
1675 let mut end = text.char_to_line(selection.to());
1604 if start == end {
1676 if start == end {
@@ -1629,17 +1701,18 @@ pub fn join_selections(cx: &mut Context) {
1629 // TODO: select inserted spaces
1701 // TODO: select inserted spaces
1630 // .with_selection(selection);
1702 // .with_selection(selection);
1632− doc.apply(&transaction);
1704+ doc.apply(&transaction, view_id);
1633− doc.append_changes_to_history();
1705+ doc.append_changes_to_history(view_id);
1636pub fn keep_selections(cx: &mut Context) {
1708pub fn keep_selections(cx: &mut Context) {
1637 // keep selections matching regex
1709 // keep selections matching regex
1710+ let view_id = cx.view_id;
1638− let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
1711+ let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |doc, regex| {
1639 let text = doc.text().slice(..);
1712 let text = doc.text().slice(..);
1641− if let Some(selection) = selection::keep_matches(text, doc.selection(), ®ex) {
1714+ if let Some(selection) = selection::keep_matches(text, doc.selection(view_id), ®ex) {
1642− doc.set_selection(selection);
1715+ doc.set_selection(view_id, selection);
@@ -1647,11 +1720,12 @@ pub fn keep_selections(cx: &mut Context) {
1649pub fn keep_primary_selection(cx: &mut Context) {
1722pub fn keep_primary_selection(cx: &mut Context) {
1723+ let view_id = cx.view_id;
1650 let doc = cx.doc();
1724 let doc = cx.doc();
1652− let range = doc.selection().primary();
1726+ let range = doc.selection(view_id).primary();
1653 let selection = Selection::single(range.anchor, range.head);
1727 let selection = Selection::single(range.anchor, range.head);
1654− doc.set_selection(selection);
1728+ doc.set_selection(view_id, selection);
@@ -1700,6 +1774,7 @@ pub fn completion(cx: &mut Context) {
1700 // The prefix still has to satisfy `company-minimum-prefix-length' before that
1774 // The prefix still has to satisfy `company-minimum-prefix-length' before that
1701 // happens. The value of nil means no idle completion."
1775 // happens. The value of nil means no idle completion."
1777+ let view_id = cx.view_id;
1703 let doc = cx.doc();
1778 let doc = cx.doc();
1705 let language_server = match doc.language_server() {
1780 let language_server = match doc.language_server() {
@@ -1708,13 +1783,13 @@ pub fn completion(cx: &mut Context) {
1710 // TODO: blocking here is not ideal
1785 // TODO: blocking here is not ideal
1711− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1786+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1713 // TODO: handle fails
1788 // TODO: handle fails
1715 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1790 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap();
1717− let trigger_offset = doc.selection().cursor();
1792+ let trigger_offset = doc.selection(view_id).cursor();
1719 cx.callback(
1794 cx.callback(
@@ -1751,6 +1826,7 @@ pub fn completion(cx: &mut Context) {
1751pub fn hover(cx: &mut Context) {
1826pub fn hover(cx: &mut Context) {
1752 use helix_lsp::lsp;
1827 use helix_lsp::lsp;
1829+ let view_id = cx.view_id;
1754 let doc = cx.doc();
1830 let doc = cx.doc();
1756 let language_server = match doc.language_server() {
1832 let language_server = match doc.language_server() {
@@ -1762,7 +1838,7 @@ pub fn hover(cx: &mut Context) {
1763 // TODO: blocking here is not ideal, make commands async fn?
1839 // TODO: blocking here is not ideal, make commands async fn?
1764 // not like we can process additional input meanwhile though
1840 // not like we can process additional input meanwhile though
1765− let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection().cursor());
1841+ let pos = helix_lsp::util::pos_to_lsp_pos(doc.text(), doc.selection(view_id).cursor());
1767 // TODO: handle fails
1843 // TODO: handle fails
1768 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1844 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
@@ -1798,33 +1874,36 @@ pub fn next_view(cx: &mut Context) {
1799// comments
1875// comments
1800pub fn toggle_comments(cx: &mut Context) {
1876pub fn toggle_comments(cx: &mut Context) {
1877+ let view_id = cx.view_id;
1801 let doc = cx.doc();
1878 let doc = cx.doc();
1802− let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1879+ let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view_id));
1804− doc.apply(&transaction);
1881+ doc.apply(&transaction, view_id);
1805− doc.append_changes_to_history();
1882+ doc.append_changes_to_history(view_id);
1808// tree sitter node selection
1885// tree sitter node selection
1810pub fn expand_selection(cx: &mut Context) {
1887pub fn expand_selection(cx: &mut Context) {
1888+ let view_id = cx.view_id;
1811 let doc = cx.doc();
1889 let doc = cx.doc();
1813 if let Some(syntax) = doc.syntax() {
1891 if let Some(syntax) = doc.syntax() {
1814 let text = doc.text().slice(..);
1892 let text = doc.text().slice(..);
1815− let selection = object::expand_selection(syntax, text, doc.selection());
1893+ let selection = object::expand_selection(syntax, text, doc.selection(view_id));
1816− doc.set_selection(selection);
1894+ doc.set_selection(view_id, selection);
1820pub fn match_brackets(cx: &mut Context) {
1898pub fn match_brackets(cx: &mut Context) {
1899+ let view_id = cx.view_id;
1821 let doc = cx.doc();
1900 let doc = cx.doc();
1823 if let Some(syntax) = doc.syntax() {
1902 if let Some(syntax) = doc.syntax() {
1824− let pos = doc.selection().cursor();
1903+ let pos = doc.selection(view_id).cursor();
1825 if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
1904 if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
1826 let selection = Selection::point(pos);
1905 let selection = Selection::point(pos);
1827− doc.set_selection(selection);
1906+ doc.set_selection(view_id, selection);
@@ -1836,6 +1915,7 @@ pub fn jump_forward(cx: &mut Context) {
1836 let view = cx.view();
1915 let view = cx.view();
1838 if let Some((id, selection)) = view.jumps.forward(count) {
1917 if let Some((id, selection)) = view.jumps.forward(count) {
1918+ // TODO: position first_line so that main cursor is centered
1839 view.first_line = 0;
1919 view.first_line = 0;
1840 view.doc = *id;
1920 view.doc = *id;
@@ -1846,11 +1926,13 @@ pub fn jump_backward(cx: &mut Context) {
1846 let view = cx.view();
1926 let view = cx.view();
1848 if let Some((id, selection)) = view.jumps.backward(count) {
1928 if let Some((id, selection)) = view.jumps.backward(count) {
1929+ // TODO: position first_line so that main cursor is centered
1849 view.first_line = 0;
1930 view.first_line = 0;
1850 view.doc = *id;
1931 view.doc = *id;
1851 let selection = selection.clone();
1932 let selection = selection.clone();
1933+ let view_id = view.id;
1852 let doc = cx.doc();
1934 let doc = cx.doc();
1853− doc.set_selection(selection);
1935+ doc.set_selection(view_id, selection);