- Add a setting for `vertical_scroll_offset`
- Fix H/M/L in vim with scrolloff



Release Notes:

- Added a settings for `vertical_scroll_offset`
- vim: Fix H/M/L with various values of vertical_scroll_offset

---------

Co-authored-by: Vbhavsar <vbhavsar@gmail.com>
Co-authored-by: fdionisi <code@fdionisi.me>
This commit is contained in:
Conrad Irwin 2024-02-02 19:24:36 -07:00 committed by GitHub
parent e1efa7298e
commit f09da1a1c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 57 additions and 28 deletions

View file

@ -133,6 +133,8 @@
// Whether to show diagnostic indicators in the scrollbar. // Whether to show diagnostic indicators in the scrollbar.
"diagnostics": true "diagnostics": true
}, },
// The number of lines to keep above/below the cursor when scrolling.
"vertical_scroll_margin": 3,
"relative_line_numbers": false, "relative_line_numbers": false,
// When to populate a new search's query based on the text under the cursor. // When to populate a new search's query based on the text under the cursor.
// This setting can take the following three values: // This setting can take the following three values:

View file

@ -586,8 +586,9 @@ impl DisplaySnapshot {
text_system, text_system,
editor_style, editor_style,
rem_size, rem_size,
anchor: _, scroll_anchor: _,
visible_rows: _, visible_rows: _,
vertical_scroll_margin: _,
}: &TextLayoutDetails, }: &TextLayoutDetails,
) -> Arc<LineLayout> { ) -> Arc<LineLayout> {
let mut runs = Vec::new(); let mut runs = Vec::new();

View file

@ -1430,7 +1430,7 @@ impl Editor {
buffer: buffer.clone(), buffer: buffer.clone(),
display_map: display_map.clone(), display_map: display_map.clone(),
selections, selections,
scroll_manager: ScrollManager::new(), scroll_manager: ScrollManager::new(cx),
columnar_selection_tail: None, columnar_selection_tail: None,
add_selections_state: None, add_selections_state: None,
select_next_state: None, select_next_state: None,
@ -3086,8 +3086,9 @@ impl Editor {
text_system: cx.text_system().clone(), text_system: cx.text_system().clone(),
editor_style: self.style.clone().unwrap(), editor_style: self.style.clone().unwrap(),
rem_size: cx.rem_size(), rem_size: cx.rem_size(),
anchor: self.scroll_manager.anchor().anchor, scroll_anchor: self.scroll_manager.anchor(),
visible_rows: self.visible_line_count(), visible_rows: self.visible_line_count(),
vertical_scroll_margin: self.scroll_manager.vertical_scroll_margin,
} }
} }
@ -8762,6 +8763,8 @@ impl Editor {
)), )),
cx, cx,
); );
self.scroll_manager.vertical_scroll_margin =
EditorSettings::get_global(cx).vertical_scroll_margin;
cx.notify(); cx.notify();
} }

View file

@ -11,6 +11,7 @@ pub struct EditorSettings {
pub completion_documentation_secondary_query_debounce: u64, pub completion_documentation_secondary_query_debounce: u64,
pub use_on_type_format: bool, pub use_on_type_format: bool,
pub scrollbar: Scrollbar, pub scrollbar: Scrollbar,
pub vertical_scroll_margin: f32,
pub relative_line_numbers: bool, pub relative_line_numbers: bool,
pub seed_search_query_from_cursor: SeedQuerySetting, pub seed_search_query_from_cursor: SeedQuerySetting,
pub redact_private_values: bool, pub redact_private_values: bool,
@ -87,6 +88,11 @@ pub struct EditorSettingsContent {
pub use_on_type_format: Option<bool>, pub use_on_type_format: Option<bool>,
/// Scrollbar related settings /// Scrollbar related settings
pub scrollbar: Option<ScrollbarContent>, pub scrollbar: Option<ScrollbarContent>,
/// The number of lines to keep above/below the cursor when auto-scrolling.
///
/// Default: 3.
pub vertical_scroll_margin: Option<f32>,
/// Whether the line numbers on editors gutter are relative or not. /// Whether the line numbers on editors gutter are relative or not.
/// ///
/// Default: false /// Default: false

View file

@ -2,14 +2,12 @@
//! in editor given a given motion (e.g. it handles converting a "move left" command into coordinates in editor). It is exposed mostly for use by vim crate. //! in editor given a given motion (e.g. it handles converting a "move left" command into coordinates in editor). It is exposed mostly for use by vim crate.
use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint}; use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
use crate::{char_kind, CharKind, EditorStyle, ToOffset, ToPoint}; use crate::{char_kind, scroll::ScrollAnchor, CharKind, EditorStyle, ToOffset, ToPoint};
use gpui::{px, Pixels, WindowTextSystem}; use gpui::{px, Pixels, WindowTextSystem};
use language::Point; use language::Point;
use std::{ops::Range, sync::Arc}; use std::{ops::Range, sync::Arc};
use multi_buffer::Anchor;
/// Defines search strategy for items in `movement` module. /// Defines search strategy for items in `movement` module.
/// `FindRange::SingeLine` only looks for a match on a single line at a time, whereas /// `FindRange::SingeLine` only looks for a match on a single line at a time, whereas
/// `FindRange::MultiLine` keeps going until the end of a string. /// `FindRange::MultiLine` keeps going until the end of a string.
@ -25,8 +23,9 @@ pub struct TextLayoutDetails {
pub(crate) text_system: Arc<WindowTextSystem>, pub(crate) text_system: Arc<WindowTextSystem>,
pub(crate) editor_style: EditorStyle, pub(crate) editor_style: EditorStyle,
pub(crate) rem_size: Pixels, pub(crate) rem_size: Pixels,
pub anchor: Anchor, pub scroll_anchor: ScrollAnchor,
pub visible_rows: Option<f32>, pub visible_rows: Option<f32>,
pub vertical_scroll_margin: f32,
} }
/// Returns a column to the left of the current point, wrapping /// Returns a column to the left of the current point, wrapping

View file

@ -6,13 +6,14 @@ use crate::{
display_map::{DisplaySnapshot, ToDisplayPoint}, display_map::{DisplaySnapshot, ToDisplayPoint},
hover_popover::hide_hover, hover_popover::hide_hover,
persistence::DB, persistence::DB,
Anchor, DisplayPoint, Editor, EditorEvent, EditorMode, InlayHintRefreshReason, Anchor, DisplayPoint, Editor, EditorEvent, EditorMode, EditorSettings, InlayHintRefreshReason,
MultiBufferSnapshot, ToPoint, MultiBufferSnapshot, ToPoint,
}; };
pub use autoscroll::{Autoscroll, AutoscrollStrategy}; pub use autoscroll::{Autoscroll, AutoscrollStrategy};
use gpui::{point, px, AppContext, Entity, Global, Pixels, Task, ViewContext}; use gpui::{point, px, AppContext, Entity, Global, Pixels, Task, ViewContext, WindowContext};
use language::{Bias, Point}; use language::{Bias, Point};
pub use scroll_amount::ScrollAmount; pub use scroll_amount::ScrollAmount;
use settings::Settings;
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
time::{Duration, Instant}, time::{Duration, Instant},
@ -21,7 +22,6 @@ use util::ResultExt;
use workspace::{ItemId, WorkspaceId}; use workspace::{ItemId, WorkspaceId};
pub const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28); pub const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28);
pub const VERTICAL_SCROLL_MARGIN: f32 = 3.;
const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1); const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
#[derive(Default)] #[derive(Default)]
@ -128,7 +128,7 @@ impl OngoingScroll {
} }
pub struct ScrollManager { pub struct ScrollManager {
vertical_scroll_margin: f32, pub(crate) vertical_scroll_margin: f32,
anchor: ScrollAnchor, anchor: ScrollAnchor,
ongoing: OngoingScroll, ongoing: OngoingScroll,
autoscroll_request: Option<(Autoscroll, bool)>, autoscroll_request: Option<(Autoscroll, bool)>,
@ -140,9 +140,9 @@ pub struct ScrollManager {
} }
impl ScrollManager { impl ScrollManager {
pub fn new() -> Self { pub fn new(cx: &mut WindowContext) -> Self {
ScrollManager { ScrollManager {
vertical_scroll_margin: VERTICAL_SCROLL_MARGIN, vertical_scroll_margin: EditorSettings::get_global(cx).vertical_scroll_margin,
anchor: ScrollAnchor::new(), anchor: ScrollAnchor::new(),
ongoing: OngoingScroll::new(), ongoing: OngoingScroll::new(),
autoscroll_request: None, autoscroll_request: None,

View file

@ -1044,9 +1044,17 @@ fn window_top(
map: &DisplaySnapshot, map: &DisplaySnapshot,
point: DisplayPoint, point: DisplayPoint,
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
times: usize, mut times: usize,
) -> (DisplayPoint, SelectionGoal) { ) -> (DisplayPoint, SelectionGoal) {
let first_visible_line = text_layout_details.anchor.to_display_point(map); let first_visible_line = text_layout_details
.scroll_anchor
.anchor
.to_display_point(map);
if first_visible_line.row() != 0 && text_layout_details.vertical_scroll_margin as usize > times
{
times = text_layout_details.vertical_scroll_margin.ceil() as usize;
}
if let Some(visible_rows) = text_layout_details.visible_rows { if let Some(visible_rows) = text_layout_details.visible_rows {
let bottom_row = first_visible_line.row() + visible_rows as u32; let bottom_row = first_visible_line.row() + visible_rows as u32;
@ -1070,7 +1078,10 @@ fn window_middle(
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
) -> (DisplayPoint, SelectionGoal) { ) -> (DisplayPoint, SelectionGoal) {
if let Some(visible_rows) = text_layout_details.visible_rows { if let Some(visible_rows) = text_layout_details.visible_rows {
let first_visible_line = text_layout_details.anchor.to_display_point(map); let first_visible_line = text_layout_details
.scroll_anchor
.anchor
.to_display_point(map);
let max_rows = (visible_rows as u32).min(map.max_buffer_row()); let max_rows = (visible_rows as u32).min(map.max_buffer_row());
let new_row = first_visible_line.row() + (max_rows.div_euclid(2)); let new_row = first_visible_line.row() + (max_rows.div_euclid(2));
let new_col = point.column().min(map.line_len(new_row)); let new_col = point.column().min(map.line_len(new_row));
@ -1085,11 +1096,20 @@ fn window_bottom(
map: &DisplaySnapshot, map: &DisplaySnapshot,
point: DisplayPoint, point: DisplayPoint,
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
times: usize, mut times: usize,
) -> (DisplayPoint, SelectionGoal) { ) -> (DisplayPoint, SelectionGoal) {
if let Some(visible_rows) = text_layout_details.visible_rows { if let Some(visible_rows) = text_layout_details.visible_rows {
let first_visible_line = text_layout_details.anchor.to_display_point(map); let first_visible_line = text_layout_details
let bottom_row = first_visible_line.row() + (visible_rows) as u32; .scroll_anchor
.anchor
.to_display_point(map);
let bottom_row = first_visible_line.row()
+ (visible_rows + text_layout_details.scroll_anchor.offset.y - 1.).floor() as u32;
if bottom_row < map.max_buffer_row()
&& text_layout_details.vertical_scroll_margin as usize > times
{
times = text_layout_details.vertical_scroll_margin.ceil() as usize;
}
let bottom_row_capped = bottom_row.min(map.max_buffer_row()); let bottom_row_capped = bottom_row.min(map.max_buffer_row());
let new_row = if bottom_row_capped.saturating_sub(times as u32) < first_visible_line.row() { let new_row = if bottom_row_capped.saturating_sub(times as u32) < first_visible_line.row() {
first_visible_line.row() first_visible_line.row()

View file

@ -1,11 +1,10 @@
use crate::Vim; use crate::Vim;
use editor::{ use editor::{
display_map::ToDisplayPoint, display_map::ToDisplayPoint, scroll::ScrollAmount, DisplayPoint, Editor, EditorSettings,
scroll::{ScrollAmount, VERTICAL_SCROLL_MARGIN},
DisplayPoint, Editor,
}; };
use gpui::{actions, ViewContext}; use gpui::{actions, ViewContext};
use language::Bias; use language::Bias;
use settings::Settings;
use workspace::Workspace; use workspace::Workspace;
actions!( actions!(
@ -77,6 +76,7 @@ fn scroll_editor(
}; };
let top_anchor = editor.scroll_manager.anchor().anchor; let top_anchor = editor.scroll_manager.anchor().anchor;
let vertical_scroll_margin = EditorSettings::get_global(cx).vertical_scroll_margin;
editor.change_selections(None, cx, |s| { editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| { s.move_with(|map, selection| {
@ -88,8 +88,8 @@ fn scroll_editor(
let new_row = top.row() + selection.head().row() - old_top.row(); let new_row = top.row() + selection.head().row() - old_top.row();
head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left) head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
} }
let min_row = top.row() + VERTICAL_SCROLL_MARGIN as u32; let min_row = top.row() + vertical_scroll_margin as u32;
let max_row = top.row() + visible_rows - VERTICAL_SCROLL_MARGIN as u32 - 1; let max_row = top.row() + visible_rows - vertical_scroll_margin as u32 - 1;
let new_head = if head.row() < min_row { let new_head = if head.row() < min_row {
map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left) map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)

View file

@ -1,4 +1,4 @@
use editor::{scroll::VERTICAL_SCROLL_MARGIN, test::editor_test_context::ContextHandle}; use editor::test::editor_test_context::ContextHandle;
use gpui::{px, size, Context}; use gpui::{px, size, Context};
use indoc::indoc; use indoc::indoc;
use settings::SettingsStore; use settings::SettingsStore;
@ -155,9 +155,7 @@ impl NeovimBackedTestContext {
pub async fn set_scroll_height(&mut self, rows: u32) { pub async fn set_scroll_height(&mut self, rows: u32) {
// match Zed's scrolling behavior // match Zed's scrolling behavior
self.neovim self.neovim.set_option(&format!("scrolloff={}", 3)).await;
.set_option(&format!("scrolloff={}", VERTICAL_SCROLL_MARGIN))
.await;
// +2 to account for the vim command UI at the bottom. // +2 to account for the vim command UI at the bottom.
self.neovim.set_option(&format!("lines={}", rows + 2)).await; self.neovim.set_option(&format!("lines={}", rows + 2)).await;
let (line_height, visible_line_count) = self.editor(|editor, cx| { let (line_height, visible_line_count) = self.editor(|editor, cx| {