This reduces the size of cursor by a usize when the summary does not
require a context making Cursor usages and constructions slightly more
efficient.
This change is a bit annoying though, as Rust has no means of
specializing, so this uses a `ContextlessSummary` trait with a blanket
impl while turning the `Context` into a GAT `Context<'a>`. This means
`Summary` implies are a bit more verbose now while contextless ones are
slimmer. It does come with the downside that the lifetime in the GAT is
always considered invariant, so some lifetime splitting occurred due to
that.
```
push/4096 time: [352.65 µs 360.87 µs 367.80 µs]
thrpt: [10.621 MiB/s 10.825 MiB/s 11.077 MiB/s]
change:
time: [-2.6633% -1.3640% -0.0561%] (p = 0.05 < 0.05)
thrpt: [+0.0561% +1.3828% +2.7361%]
Change within noise threshold.
Found 16 outliers among 100 measurements (16.00%)
7 (7.00%) low severe
3 (3.00%) low mild
2 (2.00%) high mild
4 (4.00%) high severe
push/65536 time: [1.2917 ms 1.2949 ms 1.2979 ms]
thrpt: [48.156 MiB/s 48.267 MiB/s 48.387 MiB/s]
change:
time: [+1.4428% +1.9844% +2.5299%] (p = 0.00 < 0.05)
thrpt: [-2.4675% -1.9458% -1.4223%]
Performance has regressed.
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low severe
1 (1.00%) low mild
1 (1.00%) high severe
append/4096 time: [677.87 ns 678.87 ns 679.83 ns]
thrpt: [5.6112 GiB/s 5.6192 GiB/s 5.6274 GiB/s]
change:
time: [-0.8924% -0.5017% -0.1705%] (p = 0.00 < 0.05)
thrpt: [+0.1708% +0.5043% +0.9004%]
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
append/65536 time: [9.3275 µs 9.3406 µs 9.3536 µs]
thrpt: [6.5253 GiB/s 6.5344 GiB/s 6.5435 GiB/s]
change:
time: [+0.5409% +0.7215% +0.9054%] (p = 0.00 < 0.05)
thrpt: [-0.8973% -0.7163% -0.5380%]
Change within noise threshold.
slice/4096 time: [27.673 µs 27.791 µs 27.907 µs]
thrpt: [139.97 MiB/s 140.56 MiB/s 141.16 MiB/s]
change:
time: [-1.1065% -0.6725% -0.2429%] (p = 0.00 < 0.05)
thrpt: [+0.2435% +0.6770% +1.1189%]
Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) low mild
1 (1.00%) high mild
slice/65536 time: [507.55 µs 517.40 µs 535.60 µs]
thrpt: [116.69 MiB/s 120.80 MiB/s 123.14 MiB/s]
change:
time: [-1.3489% +0.0599% +2.2591%] (p = 0.96 > 0.05)
thrpt: [-2.2092% -0.0598% +1.3674%]
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
5 (5.00%) low mild
2 (2.00%) high mild
1 (1.00%) high severe
bytes_in_range/4096 time: [3.3917 µs 3.4108 µs 3.4313 µs]
thrpt: [1.1117 GiB/s 1.1184 GiB/s 1.1247 GiB/s]
change:
time: [-5.3466% -4.7193% -4.1262%] (p = 0.00 < 0.05)
thrpt: [+4.3038% +4.9531% +5.6487%]
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
1 (1.00%) low mild
5 (5.00%) high mild
bytes_in_range/65536 time: [88.175 µs 88.613 µs 89.111 µs]
thrpt: [701.37 MiB/s 705.31 MiB/s 708.82 MiB/s]
change:
time: [-0.6935% +0.3769% +1.4655%] (p = 0.50 > 0.05)
thrpt: [-1.4443% -0.3755% +0.6984%]
No change in performance detected.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild
chars/4096 time: [678.70 ns 680.38 ns 682.08 ns]
thrpt: [5.5927 GiB/s 5.6067 GiB/s 5.6206 GiB/s]
change:
time: [-0.6969% -0.2755% +0.1485%] (p = 0.20 > 0.05)
thrpt: [-0.1483% +0.2763% +0.7018%]
No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
5 (5.00%) low mild
4 (4.00%) high mild
chars/65536 time: [12.720 µs 12.775 µs 12.830 µs]
thrpt: [4.7573 GiB/s 4.7778 GiB/s 4.7983 GiB/s]
change:
time: [-0.6172% -0.1110% +0.4179%] (p = 0.68 > 0.05)
thrpt: [-0.4162% +0.1112% +0.6211%]
No change in performance detected.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
clip_point/4096 time: [33.240 µs 33.310 µs 33.394 µs]
thrpt: [116.98 MiB/s 117.27 MiB/s 117.52 MiB/s]
change:
time: [-2.8892% -2.6305% -2.3438%] (p = 0.00 < 0.05)
thrpt: [+2.4000% +2.7015% +2.9751%]
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
1 (1.00%) low mild
4 (4.00%) high mild
7 (7.00%) high severe
clip_point/65536 time: [1.6531 ms 1.6586 ms 1.6640 ms]
thrpt: [37.560 MiB/s 37.683 MiB/s 37.808 MiB/s]
change:
time: [-6.6381% -5.9395% -5.2680%] (p = 0.00 < 0.05)
thrpt: [+5.5610% +6.3146% +7.1100%]
Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
1 (1.00%) low mild
2 (2.00%) high mild
4 (4.00%) high severe
point_to_offset/4096 time: [11.586 µs 11.603 µs 11.621 µs]
thrpt: [336.15 MiB/s 336.67 MiB/s 337.16 MiB/s]
change:
time: [-14.289% -14.111% -13.939%] (p = 0.00 < 0.05)
thrpt: [+16.197% +16.429% +16.672%]
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
3 (3.00%) low severe
5 (5.00%) low mild
4 (4.00%) high mild
point_to_offset/65536 time: [527.74 µs 532.08 µs 536.51 µs]
thrpt: [116.49 MiB/s 117.46 MiB/s 118.43 MiB/s]
change:
time: [-6.7825% -4.6235% -2.3533%] (p = 0.00 < 0.05)
thrpt: [+2.4100% +4.8477% +7.2760%]
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
cursor/4096 time: [16.154 µs 16.192 µs 16.232 µs]
thrpt: [240.66 MiB/s 241.24 MiB/s 241.81 MiB/s]
change:
time: [-3.2536% -2.9145% -2.5526%] (p = 0.00 < 0.05)
thrpt: [+2.6194% +3.0019% +3.3630%]
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) low mild
2 (2.00%) high mild
2 (2.00%) high severe
cursor/65536 time: [509.60 µs 511.24 µs 512.93 µs]
thrpt: [121.85 MiB/s 122.25 MiB/s 122.65 MiB/s]
change:
time: [-7.3677% -6.6017% -5.7840%] (p = 0.00 < 0.05)
thrpt: [+6.1391% +7.0683% +7.9537%]
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
3 (3.00%) high mild
3 (3.00%) high severe
```
Release Notes:
- N/A *or* Added/Fixed/Improved ...
513 lines
16 KiB
Rust
513 lines
16 KiB
Rust
use collections::HashMap;
|
|
use gpui::{AnyElement, IntoElement};
|
|
use multi_buffer::{Anchor, AnchorRangeExt, MultiBufferRow, MultiBufferSnapshot, ToPoint};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{cmp::Ordering, fmt::Debug, ops::Range, sync::Arc};
|
|
use sum_tree::{Bias, SeekTarget, SumTree};
|
|
use text::Point;
|
|
use ui::{App, SharedString, Window};
|
|
|
|
use crate::{BlockStyle, FoldPlaceholder, RenderBlock};
|
|
|
|
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
|
pub struct CreaseId(usize);
|
|
|
|
pub struct CreaseMap {
|
|
snapshot: CreaseSnapshot,
|
|
next_id: CreaseId,
|
|
id_to_range: HashMap<CreaseId, Range<Anchor>>,
|
|
}
|
|
|
|
impl CreaseMap {
|
|
pub fn new(snapshot: &MultiBufferSnapshot) -> Self {
|
|
CreaseMap {
|
|
snapshot: CreaseSnapshot::new(snapshot),
|
|
next_id: CreaseId::default(),
|
|
id_to_range: HashMap::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct CreaseSnapshot {
|
|
creases: SumTree<CreaseItem>,
|
|
}
|
|
|
|
impl CreaseSnapshot {
|
|
pub fn new(snapshot: &MultiBufferSnapshot) -> Self {
|
|
CreaseSnapshot {
|
|
creases: SumTree::new(snapshot),
|
|
}
|
|
}
|
|
|
|
pub fn creases(&self) -> impl Iterator<Item = (CreaseId, &Crease<Anchor>)> {
|
|
self.creases.iter().map(|item| (item.id, &item.crease))
|
|
}
|
|
|
|
/// Returns the first Crease starting on the specified buffer row.
|
|
pub fn query_row<'a>(
|
|
&'a self,
|
|
row: MultiBufferRow,
|
|
snapshot: &'a MultiBufferSnapshot,
|
|
) -> Option<&'a Crease<Anchor>> {
|
|
let start = snapshot.anchor_before(Point::new(row.0, 0));
|
|
let mut cursor = self.creases.cursor::<ItemSummary>(snapshot);
|
|
cursor.seek(&start, Bias::Left);
|
|
while let Some(item) = cursor.item() {
|
|
match Ord::cmp(&item.crease.range().start.to_point(snapshot).row, &row.0) {
|
|
Ordering::Less => cursor.next(),
|
|
Ordering::Equal => {
|
|
if item.crease.range().start.is_valid(snapshot) {
|
|
return Some(&item.crease);
|
|
} else {
|
|
cursor.next();
|
|
}
|
|
}
|
|
Ordering::Greater => break,
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn creases_in_range<'a>(
|
|
&'a self,
|
|
range: Range<MultiBufferRow>,
|
|
snapshot: &'a MultiBufferSnapshot,
|
|
) -> impl 'a + Iterator<Item = &'a Crease<Anchor>> {
|
|
let start = snapshot.anchor_before(Point::new(range.start.0, 0));
|
|
let mut cursor = self.creases.cursor::<ItemSummary>(snapshot);
|
|
cursor.seek(&start, Bias::Left);
|
|
|
|
std::iter::from_fn(move || {
|
|
while let Some(item) = cursor.item() {
|
|
cursor.next();
|
|
let crease_range = item.crease.range();
|
|
let crease_start = crease_range.start.to_point(snapshot);
|
|
let crease_end = crease_range.end.to_point(snapshot);
|
|
if crease_end.row > range.end.0 {
|
|
continue;
|
|
}
|
|
if crease_start.row >= range.start.0 && crease_end.row < range.end.0 {
|
|
return Some(&item.crease);
|
|
}
|
|
}
|
|
None
|
|
})
|
|
}
|
|
|
|
pub fn crease_items_with_offsets(
|
|
&self,
|
|
snapshot: &MultiBufferSnapshot,
|
|
) -> Vec<(CreaseId, Range<Point>)> {
|
|
let mut cursor = self.creases.cursor::<ItemSummary>(snapshot);
|
|
let mut results = Vec::new();
|
|
|
|
cursor.next();
|
|
while let Some(item) = cursor.item() {
|
|
let crease_range = item.crease.range();
|
|
let start_point = crease_range.start.to_point(snapshot);
|
|
let end_point = crease_range.end.to_point(snapshot);
|
|
results.push((item.id, start_point..end_point));
|
|
cursor.next();
|
|
}
|
|
|
|
results
|
|
}
|
|
}
|
|
|
|
type RenderToggleFn = Arc<
|
|
dyn Send
|
|
+ Sync
|
|
+ Fn(
|
|
MultiBufferRow,
|
|
bool,
|
|
Arc<dyn Send + Sync + Fn(bool, &mut Window, &mut App)>,
|
|
&mut Window,
|
|
&mut App,
|
|
) -> AnyElement,
|
|
>;
|
|
type RenderTrailerFn =
|
|
Arc<dyn Send + Sync + Fn(MultiBufferRow, bool, &mut Window, &mut App) -> AnyElement>;
|
|
|
|
#[derive(Clone)]
|
|
pub enum Crease<T> {
|
|
Inline {
|
|
range: Range<T>,
|
|
placeholder: FoldPlaceholder,
|
|
render_toggle: Option<RenderToggleFn>,
|
|
render_trailer: Option<RenderTrailerFn>,
|
|
metadata: Option<CreaseMetadata>,
|
|
},
|
|
Block {
|
|
range: Range<T>,
|
|
block_height: u32,
|
|
block_style: BlockStyle,
|
|
render_block: RenderBlock,
|
|
block_priority: usize,
|
|
render_toggle: Option<RenderToggleFn>,
|
|
},
|
|
}
|
|
|
|
/// Metadata about a [`Crease`], that is used for serialization.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct CreaseMetadata {
|
|
pub icon_path: SharedString,
|
|
pub label: SharedString,
|
|
}
|
|
|
|
impl<T> Crease<T> {
|
|
pub fn simple(range: Range<T>, placeholder: FoldPlaceholder) -> Self {
|
|
Crease::Inline {
|
|
range,
|
|
placeholder,
|
|
render_toggle: None,
|
|
render_trailer: None,
|
|
metadata: None,
|
|
}
|
|
}
|
|
|
|
pub fn block(range: Range<T>, height: u32, style: BlockStyle, render: RenderBlock) -> Self {
|
|
Self::Block {
|
|
range,
|
|
block_height: height,
|
|
block_style: style,
|
|
render_block: render,
|
|
block_priority: 0,
|
|
render_toggle: None,
|
|
}
|
|
}
|
|
|
|
pub fn inline<RenderToggle, ToggleElement, RenderTrailer, TrailerElement>(
|
|
range: Range<T>,
|
|
placeholder: FoldPlaceholder,
|
|
render_toggle: RenderToggle,
|
|
render_trailer: RenderTrailer,
|
|
) -> Self
|
|
where
|
|
RenderToggle: 'static
|
|
+ Send
|
|
+ Sync
|
|
+ Fn(
|
|
MultiBufferRow,
|
|
bool,
|
|
Arc<dyn Send + Sync + Fn(bool, &mut Window, &mut App)>,
|
|
&mut Window,
|
|
&mut App,
|
|
) -> ToggleElement
|
|
+ 'static,
|
|
ToggleElement: IntoElement,
|
|
RenderTrailer: 'static
|
|
+ Send
|
|
+ Sync
|
|
+ Fn(MultiBufferRow, bool, &mut Window, &mut App) -> TrailerElement
|
|
+ 'static,
|
|
TrailerElement: IntoElement,
|
|
{
|
|
Crease::Inline {
|
|
range,
|
|
placeholder,
|
|
render_toggle: Some(Arc::new(move |row, folded, toggle, window, cx| {
|
|
render_toggle(row, folded, toggle, window, cx).into_any_element()
|
|
})),
|
|
render_trailer: Some(Arc::new(move |row, folded, window, cx| {
|
|
render_trailer(row, folded, window, cx).into_any_element()
|
|
})),
|
|
metadata: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_metadata(self, metadata: CreaseMetadata) -> Self {
|
|
match self {
|
|
Crease::Inline {
|
|
range,
|
|
placeholder,
|
|
render_toggle,
|
|
render_trailer,
|
|
..
|
|
} => Crease::Inline {
|
|
range,
|
|
placeholder,
|
|
render_toggle,
|
|
render_trailer,
|
|
metadata: Some(metadata),
|
|
},
|
|
Crease::Block { .. } => self,
|
|
}
|
|
}
|
|
|
|
pub fn range(&self) -> &Range<T> {
|
|
match self {
|
|
Crease::Inline { range, .. } => range,
|
|
Crease::Block { range, .. } => range,
|
|
}
|
|
}
|
|
|
|
pub fn metadata(&self) -> Option<&CreaseMetadata> {
|
|
match self {
|
|
Self::Inline { metadata, .. } => metadata.as_ref(),
|
|
Self::Block { .. } => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> std::fmt::Debug for Crease<T>
|
|
where
|
|
T: Debug,
|
|
{
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Crease::Inline {
|
|
range, metadata, ..
|
|
} => f
|
|
.debug_struct("Crease::Inline")
|
|
.field("range", range)
|
|
.field("metadata", metadata)
|
|
.finish_non_exhaustive(),
|
|
Crease::Block {
|
|
range,
|
|
block_height,
|
|
..
|
|
} => f
|
|
.debug_struct("Crease::Block")
|
|
.field("range", range)
|
|
.field("height", block_height)
|
|
.finish_non_exhaustive(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
struct CreaseItem {
|
|
id: CreaseId,
|
|
crease: Crease<Anchor>,
|
|
}
|
|
|
|
impl CreaseMap {
|
|
pub fn snapshot(&self) -> CreaseSnapshot {
|
|
self.snapshot.clone()
|
|
}
|
|
|
|
pub fn insert(
|
|
&mut self,
|
|
creases: impl IntoIterator<Item = Crease<Anchor>>,
|
|
snapshot: &MultiBufferSnapshot,
|
|
) -> Vec<CreaseId> {
|
|
let mut new_ids = Vec::new();
|
|
self.snapshot.creases = {
|
|
let mut new_creases = SumTree::new(snapshot);
|
|
let mut cursor = self.snapshot.creases.cursor::<ItemSummary>(snapshot);
|
|
for crease in creases {
|
|
let crease_range = crease.range().clone();
|
|
new_creases.append(cursor.slice(&crease_range, Bias::Left), snapshot);
|
|
|
|
let id = self.next_id;
|
|
self.next_id.0 += 1;
|
|
self.id_to_range.insert(id, crease_range);
|
|
new_creases.push(CreaseItem { crease, id }, snapshot);
|
|
new_ids.push(id);
|
|
}
|
|
new_creases.append(cursor.suffix(), snapshot);
|
|
new_creases
|
|
};
|
|
new_ids
|
|
}
|
|
|
|
pub fn remove(
|
|
&mut self,
|
|
ids: impl IntoIterator<Item = CreaseId>,
|
|
snapshot: &MultiBufferSnapshot,
|
|
) -> Vec<(CreaseId, Range<Anchor>)> {
|
|
let mut removals = Vec::new();
|
|
for id in ids {
|
|
if let Some(range) = self.id_to_range.remove(&id) {
|
|
removals.push((id, range.clone()));
|
|
}
|
|
}
|
|
removals.sort_unstable_by(|(a_id, a_range), (b_id, b_range)| {
|
|
AnchorRangeExt::cmp(a_range, b_range, snapshot).then(b_id.cmp(a_id))
|
|
});
|
|
|
|
self.snapshot.creases = {
|
|
let mut new_creases = SumTree::new(snapshot);
|
|
let mut cursor = self.snapshot.creases.cursor::<ItemSummary>(snapshot);
|
|
|
|
for (id, range) in &removals {
|
|
new_creases.append(cursor.slice(range, Bias::Left), snapshot);
|
|
while let Some(item) = cursor.item() {
|
|
cursor.next();
|
|
if item.id == *id {
|
|
break;
|
|
} else {
|
|
new_creases.push(item.clone(), snapshot);
|
|
}
|
|
}
|
|
}
|
|
|
|
new_creases.append(cursor.suffix(), snapshot);
|
|
new_creases
|
|
};
|
|
|
|
removals
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct ItemSummary {
|
|
range: Range<Anchor>,
|
|
}
|
|
|
|
impl Default for ItemSummary {
|
|
fn default() -> Self {
|
|
Self {
|
|
range: Anchor::min()..Anchor::min(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl sum_tree::Summary for ItemSummary {
|
|
type Context<'a> = &'a MultiBufferSnapshot;
|
|
|
|
fn zero(_cx: Self::Context<'_>) -> Self {
|
|
Default::default()
|
|
}
|
|
|
|
fn add_summary(&mut self, other: &Self, _snapshot: &MultiBufferSnapshot) {
|
|
self.range = other.range.clone();
|
|
}
|
|
}
|
|
|
|
impl sum_tree::Item for CreaseItem {
|
|
type Summary = ItemSummary;
|
|
|
|
fn summary(&self, _cx: &MultiBufferSnapshot) -> Self::Summary {
|
|
ItemSummary {
|
|
range: self.crease.range().clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Implements `SeekTarget` for `Range<Anchor>` to enable seeking within a `SumTree` of `CreaseItem`s.
|
|
impl SeekTarget<'_, ItemSummary, ItemSummary> for Range<Anchor> {
|
|
fn cmp(&self, cursor_location: &ItemSummary, snapshot: &MultiBufferSnapshot) -> Ordering {
|
|
AnchorRangeExt::cmp(self, &cursor_location.range, snapshot)
|
|
}
|
|
}
|
|
|
|
impl SeekTarget<'_, ItemSummary, ItemSummary> for Anchor {
|
|
fn cmp(&self, other: &ItemSummary, snapshot: &MultiBufferSnapshot) -> Ordering {
|
|
self.cmp(&other.range.start, snapshot)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use gpui::{App, div};
|
|
use multi_buffer::MultiBuffer;
|
|
|
|
#[gpui::test]
|
|
fn test_insert_and_remove_creases(cx: &mut App) {
|
|
let text = "line1\nline2\nline3\nline4\nline5";
|
|
let buffer = MultiBuffer::build_simple(text, cx);
|
|
let snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx));
|
|
let mut crease_map = CreaseMap::new(&buffer.read(cx).read(cx));
|
|
|
|
// Insert creases
|
|
let creases = [
|
|
Crease::inline(
|
|
snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 5)),
|
|
FoldPlaceholder::test(),
|
|
|_row, _folded, _toggle, _window, _cx| div(),
|
|
|_row, _folded, _window, _cx| div(),
|
|
),
|
|
Crease::inline(
|
|
snapshot.anchor_before(Point::new(3, 0))..snapshot.anchor_after(Point::new(3, 5)),
|
|
FoldPlaceholder::test(),
|
|
|_row, _folded, _toggle, _window, _cx| div(),
|
|
|_row, _folded, _window, _cx| div(),
|
|
),
|
|
];
|
|
let crease_ids = crease_map.insert(creases, &snapshot);
|
|
assert_eq!(crease_ids.len(), 2);
|
|
|
|
// Verify creases are inserted
|
|
let crease_snapshot = crease_map.snapshot();
|
|
assert!(
|
|
crease_snapshot
|
|
.query_row(MultiBufferRow(1), &snapshot)
|
|
.is_some()
|
|
);
|
|
assert!(
|
|
crease_snapshot
|
|
.query_row(MultiBufferRow(3), &snapshot)
|
|
.is_some()
|
|
);
|
|
|
|
// Remove creases
|
|
crease_map.remove(crease_ids, &snapshot);
|
|
|
|
// Verify creases are removed
|
|
let crease_snapshot = crease_map.snapshot();
|
|
assert!(
|
|
crease_snapshot
|
|
.query_row(MultiBufferRow(1), &snapshot)
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
crease_snapshot
|
|
.query_row(MultiBufferRow(3), &snapshot)
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[gpui::test]
|
|
fn test_creases_in_range(cx: &mut App) {
|
|
let text = "line1\nline2\nline3\nline4\nline5\nline6\nline7";
|
|
let buffer = MultiBuffer::build_simple(text, cx);
|
|
let snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx));
|
|
let mut crease_map = CreaseMap::new(&snapshot);
|
|
|
|
let creases = [
|
|
Crease::inline(
|
|
snapshot.anchor_before(Point::new(1, 0))..snapshot.anchor_after(Point::new(1, 5)),
|
|
FoldPlaceholder::test(),
|
|
|_row, _folded, _toggle, _window, _cx| div(),
|
|
|_row, _folded, _window, _cx| div(),
|
|
),
|
|
Crease::inline(
|
|
snapshot.anchor_before(Point::new(3, 0))..snapshot.anchor_after(Point::new(3, 5)),
|
|
FoldPlaceholder::test(),
|
|
|_row, _folded, _toggle, _window, _cx| div(),
|
|
|_row, _folded, _window, _cx| div(),
|
|
),
|
|
Crease::inline(
|
|
snapshot.anchor_before(Point::new(5, 0))..snapshot.anchor_after(Point::new(5, 5)),
|
|
FoldPlaceholder::test(),
|
|
|_row, _folded, _toggle, _window, _cx| div(),
|
|
|_row, _folded, _window, _cx| div(),
|
|
),
|
|
];
|
|
crease_map.insert(creases, &snapshot);
|
|
|
|
let crease_snapshot = crease_map.snapshot();
|
|
|
|
let range = MultiBufferRow(0)..MultiBufferRow(7);
|
|
let creases: Vec<_> = crease_snapshot.creases_in_range(range, &snapshot).collect();
|
|
assert_eq!(creases.len(), 3);
|
|
|
|
let range = MultiBufferRow(2)..MultiBufferRow(5);
|
|
let creases: Vec<_> = crease_snapshot.creases_in_range(range, &snapshot).collect();
|
|
assert_eq!(creases.len(), 1);
|
|
assert_eq!(creases[0].range().start.to_point(&snapshot).row, 3);
|
|
|
|
let range = MultiBufferRow(0)..MultiBufferRow(2);
|
|
let creases: Vec<_> = crease_snapshot.creases_in_range(range, &snapshot).collect();
|
|
assert_eq!(creases.len(), 1);
|
|
assert_eq!(creases[0].range().start.to_point(&snapshot).row, 1);
|
|
|
|
let range = MultiBufferRow(6)..MultiBufferRow(7);
|
|
let creases: Vec<_> = crease_snapshot.creases_in_range(range, &snapshot).collect();
|
|
assert_eq!(creases.len(), 0);
|
|
}
|
|
}
|