Get Editor compiling with MultiBuffer as its buffer
There's a bunch of unimplemented methods in MultiBuffer, but everything compiles. Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
co-authored by
Max Brunsfeld
parent
daedf179b2
commit
87d16c271e
@@ -6,7 +6,10 @@ mod wrap_map;
|
||||
use block_map::{BlockMap, BlockPoint};
|
||||
use fold_map::{FoldMap, ToFoldPoint as _};
|
||||
use gpui::{fonts::FontId, ElementBox, Entity, ModelContext, ModelHandle};
|
||||
use language::{Anchor, Buffer, Point, Subscription as BufferSubscription, ToOffset, ToPoint};
|
||||
use language::{
|
||||
multi_buffer::{Anchor, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint},
|
||||
Point, Subscription as BufferSubscription,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
ops::Range,
|
||||
@@ -26,7 +29,7 @@ pub trait ToDisplayPoint {
|
||||
}
|
||||
|
||||
pub struct DisplayMap {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
buffer: ModelHandle<MultiBuffer>,
|
||||
buffer_subscription: BufferSubscription,
|
||||
fold_map: FoldMap,
|
||||
tab_map: TabMap,
|
||||
@@ -40,7 +43,7 @@ impl Entity for DisplayMap {
|
||||
|
||||
impl DisplayMap {
|
||||
pub fn new(
|
||||
buffer: ModelHandle<Buffer>,
|
||||
buffer: ModelHandle<MultiBuffer>,
|
||||
tab_size: usize,
|
||||
font_id: FontId,
|
||||
font_size: f32,
|
||||
@@ -48,7 +51,7 @@ impl DisplayMap {
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Self {
|
||||
let buffer_subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
|
||||
let (fold_map, snapshot) = FoldMap::new(buffer.read(cx).snapshot());
|
||||
let (fold_map, snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
|
||||
let (tab_map, snapshot) = TabMap::new(snapshot, tab_size);
|
||||
let (wrap_map, snapshot) = WrapMap::new(snapshot, font_id, font_size, wrap_width, cx);
|
||||
let block_map = BlockMap::new(buffer.clone(), snapshot);
|
||||
@@ -64,7 +67,7 @@ impl DisplayMap {
|
||||
}
|
||||
|
||||
pub fn snapshot(&self, cx: &mut ModelContext<Self>) -> DisplaySnapshot {
|
||||
let buffer_snapshot = self.buffer.read(cx).snapshot();
|
||||
let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let edits = self.buffer_subscription.consume().into_inner();
|
||||
let (folds_snapshot, edits) = self.fold_map.read(buffer_snapshot, edits);
|
||||
let (tabs_snapshot, edits) = self.tab_map.sync(folds_snapshot.clone(), edits);
|
||||
@@ -74,7 +77,7 @@ impl DisplayMap {
|
||||
let blocks_snapshot = self.block_map.read(wraps_snapshot.clone(), edits, cx);
|
||||
|
||||
DisplaySnapshot {
|
||||
buffer_snapshot: self.buffer.read(cx).snapshot(),
|
||||
buffer_snapshot: self.buffer.read(cx).snapshot(cx),
|
||||
folds_snapshot,
|
||||
tabs_snapshot,
|
||||
wraps_snapshot,
|
||||
@@ -87,7 +90,7 @@ impl DisplayMap {
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
let snapshot = self.buffer.read(cx).snapshot();
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let edits = self.buffer_subscription.consume().into_inner();
|
||||
let (mut fold_map, snapshot, edits) = self.fold_map.write(snapshot, edits);
|
||||
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
|
||||
@@ -108,7 +111,7 @@ impl DisplayMap {
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
let snapshot = self.buffer.read(cx).snapshot();
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let edits = self.buffer_subscription.consume().into_inner();
|
||||
let (mut fold_map, snapshot, edits) = self.fold_map.write(snapshot, edits);
|
||||
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
|
||||
@@ -132,7 +135,7 @@ impl DisplayMap {
|
||||
where
|
||||
P: ToOffset + Clone,
|
||||
{
|
||||
let snapshot = self.buffer.read(cx).snapshot();
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let edits = self.buffer_subscription.consume().into_inner();
|
||||
let (snapshot, edits) = self.fold_map.read(snapshot, edits);
|
||||
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
|
||||
@@ -151,7 +154,7 @@ impl DisplayMap {
|
||||
}
|
||||
|
||||
pub fn remove_blocks(&mut self, ids: HashSet<BlockId>, cx: &mut ModelContext<Self>) {
|
||||
let snapshot = self.buffer.read(cx).snapshot();
|
||||
let snapshot = self.buffer.read(cx).snapshot(cx);
|
||||
let edits = self.buffer_subscription.consume().into_inner();
|
||||
let (snapshot, edits) = self.fold_map.read(snapshot, edits);
|
||||
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
|
||||
@@ -179,7 +182,7 @@ impl DisplayMap {
|
||||
}
|
||||
|
||||
pub struct DisplaySnapshot {
|
||||
pub buffer_snapshot: language::BufferSnapshot,
|
||||
pub buffer_snapshot: MultiBufferSnapshot,
|
||||
folds_snapshot: fold_map::FoldSnapshot,
|
||||
tabs_snapshot: tab_map::TabSnapshot,
|
||||
wraps_snapshot: wrap_map::WrapSnapshot,
|
||||
@@ -457,7 +460,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{movement, test::*};
|
||||
use gpui::{color::Color, MutableAppContext};
|
||||
use language::{Language, LanguageConfig, RandomCharIter, SelectionGoal};
|
||||
use language::{Buffer, Language, LanguageConfig, RandomCharIter, SelectionGoal};
|
||||
use rand::{prelude::StdRng, Rng};
|
||||
use std::{env, sync::Arc};
|
||||
use theme::SyntaxTheme;
|
||||
@@ -489,10 +492,10 @@ mod tests {
|
||||
log::info!("tab size: {}", tab_size);
|
||||
log::info!("wrap width: {:?}", wrap_width);
|
||||
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let buffer = cx.update(|cx| {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
Buffer::new(0, text, cx)
|
||||
MultiBuffer::build_simple(&text, cx)
|
||||
});
|
||||
|
||||
let map = cx.add_model(|cx| {
|
||||
@@ -563,8 +566,10 @@ mod tests {
|
||||
assert_eq!(prev_display_bound.column(), 0);
|
||||
if next_display_bound < snapshot.max_point() {
|
||||
assert_eq!(
|
||||
buffer
|
||||
.read_with(&cx, |buffer, _| buffer.chars_at(next_buffer_bound).next()),
|
||||
buffer.read_with(&cx, |buffer, _| buffer
|
||||
.as_snapshot()
|
||||
.chars_at(next_buffer_bound)
|
||||
.next()),
|
||||
Some('\n')
|
||||
)
|
||||
}
|
||||
@@ -651,7 +656,7 @@ mod tests {
|
||||
let wrap_width = Some(64.);
|
||||
|
||||
let text = "one two three four five\nsix seven eight";
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text.to_string(), cx));
|
||||
let buffer = MultiBuffer::build_simple(text, cx);
|
||||
let map = cx.add_model(|cx| {
|
||||
DisplayMap::new(buffer.clone(), tab_size, font_id, font_size, wrap_width, cx)
|
||||
});
|
||||
@@ -724,7 +729,7 @@ mod tests {
|
||||
#[gpui::test]
|
||||
fn test_text_chunks(cx: &mut gpui::MutableAppContext) {
|
||||
let text = sample_text(6, 6, 'a');
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let buffer = MultiBuffer::build_simple(&text, cx);
|
||||
let tab_size = 4;
|
||||
let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
|
||||
let font_id = cx
|
||||
@@ -803,6 +808,7 @@ mod tests {
|
||||
let buffer =
|
||||
cx.add_model(|cx| Buffer::new(0, text, cx).with_language(Some(lang), None, cx));
|
||||
buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;
|
||||
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
||||
|
||||
let tab_size = 2;
|
||||
let font_cache = cx.font_cache();
|
||||
@@ -890,6 +896,7 @@ mod tests {
|
||||
let buffer =
|
||||
cx.add_model(|cx| Buffer::new(0, text, cx).with_language(Some(lang), None, cx));
|
||||
buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;
|
||||
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
||||
|
||||
let font_cache = cx.font_cache();
|
||||
|
||||
@@ -935,7 +942,7 @@ mod tests {
|
||||
|
||||
let text = "\n'a', 'α',\t'✋',\t'❎', '🍐'\n";
|
||||
let display_text = "\n'a', 'α', '✋', '❎', '🍐'\n";
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let buffer = MultiBuffer::build_simple(text, cx);
|
||||
|
||||
let tab_size = 4;
|
||||
let font_cache = cx.font_cache();
|
||||
@@ -979,7 +986,7 @@ mod tests {
|
||||
#[gpui::test]
|
||||
fn test_tabs_with_multibyte_chars(cx: &mut gpui::MutableAppContext) {
|
||||
let text = "✅\t\tα\nβ\t\n🏀β\t\tγ";
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let buffer = MultiBuffer::build_simple(text, cx);
|
||||
let tab_size = 4;
|
||||
let font_cache = cx.font_cache();
|
||||
let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
|
||||
@@ -1038,7 +1045,7 @@ mod tests {
|
||||
|
||||
#[gpui::test]
|
||||
fn test_max_point(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, "aaa\n\t\tbbb", cx));
|
||||
let buffer = MultiBuffer::build_simple("aaa\n\t\tbbb", cx);
|
||||
let tab_size = 4;
|
||||
let font_cache = cx.font_cache();
|
||||
let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use super::wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot};
|
||||
use gpui::{AppContext, ElementBox, ModelHandle};
|
||||
use language::{Buffer, Chunk};
|
||||
use language::{
|
||||
multi_buffer::{Anchor, MultiBuffer, ToOffset, ToPoint as _},
|
||||
Chunk,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use std::{
|
||||
cmp::{self, Ordering},
|
||||
@@ -12,14 +15,14 @@ use std::{
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
use sum_tree::SumTree;
|
||||
use text::{Anchor, Bias, Edit, Point, ToOffset, ToPoint as _};
|
||||
use sum_tree::{Bias, SumTree};
|
||||
use text::{Edit, Point};
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
const NEWLINES: &'static [u8] = &[b'\n'; u8::MAX as usize];
|
||||
|
||||
pub struct BlockMap {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
buffer: ModelHandle<MultiBuffer>,
|
||||
next_block_id: AtomicUsize,
|
||||
wrap_snapshot: Mutex<WrapSnapshot>,
|
||||
blocks: Vec<Arc<Block>>,
|
||||
@@ -109,7 +112,7 @@ pub struct BlockBufferRows<'a> {
|
||||
}
|
||||
|
||||
impl BlockMap {
|
||||
pub fn new(buffer: ModelHandle<Buffer>, wrap_snapshot: WrapSnapshot) -> Self {
|
||||
pub fn new(buffer: ModelHandle<MultiBuffer>, wrap_snapshot: WrapSnapshot) -> Self {
|
||||
Self {
|
||||
buffer,
|
||||
next_block_id: AtomicUsize::new(0),
|
||||
@@ -153,6 +156,7 @@ impl BlockMap {
|
||||
}
|
||||
|
||||
let buffer = self.buffer.read(cx);
|
||||
let buffer = buffer.as_snapshot();
|
||||
let mut transforms = self.transforms.lock();
|
||||
let mut new_transforms = SumTree::new();
|
||||
let old_row_count = transforms.summary().input_rows;
|
||||
@@ -241,7 +245,7 @@ impl BlockMap {
|
||||
let start_block_ix = match self.blocks[last_block_ix..].binary_search_by(|probe| {
|
||||
probe
|
||||
.position
|
||||
.cmp(&start_anchor, buffer)
|
||||
.cmp(&start_anchor, &buffer)
|
||||
.unwrap()
|
||||
.then(Ordering::Greater)
|
||||
}) {
|
||||
@@ -255,7 +259,7 @@ impl BlockMap {
|
||||
match self.blocks[start_block_ix..].binary_search_by(|probe| {
|
||||
probe
|
||||
.position
|
||||
.cmp(&end_anchor, buffer)
|
||||
.cmp(&end_anchor, &buffer)
|
||||
.unwrap()
|
||||
.then(Ordering::Greater)
|
||||
}) {
|
||||
@@ -268,7 +272,7 @@ impl BlockMap {
|
||||
self.blocks[start_block_ix..end_block_ix]
|
||||
.iter()
|
||||
.map(|block| {
|
||||
let mut position = block.position.to_point(buffer);
|
||||
let mut position = block.position.to_point(&buffer);
|
||||
let column = wrap_snapshot.from_point(position, Bias::Left).column();
|
||||
match block.disposition {
|
||||
BlockDisposition::Above => position.column = 0,
|
||||
@@ -380,6 +384,7 @@ impl<'a> BlockMapWriter<'a> {
|
||||
P: ToOffset + Clone,
|
||||
{
|
||||
let buffer = self.0.buffer.read(cx);
|
||||
let buffer = buffer.as_snapshot();
|
||||
let mut ids = Vec::new();
|
||||
let mut edits = Vec::<Edit<u32>>::new();
|
||||
let wrap_snapshot = &*self.0.wrap_snapshot.lock();
|
||||
@@ -389,7 +394,7 @@ impl<'a> BlockMapWriter<'a> {
|
||||
ids.push(id);
|
||||
|
||||
let position = buffer.anchor_after(block.position);
|
||||
let point = position.to_point(buffer);
|
||||
let point = position.to_point(&buffer);
|
||||
let start_row = wrap_snapshot
|
||||
.from_point(Point::new(point.row, 0), Bias::Left)
|
||||
.row();
|
||||
@@ -404,7 +409,7 @@ impl<'a> BlockMapWriter<'a> {
|
||||
let block_ix = match self
|
||||
.0
|
||||
.blocks
|
||||
.binary_search_by(|probe| probe.position.cmp(&position, buffer).unwrap())
|
||||
.binary_search_by(|probe| probe.position.cmp(&position, &buffer).unwrap())
|
||||
{
|
||||
Ok(ix) | Err(ix) => ix,
|
||||
};
|
||||
@@ -436,12 +441,13 @@ impl<'a> BlockMapWriter<'a> {
|
||||
|
||||
pub fn remove(&mut self, block_ids: HashSet<BlockId>, cx: &AppContext) {
|
||||
let buffer = self.0.buffer.read(cx);
|
||||
let buffer = buffer.as_snapshot();
|
||||
let wrap_snapshot = &*self.0.wrap_snapshot.lock();
|
||||
let mut edits = Vec::new();
|
||||
let mut last_block_buffer_row = None;
|
||||
self.0.blocks.retain(|block| {
|
||||
if block_ids.contains(&block.id) {
|
||||
let buffer_row = block.position.to_point(buffer).row;
|
||||
let buffer_row = block.position.to_point(&buffer).row;
|
||||
if last_block_buffer_row != Some(buffer_row) {
|
||||
last_block_buffer_row = Some(buffer_row);
|
||||
let start_row = wrap_snapshot
|
||||
@@ -877,7 +883,6 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::display_map::{fold_map::FoldMap, tab_map::TabMap, wrap_map::WrapMap};
|
||||
use gpui::{elements::Empty, Element};
|
||||
use language::Buffer;
|
||||
use rand::prelude::*;
|
||||
use std::env;
|
||||
use text::RandomCharIter;
|
||||
@@ -906,8 +911,9 @@ mod tests {
|
||||
|
||||
let text = "aaa\nbbb\nccc\nddd";
|
||||
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let (fold_map, folds_snapshot) = FoldMap::new(buffer.read(cx).snapshot());
|
||||
let buffer = MultiBuffer::build_simple(text, cx);
|
||||
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
|
||||
let (fold_map, folds_snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
|
||||
let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), 1);
|
||||
let (wrap_map, wraps_snapshot) = WrapMap::new(tabs_snapshot, font_id, 14.0, None, cx);
|
||||
let mut block_map = BlockMap::new(buffer.clone(), wraps_snapshot.clone());
|
||||
@@ -1050,15 +1056,14 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
// Insert a line break, separating two block decorations into separate
|
||||
// lines.
|
||||
let (buffer_snapshot, buffer_edits) = buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
// Insert a line break, separating two block decorations into separate lines.
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit([Point::new(1, 1)..Point::new(1, 1)], "!!!\n", cx);
|
||||
(buffer.snapshot(), buffer.edits_since(&v0).collect())
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
|
||||
let (folds_snapshot, fold_edits) = fold_map.read(buffer_snapshot, buffer_edits);
|
||||
let (folds_snapshot, fold_edits) =
|
||||
fold_map.read(buffer_snapshot, subscription.consume().into_inner());
|
||||
let (tabs_snapshot, tab_edits) = tab_map.sync(folds_snapshot, fold_edits);
|
||||
let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
|
||||
wrap_map.sync(tabs_snapshot, tab_edits, cx)
|
||||
@@ -1077,8 +1082,8 @@ mod tests {
|
||||
|
||||
let text = "one two three\nfour five six\nseven eight";
|
||||
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let (_, folds_snapshot) = FoldMap::new(buffer.read(cx).snapshot());
|
||||
let buffer = MultiBuffer::build_simple(text, cx);
|
||||
let (_, folds_snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
|
||||
let (_, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), 1);
|
||||
let (_, wraps_snapshot) = WrapMap::new(tabs_snapshot, font_id, 14.0, Some(60.), cx);
|
||||
let mut block_map = BlockMap::new(buffer.clone(), wraps_snapshot.clone());
|
||||
@@ -1132,13 +1137,12 @@ mod tests {
|
||||
|
||||
log::info!("Wrap width: {:?}", wrap_width);
|
||||
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
log::info!("initial buffer text: {:?}", text);
|
||||
Buffer::new(0, text, cx)
|
||||
});
|
||||
let mut buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
log::info!("initial buffer text: {:?}", text);
|
||||
let buffer = MultiBuffer::build_simple(&text, cx);
|
||||
|
||||
let mut buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let (fold_map, folds_snapshot) = FoldMap::new(buffer_snapshot.clone());
|
||||
let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
|
||||
let (wrap_map, wraps_snapshot) =
|
||||
@@ -1176,7 +1180,7 @@ mod tests {
|
||||
log::info!(
|
||||
"inserting block {:?} {:?} with height {}",
|
||||
disposition,
|
||||
position.to_point(buffer),
|
||||
position.to_point(&buffer.as_snapshot()),
|
||||
height
|
||||
);
|
||||
BlockProperties {
|
||||
@@ -1221,12 +1225,12 @@ mod tests {
|
||||
}
|
||||
_ => {
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let edit_count = rng.gen_range(1..=5);
|
||||
let subscription = buffer.subscribe();
|
||||
buffer.randomly_edit(&mut rng, edit_count, cx);
|
||||
log::info!("buffer text: {:?}", buffer.text());
|
||||
buffer_edits.extend(buffer.edits_since(&v0));
|
||||
buffer_snapshot = buffer.snapshot();
|
||||
buffer_edits.extend(subscription.consume());
|
||||
buffer_snapshot = buffer.snapshot(cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1248,7 +1252,7 @@ mod tests {
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|(id, block)| {
|
||||
let mut position = block.position.to_point(buffer);
|
||||
let mut position = block.position.to_point(&buffer.as_snapshot());
|
||||
let column = wraps_snapshot.from_point(position, Bias::Left).column();
|
||||
match block.disposition {
|
||||
BlockDisposition::Above => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use language::{
|
||||
Anchor, AnchorRangeExt, BufferSnapshot, Chunk, Edit, Point, PointUtf16, TextSummary, ToOffset,
|
||||
multi_buffer::{Anchor, AnchorRangeExt, MultiBufferChunks, MultiBufferSnapshot, ToOffset},
|
||||
Chunk, Edit, Point, PointUtf16, TextSummary,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use std::{
|
||||
@@ -189,14 +190,14 @@ impl<'a> FoldMapWriter<'a> {
|
||||
}
|
||||
|
||||
pub struct FoldMap {
|
||||
buffer: Mutex<BufferSnapshot>,
|
||||
buffer: Mutex<MultiBufferSnapshot>,
|
||||
transforms: Mutex<SumTree<Transform>>,
|
||||
folds: SumTree<Fold>,
|
||||
version: AtomicUsize,
|
||||
}
|
||||
|
||||
impl FoldMap {
|
||||
pub fn new(buffer: BufferSnapshot) -> (Self, FoldSnapshot) {
|
||||
pub fn new(buffer: MultiBufferSnapshot) -> (Self, FoldSnapshot) {
|
||||
let this = Self {
|
||||
buffer: Mutex::new(buffer.clone()),
|
||||
folds: Default::default(),
|
||||
@@ -224,7 +225,7 @@ impl FoldMap {
|
||||
|
||||
pub fn read(
|
||||
&self,
|
||||
buffer: BufferSnapshot,
|
||||
buffer: MultiBufferSnapshot,
|
||||
edits: Vec<Edit<usize>>,
|
||||
) -> (FoldSnapshot, Vec<FoldEdit>) {
|
||||
let edits = self.sync(buffer, edits);
|
||||
@@ -240,7 +241,7 @@ impl FoldMap {
|
||||
|
||||
pub fn write(
|
||||
&mut self,
|
||||
buffer: BufferSnapshot,
|
||||
buffer: MultiBufferSnapshot,
|
||||
edits: Vec<Edit<usize>>,
|
||||
) -> (FoldMapWriter, FoldSnapshot, Vec<FoldEdit>) {
|
||||
let (snapshot, edits) = self.read(buffer, edits);
|
||||
@@ -259,7 +260,7 @@ impl FoldMap {
|
||||
|
||||
fn sync(
|
||||
&self,
|
||||
new_buffer: BufferSnapshot,
|
||||
new_buffer: MultiBufferSnapshot,
|
||||
buffer_edits: Vec<text::Edit<usize>>,
|
||||
) -> Vec<FoldEdit> {
|
||||
if buffer_edits.is_empty() {
|
||||
@@ -476,7 +477,7 @@ impl FoldMap {
|
||||
pub struct FoldSnapshot {
|
||||
transforms: SumTree<Transform>,
|
||||
folds: SumTree<Fold>,
|
||||
buffer_snapshot: language::BufferSnapshot,
|
||||
buffer_snapshot: MultiBufferSnapshot,
|
||||
pub version: usize,
|
||||
}
|
||||
|
||||
@@ -699,7 +700,7 @@ impl FoldSnapshot {
|
||||
}
|
||||
|
||||
fn intersecting_folds<'a, T>(
|
||||
buffer: &'a text::BufferSnapshot,
|
||||
buffer: &'a MultiBufferSnapshot,
|
||||
folds: &'a SumTree<Fold>,
|
||||
range: Range<T>,
|
||||
inclusive: bool,
|
||||
@@ -850,9 +851,9 @@ impl Default for FoldSummary {
|
||||
}
|
||||
|
||||
impl sum_tree::Summary for FoldSummary {
|
||||
type Context = text::BufferSnapshot;
|
||||
type Context = MultiBufferSnapshot;
|
||||
|
||||
fn add_summary(&mut self, other: &Self, buffer: &text::BufferSnapshot) {
|
||||
fn add_summary(&mut self, other: &Self, buffer: &MultiBufferSnapshot) {
|
||||
if other.min_start.cmp(&self.min_start, buffer).unwrap() == Ordering::Less {
|
||||
self.min_start = other.min_start.clone();
|
||||
}
|
||||
@@ -876,20 +877,20 @@ impl sum_tree::Summary for FoldSummary {
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, FoldSummary> for Fold {
|
||||
fn add_summary(&mut self, summary: &'a FoldSummary, _: &text::BufferSnapshot) {
|
||||
fn add_summary(&mut self, summary: &'a FoldSummary, _: &MultiBufferSnapshot) {
|
||||
self.0.start = summary.start.clone();
|
||||
self.0.end = summary.end.clone();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold {
|
||||
fn cmp(&self, other: &Self, buffer: &text::BufferSnapshot) -> Ordering {
|
||||
fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering {
|
||||
self.0.cmp(&other.0, buffer).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, FoldSummary> for usize {
|
||||
fn add_summary(&mut self, summary: &'a FoldSummary, _: &text::BufferSnapshot) {
|
||||
fn add_summary(&mut self, summary: &'a FoldSummary, _: &MultiBufferSnapshot) {
|
||||
*self += summary.count;
|
||||
}
|
||||
}
|
||||
@@ -924,7 +925,7 @@ impl<'a> Iterator for FoldBufferRows<'a> {
|
||||
|
||||
pub struct FoldChunks<'a> {
|
||||
transform_cursor: Cursor<'a, Transform, (FoldOffset, usize)>,
|
||||
buffer_chunks: language::BufferChunks<'a>,
|
||||
buffer_chunks: MultiBufferChunks<'a>,
|
||||
buffer_chunk: Option<(usize, Chunk<'a>)>,
|
||||
buffer_offset: usize,
|
||||
output_offset: usize,
|
||||
@@ -1053,7 +1054,7 @@ pub type FoldEdit = Edit<FoldOffset>;
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ToPoint;
|
||||
use language::Buffer;
|
||||
use language::multi_buffer::MultiBuffer;
|
||||
use rand::prelude::*;
|
||||
use std::{env, mem};
|
||||
use text::RandomCharIter;
|
||||
@@ -1062,8 +1063,9 @@ mod tests {
|
||||
|
||||
#[gpui::test]
|
||||
fn test_basic_folds(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(5, 6, 'a'), cx));
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
|
||||
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
|
||||
let (mut writer, _, _) = map.write(buffer_snapshot.clone(), vec![]);
|
||||
@@ -1086,8 +1088,7 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
let (buffer_snapshot, edits) = buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(
|
||||
vec![
|
||||
Point::new(0, 0)..Point::new(0, 1),
|
||||
@@ -1096,9 +1097,10 @@ mod tests {
|
||||
"123",
|
||||
cx,
|
||||
);
|
||||
(buffer.snapshot(), buffer.edits_since(&v0).collect())
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot3, edits) = map.read(buffer_snapshot.clone(), edits);
|
||||
let (snapshot3, edits) =
|
||||
map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
assert_eq!(snapshot3.text(), "123a…c123c…eeeee");
|
||||
assert_eq!(
|
||||
edits,
|
||||
@@ -1114,12 +1116,11 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
let (buffer_snapshot, edits) = buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![Point::new(2, 6)..Point::new(4, 3)], "456", cx);
|
||||
(buffer.snapshot(), buffer.edits_since(&v0).collect())
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot4, _) = map.read(buffer_snapshot.clone(), edits);
|
||||
let (snapshot4, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
assert_eq!(snapshot4.text(), "123a…c123456eee");
|
||||
|
||||
let (mut writer, _, _) = map.write(buffer_snapshot.clone(), vec![]);
|
||||
@@ -1130,8 +1131,9 @@ mod tests {
|
||||
|
||||
#[gpui::test]
|
||||
fn test_adjacent_folds(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, "abcdefghijkl", cx));
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer = MultiBuffer::build_simple("abcdefghijkl", cx);
|
||||
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
|
||||
{
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
@@ -1164,20 +1166,20 @@ mod tests {
|
||||
assert_eq!(snapshot.text(), "…fghijkl");
|
||||
|
||||
// Edit within one of the folds.
|
||||
let (buffer_snapshot, edits) = buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(vec![0..1], "12345", cx);
|
||||
(buffer.snapshot(), buffer.edits_since(&v0).collect())
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), edits);
|
||||
let (snapshot, _) =
|
||||
map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
assert_eq!(snapshot.text(), "12345…fghijkl");
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn test_overlapping_folds(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(5, 6, 'a'), cx));
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
let (mut writer, _, _) = map.write(buffer_snapshot.clone(), vec![]);
|
||||
writer.fold(vec![
|
||||
@@ -1192,8 +1194,9 @@ mod tests {
|
||||
|
||||
#[gpui::test]
|
||||
fn test_merging_folds_via_edit(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(5, 6, 'a'), cx));
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
|
||||
let subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
|
||||
let (mut writer, _, _) = map.write(buffer_snapshot.clone(), vec![]);
|
||||
@@ -1204,19 +1207,18 @@ mod tests {
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), vec![]);
|
||||
assert_eq!(snapshot.text(), "aa…cccc\nd…eeeee");
|
||||
|
||||
let (buffer_snapshot, edits) = buffer.update(cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let buffer_snapshot = buffer.update(cx, |buffer, cx| {
|
||||
buffer.edit(Some(Point::new(2, 2)..Point::new(3, 1)), "", cx);
|
||||
(buffer.snapshot(), buffer.edits_since(&v0).collect())
|
||||
buffer.snapshot(cx)
|
||||
});
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), edits);
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), subscription.consume().into_inner());
|
||||
assert_eq!(snapshot.text(), "aa…eeeee");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn test_folds_in_range(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(5, 6, 'a'), cx));
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(5, 6, 'a'), cx);
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
let buffer = buffer.read(cx);
|
||||
|
||||
@@ -1230,7 +1232,9 @@ mod tests {
|
||||
let (snapshot, _) = map.read(buffer_snapshot.clone(), vec![]);
|
||||
let fold_ranges = snapshot
|
||||
.folds_in_range(Point::new(1, 0)..Point::new(1, 3))
|
||||
.map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer))
|
||||
.map(|fold| {
|
||||
fold.start.to_point(&buffer.as_snapshot())..fold.end.to_point(&buffer.as_snapshot())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
fold_ranges,
|
||||
@@ -1247,12 +1251,10 @@ mod tests {
|
||||
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
|
||||
.unwrap_or(10);
|
||||
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
Buffer::new(0, text, cx)
|
||||
});
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
let buffer = MultiBuffer::build_simple(&text, cx);
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
|
||||
let (mut initial_snapshot, _) = map.read(buffer_snapshot.clone(), vec![]);
|
||||
@@ -1260,23 +1262,21 @@ mod tests {
|
||||
|
||||
for _ in 0..operations {
|
||||
log::info!("text: {:?}", buffer.read(cx).text());
|
||||
let buffer_edits = match rng.gen_range(0..=100) {
|
||||
let mut buffer_edits = Vec::new();
|
||||
match rng.gen_range(0..=100) {
|
||||
0..=59 => {
|
||||
snapshot_edits.extend(map.randomly_mutate(&mut rng));
|
||||
vec![]
|
||||
}
|
||||
_ => buffer.update(cx, |buffer, cx| {
|
||||
let start_version = buffer.version.clone();
|
||||
let subscription = buffer.subscribe();
|
||||
let edit_count = rng.gen_range(1..=5);
|
||||
buffer.randomly_edit(&mut rng, edit_count, cx);
|
||||
let edits = buffer
|
||||
.edits_since::<Point>(&start_version)
|
||||
.collect::<Vec<_>>();
|
||||
let edits = subscription.consume().into_inner();
|
||||
log::info!("editing {:?}", edits);
|
||||
buffer.edits_since::<usize>(&start_version).collect()
|
||||
buffer_edits.extend(edits);
|
||||
}),
|
||||
};
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
|
||||
let (snapshot, edits) = map.read(buffer_snapshot.clone(), buffer_edits);
|
||||
snapshot_edits.push((snapshot.clone(), edits));
|
||||
@@ -1285,8 +1285,8 @@ mod tests {
|
||||
let mut expected_buffer_rows = Vec::new();
|
||||
let mut next_row = buffer_snapshot.max_point().row;
|
||||
for fold_range in map.merged_fold_ranges().into_iter().rev() {
|
||||
let fold_start = buffer_snapshot.point_for_offset(fold_range.start).unwrap();
|
||||
let fold_end = buffer_snapshot.point_for_offset(fold_range.end).unwrap();
|
||||
let fold_start = buffer_snapshot.offset_to_point(fold_range.start);
|
||||
let fold_end = buffer_snapshot.offset_to_point(fold_range.end);
|
||||
expected_buffer_rows.extend((fold_end.row + 1..=next_row).rev());
|
||||
next_row = fold_start.row;
|
||||
|
||||
@@ -1458,9 +1458,9 @@ mod tests {
|
||||
#[gpui::test]
|
||||
fn test_buffer_rows(cx: &mut gpui::MutableAppContext) {
|
||||
let text = sample_text(6, 6, 'a') + "\n";
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
|
||||
let buffer = MultiBuffer::build_simple(&text, cx);
|
||||
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
let mut map = FoldMap::new(buffer_snapshot.clone()).0;
|
||||
|
||||
let (mut writer, _, _) = map.write(buffer_snapshot.clone(), vec![]);
|
||||
|
||||
@@ -435,7 +435,7 @@ impl<'a> Iterator for TabChunks<'a> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::display_map::fold_map::FoldMap;
|
||||
use language::Buffer;
|
||||
use language::multi_buffer::MultiBuffer;
|
||||
use rand::{prelude::StdRng, Rng};
|
||||
use text::{RandomCharIter, Rope};
|
||||
|
||||
@@ -449,12 +449,10 @@ mod tests {
|
||||
#[gpui::test(iterations = 100)]
|
||||
fn test_random(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
|
||||
let tab_size = rng.gen_range(1..=4);
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let len = rng.gen_range(0..30);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
Buffer::new(0, text, cx)
|
||||
});
|
||||
let buffer_snapshot = buffer.read(cx).snapshot();
|
||||
let len = rng.gen_range(0..30);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
let buffer = MultiBuffer::build_simple(&text, cx);
|
||||
let buffer_snapshot = buffer.read(cx).snapshot(cx);
|
||||
log::info!("Buffer text: {:?}", buffer.read(cx).text());
|
||||
|
||||
let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
|
||||
|
||||
@@ -974,7 +974,7 @@ mod tests {
|
||||
display_map::{fold_map::FoldMap, tab_map::TabMap},
|
||||
test::Observer,
|
||||
};
|
||||
use language::{Buffer, RandomCharIter};
|
||||
use language::{multi_buffer::MultiBuffer, RandomCharIter};
|
||||
use rand::prelude::*;
|
||||
use std::{cmp, env};
|
||||
use text::Rope;
|
||||
@@ -1004,12 +1004,12 @@ mod tests {
|
||||
log::info!("Tab size: {}", tab_size);
|
||||
log::info!("Wrap width: {:?}", wrap_width);
|
||||
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let buffer = cx.update(|cx| {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
Buffer::new(0, text, cx)
|
||||
MultiBuffer::build_simple(&text, cx)
|
||||
});
|
||||
let buffer_snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
|
||||
let buffer_snapshot = buffer.read_with(&cx, |buffer, cx| buffer.snapshot(cx));
|
||||
let (mut fold_map, folds_snapshot) = FoldMap::new(buffer_snapshot.clone());
|
||||
let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
|
||||
log::info!(
|
||||
@@ -1074,15 +1074,15 @@ mod tests {
|
||||
}
|
||||
_ => {
|
||||
buffer.update(&mut cx, |buffer, cx| {
|
||||
let v0 = buffer.version();
|
||||
let subscription = buffer.subscribe();
|
||||
let edit_count = rng.gen_range(1..=5);
|
||||
buffer.randomly_edit(&mut rng, edit_count, cx);
|
||||
buffer_edits.extend(buffer.edits_since(&v0));
|
||||
buffer_edits.extend(subscription.consume());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let buffer_snapshot = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
|
||||
let buffer_snapshot = buffer.read_with(&cx, |buffer, cx| buffer.snapshot(cx));
|
||||
log::info!("Unwrapped text (no folds): {:?}", buffer_snapshot.text());
|
||||
let (folds_snapshot, fold_edits) = fold_map.read(buffer_snapshot, buffer_edits);
|
||||
log::info!(
|
||||
|
||||
+261
-219
File diff suppressed because it is too large
Load Diff
@@ -19,7 +19,7 @@ use gpui::{
|
||||
MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
|
||||
};
|
||||
use json::json;
|
||||
use language::{Chunk, ToPoint};
|
||||
use language::{multi_buffer::ToPoint, Chunk};
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
cmp::{self, Ordering},
|
||||
@@ -738,13 +738,11 @@ impl Element for EditorElement {
|
||||
self.update_view(cx.app, |view, cx| {
|
||||
highlighted_row = view.highlighted_row();
|
||||
for selection_set_id in view.active_selection_sets(cx).collect::<Vec<_>>() {
|
||||
let replica_selections = view
|
||||
.intersecting_selections(
|
||||
selection_set_id,
|
||||
DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
|
||||
cx,
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
let replica_selections = view.intersecting_selections(
|
||||
selection_set_id,
|
||||
DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
|
||||
cx,
|
||||
);
|
||||
for selection in &replica_selections {
|
||||
if selection_set_id == view.selection_set_id {
|
||||
let is_empty = selection.start == selection.end;
|
||||
@@ -1165,14 +1163,13 @@ fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Editor, EditorSettings};
|
||||
use language::Buffer;
|
||||
use language::{MultiBuffer};
|
||||
use util::test::sample_text;
|
||||
|
||||
#[gpui::test]
|
||||
fn test_layout_line_numbers(cx: &mut gpui::MutableAppContext) {
|
||||
let settings = EditorSettings::test(cx);
|
||||
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'a'), cx));
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(6, 6, 'a'), cx);
|
||||
let (window_id, editor) = cx.add_window(Default::default(), |cx| {
|
||||
Editor::for_buffer(
|
||||
buffer,
|
||||
|
||||
+24
-14
@@ -5,12 +5,15 @@ use gpui::{
|
||||
MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ViewHandle,
|
||||
WeakModelHandle,
|
||||
};
|
||||
use language::{Buffer, Diagnostic, File as _};
|
||||
use language::{
|
||||
multi_buffer::{MultiBuffer, ToPoint as _},
|
||||
Diagnostic, File as _,
|
||||
};
|
||||
use postage::watch;
|
||||
use project::{ProjectPath, Worktree};
|
||||
use std::fmt::Write;
|
||||
use std::path::Path;
|
||||
use text::{Point, Selection, ToPoint};
|
||||
use text::{Point, Selection};
|
||||
use workspace::{
|
||||
settings, EntryOpener, ItemHandle, ItemView, ItemViewHandle, Settings, StatusItemView,
|
||||
WeakItemHandle,
|
||||
@@ -19,10 +22,10 @@ use workspace::{
|
||||
pub struct BufferOpener;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BufferItemHandle(pub ModelHandle<Buffer>);
|
||||
pub struct BufferItemHandle(pub ModelHandle<MultiBuffer>);
|
||||
|
||||
#[derive(Clone)]
|
||||
struct WeakBufferItemHandle(WeakModelHandle<Buffer>);
|
||||
struct WeakBufferItemHandle(WeakModelHandle<MultiBuffer>);
|
||||
|
||||
impl EntryOpener for BufferOpener {
|
||||
fn open(
|
||||
@@ -32,10 +35,10 @@ impl EntryOpener for BufferOpener {
|
||||
cx: &mut ModelContext<Worktree>,
|
||||
) -> Option<Task<Result<Box<dyn ItemHandle>>>> {
|
||||
let buffer = worktree.open_buffer(project_path.path, cx);
|
||||
let task = cx.spawn(|_, _| async move {
|
||||
buffer
|
||||
.await
|
||||
.map(|buffer| Box::new(BufferItemHandle(buffer)) as Box<dyn ItemHandle>)
|
||||
let task = cx.spawn(|_, mut cx| async move {
|
||||
let buffer = buffer.await?;
|
||||
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
||||
Ok(Box::new(BufferItemHandle(buffer)) as Box<dyn ItemHandle>)
|
||||
});
|
||||
Some(task)
|
||||
}
|
||||
@@ -102,7 +105,7 @@ impl ItemHandle for BufferItemHandle {
|
||||
}
|
||||
|
||||
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
|
||||
self.0.read(cx).file().map(|f| ProjectPath {
|
||||
self.0.read(cx).file(cx).map(|f| ProjectPath {
|
||||
worktree_id: f.worktree_id(),
|
||||
path: f.path().clone(),
|
||||
})
|
||||
@@ -137,7 +140,7 @@ impl ItemView for Editor {
|
||||
let filename = self
|
||||
.buffer()
|
||||
.read(cx)
|
||||
.file()
|
||||
.file(cx)
|
||||
.and_then(|file| file.file_name());
|
||||
if let Some(name) = filename {
|
||||
name.to_string_lossy().into()
|
||||
@@ -147,7 +150,7 @@ impl ItemView for Editor {
|
||||
}
|
||||
|
||||
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
|
||||
self.buffer().read(cx).file().map(|file| ProjectPath {
|
||||
self.buffer().read(cx).file(cx).map(|file| ProjectPath {
|
||||
worktree_id: file.worktree_id(),
|
||||
path: file.path().clone(),
|
||||
})
|
||||
@@ -174,7 +177,14 @@ impl ItemView for Editor {
|
||||
path: &Path,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
self.buffer().update(cx, |buffer, cx| {
|
||||
let buffer = self
|
||||
.buffer()
|
||||
.read(cx)
|
||||
.as_singleton()
|
||||
.expect("cannot call save_as on an excerpt list")
|
||||
.clone();
|
||||
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
let handle = cx.handle();
|
||||
let text = buffer.as_rope().clone();
|
||||
let version = buffer.version();
|
||||
@@ -237,7 +247,7 @@ impl CursorPosition {
|
||||
|
||||
fn update_position(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
|
||||
let editor = editor.read(cx);
|
||||
let buffer = editor.buffer().read(cx);
|
||||
let buffer = editor.buffer().read(cx).snapshot(cx);
|
||||
|
||||
self.selected_count = 0;
|
||||
let mut last_selection: Option<Selection<usize>> = None;
|
||||
@@ -250,7 +260,7 @@ impl CursorPosition {
|
||||
last_selection = Some(selection);
|
||||
}
|
||||
}
|
||||
self.position = last_selection.map(|s| s.head().to_point(buffer));
|
||||
self.position = last_selection.map(|s| s.head().to_point(&buffer));
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
|
||||
use anyhow::Result;
|
||||
use language::multi_buffer::ToPoint;
|
||||
use std::{cmp, ops::Range};
|
||||
use text::ToPoint;
|
||||
|
||||
pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> Result<DisplayPoint> {
|
||||
if point.column() > 0 {
|
||||
@@ -244,7 +244,8 @@ fn char_kind(c: char) -> CharKind {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{display_map::DisplayMap, Buffer};
|
||||
use crate::display_map::DisplayMap;
|
||||
use language::MultiBuffer;
|
||||
|
||||
#[gpui::test]
|
||||
fn test_prev_next_word_boundary_multibyte(cx: &mut gpui::MutableAppContext) {
|
||||
@@ -256,7 +257,7 @@ mod tests {
|
||||
.unwrap();
|
||||
let font_size = 14.0;
|
||||
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, "a bcΔ defγ hi—jk", cx));
|
||||
let buffer = MultiBuffer::build_simple("a bcΔ defγ hi—jk", cx);
|
||||
let display_map =
|
||||
cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
|
||||
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||
@@ -312,7 +313,7 @@ mod tests {
|
||||
.select_font(family_id, &Default::default())
|
||||
.unwrap();
|
||||
let font_size = 14.0;
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, "lorem ipsum dolor\n sit", cx));
|
||||
let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit", cx);
|
||||
let display_map =
|
||||
cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
|
||||
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||
|
||||
Reference in New Issue
Block a user