From a88cff4fa099dc10bd75307c71c7ee2432ba03fe Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 7 Dec 2021 16:52:15 +0100 Subject: [PATCH] Remove lifetime parameter from TextDimension trait Co-Authored-By: Antonio Scandurra --- crates/editor/src/editor.rs | 17 ++++------------- crates/go_to_line/src/go_to_line.rs | 2 +- crates/text/src/anchor.rs | 18 +++++++++--------- crates/text/src/rope.rs | 14 +++++++------- crates/text/src/selection.rs | 17 ++++++++--------- crates/text/src/text.rs | 22 ++++++++++------------ 6 files changed, 39 insertions(+), 51 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 294e9f229ed9992dc359df445694b36dc7e868b0..848edc643f33bd2158dfa0b90ff34fa9d73ae395 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3056,7 +3056,7 @@ impl Editor { pub fn selections<'a, D>(&self, cx: &'a AppContext) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a> + Ord, + D: 'a + TextDimension + Ord, { let buffer = self.buffer.read(cx); let mut selections = self.selection_set(cx).selections::(buffer).peekable(); @@ -3086,10 +3086,7 @@ impl Editor { }) } - fn pending_selection<'a, D>(&self, cx: &'a AppContext) -> Option> - where - D: 'a + TextDimension<'a>, - { + fn pending_selection(&self, cx: &AppContext) -> Option> { let buffer = self.buffer.read(cx); self.pending_selection.as_ref().map(|pending| Selection { id: pending.selection.id, @@ -3108,10 +3105,7 @@ impl Editor { selection_count } - pub fn oldest_selection<'a, T>(&self, cx: &'a AppContext) -> Selection - where - T: 'a + TextDimension<'a>, - { + pub fn oldest_selection(&self, cx: &AppContext) -> Selection { let buffer = self.buffer.read(cx); self.selection_set(cx) .oldest_selection(buffer) @@ -3119,10 +3113,7 @@ impl Editor { .unwrap() } - pub fn newest_selection<'a, T>(&self, cx: &'a AppContext) -> Selection - where - T: 'a + TextDimension<'a>, - { + pub fn newest_selection(&self, cx: &AppContext) -> Selection { let buffer = self.buffer.read(cx); self.pending_selection(cx) .or_else(|| self.selection_set(cx).newest_selection(buffer)) diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index 77942b105fd315eddbf737be1d6968360c1b0b53..dbd36b1139402e535a8ee3c3fcf2257b503903bb 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -1,10 +1,10 @@ -use text::{Bias, Point, Selection}; use editor::{display_map::ToDisplayPoint, Autoscroll, Editor, EditorSettings}; use gpui::{ action, elements::*, geometry::vector::Vector2F, keymap::Binding, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, }; use postage::watch; +use text::{Bias, Point, Selection}; use workspace::{Settings, Workspace}; action!(Toggle); diff --git a/crates/text/src/anchor.rs b/crates/text/src/anchor.rs index 5f02a0e03bb229bebec1f0c664964c38fea6334e..5653124c68f4ec6f7207c87398b765d3dbeb51ca 100644 --- a/crates/text/src/anchor.rs +++ b/crates/text/src/anchor.rs @@ -117,7 +117,7 @@ impl Anchor { pub fn summary<'a, D>(&self, content: &'a BufferSnapshot) -> D where - D: TextDimension<'a>, + D: TextDimension, { content.summary_for_anchor(self) } @@ -137,7 +137,7 @@ impl AnchorMap { snapshot: &'a BufferSnapshot, ) -> impl Iterator + 'a where - D: 'a + TextDimension<'a>, + D: TextDimension, { snapshot .summaries_for_anchors( @@ -160,7 +160,7 @@ impl AnchorSet { pub fn iter<'a, D>(&'a self, content: &'a BufferSnapshot) -> impl Iterator + 'a where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.0.iter(content).map(|(position, _)| position) } @@ -194,7 +194,7 @@ impl AnchorRangeMap { content: &'a BufferSnapshot, ) -> impl Iterator, &'a T)> + 'a where - D: 'a + TextDimension<'a>, + D: TextDimension, { content .summaries_for_anchor_ranges( @@ -212,7 +212,7 @@ impl AnchorRangeMap { content: &'a BufferSnapshot, ) -> impl Iterator, &'a T)> + 'a where - D: 'a + TextDimension<'a>, + D: TextDimension, I: ToOffset, { let range = content.anchor_at(range.start.0, range.start.1) @@ -250,7 +250,7 @@ impl AnchorRangeMap { mut extract_key: F, ) -> Option<(Range, &T)> where - D: 'a + TextDimension<'a>, + D: TextDimension, F: FnMut(&T) -> K, K: Ord, { @@ -266,7 +266,7 @@ impl AnchorRangeMap { mut extract_key: F, ) -> Option<(Range, &T)> where - D: 'a + TextDimension<'a>, + D: TextDimension, F: FnMut(&T) -> K, K: Ord, { @@ -282,7 +282,7 @@ impl AnchorRangeMap { content: &'a BufferSnapshot, ) -> Range where - D: 'a + TextDimension<'a>, + D: TextDimension, { let mut anchor = Anchor { full_offset: range.start, @@ -342,7 +342,7 @@ impl AnchorRangeSet { content: &'a BufferSnapshot, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.0.ranges(content).map(|(range, _)| range) } diff --git a/crates/text/src/rope.rs b/crates/text/src/rope.rs index f8170a4ac086f69adebf1749c107df66f0ee76b9..ffc1b74c55de7f6c5bd631d4c4258382fa6bc262 100644 --- a/crates/text/src/rope.rs +++ b/crates/text/src/rope.rs @@ -327,7 +327,7 @@ impl<'a> Cursor<'a> { slice } - pub fn summary>(&mut self, end_offset: usize) -> D { + pub fn summary(&mut self, end_offset: usize) -> D { debug_assert!(end_offset >= self.offset); let mut summary = D::default(); @@ -719,12 +719,12 @@ impl std::ops::AddAssign for TextSummary { } } -pub trait TextDimension<'a>: Dimension<'a, TextSummary> { +pub trait TextDimension: 'static + for<'a> Dimension<'a, TextSummary> { fn from_text_summary(summary: &TextSummary) -> Self; fn add_assign(&mut self, other: &Self); } -impl<'a, D1: TextDimension<'a>, D2: TextDimension<'a>> TextDimension<'a> for (D1, D2) { +impl<'a, D1: TextDimension, D2: TextDimension> TextDimension for (D1, D2) { fn from_text_summary(summary: &TextSummary) -> Self { ( D1::from_text_summary(summary), @@ -738,7 +738,7 @@ impl<'a, D1: TextDimension<'a>, D2: TextDimension<'a>> TextDimension<'a> for (D1 } } -impl<'a> TextDimension<'a> for TextSummary { +impl TextDimension for TextSummary { fn from_text_summary(summary: &TextSummary) -> Self { summary.clone() } @@ -754,7 +754,7 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for usize { } } -impl<'a> TextDimension<'a> for usize { +impl TextDimension for usize { fn from_text_summary(summary: &TextSummary) -> Self { summary.bytes } @@ -770,7 +770,7 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for Point { } } -impl<'a> TextDimension<'a> for Point { +impl TextDimension for Point { fn from_text_summary(summary: &TextSummary) -> Self { summary.lines } @@ -786,7 +786,7 @@ impl<'a> sum_tree::Dimension<'a, TextSummary> for PointUtf16 { } } -impl<'a> TextDimension<'a> for PointUtf16 { +impl TextDimension for PointUtf16 { fn from_text_summary(summary: &TextSummary) -> Self { summary.lines_utf16 } diff --git a/crates/text/src/selection.rs b/crates/text/src/selection.rs index 6af10395272e0fcf7ebe0255a2acecdc9f0724ad..6c04da016a1a6bb0782ba3a56ccbb8a1c54d4111 100644 --- a/crates/text/src/selection.rs +++ b/crates/text/src/selection.rs @@ -1,9 +1,8 @@ -use sum_tree::Bias; - -use crate::{rope::TextDimension, BufferSnapshot}; - -use super::{AnchorRangeMap, Buffer, Point, ToOffset, ToPoint}; +use crate::{ + rope::TextDimension, AnchorRangeMap, Buffer, BufferSnapshot, Point, ToOffset, ToPoint, +}; use std::{cmp::Ordering, ops::Range, sync::Arc}; +use sum_tree::Bias; pub type SelectionSetId = clock::Lamport; pub type SelectionsVersion = usize; @@ -108,7 +107,7 @@ impl SelectionSet { content: &'a BufferSnapshot, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.selections .ranges(content) @@ -127,7 +126,7 @@ impl SelectionSet { content: &'a BufferSnapshot, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a>, + D: TextDimension, I: 'a + ToOffset, { self.selections @@ -143,7 +142,7 @@ impl SelectionSet { pub fn oldest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option> where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.selections .min_by_key(content, |selection| selection.id) @@ -158,7 +157,7 @@ impl SelectionSet { pub fn newest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option> where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.selections .max_by_key(content, |selection| selection.id) diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index f99de77ab6e1c8bdd82e763de158709453628cea..5fcd40ad89e0a08c9fd6d5c029f6b0bb6e47975c 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -295,7 +295,7 @@ impl UndoMap { } } -struct Edits<'a, D: TextDimension<'a>, F: FnMut(&FragmentSummary) -> bool> { +struct Edits<'a, D: TextDimension, F: FnMut(&FragmentSummary) -> bool> { visible_cursor: rope::Cursor<'a>, deleted_cursor: rope::Cursor<'a>, fragments_cursor: Option>, @@ -1447,7 +1447,7 @@ impl Buffer { #[cfg(test)] pub fn selection_ranges<'a, D>(&'a self, set_id: SelectionSetId) -> Result>> where - D: 'a + TextDimension<'a>, + D: TextDimension, { Ok(self .selection_set(set_id)? @@ -1467,7 +1467,7 @@ impl Buffer { &'a self, ) -> impl 'a + Iterator>)> where - D: 'a + TextDimension<'a>, + D: TextDimension, { self.selections .keys() @@ -1596,7 +1596,7 @@ impl BufferSnapshot { fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D where - D: TextDimension<'a>, + D: TextDimension, { let cx = Some(anchor.version.clone()); let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>(); @@ -1615,7 +1615,7 @@ impl BufferSnapshot { pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range) -> D where - D: TextDimension<'a>, + D: TextDimension, { self.visible_text .cursor(range.start.to_offset(self)) @@ -1629,7 +1629,7 @@ impl BufferSnapshot { ranges: I, ) -> impl 'a + Iterator where - D: 'a + TextDimension<'a>, + D: TextDimension, I: 'a + IntoIterator, { let cx = Some(version.clone()); @@ -1656,7 +1656,7 @@ impl BufferSnapshot { ranges: I, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a>, + D: TextDimension, I: 'a + IntoIterator>, { let cx = Some(version); @@ -1855,7 +1855,7 @@ impl BufferSnapshot { since: &'a clock::Global, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a> + Ord, + D: TextDimension + Ord, { self.edits_since_in_range(since, Anchor::min()..Anchor::max()) } @@ -1866,7 +1866,7 @@ impl BufferSnapshot { range: Range, ) -> impl 'a + Iterator> where - D: 'a + TextDimension<'a> + Ord, + D: TextDimension + Ord, { let fragments_cursor = if *since == self.version { None @@ -1964,9 +1964,7 @@ impl<'a> RopeBuilder<'a> { } } -impl<'a, D: TextDimension<'a> + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator - for Edits<'a, D, F> -{ +impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> { type Item = Edit; fn next(&mut self) -> Option {