Remove lifetime parameter from TextDimension trait

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Antonio Scandurra 2021-12-07 16:52:15 +01:00 committed by Max Brunsfeld
parent 6a44a7448e
commit a88cff4fa0
6 changed files with 39 additions and 51 deletions

View file

@ -3056,7 +3056,7 @@ impl Editor {
pub fn selections<'a, D>(&self, cx: &'a AppContext) -> impl 'a + Iterator<Item = Selection<D>> pub fn selections<'a, D>(&self, cx: &'a AppContext) -> impl 'a + Iterator<Item = Selection<D>>
where where
D: 'a + TextDimension<'a> + Ord, D: 'a + TextDimension + Ord,
{ {
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
let mut selections = self.selection_set(cx).selections::<D>(buffer).peekable(); let mut selections = self.selection_set(cx).selections::<D>(buffer).peekable();
@ -3086,10 +3086,7 @@ impl Editor {
}) })
} }
fn pending_selection<'a, D>(&self, cx: &'a AppContext) -> Option<Selection<D>> fn pending_selection<D: TextDimension>(&self, cx: &AppContext) -> Option<Selection<D>> {
where
D: 'a + TextDimension<'a>,
{
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
self.pending_selection.as_ref().map(|pending| Selection { self.pending_selection.as_ref().map(|pending| Selection {
id: pending.selection.id, id: pending.selection.id,
@ -3108,10 +3105,7 @@ impl Editor {
selection_count selection_count
} }
pub fn oldest_selection<'a, T>(&self, cx: &'a AppContext) -> Selection<T> pub fn oldest_selection<T: TextDimension>(&self, cx: &AppContext) -> Selection<T> {
where
T: 'a + TextDimension<'a>,
{
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
self.selection_set(cx) self.selection_set(cx)
.oldest_selection(buffer) .oldest_selection(buffer)
@ -3119,10 +3113,7 @@ impl Editor {
.unwrap() .unwrap()
} }
pub fn newest_selection<'a, T>(&self, cx: &'a AppContext) -> Selection<T> pub fn newest_selection<T: TextDimension>(&self, cx: &AppContext) -> Selection<T> {
where
T: 'a + TextDimension<'a>,
{
let buffer = self.buffer.read(cx); let buffer = self.buffer.read(cx);
self.pending_selection(cx) self.pending_selection(cx)
.or_else(|| self.selection_set(cx).newest_selection(buffer)) .or_else(|| self.selection_set(cx).newest_selection(buffer))

View file

@ -1,10 +1,10 @@
use text::{Bias, Point, Selection};
use editor::{display_map::ToDisplayPoint, Autoscroll, Editor, EditorSettings}; use editor::{display_map::ToDisplayPoint, Autoscroll, Editor, EditorSettings};
use gpui::{ use gpui::{
action, elements::*, geometry::vector::Vector2F, keymap::Binding, Axis, Entity, action, elements::*, geometry::vector::Vector2F, keymap::Binding, Axis, Entity,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
}; };
use postage::watch; use postage::watch;
use text::{Bias, Point, Selection};
use workspace::{Settings, Workspace}; use workspace::{Settings, Workspace};
action!(Toggle); action!(Toggle);

View file

@ -117,7 +117,7 @@ impl Anchor {
pub fn summary<'a, D>(&self, content: &'a BufferSnapshot) -> D pub fn summary<'a, D>(&self, content: &'a BufferSnapshot) -> D
where where
D: TextDimension<'a>, D: TextDimension,
{ {
content.summary_for_anchor(self) content.summary_for_anchor(self)
} }
@ -137,7 +137,7 @@ impl<T> AnchorMap<T> {
snapshot: &'a BufferSnapshot, snapshot: &'a BufferSnapshot,
) -> impl Iterator<Item = (D, &'a T)> + 'a ) -> impl Iterator<Item = (D, &'a T)> + 'a
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
snapshot snapshot
.summaries_for_anchors( .summaries_for_anchors(
@ -160,7 +160,7 @@ impl AnchorSet {
pub fn iter<'a, D>(&'a self, content: &'a BufferSnapshot) -> impl Iterator<Item = D> + 'a pub fn iter<'a, D>(&'a self, content: &'a BufferSnapshot) -> impl Iterator<Item = D> + 'a
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.0.iter(content).map(|(position, _)| position) self.0.iter(content).map(|(position, _)| position)
} }
@ -194,7 +194,7 @@ impl<T> AnchorRangeMap<T> {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a ) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
content content
.summaries_for_anchor_ranges( .summaries_for_anchor_ranges(
@ -212,7 +212,7 @@ impl<T> AnchorRangeMap<T> {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a ) -> impl Iterator<Item = (Range<D>, &'a T)> + 'a
where where
D: 'a + TextDimension<'a>, D: TextDimension,
I: ToOffset, I: ToOffset,
{ {
let range = content.anchor_at(range.start.0, range.start.1) let range = content.anchor_at(range.start.0, range.start.1)
@ -250,7 +250,7 @@ impl<T> AnchorRangeMap<T> {
mut extract_key: F, mut extract_key: F,
) -> Option<(Range<D>, &T)> ) -> Option<(Range<D>, &T)>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
F: FnMut(&T) -> K, F: FnMut(&T) -> K,
K: Ord, K: Ord,
{ {
@ -266,7 +266,7 @@ impl<T> AnchorRangeMap<T> {
mut extract_key: F, mut extract_key: F,
) -> Option<(Range<D>, &T)> ) -> Option<(Range<D>, &T)>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
F: FnMut(&T) -> K, F: FnMut(&T) -> K,
K: Ord, K: Ord,
{ {
@ -282,7 +282,7 @@ impl<T> AnchorRangeMap<T> {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> Range<D> ) -> Range<D>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
let mut anchor = Anchor { let mut anchor = Anchor {
full_offset: range.start, full_offset: range.start,
@ -342,7 +342,7 @@ impl AnchorRangeSet {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Range<Point>> ) -> impl 'a + Iterator<Item = Range<Point>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.0.ranges(content).map(|(range, _)| range) self.0.ranges(content).map(|(range, _)| range)
} }

View file

@ -327,7 +327,7 @@ impl<'a> Cursor<'a> {
slice slice
} }
pub fn summary<D: TextDimension<'a>>(&mut self, end_offset: usize) -> D { pub fn summary<D: TextDimension>(&mut self, end_offset: usize) -> D {
debug_assert!(end_offset >= self.offset); debug_assert!(end_offset >= self.offset);
let mut summary = D::default(); let mut summary = D::default();
@ -719,12 +719,12 @@ impl std::ops::AddAssign<Self> 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 from_text_summary(summary: &TextSummary) -> Self;
fn add_assign(&mut self, other: &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 { fn from_text_summary(summary: &TextSummary) -> Self {
( (
D1::from_text_summary(summary), 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 { fn from_text_summary(summary: &TextSummary) -> Self {
summary.clone() 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 { fn from_text_summary(summary: &TextSummary) -> Self {
summary.bytes 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 { fn from_text_summary(summary: &TextSummary) -> Self {
summary.lines 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 { fn from_text_summary(summary: &TextSummary) -> Self {
summary.lines_utf16 summary.lines_utf16
} }

View file

@ -1,9 +1,8 @@
use sum_tree::Bias; use crate::{
rope::TextDimension, AnchorRangeMap, Buffer, BufferSnapshot, Point, ToOffset, ToPoint,
use crate::{rope::TextDimension, BufferSnapshot}; };
use super::{AnchorRangeMap, Buffer, Point, ToOffset, ToPoint};
use std::{cmp::Ordering, ops::Range, sync::Arc}; use std::{cmp::Ordering, ops::Range, sync::Arc};
use sum_tree::Bias;
pub type SelectionSetId = clock::Lamport; pub type SelectionSetId = clock::Lamport;
pub type SelectionsVersion = usize; pub type SelectionsVersion = usize;
@ -108,7 +107,7 @@ impl SelectionSet {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>> ) -> impl 'a + Iterator<Item = Selection<D>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.selections self.selections
.ranges(content) .ranges(content)
@ -127,7 +126,7 @@ impl SelectionSet {
content: &'a BufferSnapshot, content: &'a BufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>> ) -> impl 'a + Iterator<Item = Selection<D>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
I: 'a + ToOffset, I: 'a + ToOffset,
{ {
self.selections self.selections
@ -143,7 +142,7 @@ impl SelectionSet {
pub fn oldest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option<Selection<D>> pub fn oldest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option<Selection<D>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.selections self.selections
.min_by_key(content, |selection| selection.id) .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<Selection<D>> pub fn newest_selection<'a, D>(&'a self, content: &'a BufferSnapshot) -> Option<Selection<D>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.selections self.selections
.max_by_key(content, |selection| selection.id) .max_by_key(content, |selection| selection.id)

View file

@ -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>, visible_cursor: rope::Cursor<'a>,
deleted_cursor: rope::Cursor<'a>, deleted_cursor: rope::Cursor<'a>,
fragments_cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>, fragments_cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
@ -1447,7 +1447,7 @@ impl Buffer {
#[cfg(test)] #[cfg(test)]
pub fn selection_ranges<'a, D>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<D>>> pub fn selection_ranges<'a, D>(&'a self, set_id: SelectionSetId) -> Result<Vec<Range<D>>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
Ok(self Ok(self
.selection_set(set_id)? .selection_set(set_id)?
@ -1467,7 +1467,7 @@ impl Buffer {
&'a self, &'a self,
) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)> ) -> impl 'a + Iterator<Item = (SelectionSetId, Vec<Range<usize>>)>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
{ {
self.selections self.selections
.keys() .keys()
@ -1596,7 +1596,7 @@ impl BufferSnapshot {
fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D
where where
D: TextDimension<'a>, D: TextDimension,
{ {
let cx = Some(anchor.version.clone()); let cx = Some(anchor.version.clone());
let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>(); 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<O>) -> D pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
where where
D: TextDimension<'a>, D: TextDimension,
{ {
self.visible_text self.visible_text
.cursor(range.start.to_offset(self)) .cursor(range.start.to_offset(self))
@ -1629,7 +1629,7 @@ impl BufferSnapshot {
ranges: I, ranges: I,
) -> impl 'a + Iterator<Item = D> ) -> impl 'a + Iterator<Item = D>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
I: 'a + IntoIterator<Item = &'a FullOffset>, I: 'a + IntoIterator<Item = &'a FullOffset>,
{ {
let cx = Some(version.clone()); let cx = Some(version.clone());
@ -1656,7 +1656,7 @@ impl BufferSnapshot {
ranges: I, ranges: I,
) -> impl 'a + Iterator<Item = Range<D>> ) -> impl 'a + Iterator<Item = Range<D>>
where where
D: 'a + TextDimension<'a>, D: TextDimension,
I: 'a + IntoIterator<Item = &'a Range<FullOffset>>, I: 'a + IntoIterator<Item = &'a Range<FullOffset>>,
{ {
let cx = Some(version); let cx = Some(version);
@ -1855,7 +1855,7 @@ impl BufferSnapshot {
since: &'a clock::Global, since: &'a clock::Global,
) -> impl 'a + Iterator<Item = Edit<D>> ) -> impl 'a + Iterator<Item = Edit<D>>
where where
D: 'a + TextDimension<'a> + Ord, D: TextDimension + Ord,
{ {
self.edits_since_in_range(since, Anchor::min()..Anchor::max()) self.edits_since_in_range(since, Anchor::min()..Anchor::max())
} }
@ -1866,7 +1866,7 @@ impl BufferSnapshot {
range: Range<Anchor>, range: Range<Anchor>,
) -> impl 'a + Iterator<Item = Edit<D>> ) -> impl 'a + Iterator<Item = Edit<D>>
where where
D: 'a + TextDimension<'a> + Ord, D: TextDimension + Ord,
{ {
let fragments_cursor = if *since == self.version { let fragments_cursor = if *since == self.version {
None None
@ -1964,9 +1964,7 @@ impl<'a> RopeBuilder<'a> {
} }
} }
impl<'a, D: TextDimension<'a> + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
for Edits<'a, D, F>
{
type Item = Edit<D>; type Item = Edit<D>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {