on master help sign in
prefer Document::set_selection to inserting selections directly (#2411)

Inserting these with the `HashMap::insert` method evades the call to `Selection::ensure_invariants`. The effect is that the scratch buffer (or other buffers opened through these code-paths) can start with a selection at (0, 0), when a file with equivalent contents ("\n") would start with (0, 1). I.e.: hx and touch f hx f start with different selections even though they have an equivalent Rope. With this change they both start with (0, 1).

commit247ab25bc04b75fede667bc2736c8449e8608714 authorMichael Davis <mcarsondavis@gmail.com> dateTue 10 May 2022, 19:53 (4 years ago) parente0b5cdf
1 file changed+3−3
helix-view/src/editor.rs+3−3
helix-view/src/editor.rs+3−3
@@ -700,7 +700,7 @@ impl Editor {
700700 let view_id = view!(self).id;
701701 let doc = self.documents.get_mut(&id).unwrap();
702702 if doc.selections().is_empty() {
703− doc.selections.insert(view_id, Selection::point(0));
703+ doc.set_selection(view_id, Selection::point(0));
704704 }
705705 return;
706706 }
@@ -716,7 +716,7 @@ impl Editor {
716716 );
717717 // initialize selection for view
718718 let doc = self.documents.get_mut(&id).unwrap();
719− doc.selections.insert(view_id, Selection::point(0));
719+ doc.set_selection(view_id, Selection::point(0));
720720 }
721721 }
722722
@@ -851,7 +851,7 @@ impl Editor {
851851 let view = View::new(doc_id, self.config().gutters.clone());
852852 let view_id = self.tree.insert(view);
853853 let doc = self.documents.get_mut(&doc_id).unwrap();
854− doc.selections.insert(view_id, Selection::point(0));
854+ doc.set_selection(view_id, Selection::point(0));
855855 }
856856
857857 self._refresh();