@@ -30,12 +30,14 @@ impl Range {
31 /// Start of the range.
31 /// Start of the range.
33 pub fn from(&self) -> usize {
34 pub fn from(&self) -> usize {
34 std::cmp::min(self.anchor, self.head)
35 std::cmp::min(self.anchor, self.head)
37 /// End of the range.
38 /// End of the range.
39 pub fn to(&self) -> usize {
41 pub fn to(&self) -> usize {
40 std::cmp::max(self.anchor, self.head)
42 std::cmp::max(self.anchor, self.head)
@@ -47,6 +49,7 @@ impl Range {
49 /// Check two ranges for overlap.
51 /// Check two ranges for overlap.
50 pub fn overlaps(&self, other: &Self) -> bool {
53 pub fn overlaps(&self, other: &Self) -> bool {
51 // cursor overlap is checked differently
54 // cursor overlap is checked differently
52 if self.is_empty() {
55 if self.is_empty() {
@@ -59,6 +62,7 @@ impl Range {
59 // TODO: map
62 // TODO: map
61 /// Extend the range to cover at least `from` `to`.
64 /// Extend the range to cover at least `from` `to`.
62 pub fn extend(&self, from: usize, to: usize) -> Self {
66 pub fn extend(&self, from: usize, to: usize) -> Self {
63 if from <= self.anchor && to >= self.anchor {
67 if from <= self.anchor && to >= self.anchor {
64 return Range {
68 return Range {
@@ -90,12 +94,14 @@ pub struct Selection {
90impl Selection {
94impl Selection {
93 pub fn primary(&self) -> Range {
99 pub fn primary(&self) -> Range {
94 self.ranges[self.primary_index]
100 self.ranges[self.primary_index]
97 /// Ensure selection containing only the primary selection.
103 /// Ensure selection containing only the primary selection.
98− pub fn as_single(self) -> Self {
104+ pub fn into_single(self) -> Self {
99 if self.ranges.len() == 1 {
105 if self.ranges.len() == 1 {
@@ -109,6 +115,7 @@ impl Selection {
109 // add_range // push
115 // add_range // push
110 // replace_range
116 // replace_range
112 /// Constructs a selection holding a single range.
119 /// Constructs a selection holding a single range.
113 pub fn single(anchor: usize, head: usize) -> Self {
120 pub fn single(anchor: usize, head: usize) -> Self {
@@ -117,11 +124,12 @@ impl Selection {
120 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
128 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
121− fn normalize(mut ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Selection {
129+ fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
122 let primary = ranges[primary_index];
130 let primary = ranges[primary_index];
123− ranges.sort_unstable_by_key(|range| range.from());
131+ ranges.sort_unstable_by_key(Range::from);
124− let mut primary_index = ranges.iter().position(|&range| range == primary).unwrap();
132+ primary_index = ranges.iter().position(|&range| range == primary).unwrap();
126 let mut result: SmallVec<[Range; 1]> = SmallVec::new();
134 let mut result: SmallVec<[Range; 1]> = SmallVec::new();