fix: center LSP definition destinations in the viewport

This commit is contained in:
Valdemar Lorenzen
2026-10-13 12:50:57 +02:00
parent e36c28c981
commit c4f72a45ba
5 changed files with 457 additions and 1 deletions
+26
View File
@@ -8,6 +8,7 @@ use crate::plugin::{
};
use serde::Serialize;
use std::borrow::Cow;
use std::cell::Cell;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::fmt;
use std::fs::{self, OpenOptions};
@@ -1337,6 +1338,7 @@ struct ViewState {
search_match: Option<SearchMatch>,
command_line: String,
caret_reveal_revision: u64,
lsp_center: Cell<Option<(u64, u64, Cursor)>>,
insert_start: Option<Snapshot>,
pending_edit: Option<EditRecipe>,
visual_motions: Option<Vec<Motion>>,
@@ -1354,6 +1356,7 @@ impl ViewState {
search_match: None,
command_line: String::new(),
caret_reveal_revision: 0,
lsp_center: Cell::new(None),
insert_start: None,
pending_edit: None,
visual_motions: None,
@@ -3527,6 +3530,7 @@ impl Editor {
target_range: Option<Utf16Range>,
) -> Result<(), String> {
self.pending_lsp_navigation = None;
self.active.view.lsp_center.set(None);
if path.as_os_str().is_empty() || path.to_string_lossy().chars().any(char::is_control) {
return Err("Navigation path must be nonempty and contain no controls".into());
}
@@ -3808,6 +3812,7 @@ impl Editor {
diff.normalize_focus();
diff.reveal_source_row(focus, row);
self.place_navigation(position);
self.request_lsp_center();
self.record_jump(source);
Ok(())
}
@@ -3829,6 +3834,7 @@ impl Editor {
self.switch_buffer(id);
}
self.place_navigation(position);
self.request_lsp_center();
self.record_jump(source);
}
@@ -3933,7 +3939,26 @@ impl Editor {
self.active.view.caret_reveal_revision
}
fn request_lsp_center(&self) {
self.active.view.lsp_center.set(Some((
self.text_revision(),
self.caret_reveal_revision(),
self.cursor(),
)));
}
/// Consume only when the destination has a usable text viewport.
pub(crate) fn take_lsp_center(&self) -> bool {
self.active.view.lsp_center.take()
== Some((
self.text_revision(),
self.caret_reveal_revision(),
self.cursor(),
))
}
fn reveal_caret(&mut self) {
self.active.view.lsp_center.set(None);
self.active.view.caret_reveal_revision =
self.active.view.caret_reveal_revision.wrapping_add(1);
}
@@ -5613,6 +5638,7 @@ impl Editor {
pub fn handle(&mut self, input: Input) -> Option<Invocation> {
self.pending_lsp_navigation = None;
self.active.view.lsp_center.set(None);
// Normalize genuine terminal aliases before list and editing dispatch.
let input = match input {
Input::Ctrl(ch) => control_key(ch),