Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Reveal all go to definitions in the center by default (#54778)

Kirill Bulatove483c3e1de63
diff --git a/assets/settings/default.json b/assets/settings/default.json
index 7e942393d57..464587e6290 100644
--- a/assets/settings/default.json
+++ b/assets/settings/default.json
@@ -355,6 +355,13 @@
// 1. Do nothing: `none`
// 2. Find references for the same symbol: `find_all_references` (default)
"go_to_definition_fallback": "find_all_references",
+ // How to scroll the target into view when navigating to a definition or reference
+ // (e.g. Go to Definition, Go to Type Definition, Find All References).
+ //
+ // 1. Vertically center the target in the viewport: `center` (default)
+ // 2. Scroll the minimum amount needed to make the target visible: `minimum`
+ // 3. Scroll so the target appears near the top of the viewport: `top`
+ "go_to_definition_scroll_strategy": "center",
// Which level to use to filter out diagnostics displayed in the editor.
//
// Affects the editor rendering only, and does not interrupt
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 7d425fa98f4..8110c211a50 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -18339,7 +18339,7 @@ impl Editor {
};
let anchor_range = range.to_anchors(&multibuffer.snapshot(cx));
self.change_selections(
- SelectionEffects::default().nav_history(true),
+ SelectionEffects::scroll(Autoscroll::for_go_to_definition(cx)).nav_history(true),
window,
cx,
|s| s.select_anchor_ranges([anchor_range]),
@@ -19145,7 +19145,8 @@ impl Editor {
}
editor.change_selections(
- SelectionEffects::default().nav_history(true),
+ SelectionEffects::scroll(Autoscroll::for_go_to_definition(cx))
+ .nav_history(true),
window,
cx,
|s| s.select_anchor_ranges(target_ranges),
@@ -19224,7 +19225,8 @@ impl Editor {
}
target_editor.change_selections(
- SelectionEffects::default().nav_history(true),
+ SelectionEffects::scroll(Autoscroll::for_go_to_definition(cx))
+ .nav_history(true),
window,
cx,
|s| s.select_anchor_ranges(target_ranges),
@@ -19504,7 +19506,7 @@ impl Editor {
let Range { start, end } = locations[destination_location_index];
editor.update_in(cx, |editor, window, cx| {
- let effects = SelectionEffects::default();
+ let effects = SelectionEffects::scroll(Autoscroll::for_go_to_definition(cx));
editor.unfold_ranges(&[start..end], false, false, cx);
editor.change_selections(effects, window, cx, |s| {
diff --git a/crates/editor/src/editor_settings.rs b/crates/editor/src/editor_settings.rs
index b35bce02af4..1999d0e537e 100644
--- a/crates/editor/src/editor_settings.rs
+++ b/crates/editor/src/editor_settings.rs
@@ -5,9 +5,10 @@ use language::CursorShape;
use project::project_settings::DiagnosticSeverity;
pub use settings::{
CodeLens, CompletionDetailAlignment, CurrentLineHighlight, DelayMs, DiffViewStyle, DisplayIn,
- DocumentColorsRenderMode, DoubleClickInMultibuffer, GoToDefinitionFallback, HideMouseMode,
- MinimapThumb, MinimapThumbBorder, MultiCursorModifier, ScrollBeyondLastLine,
- ScrollbarDiagnostics, SeedQuerySetting, ShowMinimap, SnippetSortOrder,
+ DocumentColorsRenderMode, DoubleClickInMultibuffer, GoToDefinitionFallback,
+ GoToDefinitionScrollStrategy, HideMouseMode, MinimapThumb, MinimapThumbBorder,
+ MultiCursorModifier, ScrollBeyondLastLine, ScrollbarDiagnostics, SeedQuerySetting, ShowMinimap,
+ SnippetSortOrder,
};
use settings::{RegisterSetting, RelativeLineNumbers, Settings};
use ui::scrollbars::ShowScrollbar;
@@ -52,6 +53,7 @@ pub struct EditorSettings {
pub auto_signature_help: bool,
pub show_signature_help_after_edits: bool,
pub go_to_definition_fallback: GoToDefinitionFallback,
+ pub go_to_definition_scroll_strategy: GoToDefinitionScrollStrategy,
pub jupyter: Jupyter,
pub hide_mouse: Option<HideMouseMode>,
pub snippet_sort_order: SnippetSortOrder,
@@ -285,6 +287,7 @@ impl Settings for EditorSettings {
auto_signature_help: editor.auto_signature_help.unwrap(),
show_signature_help_after_edits: editor.show_signature_help_after_edits.unwrap(),
go_to_definition_fallback: editor.go_to_definition_fallback.unwrap(),
+ go_to_definition_scroll_strategy: editor.go_to_definition_scroll_strategy.unwrap(),
jupyter: Jupyter {
enabled: editor.jupyter.unwrap().enabled.unwrap(),
},
diff --git a/crates/editor/src/scroll/autoscroll.rs b/crates/editor/src/scroll/autoscroll.rs
index c2b07ffa96a..38f0f4b0228 100644
--- a/crates/editor/src/scroll/autoscroll.rs
+++ b/crates/editor/src/scroll/autoscroll.rs
@@ -2,6 +2,7 @@ use crate::{
DisplayPoint, DisplayRow, Editor, EditorMode, EditorSettings, LineWithInvisibles, RowExt,
SelectionEffects,
display_map::{DisplaySnapshot, ToDisplayPoint},
+ editor_settings::GoToDefinitionScrollStrategy,
scroll::{ScrollOffset, WasScrolled},
};
use gpui::{App, Bounds, Context, Pixels, Window};
@@ -33,6 +34,16 @@ impl Autoscroll {
Self::Strategy(AutoscrollStrategy::Center, None)
}
+ /// Returns the autoscroll strategy configured for navigation to definitions
+ /// and references, based on `go_to_definition_scroll_strategy`.
+ pub fn for_go_to_definition(cx: &App) -> Self {
+ match EditorSettings::get_global(cx).go_to_definition_scroll_strategy {
+ GoToDefinitionScrollStrategy::Center => Self::center(),
+ GoToDefinitionScrollStrategy::Minimum => Self::fit(),
+ GoToDefinitionScrollStrategy::Top => Self::focused(),
+ }
+ }
+
/// scrolls so the newest cursor is near the top
/// (offset by vertical_scroll_margin)
pub fn focused() -> Self {
diff --git a/crates/settings/src/vscode_import.rs b/crates/settings/src/vscode_import.rs
index 31bcaf847b0..64f00bc5d65 100644
--- a/crates/settings/src/vscode_import.rs
+++ b/crates/settings/src/vscode_import.rs
@@ -265,6 +265,7 @@ impl VsCodeSettings {
fast_scroll_sensitivity: self.read_f32("editor.fastScrollSensitivity"),
sticky_scroll: self.sticky_scroll_content(),
go_to_definition_fallback: None,
+ go_to_definition_scroll_strategy: None,
gutter: self.gutter_content(),
hide_mouse: None,
horizontal_scroll_margin: None,
diff --git a/crates/settings_content/src/editor.rs b/crates/settings_content/src/editor.rs
index 3ba21e83082..18a7bd5fd49 100644
--- a/crates/settings_content/src/editor.rs
+++ b/crates/settings_content/src/editor.rs
@@ -192,6 +192,12 @@ pub struct EditorSettingsContent {
/// Default: FindAllReferences
pub go_to_definition_fallback: Option<GoToDefinitionFallback>,
+ /// How to scroll the target into view when navigating to a definition or reference
+ /// (e.g. Go to Definition, Go to Type Definition, Find All References).
+ ///
+ /// Default: center
+ pub go_to_definition_scroll_strategy: Option<GoToDefinitionScrollStrategy>,
+
/// Jupyter REPL settings.
pub jupyter: Option<JupyterContent>,
@@ -799,6 +805,34 @@ pub enum GoToDefinitionFallback {
FindAllReferences,
}
+/// How to scroll the target into view when navigating to a definition or reference.
+///
+/// Default: center
+#[derive(
+ Copy,
+ Clone,
+ Debug,
+ Default,
+ Serialize,
+ Deserialize,
+ PartialEq,
+ Eq,
+ JsonSchema,
+ MergeFrom,
+ strum::VariantArray,
+ strum::VariantNames,
+)]
+#[serde(rename_all = "snake_case")]
+pub enum GoToDefinitionScrollStrategy {
+ /// Vertically center the target in the viewport.
+ #[default]
+ Center,
+ /// Scroll the minimum amount needed to make the target visible.
+ Minimum,
+ /// Scroll so the target appears near the top of the viewport.
+ Top,
+}
+
/// Determines when the mouse cursor should be hidden in an editor or input box.
///
/// Default: on_typing_and_movement
diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs
index 8a27197f7f0..5b43d0a1842 100644
--- a/crates/settings_ui/src/page_data.rs
+++ b/crates/settings_ui/src/page_data.rs
@@ -9106,7 +9106,7 @@ fn language_settings_data() -> Box<[SettingsPageItem]> {
/// LanguageSettings items that should be included in the "Languages & Tools" page
/// not the "Editor" page
fn non_editor_language_settings_data() -> Box<[SettingsPageItem]> {
- fn lsp_section() -> [SettingsPageItem; 8] {
+ fn lsp_section() -> [SettingsPageItem; 9] {
[
SettingsPageItem::SectionHeader("LSP"),
SettingsPageItem::SettingItem(SettingItem {
@@ -9188,6 +9188,24 @@ fn non_editor_language_settings_data() -> Box<[SettingsPageItem]> {
metadata: None,
files: USER,
}),
+ SettingsPageItem::SettingItem(SettingItem {
+ title: "Go To Definition Scroll Strategy",
+ description: "How to scroll the target into view when navigating to a definition or reference.",
+ field: Box::new(SettingField {
+ json_path: Some("go_to_definition_scroll_strategy"),
+ pick: |settings_content| {
+ settings_content
+ .editor
+ .go_to_definition_scroll_strategy
+ .as_ref()
+ },
+ write: |settings_content, value, _| {
+ settings_content.editor.go_to_definition_scroll_strategy = value;
+ },
+ }),
+ metadata: None,
+ files: USER,
+ }),
SettingsPageItem::SettingItem(SettingItem {
title: "Semantic Tokens",
description: {
diff --git a/crates/settings_ui/src/settings_ui.rs b/crates/settings_ui/src/settings_ui.rs
index d6696cc7033..a718e1277ef 100644
--- a/crates/settings_ui/src/settings_ui.rs
+++ b/crates/settings_ui/src/settings_ui.rs
@@ -483,6 +483,7 @@ fn init_renderers(cx: &mut App) {
.add_basic_renderer::<settings::SeedQuerySetting>(render_dropdown)
.add_basic_renderer::<settings::DoubleClickInMultibuffer>(render_dropdown)
.add_basic_renderer::<settings::GoToDefinitionFallback>(render_dropdown)
+ .add_basic_renderer::<settings::GoToDefinitionScrollStrategy>(render_dropdown)
.add_basic_renderer::<settings::ActivateOnClose>(render_dropdown)
.add_basic_renderer::<settings::ShowDiagnostics>(render_dropdown)
.add_basic_renderer::<settings::ShowCloseButton>(render_dropdown)
diff --git a/docs/src/reference/all-settings.md b/docs/src/reference/all-settings.md
index e96dfe0655d..c6c33b8b5ed 100644
--- a/docs/src/reference/all-settings.md
+++ b/docs/src/reference/all-settings.md
@@ -2424,6 +2424,38 @@ Example:
}
```
+## Go to Definition Scroll Strategy
+
+- Description: How to scroll the target into view when navigating to a definition or reference (e.g. {#action editor::GoToDefinition}, {#action editor::GoToTypeDefinition}, {#action editor::FindAllReferences}).
+- Setting: `go_to_definition_scroll_strategy`
+- Default: `"center"`
+
+**Options**
+
+1. Vertically center the target in the viewport (default):
+
+```json [settings]
+{
+ "go_to_definition_scroll_strategy": "center"
+}
+```
+
+2. Scroll the minimum amount needed to make the target visible:
+
+```json [settings]
+{
+ "go_to_definition_scroll_strategy": "minimum"
+}
+```
+
+3. Scroll so the target appears near the top of the viewport:
+
+```json [settings]
+{
+ "go_to_definition_scroll_strategy": "top"
+}
+```
+
## Hard Tabs
- Description: Whether to indent lines using tab characters or multiple spaces.