From bd7701ed82385ce6e84b0786795e4ce05079d03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 30 Sep 2022 20:58:12 +0200 Subject: [PATCH] Clean up a few selection methods (#1403) --- lapce-core/src/selection.rs | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/lapce-core/src/selection.rs b/lapce-core/src/selection.rs index 55c59eed..b38a8211 100644 --- a/lapce-core/src/selection.rs +++ b/lapce-core/src/selection.rs @@ -220,34 +220,22 @@ pub fn min(&self) -> Selection { /// Get the leftmost [`SelRegion`] in this selection if present. pub fn first(&self) -> Option<&SelRegion> { - if self.is_empty() { - return None; - } - Some(&self.regions[0]) + self.regions.get(0) } /// Get the rightmost [`SelRegion`] in this selection if present. pub fn last(&self) -> Option<&SelRegion> { - if self.is_empty() { - return None; - } - Some(&self.regions[self.len() - 1]) + self.regions.get(self.len() - 1) } /// Get the last inserted [`SelRegion`] in this selection if present. pub fn last_inserted(&self) -> Option<&SelRegion> { - if self.is_empty() { - return None; - } - Some(&self.regions[self.last_inserted]) + self.regions.get(self.last_inserted) } /// Get a mutable reference to the last inserted [`SelRegion`] in this selection if present. pub fn last_inserted_mut(&mut self) -> Option<&mut SelRegion> { - if self.is_empty() { - return None; - } - Some(&mut self.regions[self.last_inserted]) + self.regions.get_mut(self.last_inserted) } /// The number of [`SelRegion`] in this selection. @@ -258,12 +246,7 @@ pub fn len(&self) -> usize { /// A [`Selection`] is considered to be a caret if it contains /// only caret [`SelRegion`] (see [`SelRegion::is_caret`]) pub fn is_caret(&self) -> bool { - for region in self.regions.iter() { - if !region.is_caret() { - return false; - } - } - true + self.regions.iter().all(|region| region.is_caret()) } /// Returns `true` if `self` has zero [`SelRegion`] @@ -273,6 +256,8 @@ pub fn is_empty(&self) -> bool { /// Returns the minimal offset across all region of this selection. /// + /// This function panics if the selection is empty. + /// /// **Example:** /// /// ```rust @@ -293,6 +278,8 @@ pub fn min_offset(&self) -> usize { /// Returns the maximal offset across all region of this selection. /// + /// This function panics if the selection is empty. + /// /// **Example:** /// /// ```rust