@@ -417,6 +417,7 @@ impl From<Range> for helix_stdx::Range {
417417pub struct Selection {
418418 ranges : SmallVec < [ Range ; 1 ] > ,
419419 primary_index : usize ,
420+ edit_only_primary : bool ,
420421}
421422
422423#[ allow( clippy:: len_without_is_empty) ] // a Selection is never empty
@@ -440,10 +441,7 @@ impl Selection {
440441 if self . ranges . len ( ) == 1 {
441442 self
442443 } else {
443- Self {
444- ranges : smallvec ! [ self . ranges[ self . primary_index] ] ,
445- primary_index : 0 ,
446- }
444+ Self :: new ( smallvec ! [ self . ranges[ self . primary_index] ] , 0 )
447445 }
448446 }
449447
@@ -536,6 +534,14 @@ impl Selection {
536534 self . primary_index = idx;
537535 }
538536
537+ pub fn edit_only_primary ( & self ) -> bool {
538+ self . edit_only_primary
539+ }
540+
541+ pub fn set_edit_only_primary ( & mut self , val : bool ) {
542+ self . edit_only_primary = val;
543+ }
544+
539545 #[ must_use]
540546 /// Constructs a selection holding a single range.
541547 pub fn single ( anchor : usize , head : usize ) -> Self {
@@ -546,6 +552,7 @@ impl Selection {
546552 old_visual_position: None
547553 } ] ,
548554 primary_index : 0 ,
555+ edit_only_primary : false ,
549556 }
550557 }
551558
@@ -627,6 +634,9 @@ impl Selection {
627634 let selection = Self {
628635 ranges,
629636 primary_index,
637+
638+ // This is not a parameter since everywhere a Selection is constructed, editing only the primary is not desired.
639+ edit_only_primary : false ,
630640 } ;
631641
632642 selection. normalize ( )
@@ -637,9 +647,14 @@ impl Selection {
637647 where
638648 F : FnMut ( Range ) -> Range ,
639649 {
640- for range in self . ranges . iter_mut ( ) {
641- * range = f ( * range)
650+ if self . edit_only_primary {
651+ self . ranges [ self . primary_index ] = f ( self . ranges [ self . primary_index ] )
652+ } else {
653+ for range in self . ranges . iter_mut ( ) {
654+ * range = f ( * range)
655+ }
642656 }
657+
643658 self . normalize ( )
644659 }
645660
@@ -728,10 +743,7 @@ impl FromIterator<Range> for Selection {
728743
729744impl From < Range > for Selection {
730745 fn from ( range : Range ) -> Self {
731- Self {
732- ranges : smallvec ! [ range] ,
733- primary_index : 0 ,
734- }
746+ Self :: new ( smallvec ! [ range] , 0 )
735747 }
736748}
737749
0 commit comments