Diff viewer: vim keys, scrolling, find in diff and an all-files view

Left/right and h/l step between files (or headers in the all-files
view), up/down and j/k scroll, / opens find-in-diff (highlighted
matches across every file, n/N or Enter/Shift+Enter to move, Escape to
close), and a top-bar toggle switches between one file at a time and
every file stacked with collapsible headers, like a pull request's
Files changed tab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 18:13:08 +00:00
parent cfebd3a05b
commit d407a72a06
12 changed files with 1072 additions and 98 deletions
@@ -1,21 +1,20 @@
import '../../../ui/adaptive/segmented_bar.dart';
import '../../../ui/input/arrow_key_pager.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../ui/navigation/chevron_pager.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../../../ui/adaptive/glass_bar.dart';
import '../domain/change_kind.dart';
import '../domain/change_wording.dart';
import '../domain/file_change.dart';
import '../domain/unified_patch.dart';
import 'change_style.dart';
import 'diff_highlight.dart';
import 'patch_view.dart';
import 'raw_patch_view.dart';
export 'file_diff_page.dart';
/// The reusable viewer for one recorded file change: its kind, path, stat,
/// tags, the rendered or raw unified patch, Copy patch (the server's exact
/// bytes) and Close. Binary/truncated/mode-only/no-patch entries show an honest
@@ -26,10 +25,15 @@ class FileDiffViewer extends StatefulWidget {
required this.file,
this.onClose,
this.floatBehindTopBar = false,
this.scrollController,
});
final FileChange file;
/// Scrolls the diff body; the page passes its own so the keyboard can
/// scroll it.
final ScrollController? scrollController;
/// Called by the Close control. Defaults to popping the enclosing route.
final VoidCallback? onClose;
@@ -212,15 +216,22 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
if (!file.hasPatch) {
return _message(context, patchUnavailableReason(file));
}
if (_form == DiffForm.raw) {
// A search highlights rendered lines, so it shows the rendered form.
final searching = DiffHighlight.maybeOf(context)?.active ?? false;
if (_form == DiffForm.raw && !searching) {
return Semantics(
container: true,
label: 'Raw patch for ${file.path}',
value: file.patch,
child: RawPatchView(patch: file.patch!, style: mono),
child: RawPatchView(
patch: file.patch!,
style: mono,
controller: widget.scrollController,
),
);
}
return SingleChildScrollView(
controller: widget.scrollController,
padding: const EdgeInsets.only(bottom: 16),
child: PatchView(file: file),
);
@@ -228,6 +239,7 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
Widget _message(BuildContext context, String text) {
return SingleChildScrollView(
controller: widget.scrollController,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
child: Text(text),
);
@@ -242,85 +254,3 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
/// keeps its usual layout until scaled text or a narrow phone demands more room.
Widget _scrollsWhenTooWide(Widget control) =>
SingleChildScrollView(scrollDirection: Axis.horizontal, child: control);
/// Open [file] on its own page, as the browser's file view. [files] is the
/// list it was picked from (the visit's shown files); the page steps through
/// them with previous/next chevrons at its bottom right.
Future<void> showFileDiffViewer(
BuildContext context,
FileChange file, {
List<FileChange>? files,
}) {
final list = files == null || !files.contains(file) ? [file] : files;
final page = FileDiffPage(files: list, initialIndex: list.indexOf(file));
return Navigator.of(
context,
).push<void>(MaterialPageRoute<void>(builder: (_) => page));
}
/// One recorded file's diff as a page, with the previous and next file of the
/// list it came from.
class FileDiffPage extends StatefulWidget {
const FileDiffPage({super.key, required this.files, this.initialIndex = 0});
final List<FileChange> files;
final int initialIndex;
@override
State<FileDiffPage> createState() => _FileDiffPageState();
}
class _FileDiffPageState extends State<FileDiffPage> {
late int _index = widget.initialIndex.clamp(0, widget.files.length - 1);
void _step(int delta) => setState(() => _index += delta);
@override
Widget build(BuildContext context) {
final file = widget.files[_index];
final name = file.path.split('/').last;
final count = widget.files.length;
final viewer = FileDiffViewer(
key: ValueKey<String>('file-diff-${file.path}'),
file: file,
floatBehindTopBar: true,
);
final pager = count < 2
? null
: ChevronPager(
key: const ValueKey('file-diff-pager'),
index: _index,
count: count,
previousLabel: 'Previous file',
nextLabel: 'Next file',
onPrevious: _index > 0 ? () => _step(-1) : null,
onNext: _index < count - 1 ? () => _step(1) : null,
);
// Left/right arrows step between changed files, like the chevrons;
// Escape closes the page, like its back button.
final body = ArrowKeyPager(
onPrevious: _index > 0 ? () => _step(-1) : null,
onNext: _index < count - 1 ? () => _step(1) : null,
onEscape: () => Navigator.of(context).maybePop(),
child: FloatingBarOverlay(
bar: pager == null
? const SizedBox.shrink()
: Align(
alignment: AlignmentDirectional.centerEnd,
heightFactor: 1,
child: SafeArea(
top: false,
minimum: const EdgeInsets.all(8),
child: GlassControlBar(child: pager),
),
),
child: viewer,
),
);
return Scaffold(
extendBodyBehindAppBar: true,
appBar: SegmentedBar(title: name),
body: body,
);
}
}