Merge pull request #661 from zed-industries/follow
Introduce basic following experience
This commit is contained in:
+145
-33
@@ -340,9 +340,8 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_async_action(Editor::confirm_rename);
|
||||
cx.add_async_action(Editor::find_all_references);
|
||||
|
||||
workspace::register_project_item(cx, |project, buffer, cx| {
|
||||
Editor::for_buffer(buffer, Some(project), cx)
|
||||
});
|
||||
workspace::register_project_item::<Editor>(cx);
|
||||
workspace::register_followable_item::<Editor>(cx);
|
||||
}
|
||||
|
||||
trait InvalidationRegion {
|
||||
@@ -431,8 +430,8 @@ pub struct Editor {
|
||||
select_larger_syntax_node_stack: Vec<Box<[Selection<usize>]>>,
|
||||
active_diagnostics: Option<ActiveDiagnosticGroup>,
|
||||
scroll_position: Vector2F,
|
||||
scroll_top_anchor: Option<Anchor>,
|
||||
autoscroll_request: Option<Autoscroll>,
|
||||
scroll_top_anchor: Anchor,
|
||||
autoscroll_request: Option<(Autoscroll, bool)>,
|
||||
soft_wrap_mode_override: Option<settings::SoftWrap>,
|
||||
get_field_editor_theme: Option<GetFieldEditorTheme>,
|
||||
override_text_style: Option<Box<OverrideTextStyle>>,
|
||||
@@ -457,6 +456,7 @@ pub struct Editor {
|
||||
pending_rename: Option<RenameState>,
|
||||
searchable: bool,
|
||||
cursor_shape: CursorShape,
|
||||
leader_replica_id: Option<u16>,
|
||||
}
|
||||
|
||||
pub struct EditorSnapshot {
|
||||
@@ -465,7 +465,7 @@ pub struct EditorSnapshot {
|
||||
pub placeholder_text: Option<Arc<str>>,
|
||||
is_focused: bool,
|
||||
scroll_position: Vector2F,
|
||||
scroll_top_anchor: Option<Anchor>,
|
||||
scroll_top_anchor: Anchor,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -909,7 +909,7 @@ impl Editor {
|
||||
get_field_editor_theme,
|
||||
project,
|
||||
scroll_position: Vector2F::zero(),
|
||||
scroll_top_anchor: None,
|
||||
scroll_top_anchor: Anchor::min(),
|
||||
autoscroll_request: None,
|
||||
focused: false,
|
||||
show_local_cursors: false,
|
||||
@@ -932,6 +932,7 @@ impl Editor {
|
||||
searchable: true,
|
||||
override_text_style: None,
|
||||
cursor_shape: Default::default(),
|
||||
leader_replica_id: None,
|
||||
};
|
||||
this.end_selection(cx);
|
||||
|
||||
@@ -1014,10 +1015,19 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {
|
||||
self.set_scroll_position_internal(scroll_position, true, cx);
|
||||
}
|
||||
|
||||
fn set_scroll_position_internal(
|
||||
&mut self,
|
||||
scroll_position: Vector2F,
|
||||
local: bool,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||
|
||||
if scroll_position.y() == 0. {
|
||||
self.scroll_top_anchor = None;
|
||||
self.scroll_top_anchor = Anchor::min();
|
||||
self.scroll_position = scroll_position;
|
||||
} else {
|
||||
let scroll_top_buffer_offset =
|
||||
@@ -1029,9 +1039,22 @@ impl Editor {
|
||||
scroll_position.x(),
|
||||
scroll_position.y() - anchor.to_display_point(&map).row() as f32,
|
||||
);
|
||||
self.scroll_top_anchor = Some(anchor);
|
||||
self.scroll_top_anchor = anchor;
|
||||
}
|
||||
|
||||
cx.emit(Event::ScrollPositionChanged { local });
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn set_scroll_top_anchor(
|
||||
&mut self,
|
||||
anchor: Anchor,
|
||||
position: Vector2F,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
self.scroll_top_anchor = anchor;
|
||||
self.scroll_position = position;
|
||||
cx.emit(Event::ScrollPositionChanged { local: false });
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -1074,7 +1097,7 @@ impl Editor {
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
}
|
||||
|
||||
let autoscroll = if let Some(autoscroll) = self.autoscroll_request.take() {
|
||||
let (autoscroll, local) = if let Some(autoscroll) = self.autoscroll_request.take() {
|
||||
autoscroll
|
||||
} else {
|
||||
return false;
|
||||
@@ -1126,15 +1149,15 @@ impl Editor {
|
||||
|
||||
if target_top < start_row {
|
||||
scroll_position.set_y(target_top);
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
self.set_scroll_position_internal(scroll_position, local, cx);
|
||||
} else if target_bottom >= end_row {
|
||||
scroll_position.set_y(target_bottom - visible_lines);
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
self.set_scroll_position_internal(scroll_position, local, cx);
|
||||
}
|
||||
}
|
||||
Autoscroll::Center => {
|
||||
scroll_position.set_y((first_cursor_top - margin).max(0.0));
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
self.set_scroll_position_internal(scroll_position, local, cx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1316,7 +1339,7 @@ impl Editor {
|
||||
_ => {}
|
||||
}
|
||||
|
||||
self.set_selections(self.selections.clone(), Some(pending), cx);
|
||||
self.set_selections(self.selections.clone(), Some(pending), true, cx);
|
||||
}
|
||||
|
||||
fn begin_selection(
|
||||
@@ -1396,7 +1419,12 @@ impl Editor {
|
||||
} else {
|
||||
selections = Arc::from([]);
|
||||
}
|
||||
self.set_selections(selections, Some(PendingSelection { selection, mode }), cx);
|
||||
self.set_selections(
|
||||
selections,
|
||||
Some(PendingSelection { selection, mode }),
|
||||
true,
|
||||
cx,
|
||||
);
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
@@ -1510,7 +1538,7 @@ impl Editor {
|
||||
pending.selection.end = buffer.anchor_before(head);
|
||||
pending.selection.reversed = false;
|
||||
}
|
||||
self.set_selections(self.selections.clone(), Some(pending), cx);
|
||||
self.set_selections(self.selections.clone(), Some(pending), true, cx);
|
||||
} else {
|
||||
log::error!("update_selection dispatched with no pending selection");
|
||||
return;
|
||||
@@ -1597,7 +1625,7 @@ impl Editor {
|
||||
if selections.is_empty() {
|
||||
selections = Arc::from([pending.selection]);
|
||||
}
|
||||
self.set_selections(selections, None, cx);
|
||||
self.set_selections(selections, None, true, cx);
|
||||
self.request_autoscroll(Autoscroll::Fit, cx);
|
||||
} else {
|
||||
let mut oldest_selection = self.oldest_selection::<usize>(&cx);
|
||||
@@ -1617,7 +1645,7 @@ impl Editor {
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub fn selected_ranges<D: TextDimension + Ord + Sub<D, Output = D>>(
|
||||
&self,
|
||||
cx: &mut MutableAppContext,
|
||||
cx: &AppContext,
|
||||
) -> Vec<Range<D>> {
|
||||
self.local_selections::<D>(cx)
|
||||
.iter()
|
||||
@@ -1944,7 +1972,7 @@ impl Editor {
|
||||
}
|
||||
drop(snapshot);
|
||||
|
||||
self.set_selections(selections.into(), None, cx);
|
||||
self.set_selections(selections.into(), None, true, cx);
|
||||
true
|
||||
}
|
||||
} else {
|
||||
@@ -3334,7 +3362,7 @@ impl Editor {
|
||||
pub fn undo(&mut self, _: &Undo, cx: &mut ViewContext<Self>) {
|
||||
if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.undo(cx)) {
|
||||
if let Some((selections, _)) = self.selection_history.get(&tx_id).cloned() {
|
||||
self.set_selections(selections, None, cx);
|
||||
self.set_selections(selections, None, true, cx);
|
||||
}
|
||||
self.request_autoscroll(Autoscroll::Fit, cx);
|
||||
}
|
||||
@@ -3343,7 +3371,7 @@ impl Editor {
|
||||
pub fn redo(&mut self, _: &Redo, cx: &mut ViewContext<Self>) {
|
||||
if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.redo(cx)) {
|
||||
if let Some((_, Some(selections))) = self.selection_history.get(&tx_id).cloned() {
|
||||
self.set_selections(selections, None, cx);
|
||||
self.set_selections(selections, None, true, cx);
|
||||
}
|
||||
self.request_autoscroll(Autoscroll::Fit, cx);
|
||||
}
|
||||
@@ -4870,6 +4898,7 @@ impl Editor {
|
||||
}
|
||||
})),
|
||||
None,
|
||||
true,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
@@ -4930,6 +4959,7 @@ impl Editor {
|
||||
&mut self,
|
||||
selections: Arc<[Selection<Anchor>]>,
|
||||
pending_selection: Option<PendingSelection>,
|
||||
local: bool,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
assert!(
|
||||
@@ -4941,7 +4971,7 @@ impl Editor {
|
||||
|
||||
self.selections = selections;
|
||||
self.pending_selection = pending_selection;
|
||||
if self.focused {
|
||||
if self.focused && self.leader_replica_id.is_none() {
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.set_active_selections(&self.selections, cx)
|
||||
});
|
||||
@@ -4998,11 +5028,16 @@ impl Editor {
|
||||
self.refresh_document_highlights(cx);
|
||||
|
||||
self.pause_cursor_blinking(cx);
|
||||
cx.emit(Event::SelectionsChanged);
|
||||
cx.emit(Event::SelectionsChanged { local });
|
||||
}
|
||||
|
||||
pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
|
||||
self.autoscroll_request = Some(autoscroll);
|
||||
self.autoscroll_request = Some((autoscroll, true));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn request_autoscroll_remotely(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
|
||||
self.autoscroll_request = Some((autoscroll, false));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -5407,7 +5442,7 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn show_local_cursors(&self) -> bool {
|
||||
self.show_local_cursors
|
||||
self.show_local_cursors && self.focused
|
||||
}
|
||||
|
||||
fn on_buffer_changed(&mut self, _: ModelHandle<MultiBuffer>, cx: &mut ViewContext<Self>) {
|
||||
@@ -5421,10 +5456,10 @@ impl Editor {
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
match event {
|
||||
language::Event::Edited => {
|
||||
language::Event::Edited { local } => {
|
||||
self.refresh_active_diagnostics(cx);
|
||||
self.refresh_code_actions(cx);
|
||||
cx.emit(Event::Edited);
|
||||
cx.emit(Event::Edited { local: *local });
|
||||
}
|
||||
language::Event::Dirtied => cx.emit(Event::Dirtied),
|
||||
language::Event::Saved => cx.emit(Event::Saved),
|
||||
@@ -5537,10 +5572,10 @@ impl Deref for EditorSnapshot {
|
||||
fn compute_scroll_position(
|
||||
snapshot: &DisplaySnapshot,
|
||||
mut scroll_position: Vector2F,
|
||||
scroll_top_anchor: &Option<Anchor>,
|
||||
scroll_top_anchor: &Anchor,
|
||||
) -> Vector2F {
|
||||
if let Some(anchor) = scroll_top_anchor {
|
||||
let scroll_top = anchor.to_display_point(snapshot).row() as f32;
|
||||
if *scroll_top_anchor != Anchor::min() {
|
||||
let scroll_top = scroll_top_anchor.to_display_point(snapshot).row() as f32;
|
||||
scroll_position.set_y(scroll_top + scroll_position.y());
|
||||
} else {
|
||||
scroll_position.set_y(0.);
|
||||
@@ -5551,12 +5586,13 @@ fn compute_scroll_position(
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Event {
|
||||
Activate,
|
||||
Edited,
|
||||
Edited { local: bool },
|
||||
Blurred,
|
||||
Dirtied,
|
||||
Saved,
|
||||
TitleChanged,
|
||||
SelectionsChanged,
|
||||
SelectionsChanged { local: bool },
|
||||
ScrollPositionChanged { local: bool },
|
||||
Closed,
|
||||
}
|
||||
|
||||
@@ -5595,7 +5631,9 @@ impl View for Editor {
|
||||
self.blink_cursors(self.blink_epoch, cx);
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.finalize_last_transaction(cx);
|
||||
buffer.set_active_selections(&self.selections, cx)
|
||||
if self.leader_replica_id.is_none() {
|
||||
buffer.set_active_selections(&self.selections, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -6013,6 +6051,10 @@ mod tests {
|
||||
use crate::test::marked_text_by;
|
||||
|
||||
use super::*;
|
||||
use gpui::{
|
||||
geometry::rect::RectF,
|
||||
platform::{WindowBounds, WindowOptions},
|
||||
};
|
||||
use language::{LanguageConfig, LanguageServerConfig};
|
||||
use lsp::FakeLanguageServer;
|
||||
use project::FakeFs;
|
||||
@@ -6021,6 +6063,7 @@ mod tests {
|
||||
use text::Point;
|
||||
use unindent::Unindent;
|
||||
use util::test::sample_text;
|
||||
use workspace::FollowableItem;
|
||||
|
||||
#[gpui::test]
|
||||
fn test_undo_redo_with_selection_restoration(cx: &mut MutableAppContext) {
|
||||
@@ -8921,6 +8964,75 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn test_following(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer = MultiBuffer::build_simple(&sample_text(16, 8, 'a'), cx);
|
||||
populate_settings(cx);
|
||||
|
||||
let (_, leader) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
|
||||
let (_, follower) = cx.add_window(
|
||||
WindowOptions {
|
||||
bounds: WindowBounds::Fixed(RectF::from_points(vec2f(0., 0.), vec2f(10., 80.))),
|
||||
..Default::default()
|
||||
},
|
||||
|cx| build_editor(buffer.clone(), cx),
|
||||
);
|
||||
|
||||
let pending_update = Rc::new(RefCell::new(None));
|
||||
follower.update(cx, {
|
||||
let update = pending_update.clone();
|
||||
|_, cx| {
|
||||
cx.subscribe(&leader, move |_, leader, event, cx| {
|
||||
leader
|
||||
.read(cx)
|
||||
.add_event_to_update_proto(event, &mut *update.borrow_mut(), cx);
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
});
|
||||
|
||||
// Update the selections only
|
||||
leader.update(cx, |leader, cx| {
|
||||
leader.select_ranges([1..1], None, cx);
|
||||
});
|
||||
follower.update(cx, |follower, cx| {
|
||||
follower
|
||||
.apply_update_proto(pending_update.borrow_mut().take().unwrap(), cx)
|
||||
.unwrap();
|
||||
});
|
||||
assert_eq!(follower.read(cx).selected_ranges(cx), vec![1..1]);
|
||||
|
||||
// Update the scroll position only
|
||||
leader.update(cx, |leader, cx| {
|
||||
leader.set_scroll_position(vec2f(1.5, 3.5), cx);
|
||||
});
|
||||
follower.update(cx, |follower, cx| {
|
||||
follower
|
||||
.apply_update_proto(pending_update.borrow_mut().take().unwrap(), cx)
|
||||
.unwrap();
|
||||
});
|
||||
assert_eq!(
|
||||
follower.update(cx, |follower, cx| follower.scroll_position(cx)),
|
||||
vec2f(1.5, 3.5)
|
||||
);
|
||||
|
||||
// Update the selections and scroll position
|
||||
leader.update(cx, |leader, cx| {
|
||||
leader.select_ranges([0..0], None, cx);
|
||||
leader.request_autoscroll(Autoscroll::Newest, cx);
|
||||
leader.set_scroll_position(vec2f(1.5, 3.5), cx);
|
||||
});
|
||||
follower.update(cx, |follower, cx| {
|
||||
let initial_scroll_position = follower.scroll_position(cx);
|
||||
follower
|
||||
.apply_update_proto(pending_update.borrow_mut().take().unwrap(), cx)
|
||||
.unwrap();
|
||||
assert_eq!(follower.scroll_position(cx), initial_scroll_position);
|
||||
assert!(follower.autoscroll_request.is_some());
|
||||
});
|
||||
assert_eq!(follower.read(cx).selected_ranges(cx), vec![0..0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_combine_syntax_and_fuzzy_match_highlights() {
|
||||
let string = "abcdefghijklmnop";
|
||||
|
||||
@@ -909,7 +909,7 @@ impl Element for EditorElement {
|
||||
.anchor_before(DisplayPoint::new(end_row, 0).to_offset(&snapshot, Bias::Right))
|
||||
};
|
||||
|
||||
let mut selections = HashMap::default();
|
||||
let mut selections = Vec::new();
|
||||
let mut active_rows = BTreeMap::new();
|
||||
let mut highlighted_rows = None;
|
||||
let mut highlighted_ranges = Vec::new();
|
||||
@@ -922,11 +922,32 @@ impl Element for EditorElement {
|
||||
&display_map,
|
||||
);
|
||||
|
||||
let mut remote_selections = HashMap::default();
|
||||
for (replica_id, selection) in display_map
|
||||
.buffer_snapshot
|
||||
.remote_selections_in_range(&(start_anchor.clone()..end_anchor.clone()))
|
||||
{
|
||||
// The local selections match the leader's selections.
|
||||
if Some(replica_id) == view.leader_replica_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
remote_selections
|
||||
.entry(replica_id)
|
||||
.or_insert(Vec::new())
|
||||
.push(crate::Selection {
|
||||
id: selection.id,
|
||||
goal: selection.goal,
|
||||
reversed: selection.reversed,
|
||||
start: selection.start.to_display_point(&display_map),
|
||||
end: selection.end.to_display_point(&display_map),
|
||||
});
|
||||
}
|
||||
selections.extend(remote_selections);
|
||||
|
||||
if view.show_local_selections {
|
||||
let local_selections = view.local_selections_in_range(
|
||||
start_anchor.clone()..end_anchor.clone(),
|
||||
&display_map,
|
||||
);
|
||||
let local_selections =
|
||||
view.local_selections_in_range(start_anchor..end_anchor, &display_map);
|
||||
for selection in &local_selections {
|
||||
let is_empty = selection.start == selection.end;
|
||||
let selection_start = snapshot.prev_line_boundary(selection.start).1;
|
||||
@@ -939,8 +960,12 @@ impl Element for EditorElement {
|
||||
*contains_non_empty_selection |= !is_empty;
|
||||
}
|
||||
}
|
||||
selections.insert(
|
||||
view.replica_id(cx),
|
||||
|
||||
// Render the local selections in the leader's color when following.
|
||||
let local_replica_id = view.leader_replica_id.unwrap_or(view.replica_id(cx));
|
||||
|
||||
selections.push((
|
||||
local_replica_id,
|
||||
local_selections
|
||||
.into_iter()
|
||||
.map(|selection| crate::Selection {
|
||||
@@ -951,23 +976,7 @@ impl Element for EditorElement {
|
||||
end: selection.end.to_display_point(&display_map),
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
for (replica_id, selection) in display_map
|
||||
.buffer_snapshot
|
||||
.remote_selections_in_range(&(start_anchor..end_anchor))
|
||||
{
|
||||
selections
|
||||
.entry(replica_id)
|
||||
.or_insert(Vec::new())
|
||||
.push(crate::Selection {
|
||||
id: selection.id,
|
||||
goal: selection.goal,
|
||||
reversed: selection.reversed,
|
||||
start: selection.start.to_display_point(&display_map),
|
||||
end: selection.end.to_display_point(&display_map),
|
||||
});
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1213,7 +1222,7 @@ pub struct LayoutState {
|
||||
em_width: f32,
|
||||
em_advance: f32,
|
||||
highlighted_ranges: Vec<(Range<DisplayPoint>, Color)>,
|
||||
selections: HashMap<ReplicaId, Vec<text::Selection<DisplayPoint>>>,
|
||||
selections: Vec<(ReplicaId, Vec<text::Selection<DisplayPoint>>)>,
|
||||
context_menu: Option<(DisplayPoint, ElementBox)>,
|
||||
code_actions_indicator: Option<(u32, ElementBox)>,
|
||||
}
|
||||
|
||||
+241
-9
@@ -1,16 +1,244 @@
|
||||
use crate::{Autoscroll, Editor, Event, NavigationData, ToOffset, ToPoint as _};
|
||||
use anyhow::Result;
|
||||
use crate::{Anchor, Autoscroll, Editor, Event, ExcerptId, NavigationData, ToOffset, ToPoint as _};
|
||||
use anyhow::{anyhow, Result};
|
||||
use gpui::{
|
||||
elements::*, AppContext, Entity, ModelHandle, RenderContext, Subscription, Task, View,
|
||||
ViewContext, ViewHandle,
|
||||
elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
|
||||
RenderContext, Subscription, Task, View, ViewContext, ViewHandle,
|
||||
};
|
||||
use language::{Bias, Buffer, Diagnostic, File as _};
|
||||
use project::{File, Project, ProjectPath};
|
||||
use std::fmt::Write;
|
||||
use std::path::PathBuf;
|
||||
use language::{Bias, Buffer, Diagnostic, File as _, SelectionGoal};
|
||||
use project::{File, Project, ProjectEntryId, ProjectPath};
|
||||
use rpc::proto::{self, update_view};
|
||||
use std::{fmt::Write, path::PathBuf};
|
||||
use text::{Point, Selection};
|
||||
use util::ResultExt;
|
||||
use workspace::{Item, ItemHandle, ItemNavHistory, ProjectItem, Settings, StatusItemView};
|
||||
use workspace::{
|
||||
FollowableItem, Item, ItemHandle, ItemNavHistory, ProjectItem, Settings, StatusItemView,
|
||||
};
|
||||
|
||||
impl FollowableItem for Editor {
|
||||
fn from_state_proto(
|
||||
pane: ViewHandle<workspace::Pane>,
|
||||
project: ModelHandle<Project>,
|
||||
state: &mut Option<proto::view::Variant>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Option<Task<Result<ViewHandle<Self>>>> {
|
||||
let state = if matches!(state, Some(proto::view::Variant::Editor(_))) {
|
||||
if let Some(proto::view::Variant::Editor(state)) = state.take() {
|
||||
state
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let buffer = project.update(cx, |project, cx| {
|
||||
project.open_buffer_by_id(state.buffer_id, cx)
|
||||
});
|
||||
Some(cx.spawn(|mut cx| async move {
|
||||
let buffer = buffer.await?;
|
||||
let editor = pane
|
||||
.read_with(&cx, |pane, cx| {
|
||||
pane.items_of_type::<Self>().find(|editor| {
|
||||
editor.read(cx).buffer.read(cx).as_singleton().as_ref() == Some(&buffer)
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
cx.add_view(pane.window_id(), |cx| {
|
||||
Editor::for_buffer(buffer, Some(project), cx)
|
||||
})
|
||||
});
|
||||
editor.update(&mut cx, |editor, cx| {
|
||||
let excerpt_id;
|
||||
let buffer_id;
|
||||
{
|
||||
let buffer = editor.buffer.read(cx).read(cx);
|
||||
let singleton = buffer.as_singleton().unwrap();
|
||||
excerpt_id = singleton.0.clone();
|
||||
buffer_id = singleton.1;
|
||||
}
|
||||
let selections = state
|
||||
.selections
|
||||
.into_iter()
|
||||
.map(|selection| {
|
||||
deserialize_selection(&excerpt_id, buffer_id, selection)
|
||||
.ok_or_else(|| anyhow!("invalid selection"))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
if !selections.is_empty() {
|
||||
editor.set_selections(selections.into(), None, false, cx);
|
||||
}
|
||||
|
||||
if let Some(anchor) = state.scroll_top_anchor {
|
||||
editor.set_scroll_top_anchor(
|
||||
Anchor {
|
||||
buffer_id: Some(state.buffer_id as usize),
|
||||
excerpt_id: excerpt_id.clone(),
|
||||
text_anchor: language::proto::deserialize_anchor(anchor)
|
||||
.ok_or_else(|| anyhow!("invalid scroll top"))?,
|
||||
},
|
||||
vec2f(state.scroll_x, state.scroll_y),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
Ok::<_, anyhow::Error>(())
|
||||
})?;
|
||||
Ok(editor)
|
||||
}))
|
||||
}
|
||||
|
||||
fn set_leader_replica_id(
|
||||
&mut self,
|
||||
leader_replica_id: Option<u16>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
self.leader_replica_id = leader_replica_id;
|
||||
if self.leader_replica_id.is_some() {
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.remove_active_selections(cx);
|
||||
});
|
||||
} else {
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
if self.focused {
|
||||
buffer.set_active_selections(&self.selections, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
|
||||
let buffer_id = self.buffer.read(cx).as_singleton()?.read(cx).remote_id();
|
||||
Some(proto::view::Variant::Editor(proto::view::Editor {
|
||||
buffer_id,
|
||||
scroll_top_anchor: Some(language::proto::serialize_anchor(
|
||||
&self.scroll_top_anchor.text_anchor,
|
||||
)),
|
||||
scroll_x: self.scroll_position.x(),
|
||||
scroll_y: self.scroll_position.y(),
|
||||
selections: self.selections.iter().map(serialize_selection).collect(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn add_event_to_update_proto(
|
||||
&self,
|
||||
event: &Self::Event,
|
||||
update: &mut Option<proto::update_view::Variant>,
|
||||
_: &AppContext,
|
||||
) -> bool {
|
||||
let update =
|
||||
update.get_or_insert_with(|| proto::update_view::Variant::Editor(Default::default()));
|
||||
|
||||
match update {
|
||||
proto::update_view::Variant::Editor(update) => match event {
|
||||
Event::ScrollPositionChanged { .. } => {
|
||||
update.scroll_top_anchor = Some(language::proto::serialize_anchor(
|
||||
&self.scroll_top_anchor.text_anchor,
|
||||
));
|
||||
update.scroll_x = self.scroll_position.x();
|
||||
update.scroll_y = self.scroll_position.y();
|
||||
true
|
||||
}
|
||||
Event::SelectionsChanged { .. } => {
|
||||
update.selections = self
|
||||
.selections
|
||||
.iter()
|
||||
.chain(self.pending_selection.as_ref().map(|p| &p.selection))
|
||||
.map(serialize_selection)
|
||||
.collect();
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_update_proto(
|
||||
&mut self,
|
||||
message: update_view::Variant,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Result<()> {
|
||||
match message {
|
||||
update_view::Variant::Editor(message) => {
|
||||
let buffer = self.buffer.read(cx);
|
||||
let buffer = buffer.read(cx);
|
||||
let (excerpt_id, buffer_id, _) = buffer.as_singleton().unwrap();
|
||||
let excerpt_id = excerpt_id.clone();
|
||||
drop(buffer);
|
||||
|
||||
let selections = message
|
||||
.selections
|
||||
.into_iter()
|
||||
.filter_map(|selection| {
|
||||
deserialize_selection(&excerpt_id, buffer_id, selection)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if !selections.is_empty() {
|
||||
self.set_selections(selections.into(), None, false, cx);
|
||||
self.request_autoscroll_remotely(Autoscroll::Newest, cx);
|
||||
} else {
|
||||
if let Some(anchor) = message.scroll_top_anchor {
|
||||
self.set_scroll_top_anchor(
|
||||
Anchor {
|
||||
buffer_id: Some(buffer_id),
|
||||
excerpt_id: excerpt_id.clone(),
|
||||
text_anchor: language::proto::deserialize_anchor(anchor)
|
||||
.ok_or_else(|| anyhow!("invalid scroll top"))?,
|
||||
},
|
||||
vec2f(message.scroll_x, message.scroll_y),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn should_unfollow_on_event(event: &Self::Event, _: &AppContext) -> bool {
|
||||
match event {
|
||||
Event::Edited { local } => *local,
|
||||
Event::SelectionsChanged { local } => *local,
|
||||
Event::ScrollPositionChanged { local } => *local,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
|
||||
proto::Selection {
|
||||
id: selection.id as u64,
|
||||
start: Some(language::proto::serialize_anchor(
|
||||
&selection.start.text_anchor,
|
||||
)),
|
||||
end: Some(language::proto::serialize_anchor(
|
||||
&selection.end.text_anchor,
|
||||
)),
|
||||
reversed: selection.reversed,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_selection(
|
||||
excerpt_id: &ExcerptId,
|
||||
buffer_id: usize,
|
||||
selection: proto::Selection,
|
||||
) -> Option<Selection<Anchor>> {
|
||||
Some(Selection {
|
||||
id: selection.id as usize,
|
||||
start: Anchor {
|
||||
buffer_id: Some(buffer_id),
|
||||
excerpt_id: excerpt_id.clone(),
|
||||
text_anchor: language::proto::deserialize_anchor(selection.start?)?,
|
||||
},
|
||||
end: Anchor {
|
||||
buffer_id: Some(buffer_id),
|
||||
excerpt_id: excerpt_id.clone(),
|
||||
text_anchor: language::proto::deserialize_anchor(selection.end?)?,
|
||||
},
|
||||
reversed: selection.reversed,
|
||||
goal: SelectionGoal::None,
|
||||
})
|
||||
}
|
||||
|
||||
impl Item for Editor {
|
||||
fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) {
|
||||
@@ -41,6 +269,10 @@ impl Item for Editor {
|
||||
})
|
||||
}
|
||||
|
||||
fn project_entry_id(&self, cx: &AppContext) -> Option<ProjectEntryId> {
|
||||
File::from_dyn(self.buffer().read(cx).file(cx)).and_then(|file| file.project_entry_id(cx))
|
||||
}
|
||||
|
||||
fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
|
||||
@@ -821,6 +821,14 @@ impl MultiBuffer {
|
||||
.map_or(Vec::new(), |state| state.excerpts.clone())
|
||||
}
|
||||
|
||||
pub fn excerpt_ids(&self) -> Vec<ExcerptId> {
|
||||
self.buffers
|
||||
.borrow()
|
||||
.values()
|
||||
.flat_map(|state| state.excerpts.iter().cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn excerpt_containing(
|
||||
&self,
|
||||
position: impl ToOffset,
|
||||
|
||||
Reference in New Issue
Block a user