editor: Move blame popover from hover_tooltip to editor prepaint (#29320)

WIP!

In light of having more control over blame popover from editor.

This fixes: https://github.com/zed-industries/zed/issues/28645,
https://github.com/zed-industries/zed/issues/26304

- [x] Initial rendering
- [x] Handle smart positioning (edge detection, etc)
- [x] Delayed hovering, release, etc
- [x] Test blame message selection
- [x] Fix tagged issues

Release Notes:

- Git inline blame popover now dismisses when the cursor is moved, the
editor is scrolled, or the command palette is opened.
This commit is contained in:
Smit Barmase 2025-04-25 01:52:24 +05:30 committed by GitHub
parent 87f85f1863
commit d3911e34de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 501 additions and 83 deletions

View file

@ -78,18 +78,19 @@ use futures::{
};
use fuzzy::StringMatchCandidate;
use ::git::Restore;
use ::git::blame::BlameEntry;
use ::git::{Restore, blame::ParsedCommitMessage};
use code_context_menus::{
AvailableCodeAction, CodeActionContents, CodeActionsItem, CodeActionsMenu, CodeContextMenu,
CompletionsMenu, ContextMenuOrigin,
};
use git::blame::{GitBlame, GlobalBlameRenderer};
use gpui::{
Action, Animation, AnimationExt, AnyElement, AnyWeakEntity, App, AppContext,
AsyncWindowContext, AvailableSpace, Background, Bounds, ClickEvent, ClipboardEntry,
ClipboardItem, Context, DispatchPhase, Edges, Entity, EntityInputHandler, EventEmitter,
FocusHandle, FocusOutEvent, Focusable, FontId, FontWeight, Global, HighlightStyle, Hsla,
KeyContext, Modifiers, MouseButton, MouseDownEvent, PaintQuad, ParentElement, Pixels, Render,
Action, Animation, AnimationExt, AnyElement, App, AppContext, AsyncWindowContext,
AvailableSpace, Background, Bounds, ClickEvent, ClipboardEntry, ClipboardItem, Context,
DispatchPhase, Edges, Entity, EntityInputHandler, EventEmitter, FocusHandle, FocusOutEvent,
Focusable, FontId, FontWeight, Global, HighlightStyle, Hsla, KeyContext, Modifiers,
MouseButton, MouseDownEvent, PaintQuad, ParentElement, Pixels, Render, ScrollHandle,
SharedString, Size, Stateful, Styled, Subscription, Task, TextStyle, TextStyleRefinement,
UTF16Selection, UnderlineStyle, UniformListScrollHandle, WeakEntity, WeakFocusHandle, Window,
div, impl_actions, point, prelude::*, pulsating_between, px, relative, size,
@ -117,6 +118,7 @@ use language::{
};
use language::{BufferRow, CharClassifier, Runnable, RunnableRange, point_to_lsp};
use linked_editing_ranges::refresh_linked_ranges;
use markdown::Markdown;
use mouse_context_menu::MouseContextMenu;
use persistence::DB;
use project::{
@ -798,6 +800,21 @@ impl ChangeList {
}
}
#[derive(Clone)]
struct InlineBlamePopoverState {
scroll_handle: ScrollHandle,
commit_message: Option<ParsedCommitMessage>,
markdown: Entity<Markdown>,
}
struct InlineBlamePopover {
position: gpui::Point<Pixels>,
show_task: Option<Task<()>>,
hide_task: Option<Task<()>>,
popover_bounds: Option<Bounds<Pixels>>,
popover_state: InlineBlamePopoverState,
}
/// Zed's primary implementation of text input, allowing users to edit a [`MultiBuffer`].
///
/// See the [module level documentation](self) for more information.
@ -866,6 +883,7 @@ pub struct Editor {
context_menu_options: Option<ContextMenuOptions>,
mouse_context_menu: Option<MouseContextMenu>,
completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
inline_blame_popover: Option<InlineBlamePopover>,
signature_help_state: SignatureHelpState,
auto_signature_help: Option<bool>,
find_all_references_task_sources: Vec<Anchor>,
@ -922,7 +940,6 @@ pub struct Editor {
show_git_blame_gutter: bool,
show_git_blame_inline: bool,
show_git_blame_inline_delay_task: Option<Task<()>>,
pub git_blame_inline_tooltip: Option<AnyWeakEntity>,
git_blame_inline_enabled: bool,
render_diff_hunk_controls: RenderDiffHunkControlsFn,
serialize_dirty_buffers: bool,
@ -1665,6 +1682,7 @@ impl Editor {
context_menu_options: None,
mouse_context_menu: None,
completion_tasks: Default::default(),
inline_blame_popover: Default::default(),
signature_help_state: SignatureHelpState::default(),
auto_signature_help: None,
find_all_references_task_sources: Vec::new(),
@ -1730,7 +1748,6 @@ impl Editor {
show_git_blame_inline: false,
show_selection_menu: None,
show_git_blame_inline_delay_task: None,
git_blame_inline_tooltip: None,
git_blame_inline_enabled: ProjectSettings::get_global(cx).git.inline_blame_enabled(),
render_diff_hunk_controls: Arc::new(render_diff_hunk_controls),
serialize_dirty_buffers: ProjectSettings::get_global(cx)
@ -1806,6 +1823,7 @@ impl Editor {
);
});
editor.hide_signature_help(cx, SignatureHelpHiddenBy::Escape);
editor.inline_blame_popover.take();
}
}
EditorEvent::Edited { .. } => {
@ -2603,6 +2621,7 @@ impl Editor {
self.update_visible_inline_completion(window, cx);
self.edit_prediction_requires_modifier_in_indent_conflict = true;
linked_editing_ranges::refresh_linked_ranges(self, window, cx);
self.inline_blame_popover.take();
if self.git_blame_inline_enabled {
self.start_inline_blame_timer(window, cx);
}
@ -5483,6 +5502,82 @@ impl Editor {
}
}
fn show_blame_popover(
&mut self,
blame_entry: &BlameEntry,
position: gpui::Point<Pixels>,
cx: &mut Context<Self>,
) {
if let Some(state) = &mut self.inline_blame_popover {
state.hide_task.take();
cx.notify();
} else {
let delay = EditorSettings::get_global(cx).hover_popover_delay;
let show_task = cx.spawn(async move |editor, cx| {
cx.background_executor()
.timer(std::time::Duration::from_millis(delay))
.await;
editor
.update(cx, |editor, cx| {
if let Some(state) = &mut editor.inline_blame_popover {
state.show_task = None;
cx.notify();
}
})
.ok();
});
let Some(blame) = self.blame.as_ref() else {
return;
};
let blame = blame.read(cx);
let details = blame.details_for_entry(&blame_entry);
let markdown = cx.new(|cx| {
Markdown::new(
details
.as_ref()
.map(|message| message.message.clone())
.unwrap_or_default(),
None,
None,
cx,
)
});
self.inline_blame_popover = Some(InlineBlamePopover {
position,
show_task: Some(show_task),
hide_task: None,
popover_bounds: None,
popover_state: InlineBlamePopoverState {
scroll_handle: ScrollHandle::new(),
commit_message: details,
markdown,
},
});
}
}
fn hide_blame_popover(&mut self, cx: &mut Context<Self>) {
if let Some(state) = &mut self.inline_blame_popover {
if state.show_task.is_some() {
self.inline_blame_popover.take();
cx.notify();
} else {
let hide_task = cx.spawn(async move |editor, cx| {
cx.background_executor()
.timer(std::time::Duration::from_millis(100))
.await;
editor
.update(cx, |editor, cx| {
editor.inline_blame_popover.take();
cx.notify();
})
.ok();
});
state.hide_task = Some(hide_task);
}
}
}
fn refresh_document_highlights(&mut self, cx: &mut Context<Self>) -> Option<()> {
if self.pending_rename.is_some() {
return None;
@ -16657,12 +16752,7 @@ impl Editor {
pub fn render_git_blame_inline(&self, window: &Window, cx: &App) -> bool {
self.show_git_blame_inline
&& (self.focus_handle.is_focused(window)
|| self
.git_blame_inline_tooltip
.as_ref()
.and_then(|t| t.upgrade())
.is_some())
&& (self.focus_handle.is_focused(window) || self.inline_blame_popover.is_some())
&& !self.newest_selection_head_on_empty_line(cx)
&& self.has_blame_entries(cx)
}