feat: implement inline git blame

This commit is contained in:
Nik Revenco 2025-03-18 19:09:02 +00:00
parent 711aa58d40
commit ddf8ac1158
7 changed files with 23 additions and 54 deletions

View file

@ -37,6 +37,6 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
auto_save::register_hooks(&handlers);
diagnostics::register_hooks(&handlers);
snippet::register_hooks(&handlers);
// blame::register_hooks(&handlers);
blame::register_hooks(&handlers);
handlers
}

View file

@ -62,12 +62,13 @@ fn request_git_blame(editor: &mut Editor) {
return;
};
doc.blame = Some(vec![InlineAnnotation::new(
text.try_line_to_char(cursor_lin + 1)
.unwrap_or(text.len_chars())
// to get the last position in the current line
- 1,
output.to_string(),
)]);
log::error!("{:?}", doc.blame);
doc.blame = Some(output.to_string());
// doc.blame = Some(vec![InlineAnnotation::new(
// text.try_line_to_char(cursor_lin + 1)
// .unwrap_or(text.len_chars())
// // to get the last position in the current line
// - 1,
// output.to_string(),
// )]);
// log::error!("{:?}", doc.blame);
}

View file

@ -202,12 +202,14 @@ impl EditorView {
config.end_of_line_diagnostics,
));
if config.vcs.blame {
decorations.add_decoration(text_decorations::blame::EolBlame::new(
doc,
theme,
doc.text().char_to_line(primary_cursor),
"hello world".to_string(),
));
if let Some(blame) = &doc.blame {
decorations.add_decoration(text_decorations::blame::EolBlame::new(
doc,
theme,
doc.text().char_to_line(primary_cursor),
blame,
));
}
}
render_document(
surface,

View file

@ -1,6 +1,5 @@
#![allow(dead_code, unused_variables, unused_mut)]
use helix_core::doc_formatter::FormattedGrapheme;
use helix_core::Position;
use helix_view::theme::Style;
@ -10,14 +9,14 @@ use crate::ui::document::{LinePos, TextRenderer};
use crate::ui::text_decorations::Decoration;
pub struct EolBlame<'a> {
message: String,
message: &'a str,
doc: &'a Document,
cursor: usize,
style: Style,
}
impl<'a> EolBlame<'a> {
pub fn new(doc: &'a Document, theme: &Theme, cursor: usize, message: String) -> Self {
pub fn new(doc: &'a Document, theme: &Theme, cursor: usize, message: &'a str) -> Self {
EolBlame {
style: theme.get("ui.virtual.blame"),
message,
@ -28,12 +27,6 @@ impl<'a> EolBlame<'a> {
}
impl Decoration for EolBlame<'_> {
// fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
// // renderer.draw_dec
// // ration_grapheme(grapheme, style, row, col)
// let col_off = 50;
// }
fn render_virt_lines(
&mut self,
renderer: &mut TextRenderer,
@ -45,9 +38,6 @@ impl Decoration for EolBlame<'_> {
}
let row = pos.visual_line;
let col = virt_off.col as u16;
// if col != self.cursor as u16 {
// return Position::new(0, 0);
// }
let style = self.style;
let width = renderer.viewport.width;
let start_col = col - renderer.offset.col as u16;
@ -61,7 +51,7 @@ impl Decoration for EolBlame<'_> {
.set_string_truncated(
renderer.viewport.x + draw_col,
row,
&self.message,
self.message,
width.saturating_sub(draw_col) as usize,
|_| self.style,
true,
@ -75,20 +65,4 @@ impl Decoration for EolBlame<'_> {
Position::new(0, col_off as usize)
}
// fn reset_pos(&mut self, _pos: usize) -> usize {
// usize::MAX
// }
// fn skip_concealed_anchor(&mut self, conceal_end_char_idx: usize) -> usize {
// self.reset_pos(conceal_end_char_idx)
// }
// fn decorate_grapheme(
// &mut self,
// _renderer: &mut TextRenderer,
// _grapheme: &FormattedGrapheme,
// ) -> usize {
// usize::MAX
// }
}

View file

@ -137,7 +137,7 @@ impl fmt::Display for BlameInformation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
" {} - {} - {} - {}",
"{} • {} • {} • {}",
self.author_name, self.commit_date, self.commit_message, self.commit_hash
)
}

View file

@ -143,7 +143,7 @@ pub struct Document {
///
/// To know if they're up-to-date, check the `id` field in `DocumentInlayHints`.
pub(crate) inlay_hints: HashMap<ViewId, DocumentInlayHints>,
pub blame: Option<Vec<InlineAnnotation>>,
pub blame: Option<String>,
pub(crate) jump_labels: HashMap<ViewId, Vec<Overlay>>,
/// Set to `true` when the document is updated, reset to `false` on the next inlay hints
/// update from the LSP

View file

@ -452,14 +452,6 @@ impl View {
text_annotations.add_overlay(labels, style);
}
if let Some(blame_annotation) = &doc.blame {
let annotation_style = theme
.and_then(|t| t.find_scope_index("ui.virtual.inlay-hint.type"))
.map(Highlight);
text_annotations.add_inline_annotations(blame_annotation, annotation_style);
}
if let Some(DocumentInlayHints {
id: _,
type_inlay_hints,