mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-03 17:44:30 +00:00
WIP
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
b2caf9e905
commit
59bbe43a46
3 changed files with 91 additions and 602 deletions
|
@ -1,6 +1,5 @@
|
|||
mod block_map;
|
||||
mod fold_map;
|
||||
mod injection_map;
|
||||
mod tab_map;
|
||||
mod wrap_map;
|
||||
|
||||
|
@ -53,7 +52,7 @@ impl DisplayMap {
|
|||
pub fn snapshot(&self, cx: &mut ModelContext<Self>) -> DisplayMapSnapshot {
|
||||
let (folds_snapshot, edits) = self.fold_map.read(cx);
|
||||
let (tabs_snapshot, edits) = self.tab_map.sync(folds_snapshot.clone(), edits);
|
||||
let wraps_snapshot = self
|
||||
let (wraps_snapshot, _) = self
|
||||
.wrap_map
|
||||
.update(cx, |map, cx| map.sync(tabs_snapshot.clone(), edits, cx));
|
||||
DisplayMapSnapshot {
|
||||
|
|
|
@ -1,586 +0,0 @@
|
|||
use std::{
|
||||
cmp::{self, Ordering},
|
||||
collections::BTreeMap,
|
||||
mem,
|
||||
sync::atomic::{AtomicUsize, Ordering::SeqCst},
|
||||
};
|
||||
|
||||
use buffer::{Anchor, Bias, Edit, Point, Rope, TextSummary, ToOffset, ToPoint};
|
||||
use gpui::{fonts::HighlightStyle, AppContext, ModelHandle};
|
||||
use language::Buffer;
|
||||
use parking_lot::Mutex;
|
||||
use sum_tree::SumTree;
|
||||
use util::post_inc;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
|
||||
pub struct InjectionId(usize);
|
||||
|
||||
pub struct InjectionMap {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
transforms: Mutex<SumTree<Transform>>,
|
||||
injections: SumTree<Injection>,
|
||||
injection_sites: SumTree<InjectionSite>,
|
||||
version: AtomicUsize,
|
||||
last_sync: Mutex<SyncState>,
|
||||
next_injection_id: usize,
|
||||
}
|
||||
|
||||
pub struct Snapshot {
|
||||
transforms: SumTree<Transform>,
|
||||
injections: SumTree<Injection>,
|
||||
buffer_snapshot: language::Snapshot,
|
||||
pub version: usize,
|
||||
}
|
||||
|
||||
pub struct InjectionMapWriter<'a>(&'a mut InjectionMap);
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SyncState {
|
||||
version: clock::Global,
|
||||
parse_count: usize,
|
||||
diagnostics_update_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct InjectionSummary {
|
||||
min_id: InjectionId,
|
||||
max_id: InjectionId,
|
||||
min_position: Anchor,
|
||||
max_position: Anchor,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Injection {
|
||||
id: InjectionId,
|
||||
text: Rope,
|
||||
runs: Vec<(usize, HighlightStyle)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct InjectionProps {
|
||||
text: Rope,
|
||||
runs: Vec<(usize, HighlightStyle)>,
|
||||
disposition: Disposition,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Disposition {
|
||||
BeforeLine,
|
||||
AfterLine,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct InjectionSite {
|
||||
injection_id: InjectionId,
|
||||
position: Anchor,
|
||||
disposition: Disposition,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct InjectionSitePosition(Anchor);
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
struct InjectionSiteSummary {
|
||||
min_injection_id: InjectionId,
|
||||
max_injection_id: InjectionId,
|
||||
min_position: Anchor,
|
||||
max_position: Anchor,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
struct Transform {
|
||||
input: TextSummary,
|
||||
output: TextSummary,
|
||||
injection_id: Option<InjectionId>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
struct TransformSummary {
|
||||
input: TextSummary,
|
||||
output: TextSummary,
|
||||
min_injection_id: InjectionId,
|
||||
max_injection_id: InjectionId,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
pub struct InjectionOffset(usize);
|
||||
|
||||
impl sum_tree::Summary for InjectionId {
|
||||
type Context = ();
|
||||
|
||||
fn add_summary(&mut self, summary: &Self, cx: &Self::Context) {
|
||||
*self = *summary
|
||||
}
|
||||
}
|
||||
|
||||
impl InjectionMap {
|
||||
pub fn new(buffer_handle: ModelHandle<Buffer>, cx: &AppContext) -> (Self, Snapshot) {
|
||||
let buffer = buffer_handle.read(cx);
|
||||
let this = Self {
|
||||
buffer: buffer_handle,
|
||||
injections: Default::default(),
|
||||
injection_sites: Default::default(),
|
||||
transforms: Mutex::new(SumTree::from_item(
|
||||
Transform::isomorphic(buffer.text_summary()),
|
||||
&(),
|
||||
)),
|
||||
last_sync: Mutex::new(SyncState {
|
||||
version: buffer.version(),
|
||||
parse_count: buffer.parse_count(),
|
||||
diagnostics_update_count: buffer.diagnostics_update_count(),
|
||||
}),
|
||||
version: AtomicUsize::new(0),
|
||||
next_injection_id: 0,
|
||||
};
|
||||
let (snapshot, _) = this.read(cx);
|
||||
(this, snapshot)
|
||||
}
|
||||
|
||||
pub fn read(&self, cx: &AppContext) -> (Snapshot, Vec<Edit<InjectionOffset>>) {
|
||||
let edits = self.sync(cx);
|
||||
// self.check_invariants(cx);
|
||||
let snapshot = Snapshot {
|
||||
transforms: self.transforms.lock().clone(),
|
||||
injections: self.injections.clone(),
|
||||
buffer_snapshot: self.buffer.read(cx).snapshot(),
|
||||
version: self.version.load(SeqCst),
|
||||
};
|
||||
(snapshot, edits)
|
||||
}
|
||||
|
||||
pub fn write(
|
||||
&mut self,
|
||||
cx: &AppContext,
|
||||
) -> (InjectionMapWriter, Snapshot, Vec<Edit<InjectionOffset>>) {
|
||||
let (snapshot, edits) = self.read(cx);
|
||||
(InjectionMapWriter(self), snapshot, edits)
|
||||
}
|
||||
|
||||
fn sync(&self, cx: &AppContext) -> Vec<Edit<InjectionOffset>> {
|
||||
let buffer = self.buffer.read(cx);
|
||||
let last_sync = mem::replace(
|
||||
&mut *self.last_sync.lock(),
|
||||
SyncState {
|
||||
version: buffer.version(),
|
||||
parse_count: buffer.parse_count(),
|
||||
diagnostics_update_count: buffer.diagnostics_update_count(),
|
||||
},
|
||||
);
|
||||
let edits = buffer
|
||||
.edits_since(&last_sync.version)
|
||||
.map(Into::into)
|
||||
.collect::<Vec<_>>();
|
||||
if edits.is_empty() {
|
||||
if last_sync.parse_count != buffer.parse_count()
|
||||
|| last_sync.diagnostics_update_count != buffer.diagnostics_update_count()
|
||||
{
|
||||
self.version.fetch_add(1, SeqCst);
|
||||
}
|
||||
Vec::new()
|
||||
} else {
|
||||
self.apply_edits(edits, cx)
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_edits(
|
||||
&self,
|
||||
buffer_edits: Vec<buffer::Edit<Point>>,
|
||||
cx: &AppContext,
|
||||
) -> Vec<Edit<InjectionOffset>> {
|
||||
let buffer = self.buffer.read(cx);
|
||||
let mut buffer_edits_iter = buffer_edits.iter().cloned().peekable();
|
||||
|
||||
let mut new_transforms = SumTree::<Transform>::new();
|
||||
let mut transforms = self.transforms.lock();
|
||||
let old_max_point = transforms.summary().input.lines;
|
||||
let new_max_point = buffer.max_point();
|
||||
let mut cursor = transforms.cursor::<Point>();
|
||||
let mut injection_sites = self.injection_sites.cursor::<InjectionSitePosition>();
|
||||
let mut pending_after_injections: Vec<InjectionId> = Vec::new();
|
||||
|
||||
while let Some(mut edit) = buffer_edits_iter.next() {
|
||||
dbg!(&edit);
|
||||
// Expand this edit to line boundaries.
|
||||
edit.old.start.column = 0;
|
||||
edit.old.end += Point::new(1, 0);
|
||||
edit.new.start.column = 0;
|
||||
edit.new.end += Point::new(1, 0);
|
||||
|
||||
// Push any transforms preceding the edit.
|
||||
new_transforms.push_tree(cursor.slice(&edit.old.start, Bias::Left, &()), &());
|
||||
|
||||
// Snap edits to row boundaries of intersecting transforms.
|
||||
loop {
|
||||
if cmp::min(edit.old.end, old_max_point) <= cursor.end(&()) {
|
||||
cursor.seek(&edit.old.end, Bias::Left, &());
|
||||
cursor.next(&());
|
||||
let new_old_end = *cursor.start() + Point::new(1, 0);
|
||||
edit.new.end += new_old_end - edit.old.end;
|
||||
edit.old.end = new_old_end;
|
||||
}
|
||||
|
||||
if buffer_edits_iter.peek().map_or(false, |next_edit| {
|
||||
edit.old.end.row >= next_edit.old.start.row
|
||||
}) {
|
||||
let next_edit = buffer_edits_iter.next().unwrap();
|
||||
edit.old.end = cmp::max(edit.old.end, next_edit.old.end + Point::new(1, 0));
|
||||
let row_delta = (next_edit.new.end.row as i32 - next_edit.new.start.row as i32)
|
||||
- (next_edit.old.end.row as i32 - next_edit.old.start.row as i32);
|
||||
edit.new.end.row = (edit.new.end.row as i32 + row_delta) as u32;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dbg!(&edit);
|
||||
|
||||
// Find and insert all injections on the lines spanned by the edit, interleaved with isomorphic regions
|
||||
injection_sites.seek(
|
||||
&InjectionSitePosition(buffer.anchor_before(edit.new.start)),
|
||||
Bias::Right,
|
||||
buffer,
|
||||
);
|
||||
let mut last_injection_row: Option<u32> = None;
|
||||
while let Some(site) = injection_sites.item() {
|
||||
let injection_row = site.position.to_point(buffer).row;
|
||||
|
||||
if injection_row > edit.new.end.row {
|
||||
break;
|
||||
}
|
||||
|
||||
// If we've moved on to a new injection row, ensure that any pending injections with an after
|
||||
// disposition are inserted after their target row
|
||||
if let Some(last_injection_row) = last_injection_row {
|
||||
if injection_row != last_injection_row {
|
||||
let injection_point = Point::new(last_injection_row + 1, 0);
|
||||
if injection_point > new_transforms.summary().input.lines {
|
||||
let injection_offset = injection_point.to_offset(buffer);
|
||||
new_transforms.push(
|
||||
Transform::isomorphic(buffer.text_summary_for_range(
|
||||
new_transforms.summary().input.bytes..injection_offset,
|
||||
)),
|
||||
&(),
|
||||
);
|
||||
}
|
||||
for injection_id in pending_after_injections.drain(..) {
|
||||
new_transforms.push(
|
||||
Transform::for_injection(
|
||||
self.injections.get(&injection_id, &()).unwrap(),
|
||||
),
|
||||
&(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match site.disposition {
|
||||
Disposition::AfterLine => pending_after_injections.push(site.injection_id),
|
||||
Disposition::BeforeLine => {
|
||||
let injection_point = Point::new(injection_row, 0);
|
||||
if injection_point > new_transforms.summary().input.lines {
|
||||
let injection_offset = injection_point.to_offset(buffer);
|
||||
new_transforms.push(
|
||||
Transform::isomorphic(buffer.text_summary_for_range(
|
||||
new_transforms.summary().input.bytes..injection_offset,
|
||||
)),
|
||||
&(),
|
||||
);
|
||||
}
|
||||
new_transforms.push(
|
||||
Transform::for_injection(
|
||||
self.injections.get(&site.injection_id, &()).unwrap(),
|
||||
),
|
||||
&(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
last_injection_row = Some(injection_row);
|
||||
}
|
||||
|
||||
if let Some(last_injection_row) = last_injection_row {
|
||||
let injection_point = Point::new(last_injection_row + 1, 0);
|
||||
if injection_point > new_transforms.summary().input.lines {
|
||||
let injection_offset = injection_point.to_offset(buffer);
|
||||
new_transforms.push(
|
||||
Transform::isomorphic(buffer.text_summary_for_range(
|
||||
new_transforms.summary().input.bytes..injection_offset,
|
||||
)),
|
||||
&(),
|
||||
);
|
||||
}
|
||||
for injection_id in pending_after_injections.drain(..) {
|
||||
new_transforms.push(
|
||||
Transform::for_injection(self.injections.get(&injection_id, &()).unwrap()),
|
||||
&(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let sum = new_transforms.summary();
|
||||
let new_end = cmp::min(edit.new.end, new_max_point);
|
||||
if sum.input.lines < new_end {
|
||||
let text_summary =
|
||||
buffer.text_summary_for_range(sum.input.bytes..new_end.to_offset(buffer));
|
||||
new_transforms.push(Transform::isomorphic(text_summary), &());
|
||||
}
|
||||
}
|
||||
new_transforms.push_tree(cursor.suffix(&()), &());
|
||||
drop(cursor);
|
||||
|
||||
*transforms = new_transforms;
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> InjectionMapWriter<'a> {
|
||||
pub fn insert<'b, T, U>(
|
||||
&mut self,
|
||||
injections: T,
|
||||
cx: &AppContext,
|
||||
) -> (Vec<InjectionId>, Snapshot, Vec<Edit<InjectionOffset>>)
|
||||
where
|
||||
T: IntoIterator<Item = (Anchor, InjectionProps)>,
|
||||
{
|
||||
let buffer = self.0.buffer.read(cx);
|
||||
let mut cursor = self.0.injection_sites.cursor::<InjectionSitePosition>();
|
||||
let mut new_sites = SumTree::new();
|
||||
let mut injection_ids = Vec::new();
|
||||
let mut edits = Vec::new();
|
||||
|
||||
for (position, props) in injections {
|
||||
let point = position.to_point(buffer);
|
||||
edits.push(Edit {
|
||||
old: point..point,
|
||||
new: point..point,
|
||||
});
|
||||
|
||||
let id = InjectionId(post_inc(&mut self.0.next_injection_id));
|
||||
injection_ids.push(id);
|
||||
new_sites.push_tree(
|
||||
cursor.slice(
|
||||
&InjectionSitePosition(position.clone()),
|
||||
Bias::Right,
|
||||
buffer,
|
||||
),
|
||||
buffer,
|
||||
);
|
||||
new_sites.push(
|
||||
InjectionSite {
|
||||
injection_id: id,
|
||||
position,
|
||||
disposition: props.disposition,
|
||||
},
|
||||
buffer,
|
||||
);
|
||||
self.0.injections.push(
|
||||
Injection {
|
||||
id,
|
||||
text: props.text,
|
||||
runs: props.runs,
|
||||
},
|
||||
&(),
|
||||
);
|
||||
}
|
||||
new_sites.push_tree(cursor.suffix(buffer), buffer);
|
||||
|
||||
drop(cursor);
|
||||
self.0.injection_sites = new_sites;
|
||||
|
||||
let edits = self.0.apply_edits(edits, cx);
|
||||
let snapshot = Snapshot {
|
||||
transforms: self.0.transforms.lock().clone(),
|
||||
injections: self.0.injections.clone(),
|
||||
buffer_snapshot: buffer.snapshot(),
|
||||
version: self.0.version.load(SeqCst),
|
||||
};
|
||||
|
||||
(injection_ids, snapshot, edits)
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Item for Injection {
|
||||
type Summary = InjectionId;
|
||||
|
||||
fn summary(&self) -> Self::Summary {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::KeyedItem for Injection {
|
||||
type Key = InjectionId;
|
||||
|
||||
fn key(&self) -> Self::Key {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Item for InjectionSite {
|
||||
type Summary = InjectionSiteSummary;
|
||||
|
||||
fn summary(&self) -> Self::Summary {
|
||||
InjectionSiteSummary {
|
||||
min_injection_id: self.injection_id,
|
||||
max_injection_id: self.injection_id,
|
||||
min_position: self.position.clone(),
|
||||
max_position: self.position.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InjectionSitePosition {
|
||||
fn default() -> Self {
|
||||
Self(Anchor::min())
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Summary for InjectionSiteSummary {
|
||||
type Context = buffer::Buffer;
|
||||
|
||||
fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
|
||||
self.min_injection_id = cmp::min(self.min_injection_id, summary.min_injection_id);
|
||||
self.max_injection_id = cmp::max(self.max_injection_id, summary.max_injection_id);
|
||||
self.max_position = summary.max_position.clone();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, InjectionSiteSummary> for InjectionSitePosition {
|
||||
fn add_summary(&mut self, summary: &'a InjectionSiteSummary, _: &buffer::Buffer) {
|
||||
self.0 = summary.max_position.clone();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::SeekTarget<'a, InjectionSiteSummary, Self> for InjectionSitePosition {
|
||||
fn cmp(&self, cursor_location: &Self, snapshot: &buffer::Buffer) -> Ordering {
|
||||
self.0.cmp(&cursor_location.0, snapshot).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InjectionSiteSummary {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
min_injection_id: InjectionId(usize::MAX),
|
||||
max_injection_id: InjectionId(0),
|
||||
min_position: Anchor::max(),
|
||||
max_position: Anchor::min(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
fn isomorphic(text_summary: TextSummary) -> Self {
|
||||
Self {
|
||||
input: text_summary.clone(),
|
||||
output: text_summary,
|
||||
injection_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn for_injection(injection: &Injection) -> Self {
|
||||
Self {
|
||||
input: Default::default(),
|
||||
output: injection.text.summary(),
|
||||
injection_id: Some(injection.id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Item for Transform {
|
||||
type Summary = TransformSummary;
|
||||
|
||||
fn summary(&self) -> Self::Summary {
|
||||
let min_injection_id;
|
||||
let max_injection_id;
|
||||
if let Some(id) = self.injection_id {
|
||||
min_injection_id = id;
|
||||
max_injection_id = id;
|
||||
} else {
|
||||
min_injection_id = InjectionId(usize::MAX);
|
||||
max_injection_id = InjectionId(0);
|
||||
}
|
||||
|
||||
TransformSummary {
|
||||
input: self.input.clone(),
|
||||
output: self.output.clone(),
|
||||
min_injection_id,
|
||||
max_injection_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Summary for TransformSummary {
|
||||
type Context = ();
|
||||
|
||||
fn add_summary(&mut self, other: &Self, _: &()) {
|
||||
self.input += &other.input;
|
||||
self.output += &other.output;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, TransformSummary> for usize {
|
||||
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
|
||||
*self += summary.input.bytes
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, TransformSummary> for Point {
|
||||
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
|
||||
*self += summary.input.lines
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, TransformSummary> for InjectionOffset {
|
||||
fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
|
||||
self.0 += summary.output.bytes
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::env;
|
||||
|
||||
use super::*;
|
||||
use buffer::RandomCharIter;
|
||||
use rand::prelude::*;
|
||||
|
||||
#[gpui::test(iterations = 1000)]
|
||||
fn test_random(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
|
||||
let operations = env::var("OPERATIONS")
|
||||
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
|
||||
.unwrap_or(1);
|
||||
|
||||
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 (map, initial_snapshot) = InjectionMap::new(buffer.clone(), cx.as_ref());
|
||||
assert_eq!(
|
||||
initial_snapshot.transforms.summary().input,
|
||||
buffer.read(cx).text_summary()
|
||||
);
|
||||
|
||||
for _ in 0..operations {
|
||||
log::info!("text: {:?}", buffer.read(cx).text());
|
||||
match rng.gen_range(0..=100) {
|
||||
_ => {
|
||||
let edits = buffer.update(cx, |buffer, _| {
|
||||
let start_version = buffer.version.clone();
|
||||
let edit_count = rng.gen_range(1..=5);
|
||||
buffer.randomly_edit(&mut rng, edit_count);
|
||||
buffer
|
||||
.edits_since::<Point>(&start_version)
|
||||
.collect::<Vec<_>>()
|
||||
});
|
||||
log::info!("editing {:?}", edits);
|
||||
}
|
||||
}
|
||||
|
||||
let (snapshot, edits) = map.read(cx.as_ref());
|
||||
assert_eq!(
|
||||
snapshot.transforms.summary().input,
|
||||
buffer.read(cx).text_summary()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ use gpui::{fonts::FontId, text_layout::LineWrapper, Entity, ModelContext, Task};
|
|||
use language::{HighlightedChunk, Point};
|
||||
use lazy_static::lazy_static;
|
||||
use smol::future::yield_now;
|
||||
use std::{collections::VecDeque, ops::Range, time::Duration};
|
||||
use std::{collections::VecDeque, mem, ops::Range, time::Duration};
|
||||
use sum_tree::{Bias, Cursor, SumTree};
|
||||
|
||||
pub type Edit = buffer::Edit<u32>;
|
||||
|
@ -14,11 +14,16 @@ pub type Edit = buffer::Edit<u32>;
|
|||
pub struct WrapMap {
|
||||
snapshot: Snapshot,
|
||||
pending_edits: VecDeque<(TabSnapshot, Vec<TabEdit>)>,
|
||||
interpolated_edits: Patch,
|
||||
edits_since_sync: Patch,
|
||||
wrap_width: Option<f32>,
|
||||
background_task: Option<Task<()>>,
|
||||
font: (FontId, f32),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Patch(Vec<Edit>);
|
||||
|
||||
impl Entity for WrapMap {
|
||||
type Event = ();
|
||||
}
|
||||
|
@ -81,6 +86,8 @@ impl WrapMap {
|
|||
font: (font_id, font_size),
|
||||
wrap_width: None,
|
||||
pending_edits: Default::default(),
|
||||
interpolated_edits: Default::default(),
|
||||
edits_since_sync: Default::default(),
|
||||
snapshot: Snapshot::new(tab_snapshot),
|
||||
background_task: None,
|
||||
};
|
||||
|
@ -99,10 +106,13 @@ impl WrapMap {
|
|||
tab_snapshot: TabSnapshot,
|
||||
edits: Vec<TabEdit>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Snapshot {
|
||||
) -> (Snapshot, Vec<Edit>) {
|
||||
self.pending_edits.push_back((tab_snapshot, edits));
|
||||
self.flush_edits(cx);
|
||||
self.snapshot.clone()
|
||||
(
|
||||
self.snapshot.clone(),
|
||||
mem::take(&mut self.edits_since_sync).0,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn set_font(&mut self, font_id: FontId, font_size: f32, cx: &mut ModelContext<Self>) {
|
||||
|
@ -204,26 +214,32 @@ impl WrapMap {
|
|||
let update_task = cx.background().spawn(async move {
|
||||
let mut line_wrapper = font_cache.line_wrapper(font_id, font_size);
|
||||
|
||||
let mut output_edits = Patch::default();
|
||||
for (tab_snapshot, edits) in pending_edits {
|
||||
snapshot
|
||||
let wrap_edits = snapshot
|
||||
.update(tab_snapshot, &edits, wrap_width, &mut line_wrapper)
|
||||
.await;
|
||||
output_edits.compose(&wrap_edits);
|
||||
}
|
||||
snapshot
|
||||
(snapshot, output_edits)
|
||||
});
|
||||
|
||||
match cx
|
||||
.background()
|
||||
.block_with_timeout(Duration::from_millis(1), update_task)
|
||||
{
|
||||
Ok(snapshot) => {
|
||||
Ok((snapshot, output_edits)) => {
|
||||
self.snapshot = snapshot;
|
||||
self.edits_since_sync.compose(&output_edits);
|
||||
}
|
||||
Err(update_task) => {
|
||||
self.background_task = Some(cx.spawn(|this, mut cx| async move {
|
||||
let snapshot = update_task.await;
|
||||
let (snapshot, output_edits) = update_task.await;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.snapshot = snapshot;
|
||||
this.edits_since_sync
|
||||
.compose(mem::take(&mut this.interpolated_edits).invert())
|
||||
.compose(&output_edits);
|
||||
this.background_task = None;
|
||||
this.flush_edits(cx);
|
||||
cx.notify();
|
||||
|
@ -240,7 +256,9 @@ impl WrapMap {
|
|||
if tab_snapshot.version() <= self.snapshot.tab_snapshot.version() {
|
||||
to_remove_len += 1;
|
||||
} else {
|
||||
self.snapshot.interpolate(tab_snapshot.clone(), &edits);
|
||||
let interpolated_edits = self.snapshot.interpolate(tab_snapshot.clone(), &edits);
|
||||
self.edits_since_sync.compose(&interpolated_edits);
|
||||
self.interpolated_edits.compose(&interpolated_edits);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -250,6 +268,59 @@ impl WrapMap {
|
|||
}
|
||||
}
|
||||
|
||||
impl Patch {
|
||||
fn compose(&mut self, other: &Self) -> &mut Self {
|
||||
|
||||
// let mut other_ranges = edit.ranges.iter().peekable();
|
||||
// let mut new_ranges = Vec::new();
|
||||
// let insertion_len = edit.new_text.as_ref().map_or(0, |t| t.len());
|
||||
// let mut delta = 0;
|
||||
|
||||
// for mut self_range in self.ranges.iter().cloned() {
|
||||
// self_range.start += delta;
|
||||
// self_range.end += delta;
|
||||
|
||||
// while let Some(other_range) = other_ranges.peek() {
|
||||
// let mut other_range = (*other_range).clone();
|
||||
// other_range.start += delta;
|
||||
// other_range.end += delta;
|
||||
|
||||
// if other_range.start <= self_range.end {
|
||||
// other_ranges.next().unwrap();
|
||||
// delta += insertion_len;
|
||||
|
||||
// if other_range.end < self_range.start {
|
||||
// new_ranges.push(other_range.start..other_range.end + insertion_len);
|
||||
// self_range.start += insertion_len;
|
||||
// self_range.end += insertion_len;
|
||||
// } else {
|
||||
// self_range.start = cmp::min(self_range.start, other_range.start);
|
||||
// self_range.end = cmp::max(self_range.end, other_range.end) + insertion_len;
|
||||
// }
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// new_ranges.push(self_range);
|
||||
// }
|
||||
|
||||
// for other_range in other_ranges {
|
||||
// new_ranges.push(other_range.start + delta..other_range.end + delta + insertion_len);
|
||||
// delta += insertion_len;
|
||||
// }
|
||||
|
||||
// self.ranges = new_ranges;
|
||||
}
|
||||
|
||||
fn invert(&mut self) -> &mut Self {
|
||||
for edit in &mut self.0 {
|
||||
mem::swap(&mut edit.old, &mut edit.new);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Snapshot {
|
||||
fn new(tab_snapshot: TabSnapshot) -> Self {
|
||||
let mut transforms = SumTree::new();
|
||||
|
@ -264,7 +335,7 @@ impl Snapshot {
|
|||
}
|
||||
}
|
||||
|
||||
fn interpolate(&mut self, new_tab_snapshot: TabSnapshot, edits: &[TabEdit]) {
|
||||
fn interpolate(&mut self, new_tab_snapshot: TabSnapshot, edits: &[TabEdit]) -> Patch {
|
||||
let mut new_transforms;
|
||||
if edits.is_empty() {
|
||||
new_transforms = self.transforms.clone();
|
||||
|
@ -320,6 +391,7 @@ impl Snapshot {
|
|||
self.tab_snapshot = new_tab_snapshot;
|
||||
self.interpolated = true;
|
||||
self.check_invariants();
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn update(
|
||||
|
@ -328,7 +400,7 @@ impl Snapshot {
|
|||
edits: &[TabEdit],
|
||||
wrap_width: f32,
|
||||
line_wrapper: &mut LineWrapper,
|
||||
) {
|
||||
) -> Patch {
|
||||
#[derive(Debug)]
|
||||
struct RowEdit {
|
||||
old_rows: Range<u32>,
|
||||
|
@ -458,6 +530,7 @@ impl Snapshot {
|
|||
self.tab_snapshot = new_tab_snapshot;
|
||||
self.interpolated = false;
|
||||
self.check_invariants();
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn chunks_at(&self, wrap_row: u32) -> Chunks {
|
||||
|
@ -896,6 +969,8 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapPoint {
|
|||
}
|
||||
}
|
||||
|
||||
fn compose(prev: &mut Vec<Edit>, next: &[Edit]) {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -962,7 +1037,8 @@ mod tests {
|
|||
notifications.recv().await.unwrap();
|
||||
}
|
||||
|
||||
let snapshot = wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
|
||||
let (snapshot, _) =
|
||||
wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
|
||||
let actual_text = snapshot.text();
|
||||
assert_eq!(
|
||||
actual_text, expected_text,
|
||||
|
@ -987,7 +1063,7 @@ mod tests {
|
|||
cx.read(|cx| fold_map.randomly_mutate(&mut rng, cx))
|
||||
{
|
||||
let (tabs_snapshot, edits) = tab_map.sync(folds_snapshot, edits);
|
||||
let mut snapshot =
|
||||
let (mut snapshot, _) =
|
||||
wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, edits, cx));
|
||||
snapshot.check_invariants();
|
||||
snapshot.verify_chunks(&mut rng);
|
||||
|
@ -1012,7 +1088,7 @@ mod tests {
|
|||
|
||||
let unwrapped_text = tabs_snapshot.text();
|
||||
let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
|
||||
let mut snapshot = wrap_map.update(&mut cx, |map, cx| {
|
||||
let (mut snapshot, _) = wrap_map.update(&mut cx, |map, cx| {
|
||||
map.sync(tabs_snapshot.clone(), edits, cx)
|
||||
});
|
||||
snapshot.check_invariants();
|
||||
|
@ -1026,7 +1102,7 @@ mod tests {
|
|||
}
|
||||
|
||||
if !wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
|
||||
let mut wrapped_snapshot =
|
||||
let (mut wrapped_snapshot, _) =
|
||||
wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
|
||||
let actual_text = wrapped_snapshot.text();
|
||||
log::info!("Wrapping finished: {:?}", actual_text);
|
||||
|
|
Loading…
Reference in a new issue