Make Workspace::open_entry2, which returns a dyn ItemViewHandle

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo
2021-05-03 16:25:18 -06:00
co-authored by Max Brunsfeld
parent 8cffa8bdb2
commit afb623b6b5
8 changed files with 144 additions and 90 deletions
+21 -29
View File
@@ -351,23 +351,15 @@ pub struct UndoOperation {
}
impl Buffer {
pub fn new<T: Into<Arc<str>>>(
replica_id: ReplicaId,
base_text: T,
ctx: &mut ModelContext<Self>,
) -> Self {
Self::build(replica_id, History::new(base_text.into()), ctx)
pub fn new<T: Into<Arc<str>>>(replica_id: ReplicaId, base_text: T) -> Self {
Self::build(replica_id, History::new(base_text.into()))
}
pub fn from_history(
replica_id: ReplicaId,
history: History,
ctx: &mut ModelContext<Self>,
) -> Self {
Self::build(replica_id, history, ctx)
pub fn from_history(replica_id: ReplicaId, history: History) -> Self {
Self::build(replica_id, history)
}
fn build(replica_id: ReplicaId, history: History, _: &mut ModelContext<Self>) -> Self {
fn build(replica_id: ReplicaId, history: History) -> Self {
let mut insertion_splits = HashMap::default();
let mut fragments = SumTree::new();
@@ -2304,8 +2296,8 @@ mod tests {
#[test]
fn test_edit() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "abc", ctx);
ctx.add_model(|_| {
let mut buffer = Buffer::new(0, "abc");
assert_eq!(buffer.text(), "abc");
buffer.edit(vec![3..3], "def", None).unwrap();
assert_eq!(buffer.text(), "abcdef");
@@ -2329,8 +2321,8 @@ mod tests {
let buffer_1_events = Rc::new(RefCell::new(Vec::new()));
let buffer_2_events = Rc::new(RefCell::new(Vec::new()));
let buffer1 = app.add_model(|ctx| Buffer::new(0, "abcdef", ctx));
let buffer2 = app.add_model(|ctx| Buffer::new(1, "abcdef", ctx));
let buffer1 = app.add_model(|_| Buffer::new(0, "abcdef"));
let buffer2 = app.add_model(|_| Buffer::new(1, "abcdef"));
let mut buffer_ops = Vec::new();
buffer1.update(app, |buffer, ctx| {
let buffer_1_events = buffer_1_events.clone();
@@ -2417,7 +2409,7 @@ mod tests {
.take(reference_string_len)
.collect::<String>();
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, reference_string.as_str(), ctx);
let mut buffer = Buffer::new(0, reference_string.as_str());
let mut buffer_versions = Vec::new();
for _i in 0..10 {
let (old_ranges, new_text, _) = buffer.randomly_mutate(rng, None);
@@ -2503,7 +2495,7 @@ mod tests {
fn test_line_len() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
buffer.edit(vec![0..0], "abcd\nefg\nhij", None).unwrap();
buffer.edit(vec![12..12], "kl\nmno", None).unwrap();
buffer.edit(vec![18..18], "\npqrs\n", None).unwrap();
@@ -2525,7 +2517,7 @@ mod tests {
fn test_rightmost_point() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
assert_eq!(buffer.rightmost_point().row, 0);
buffer.edit(vec![0..0], "abcd\nefg\nhij", None).unwrap();
assert_eq!(buffer.rightmost_point().row, 0);
@@ -2546,7 +2538,7 @@ mod tests {
fn test_text_summary_for_range() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz", ctx);
let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz");
let text = Text::from(buffer.text());
assert_eq!(
buffer.text_summary_for_range(1..3),
@@ -2577,7 +2569,7 @@ mod tests {
fn test_chars_at() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
buffer.edit(vec![0..0], "abcd\nefgh\nij", None).unwrap();
buffer.edit(vec![12..12], "kl\nmno", None).unwrap();
buffer.edit(vec![18..18], "\npqrs", None).unwrap();
@@ -2599,7 +2591,7 @@ mod tests {
assert_eq!(chars.collect::<String>(), "PQrs");
// Regression test:
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n", None).unwrap();
buffer.edit(vec![60..60], "\n", None).unwrap();
@@ -2729,7 +2721,7 @@ mod tests {
fn test_anchors() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
buffer.edit(vec![0..0], "abc", None).unwrap();
let left_anchor = buffer.anchor_before(2).unwrap();
let right_anchor = buffer.anchor_after(2).unwrap();
@@ -2894,7 +2886,7 @@ mod tests {
fn test_anchors_at_start_and_end() {
App::test((), |ctx| {
ctx.add_model(|ctx| {
let mut buffer = Buffer::new(0, "", ctx);
let mut buffer = Buffer::new(0, "");
let before_start_anchor = buffer.anchor_before(0).unwrap();
let after_end_anchor = buffer.anchor_after(0).unwrap();
@@ -2921,7 +2913,7 @@ mod tests {
#[test]
fn test_is_modified() {
App::test((), |app| {
let model = app.add_model(|ctx| Buffer::new(0, "abc", ctx));
let model = app.add_model(|ctx| Buffer::new(0, "abc"));
let events = Rc::new(RefCell::new(Vec::new()));
// initially, the buffer isn't dirty.
@@ -3009,7 +3001,7 @@ mod tests {
fn test_undo_redo() {
App::test((), |app| {
app.add_model(|ctx| {
let mut buffer = Buffer::new(0, "1234", ctx);
let mut buffer = Buffer::new(0, "1234");
let edit1 = buffer.edit(vec![1..1], "abx", None).unwrap();
let edit2 = buffer.edit(vec![3..4], "yzef", None).unwrap();
@@ -3047,7 +3039,7 @@ mod tests {
App::test((), |app| {
app.add_model(|ctx| {
let mut now = Instant::now();
let mut buffer = Buffer::new(0, "123456", ctx);
let mut buffer = Buffer::new(0, "123456");
let (set_id, _) = buffer
.add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap(), None);
@@ -3132,7 +3124,7 @@ mod tests {
let mut network = Network::new();
for i in 0..PEERS {
let buffer =
ctx.add_model(|ctx| Buffer::new(i as ReplicaId, base_text.as_str(), ctx));
ctx.add_model(|ctx| Buffer::new(i as ReplicaId, base_text.as_str()));
buffers.push(buffer);
replica_ids.push(i as u16);
network.add_peer(i as u16);
+8 -14
View File
@@ -120,7 +120,7 @@ struct ClipboardSelection {
impl BufferView {
pub fn single_line(settings: watch::Receiver<Settings>, ctx: &mut ViewContext<Self>) -> Self {
let buffer = ctx.add_model(|ctx| Buffer::new(0, String::new(), ctx));
let buffer = ctx.add_model(|_| Buffer::new(0, String::new()));
let mut view = Self::for_buffer(buffer, None, settings, ctx);
view.single_line = true;
view
@@ -1421,8 +1421,7 @@ mod tests {
#[test]
fn test_selection_with_mouse() {
App::test((), |app| {
let buffer =
app.add_model(|ctx| Buffer::new(0, "aaaaaa\nbbbbbb\ncccccc\ndddddd\n", ctx));
let buffer = app.add_model(|ctx| Buffer::new(0, "aaaaaa\nbbbbbb\ncccccc\ndddddd\n"));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, buffer_view) =
app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx));
@@ -1536,7 +1535,7 @@ mod tests {
let layout_cache = TextLayoutCache::new(app.platform().fonts());
let font_cache = app.font_cache().clone();
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx));
let buffer = app.add_model(|_| Buffer::new(0, sample_text(6, 6)));
let settings = settings::channel(&font_cache).unwrap().1;
let (_, view) =
@@ -1553,7 +1552,7 @@ mod tests {
#[test]
fn test_fold() {
App::test((), |app| {
let buffer = app.add_model(|ctx| {
let buffer = app.add_model(|_| {
Buffer::new(
0,
"
@@ -1574,7 +1573,6 @@ mod tests {
}
"
.unindent(),
ctx,
)
});
let settings = settings::channel(&app.font_cache()).unwrap().1;
@@ -1648,7 +1646,7 @@ mod tests {
#[test]
fn test_move_cursor() -> Result<()> {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx));
let buffer = app.add_model(|_| Buffer::new(0, sample_text(6, 6)));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx));
@@ -1685,12 +1683,8 @@ mod tests {
#[test]
fn test_backspace() {
App::test((), |app| {
let buffer = app.add_model(|ctx| {
Buffer::new(
0,
"one two three\nfour five six\nseven eight nine\nten\n",
ctx,
)
let buffer = app.add_model(|_| {
Buffer::new(0, "one two three\nfour five six\nseven eight nine\nten\n")
});
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) =
@@ -1722,7 +1716,7 @@ mod tests {
#[test]
fn test_clipboard() {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, "one two three four five six ", ctx));
let buffer = app.add_model(|_| Buffer::new(0, "one two three four five six "));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let view = app
.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx))
+6 -6
View File
@@ -471,7 +471,7 @@ mod tests {
#[test]
fn test_basic_folds() {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx));
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6)));
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
map.fold(
@@ -522,7 +522,7 @@ mod tests {
#[test]
fn test_overlapping_folds() {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx));
let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6)));
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
map.fold(
vec![
@@ -541,7 +541,7 @@ mod tests {
#[test]
fn test_merging_folds_via_edit() {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx));
let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6)));
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
map.fold(
@@ -589,10 +589,10 @@ mod tests {
let mut rng = StdRng::seed_from_u64(seed);
App::test((), |app| {
let buffer = app.add_model(|ctx| {
let buffer = app.add_model(|_| {
let len = rng.gen_range(0..10);
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
Buffer::new(0, text, ctx)
Buffer::new(0, text)
});
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
@@ -664,7 +664,7 @@ mod tests {
fn test_buffer_rows() {
App::test((), |app| {
let text = sample_text(6, 6) + "\n";
let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx));
let buffer = app.add_model(|_| Buffer::new(0, text));
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
+2 -2
View File
@@ -298,7 +298,7 @@ mod tests {
fn test_chars_at() {
App::test((), |app| {
let text = sample_text(6, 6);
let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx));
let buffer = app.add_model(|_| Buffer::new(0, text));
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
buffer
.update(app, |buffer, ctx| {
@@ -365,7 +365,7 @@ mod tests {
#[test]
fn test_max_point() {
App::test((), |app| {
let buffer = app.add_model(|ctx| Buffer::new(0, "aaa\n\t\tbbb", ctx));
let buffer = app.add_model(|_| Buffer::new(0, "aaa\n\t\tbbb"));
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
assert_eq!(
map.read(app).max_point(app.as_ref()),