Don't represent editor's scroll top anchor as an option

Use Anchor::min as the special value representing a scroll top of zero
This commit is contained in:
Max Brunsfeld
2022-03-22 13:32:25 -07:00
parent 0a3f013e00
commit fc811e0856
2 changed files with 19 additions and 30 deletions
+9 -14
View File
@@ -438,7 +438,7 @@ pub struct Editor {
select_larger_syntax_node_stack: Vec<Box<[Selection<usize>]>>, select_larger_syntax_node_stack: Vec<Box<[Selection<usize>]>>,
active_diagnostics: Option<ActiveDiagnosticGroup>, active_diagnostics: Option<ActiveDiagnosticGroup>,
scroll_position: Vector2F, scroll_position: Vector2F,
scroll_top_anchor: Option<Anchor>, scroll_top_anchor: Anchor,
autoscroll_request: Option<Autoscroll>, autoscroll_request: Option<Autoscroll>,
soft_wrap_mode_override: Option<settings::SoftWrap>, soft_wrap_mode_override: Option<settings::SoftWrap>,
get_field_editor_theme: Option<GetFieldEditorTheme>, get_field_editor_theme: Option<GetFieldEditorTheme>,
@@ -473,7 +473,7 @@ pub struct EditorSnapshot {
pub placeholder_text: Option<Arc<str>>, pub placeholder_text: Option<Arc<str>>,
is_focused: bool, is_focused: bool,
scroll_position: Vector2F, scroll_position: Vector2F,
scroll_top_anchor: Option<Anchor>, scroll_top_anchor: Anchor,
} }
#[derive(Clone)] #[derive(Clone)]
@@ -915,7 +915,7 @@ impl Editor {
get_field_editor_theme, get_field_editor_theme,
project, project,
scroll_position: Vector2F::zero(), scroll_position: Vector2F::zero(),
scroll_top_anchor: None, scroll_top_anchor: Anchor::min(),
autoscroll_request: None, autoscroll_request: None,
focused: false, focused: false,
show_local_cursors: false, show_local_cursors: false,
@@ -1020,7 +1020,7 @@ impl Editor {
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
if scroll_position.y() == 0. { if scroll_position.y() == 0. {
self.scroll_top_anchor = None; self.scroll_top_anchor = Anchor::min();
self.scroll_position = scroll_position; self.scroll_position = scroll_position;
} else { } else {
let scroll_top_buffer_offset = let scroll_top_buffer_offset =
@@ -1032,19 +1032,14 @@ impl Editor {
scroll_position.x(), scroll_position.x(),
scroll_position.y() - anchor.to_display_point(&map).row() as f32, 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: true }); cx.emit(Event::ScrollPositionChanged { local: true });
cx.notify(); cx.notify();
} }
fn set_scroll_top_anchor( fn set_scroll_top_anchor(&mut self, anchor: Anchor, local: bool, cx: &mut ViewContext<Self>) {
&mut self,
anchor: Option<Anchor>,
local: bool,
cx: &mut ViewContext<Self>,
) {
self.scroll_position = Vector2F::zero(); self.scroll_position = Vector2F::zero();
self.scroll_top_anchor = anchor; self.scroll_top_anchor = anchor;
cx.emit(Event::ScrollPositionChanged { local }); cx.emit(Event::ScrollPositionChanged { local });
@@ -5636,10 +5631,10 @@ impl Deref for EditorSnapshot {
fn compute_scroll_position( fn compute_scroll_position(
snapshot: &DisplaySnapshot, snapshot: &DisplaySnapshot,
mut scroll_position: Vector2F, mut scroll_position: Vector2F,
scroll_top_anchor: &Option<Anchor>, scroll_top_anchor: &Anchor,
) -> Vector2F { ) -> Vector2F {
if let Some(anchor) = scroll_top_anchor { if *scroll_top_anchor != Anchor::min() {
let scroll_top = anchor.to_display_point(snapshot).row() as f32; let scroll_top = scroll_top_anchor.to_display_point(snapshot).row() as f32;
scroll_position.set_y(scroll_top + scroll_position.y()); scroll_position.set_y(scroll_top + scroll_position.y());
} else { } else {
scroll_position.set_y(0.); scroll_position.set_y(0.);
+10 -16
View File
@@ -70,17 +70,15 @@ impl FollowableItem for Editor {
if let Some(anchor) = state.scroll_top { if let Some(anchor) = state.scroll_top {
editor.set_scroll_top_anchor( editor.set_scroll_top_anchor(
Some(Anchor { Anchor {
buffer_id: Some(state.buffer_id as usize), buffer_id: Some(state.buffer_id as usize),
excerpt_id: excerpt_id.clone(), excerpt_id: excerpt_id.clone(),
text_anchor: language::proto::deserialize_anchor(anchor) text_anchor: language::proto::deserialize_anchor(anchor)
.ok_or_else(|| anyhow!("invalid scroll top"))?, .ok_or_else(|| anyhow!("invalid scroll top"))?,
}), },
false, false,
cx, cx,
); );
} else {
editor.set_scroll_top_anchor(None, false, cx);
} }
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
@@ -113,10 +111,9 @@ impl FollowableItem for Editor {
let buffer_id = self.buffer.read(cx).as_singleton()?.read(cx).remote_id(); let buffer_id = self.buffer.read(cx).as_singleton()?.read(cx).remote_id();
Some(proto::view::Variant::Editor(proto::view::Editor { Some(proto::view::Variant::Editor(proto::view::Editor {
buffer_id, buffer_id,
scroll_top: self scroll_top: Some(language::proto::serialize_anchor(
.scroll_top_anchor &self.scroll_top_anchor.text_anchor,
.as_ref() )),
.map(|anchor| language::proto::serialize_anchor(&anchor.text_anchor)),
selections: self.selections.iter().map(serialize_selection).collect(), selections: self.selections.iter().map(serialize_selection).collect(),
})) }))
} }
@@ -129,10 +126,9 @@ impl FollowableItem for Editor {
match event { match event {
Event::ScrollPositionChanged { .. } | Event::SelectionsChanged { .. } => { Event::ScrollPositionChanged { .. } | Event::SelectionsChanged { .. } => {
Some(update_view::Variant::Editor(update_view::Editor { Some(update_view::Variant::Editor(update_view::Editor {
scroll_top: self scroll_top: Some(language::proto::serialize_anchor(
.scroll_top_anchor &self.scroll_top_anchor.text_anchor,
.as_ref() )),
.map(|anchor| language::proto::serialize_anchor(&anchor.text_anchor)),
selections: self.selections.iter().map(serialize_selection).collect(), selections: self.selections.iter().map(serialize_selection).collect(),
})) }))
} }
@@ -155,17 +151,15 @@ impl FollowableItem for Editor {
if let Some(anchor) = message.scroll_top { if let Some(anchor) = message.scroll_top {
self.set_scroll_top_anchor( self.set_scroll_top_anchor(
Some(Anchor { Anchor {
buffer_id: Some(buffer_id), buffer_id: Some(buffer_id),
excerpt_id: excerpt_id.clone(), excerpt_id: excerpt_id.clone(),
text_anchor: language::proto::deserialize_anchor(anchor) text_anchor: language::proto::deserialize_anchor(anchor)
.ok_or_else(|| anyhow!("invalid scroll top"))?, .ok_or_else(|| anyhow!("invalid scroll top"))?,
}), },
false, false,
cx, cx,
); );
} else {
self.set_scroll_top_anchor(None, false, cx);
} }
let selections = message let selections = message