Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go to line feature for the blame view #2262

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Set `CREATE_NO_WINDOW` flag when executing Git hooks on Windows ([#2371](https://github.com/extrawurst/gitui/pull/2371))

### Added

* Add "go to line" command for the blame view [[@andrea-berling](https://github.com/andrea-berling)] ([#2262](https://github.com/extrawurst/gitui/pull/2262))
* support for "Copy Path" action in WSL [[@johnDeSilencio](https://github.com/johnDeSilencio)] ([#2413](https://github.com/extrawurst/gitui/pull/2413))
* help popup scrollbar [[@wugeer](https://github.com/wugeer)](https://github.com/extrawurst/gitui/pull/2388))
* add popups for viewing, adding, updating and removing remotes [[@robin-thoene](https://github.com/robin-thoene)] ([#2172](https://github.com/extrawurst/gitui/issues/2172))
Expand Down
6 changes: 4 additions & 2 deletions asyncgit/src/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ use std::{
};

///
#[derive(Hash, Clone, PartialEq, Eq)]
#[derive(Hash, Clone, PartialEq, Eq, Debug)]
pub struct BlameParams {
/// path to the file to blame
pub file_path: String,
/// blame at a specific revision
pub commit_id: Option<CommitId>,
}

#[derive(Debug)]
struct Request<R, A>(R, Option<A>);

#[derive(Default, Clone)]
#[derive(Debug, Default, Clone)]
struct LastResult<P, R> {
params: P,
result: R,
}

///
#[derive(Debug, Clone)]
pub struct AsyncBlame {
current: Arc<Mutex<Request<u64, FileBlame>>>,
last: Arc<Mutex<Option<LastResult<BlameParams, FileBlame>>>>,
Expand Down
49 changes: 38 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use crate::{
options::{Options, SharedOptions},
popup_stack::PopupStack,
popups::{
AppOption, BlameFilePopup, BranchListPopup, CommitPopup,
CompareCommitsPopup, ConfirmPopup, CreateBranchPopup,
CreateRemotePopup, ExternalEditorPopup, FetchPopup,
FileRevlogPopup, FuzzyFindPopup, HelpPopup,
InspectCommitPopup, LogSearchPopupPopup, MsgPopup,
OptionsPopup, PullPopup, PushPopup, PushTagsPopup,
RemoteListPopup, RenameBranchPopup, RenameRemotePopup,
ResetPopup, RevisionFilesPopup, StashMsgPopup,
SubmodulesListPopup, TagCommitPopup, TagListPopup,
UpdateRemoteUrlPopup,
AppOption, BlameFileOpen, BlameFilePopup, BlameRequest,
BranchListPopup, CommitPopup, CompareCommitsPopup,
ConfirmPopup, CreateBranchPopup, CreateRemotePopup,
ExternalEditorPopup, FetchPopup, FileRevlogPopup,
FuzzyFindPopup, GotoLinePopup, HelpPopup, InspectCommitPopup,
LogSearchPopupPopup, MsgPopup, OptionsPopup, PullPopup,
PushPopup, PushTagsPopup, RemoteListPopup, RenameBranchPopup,
RenameRemotePopup, ResetPopup, RevisionFilesPopup,
StashMsgPopup, SubmodulesListPopup, TagCommitPopup,
TagListPopup, UpdateRemoteUrlPopup,
},
queue::{
Action, AppTabs, InternalEvent, NeedsUpdate, Queue,
Expand Down Expand Up @@ -112,6 +112,7 @@ pub struct App {
popup_stack: PopupStack,
options: SharedOptions,
repo_path_text: String,
goto_line_popup: GotoLinePopup,

// "Flags"
requires_redraw: Cell<bool>,
Expand Down Expand Up @@ -218,6 +219,7 @@ impl App {
stashing_tab: Stashing::new(&env),
stashlist_tab: StashList::new(&env),
files_tab: FilesTab::new(&env),
goto_line_popup: GotoLinePopup::new(&env),
tab: 0,
queue: env.queue,
theme: env.theme,
Expand Down Expand Up @@ -481,6 +483,7 @@ impl App {
msg_popup,
confirm_popup,
commit_popup,
goto_line_popup,
blame_file_popup,
file_revlog_popup,
stashmsg_popup,
Expand Down Expand Up @@ -544,7 +547,8 @@ impl App {
fetch_popup,
options_popup,
confirm_popup,
msg_popup
msg_popup,
goto_line_popup
]
);

Expand Down Expand Up @@ -691,6 +695,9 @@ impl App {
StackablePopupOpen::CompareCommits(param) => {
self.compare_commits_popup.open(param)?;
}
StackablePopupOpen::GotoLine(param) => {
self.goto_line_popup.open(param);
}
}

Ok(())
Expand Down Expand Up @@ -905,6 +912,26 @@ impl App {
InternalEvent::CommitSearch(options) => {
self.revlog.search(options);
}
InternalEvent::GotoLine(line) => {
if let Some(popup) = self.popup_stack.pop() {
if let StackablePopupOpen::BlameFile(params) =
popup
{
self.popup_stack.push(
StackablePopupOpen::BlameFile(
BlameFileOpen {
selection: Some(line),
blame: BlameRequest::KeepExisting,
..params
},
),
);
}
flags.insert(
NeedsUpdate::ALL | NeedsUpdate::COMMANDS,
);
}
}
};

Ok(flags)
Expand Down
3 changes: 2 additions & 1 deletion src/components/revision_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
use crate::{
app::Environment,
keys::{key_match, SharedKeyConfig},
popups::{BlameFileOpen, FileRevOpen},
popups::{BlameFileOpen, BlameRequest, FileRevOpen},
queue::{InternalEvent, Queue, StackablePopupOpen},
strings::{self, order, symbol},
try_or_popup,
Expand Down Expand Up @@ -193,6 +193,7 @@ impl RevisionFilesComponent {
file_path: path,
commit_id: self.revision.as_ref().map(|c| c.id),
selection: None,
blame: BlameRequest::StartNew,
}),
));

Expand Down
3 changes: 2 additions & 1 deletion src/components/status_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
app::Environment,
components::{CommandInfo, Component, EventState},
keys::{key_match, SharedKeyConfig},
popups::{BlameFileOpen, FileRevOpen},
popups::{BlameFileOpen, BlameRequest, FileRevOpen},
queue::{InternalEvent, NeedsUpdate, Queue, StackablePopupOpen},
strings::{self, order},
ui::{self, style::SharedTheme},
Expand Down Expand Up @@ -456,6 +456,7 @@ impl Component for StatusTreeComponent {
file_path: status_item.path,
commit_id: self.revision,
selection: None,
blame: BlameRequest::StartNew,
},
),
));
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub struct KeysList {
pub commit_history_next: GituiKeyEvent,
pub commit: GituiKeyEvent,
pub newline: GituiKeyEvent,
pub goto_line: GituiKeyEvent,
}

#[rustfmt::skip]
Expand Down Expand Up @@ -225,6 +226,7 @@ impl Default for KeysList {
commit_history_next: GituiKeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL),
commit: GituiKeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
newline: GituiKeyEvent::new(KeyCode::Enter, KeyModifiers::empty()),
goto_line: GituiKeyEvent::new(KeyCode::Char('L'), KeyModifiers::SHIFT),
}
}
}
Expand Down
69 changes: 60 additions & 9 deletions src/popups/blame_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ use ratatui::{
};
use std::path::Path;

use super::{goto_line::GotoLineContext, GotoLineOpen};

static NO_COMMIT_ID: &str = "0000000";
static NO_AUTHOR: &str = "<no author>";
static MIN_AUTHOR_WIDTH: usize = 3;
static MAX_AUTHOR_WIDTH: usize = 20;

struct SyntaxFileBlame {
#[derive(Debug, Clone)]
pub struct SyntaxFileBlame {
pub file_blame: FileBlame,
pub styled_text: Option<SyntaxText>,
}
Expand All @@ -54,7 +57,8 @@ impl SyntaxFileBlame {
}
}

enum BlameProcess {
#[derive(Clone, Debug)]
pub enum BlameProcess {
GettingBlame(AsyncBlame),
SyntaxHighlighting {
unstyled_file_blame: SyntaxFileBlame,
Expand All @@ -76,11 +80,18 @@ impl BlameProcess {
}
}

#[derive(Clone, Debug)]
pub enum BlameRequest {
StartNew,
KeepExisting,
}

#[derive(Clone, Debug)]
pub struct BlameFileOpen {
pub file_path: String,
pub commit_id: Option<CommitId>,
pub selection: Option<usize>,
pub blame: BlameRequest,
}

pub struct BlameFilePopup {
Expand Down Expand Up @@ -234,6 +245,16 @@ impl Component for BlameFilePopup {
)
.order(1),
);
out.push(
CommandInfo::new(
strings::commands::open_line_number_popup(
&self.key_config,
),
true,
has_result,
)
.order(1),
);
}

visibility_blocking(self)
Expand Down Expand Up @@ -307,6 +328,31 @@ impl Component for BlameFilePopup {
),
));
}
} else if key_match(
key,
self.key_config.keys.goto_line,
) {
let maybe_blame_result = &self
.blame
.as_ref()
.and_then(|blame| blame.result());
if maybe_blame_result.is_some() {
let max_line = maybe_blame_result
.expect("This can not be None")
.lines()
.len() - 1;
self.hide_stacked(true);
self.visible = true;
self.queue.push(InternalEvent::OpenPopup(
StackablePopupOpen::GotoLine(
GotoLineOpen {
context: GotoLineContext {
max_line,
},
},
),
));
}
}

return Ok(EventState::Consumed);
Expand Down Expand Up @@ -356,6 +402,7 @@ impl BlameFilePopup {
file_path: request.file_path,
commit_id: request.commit_id,
selection: self.get_selection(),
blame: BlameRequest::KeepExisting,
}),
));
}
Expand All @@ -371,11 +418,13 @@ impl BlameFilePopup {
file_path: open.file_path,
commit_id: open.commit_id,
});
self.blame =
Some(BlameProcess::GettingBlame(AsyncBlame::new(
self.repo.borrow().clone(),
&self.git_sender,
)));
if matches!(open.blame, BlameRequest::StartNew) {
self.blame =
Some(BlameProcess::GettingBlame(AsyncBlame::new(
self.repo.borrow().clone(),
&self.git_sender,
)));
}
self.table_state.get_mut().select(Some(0));
self.visible = true;
self.update()?;
Expand Down Expand Up @@ -438,7 +487,6 @@ impl BlameFilePopup {
),
},
);
self.set_open_selection();
self.highlight_blame_lines();

return Ok(());
Expand All @@ -449,6 +497,7 @@ impl BlameFilePopup {
}
}
}
self.set_open_selection();

Ok(())
}
Expand Down Expand Up @@ -721,7 +770,9 @@ impl BlameFilePopup {
self.open_request.as_ref().and_then(|req| req.selection)
{
let mut table_state = self.table_state.take();
table_state.select(Some(selection));
let max_line_number = self.get_max_line_number();
table_state
.select(Some(selection.clamp(0, max_line_number)));
self.table_state.set(table_state);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/popups/file_revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use ratatui::{
Frame,
};

use super::{BlameFileOpen, InspectCommitOpen};
use super::{BlameFileOpen, BlameRequest, InspectCommitOpen};

const SLICE_SIZE: usize = 1200;

Expand Down Expand Up @@ -536,6 +536,7 @@ impl Component for FileRevlogPopup {
file_path: open_request.file_path,
commit_id: self.selected_commit(),
selection: None,
blame: BlameRequest::StartNew,
},
),
));
Expand Down
Loading