Skip to content

Commit 3454a0a

Browse files
feat(git): GitHub-style diff preview and commit view
One DiffView presents both the diff preview and the commit view as plain editor tabs. Each file has a header row (status letter, dir + name, rename arrow, +N -M, five squares, badges), a sticky header, a toolbar (file tree, expand/collapse all, hide whitespace, wrap, previous/next change) and, in the commit view, a filterable changed-files tree. The line-number margin (old / new) is filled lazily; lockfiles, generated, deleted and large files start collapsed behind "Load diff". Pipeline: parse (file + hunk tables) -> classify -> per-file lexer style slots -> document builder -> tree, in one pool job. The UI thread applies one APPENDTEXT + SETSTYLINGEX. Markers and word indicators go on the visible window at once and the rest in 4 ms slices. The commit view runs a single `git show --diff-merges=first-parent` (one retry on old git), reads `check-attr linguist-generated` for 2+ files and caches built documents. Removes GitDiffPainter and GitCommitRenderer. Bugs fixed on the way: the lexing result was computed but never used, numstat was unused, the hunk colour lost its alpha, `--` deleted lines were misparsed as file headers, and the diff lexers had no keywords. Review fixes: diff requests carry a token so stale replies (whitespace toggle in flight) are dropped; the preview cancels pending builds before fetching; the commit view resolves revisions from the shown document; the sticky header refreshes after theme and zoom changes; queued header-menu actions check the document serial; files with nothing to fold show a muted, non-clickable chevron; the file tree's context menu can collapse a file; toolbar buttons keep focus and use 4px spacing; buildBody counts one file's hunks instead of all of them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bd16a0a commit 3454a0a

76 files changed

Lines changed: 7174 additions & 1538 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/ApplicationSettings.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ CREATE_SETTING(QuickBrowser, LastProxyPort, lastProxyPort, int, 0)
177177
CREATE_SETTING(QuickBrowser, LastProxyBypassList, lastProxyBypassList, QString, QStringLiteral(""))
178178

179179
CREATE_SETTING(Git, SyntaxHighlightDiffEnabled, syntaxHighlightDiffEnabled, bool, true)
180+
CREATE_SETTING(Git, DiffIgnoreWhitespace, diffIgnoreWhitespace, bool, false)
181+
CREATE_SETTING(Git, DiffWrapLines, diffWrapLines, bool, false)
182+
CREATE_SETTING(Git, DiffTreeWidth, diffTreeWidth, int, 256)
183+
CREATE_SETTING(Git, DiffTreeVisible, diffTreeVisible, bool, true)
180184

181185
// Files-tab decoration master toggle — see file-tree-git-decorations spec.
182186
// Default ON so users see the feature out of the box. No PreferencesDialog

‎src/ApplicationSettings.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ public slots:
208208

209209
DEFINE_SETTING(SyntaxHighlightDiffEnabled, syntaxHighlightDiffEnabled, bool)
210210

211+
// Diff views (DiffView): toolbar toggles, file tree width and visibility.
212+
DEFINE_SETTING(DiffIgnoreWhitespace, diffIgnoreWhitespace, bool)
213+
DEFINE_SETTING(DiffWrapLines, diffWrapLines, bool)
214+
DEFINE_SETTING(DiffTreeWidth, diffTreeWidth, int)
215+
DEFINE_SETTING(DiffTreeVisible, diffTreeVisible, bool)
216+
211217
// Files-tab git decoration master toggle. When false, the workspace
212218
// tree shows default text colour regardless of git status.
213219
DEFINE_SETTING(FileTreeGitColors, fileTreeGitColors, bool)

‎src/CMakeLists.txt‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ qt_add_executable(NotepadAI
364364
git/CommitComposer.cpp
365365
git/CommitComposer.h
366366
git/CommitSubmitPolicy.h
367+
git/DiffDocument.h
368+
git/DiffDocumentBuilder.cpp
369+
git/DiffDocumentBuilder.h
370+
git/DiffFileClassifier.cpp
371+
git/DiffFileClassifier.h
372+
git/DiffFileTreeModel.cpp
373+
git/DiffFileTreeModel.h
374+
git/DiffLanguages.cpp
375+
git/DiffLanguages.h
376+
git/DiffLanguagesLua.cpp
377+
git/DiffLanguagesLua.h
378+
git/DiffStickyHeader.cpp
379+
git/DiffStickyHeader.h
380+
git/DiffView.cpp
381+
git/DiffView.h
382+
git/DiffViewApp.cpp
383+
git/DiffViewApp.h
367384
git/FileEncodingDetector.cpp
368385
git/FileEncodingDetector.h
369386
git/ForceRemovePath.cpp
@@ -379,16 +396,12 @@ qt_add_executable(NotepadAI
379396
git/GitCommitFetcher.cpp
380397
git/GitCommitFetcher.h
381398
git/GitCommitInfo.h
382-
git/GitCommitRenderer.cpp
383-
git/GitCommitRenderer.h
384399
git/GitCommitView.cpp
385400
git/GitCommitView.h
386401
git/GitDiffCache.cpp
387402
git/GitDiffCache.h
388403
git/GitDiffFetcher.cpp
389404
git/GitDiffFetcher.h
390-
git/GitDiffPainter.cpp
391-
git/GitDiffPainter.h
392405
git/GitDiffPalette.cpp
393406
git/GitDiffPalette.h
394407
git/GitDiffParser.cpp
@@ -418,6 +431,8 @@ qt_add_executable(NotepadAI
418431
git/GitNumstatParser.h
419432
git/GitProcessRunner.cpp
420433
git/GitProcessRunner.h
434+
git/GitRelativeTime.cpp
435+
git/GitRelativeTime.h
421436
git/GitRunnerFactory.cpp
422437
git/GitRunnerFactory.h
423438
git/PathStatusIndex.cpp

‎src/DockedEditor.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ ScintillaNext *DockedEditor::initialEditor() const
194194
// Reject anything the user might care about: a temporary buffer, a real or
195195
// missing file, or a buffer with undo/redo history (i.e. it was edited).
196196
// Only a truly pristine "New X" survives and may be transparently closed.
197-
if (editor->isTemporary() || editor->isFile() || editor->canUndo() || editor->canRedo())
197+
// Diff views (EditorManager::registerAsDiffView) have no undo history either.
198+
if (editor->isTemporary() || editor->isFile() || editor->canUndo() || editor->canRedo()
199+
|| editor->QObject::property("nn_diffView").toBool())
198200
return nullptr;
199201

200202
return editor;

‎src/EditorManager.cpp‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ EditorManager::EditorManager(ApplicationSettings *settings, QObject *parent)
168168
connect(settings, &ApplicationSettings::showLineNumbersChanged, this, [=](bool b){
169169
for (auto &editor : getEditors()) {
170170
// Diff views own margin 0 themselves (per-row old/new file line numbers
171-
// rendered by GitDiffPainter). Their LineNumbers decorator was disabled
171+
// filled by DiffView). Their LineNumbers decorator was disabled
172172
// at creation; don't let the user's "Show Line Numbers" toggle clobber it.
173173
if (isDiffView(editor)) continue;
174174
LineNumbers *decorator = editor->findChild<LineNumbers *>(QString(), Qt::FindDirectChildrenOnly);
@@ -389,9 +389,9 @@ void EditorManager::setupEditor(ScintillaNext *editor)
389389
// applied (lexerChanged is emitted *after* SetLanguage from
390390
// NotepadNextApplication::setEditorLanguage) so dark mode survives.
391391
connect(editor, &ScintillaNext::lexerChanged, this, [this, editor]() {
392-
// Diff views own their palette (GitDiffPainter); the chrome theme's
393-
// defaultBack does not match canvasBg, so applying it would corrupt
394-
// the diff colors until the next configureEditor call.
392+
// Diff views own their palette (DiffView); the chrome theme's
393+
// defaultBack does not match the diff canvas, so applying it would
394+
// corrupt the diff colors.
395395
if (isDiffView(editor)) return;
396396
applyThemeToEditor(editor, darkTheme, /*initialSetup=*/false);
397397
});
@@ -518,12 +518,10 @@ void EditorManager::applyTheme(bool dark)
518518
for (auto &editor : getEditors()) {
519519
if (!editor) continue;
520520

521-
// Diff views own their palette via GitDiffPainter::configureEditor and
522-
// are re-skinned by GitDiffViewController::setDarkPalette on the
523-
// effectiveThemeChanged signal. Running applyThemeToEditor on them
524-
// would overwrite canvasBg/canvasFg with the chrome's defaultBack
525-
// (visible as gray banding under the diff content) until the diff
526-
// controller's slot fires next tick.
521+
// Diff views own their palette (DiffView::setDark, driven by the
522+
// controllers on effectiveThemeChanged). Running applyThemeToEditor
523+
// on them would overwrite the diff canvas with the chrome's
524+
// defaultBack (visible as gray banding under the diff content).
527525
if (isDiffView(editor)) continue;
528526

529527
if (transitionToLight) {
@@ -714,6 +712,9 @@ void EditorManager::registerAsDiffView(ScintillaNext *editor)
714712
{
715713
if (!editor) return;
716714
m_diffViews.insert(editor);
715+
// Diff views keep no undo history; the tag stops DockedEditor::initialEditor
716+
// from taking one for a pristine scratch tab.
717+
editor->QObject::setProperty("nn_diffView", true);
717718
// Auto-untrack on destruction. Capturing the raw pointer is safe because
718719
// QObject::destroyed fires before the pointer is invalidated for QSet ops.
719720
connect(editor, &QObject::destroyed, this, [this](QObject *obj) {

‎src/ScintillaNext.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class ScintillaNext : public ScintillaEdit
101101

102102
int allocateIndicator(const QString &name);
103103

104+
// Space around the text viewport for chrome placed by the owner of the
105+
// editor (diff views: toolbar and file tree). Children placed in it stay
106+
// the owner's job.
107+
void setChromeMargins(const QMargins &margins) { setViewportMargins(margins); }
108+
104109
template<typename Func>
105110
void forEachMatch(const QString &text, Func callback) { forEachMatch(text.toUtf8(), callback); }
106111

‎src/decorators/LineNumbers.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void LineNumbers::adjustMarginWidth()
6060

6161
void LineNumbers::notify(const NotificationData *pscn)
6262
{
63-
// Update::Content covers programmatic text replacement (e.g. GitDiffPainter
64-
// rebuilding the diff preview via SCI_APPENDTEXT), which doesn't fire
63+
// Update::Content covers programmatic text replacement (e.g. the
64+
// file-at-revision tab filled via SCI_APPENDTEXT), which doesn't fire
6565
// VScroll on its own — without it the margin would stay at whatever width
6666
// it had before, and the user would only see line numbers appear after
6767
// their first scroll.

‎src/dialogs/MainWindow.cpp‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,32 @@ void MainWindow::wireWorkspaceGitSignals(FolderAsWorkspaceDock *dock)
36923692
qWarning("Commit detail failed for %s: %s",
36933693
sha.constData(), qPrintable(message));
36943694
});
3695+
// Diff / commit views: jump to source and "Reveal in file tree".
3696+
connect(dock, &FolderAsWorkspaceDock::gitOpenFileAtLineRequested,
3697+
this, [this](const QString &path, int line) {
3698+
if (path.isEmpty()) return;
3699+
// A file deleted in the working tree has no source to jump to;
3700+
// never offer to create it.
3701+
if (!remote::isSshUri(path) && !QFileInfo(path).isFile()) return;
3702+
openFile(path);
3703+
ScintillaNext *editor = app->getEditorManager()->getEditorByFilePath(path);
3704+
if (!editor || line <= 1) return;
3705+
auto go = [editor = QPointer<ScintillaNext>(editor), line] {
3706+
if (!editor) return;
3707+
editor->ensureVisible(line - 1);
3708+
editor->gotoLine(line - 1);
3709+
editor->verticalCentreCaret();
3710+
};
3711+
// A remote file is still loading: go once its text is in.
3712+
if (editor->loadState() == ScintillaNext::LoadState::Loading)
3713+
connect(editor, &ScintillaNext::loaded, editor, go, Qt::SingleShotConnection);
3714+
else
3715+
go();
3716+
});
3717+
connect(dock, &FolderAsWorkspaceDock::gitRevealRequested,
3718+
this, [this](const QString &path) {
3719+
if (!path.isEmpty()) revealInWorkspace(path);
3720+
});
36953721

36963722
connect(dock, &FolderAsWorkspaceDock::gitChangesContextMenuRequested, this,
36973723
[this](QMenu *menu, const GitStatusEntry &entry) {
@@ -5225,10 +5251,9 @@ void MainWindow::addEditor(ScintillaNext *editor)
52255251
QTimer::singleShot(0, editor, [this, editor]() {
52265252
// Skip if a language was already assigned (e.g. by session restore)
52275253
if (!editor->languageName.isEmpty()) return;
5228-
// Diff views are owned by GitDiffPainter — assigning the "Text"
5254+
// Diff views own their styles (DiffView) — assigning the "Text"
52295255
// lexer here would emit lexerChanged and re-skin every style with
5230-
// the chrome's defaultBack, corrupting canvasBg until the next
5231-
// render pass undoes it.
5256+
// the chrome's defaultBack, corrupting the diff canvas.
52325257
if (app->getEditorManager()->isDiffView(editor)) return;
52335258
PROFILE_SCOPE("MainWindow::addEditor.detectLanguage");
52345259
detectLanguage(editor);

‎src/docks/FolderAsWorkspaceDock.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,13 @@ QString FolderAsWorkspaceDock::resolvedPath(const QString &posixPath) const
10211021
return posixPath;
10221022
}
10231023

1024+
QString FolderAsWorkspaceDock::gitRepoFilePath(const QString &relPath) const
1025+
{
1026+
const QString repo = gitTab && gitTab->controller() ? gitTab->controller()->currentRepo() : QString();
1027+
if (repo.isEmpty() || relPath.isEmpty()) return QString();
1028+
return resolvedPath(QDir(repo).filePath(relPath));
1029+
}
1030+
10241031
QString FolderAsWorkspaceDock::resolvedFilePath(const QModelIndex &proxyIdx) const
10251032
{
10261033
return resolvedPath(proxy->filePath(proxyIdx));
@@ -1337,6 +1344,12 @@ GitDiffViewController *FolderAsWorkspaceDock::ensureGitDiffViewController()
13371344
this, &FolderAsWorkspaceDock::onGitDiffPreviewRendered);
13381345
connect(gitDiffViewController, &GitDiffViewController::diffFailed,
13391346
this, &FolderAsWorkspaceDock::onGitDiffPreviewFailed);
1347+
connect(gitDiffViewController, &GitDiffViewController::openFileRequested,
1348+
this, [this](const QString &relPath, int line) {
1349+
emit gitOpenFileAtLineRequested(gitRepoFilePath(relPath), line);
1350+
});
1351+
connect(gitDiffViewController, &GitDiffViewController::revealRequested,
1352+
this, [this](const QString &relPath) { emit gitRevealRequested(gitRepoFilePath(relPath)); });
13401353

13411354
connect(app, &NotepadNextApplication::effectiveThemeChanged,
13421355
gitDiffViewController, [this, app]() {
@@ -1415,6 +1428,16 @@ GitCommitView *FolderAsWorkspaceDock::ensureGitCommitView()
14151428
this, &FolderAsWorkspaceDock::gitFileAtShaEditorFocus);
14161429
connect(gitCommitView, &GitCommitView::fetchFailed,
14171430
this, &FolderAsWorkspaceDock::gitCommitDetailFailed);
1431+
connect(gitCommitView, &GitCommitView::openWorkingFileRequested,
1432+
this, [this](const QString &relPath, int line) {
1433+
emit gitOpenFileAtLineRequested(gitRepoFilePath(relPath), line);
1434+
});
1435+
connect(gitCommitView, &GitCommitView::revealRequested,
1436+
this, [this](const QString &relPath) { emit gitRevealRequested(gitRepoFilePath(relPath)); });
1437+
// A parent link moved the tab: keep the history selection on it.
1438+
connect(gitCommitView, &GitCommitView::commitShown, this, [this](const QByteArray &sha) {
1439+
if (gitTab) gitTab->selectHistoryCommit(sha);
1440+
});
14181441

14191442
connect(app, &NotepadNextApplication::effectiveThemeChanged,
14201443
gitCommitView, [this, app]() {

‎src/docks/FolderAsWorkspaceDock.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class FolderAsWorkspaceDock : public QDockWidget
201201
void gitFileAtShaEditorCreated(ScintillaNext *editor);
202202
void gitFileAtShaEditorFocus(ScintillaNext *editor);
203203
void gitCommitDetailFailed(const QByteArray &sha, const QString &message);
204+
// From the diff preview / commit view: open the working-tree file at
205+
// `line` (1-based), or reveal it in the file tree.
206+
void gitOpenFileAtLineRequested(const QString &filePath, int line);
207+
void gitRevealRequested(const QString &filePath);
204208

205209
// Emitted from closeEvent BEFORE the dock destructs (WA_DeleteOnClose).
206210
// MainWindow snapshots state into FolderAsWorkspace/WorkspaceStates here
@@ -375,6 +379,8 @@ private slots:
375379
// paths that arrive as pre-built absolute strings (onGitTabFileActivated).
376380
QString resolvedFilePath(const QModelIndex &proxyIdx) const;
377381
QString resolvedPath(const QString &posixPath) const;
382+
// A repo-relative path from the diff / commit views, resolved like above.
383+
QString gitRepoFilePath(const QString &relPath) const;
378384

379385
void ensureGitTab();
380386
GitDiffViewController *ensureGitDiffViewController();

0 commit comments

Comments
 (0)