Skip to content

Commit

Permalink
Add selectCommitRelative()
Browse files Browse the repository at this point in the history
  • Loading branch information
jensenr30 committed Mar 21, 2024
1 parent 770ac3c commit 7e18273
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/ui/CommitList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,11 +1169,11 @@ class SelectionModel : public QItemSelectionModel {

} // namespace

static Hotkey prevCommitHotKey = HotkeyManager::registerHotkey(
"J", "commitList/prevCommit", "CommitList/PrevCommit");
static Hotkey selectCommitDownHotKey = HotkeyManager::registerHotkey(
"j", "commitList/selectCommitDown", "CommitList/Select Next Commit Down");

static Hotkey nextCommitHotKey = HotkeyManager::registerHotkey(
"K", "commitList/nextCommit", "CommitList/NextCommit");
static Hotkey selectCommitUpHotKey = HotkeyManager::registerHotkey(
"k", "commitList/selectCommitUp", "CommitList/Select Next Commit Up");

CommitList::CommitList(Index *index, QWidget *parent)
: QListView(parent), mIndex(index) {
Expand Down Expand Up @@ -1231,16 +1231,13 @@ CommitList::CommitList(Index *index, QWidget *parent)
[this](const QModelIndex &index) { update(index); });

QShortcut *shortcut = new QShortcut(this);
prevCommitHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] {
printf("prevCommit\n");
});
selectCommitDownHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] { selectCommitRelative(1); });

shortcut = new QShortcut(this);
nextCommitHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated, [this] {
printf("nextCommit\n");
});
selectCommitUpHotKey.use(shortcut);
connect(shortcut, &QShortcut::activated,
[this] { selectCommitRelative(-1); });

#ifdef Q_OS_MAC
QFont font = this->font();
Expand Down Expand Up @@ -1364,6 +1361,19 @@ void CommitList::selectFirstCommit(bool spontaneous) {
}
}

void CommitList::selectCommitRelative(int offset) {
QModelIndexList indices = selectionModel()->selectedIndexes();
QModelIndex index = indices[0];
if (!index.isValid()) {
return;
}
QModelIndex new_index = model()->index(index.row() + offset, index.column());
if (!new_index.isValid()) {
return;
}
selectIndexes(QItemSelection(new_index, new_index), QString(), true);
}

bool CommitList::selectRange(const QString &range, const QString &file,
bool spontaneous) {
// Try to select the "status" index.
Expand Down
1 change: 1 addition & 0 deletions src/ui/CommitList.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CommitList : public QListView {
void selectReference(const git::Reference &ref);
void resetSelection(bool spontaneous = false);
void selectFirstCommit(bool spontaneous = false);
void selectCommitRelative(int offset);
bool selectRange(const QString &range, const QString &file = QString(),
bool spontaneous = false);
void suppressResetWalker(bool suppress);
Expand Down

0 comments on commit 7e18273

Please sign in to comment.