Represent scroll position correctly when scrolled mid-block

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-11-17 14:56:55 -08:00
parent 707ffe8ff3
commit cb18131432

View file

@ -11,9 +11,11 @@ pub use display_map::DisplayPoint;
use display_map::*; use display_map::*;
pub use element::*; pub use element::*;
use gpui::{ use gpui::{
action, geometry::vector::Vector2F, keymap::Binding, text_layout, AppContext, ClipboardItem, action,
Element, ElementBox, Entity, ModelHandle, MutableAppContext, RenderContext, View, ViewContext, geometry::vector::{vec2f, Vector2F},
WeakViewHandle, keymap::Binding,
text_layout, AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle,
MutableAppContext, RenderContext, View, ViewContext, WeakViewHandle,
}; };
use language::*; use language::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -469,7 +471,7 @@ impl Editor {
cx.notify(); cx.notify();
} }
fn set_scroll_position(&mut self, mut scroll_position: Vector2F, cx: &mut ViewContext<Self>) { fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let scroll_top_buffer_offset = let scroll_top_buffer_offset =
DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right); DisplayPoint::new(scroll_position.y() as u32, 0).to_offset(&map, Bias::Right);
@ -477,8 +479,16 @@ impl Editor {
.buffer .buffer
.read(cx) .read(cx)
.anchor_at(scroll_top_buffer_offset, Bias::Right); .anchor_at(scroll_top_buffer_offset, Bias::Right);
scroll_position.set_y(scroll_position.y().fract()); self.scroll_position = vec2f(
self.scroll_position = scroll_position; scroll_position.x(),
scroll_position.y() - self.scroll_top_anchor.to_display_point(&map).row() as f32,
);
debug_assert_eq!(
compute_scroll_position(&map, self.scroll_position, &self.scroll_top_anchor),
scroll_position
);
cx.notify(); cx.notify();
} }