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:
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/change_wording.dart';
|
||||
import '../domain/diff_search.dart';
|
||||
import '../domain/file_change.dart';
|
||||
import '../domain/unified_patch.dart';
|
||||
import 'change_style.dart';
|
||||
import 'diff_highlight.dart';
|
||||
|
||||
/// The rendered reading of one recorded unified patch: each hunk's `@@` header,
|
||||
/// the shared diff line language (`+`/`-`/space) and the old/new gutter numbers.
|
||||
@@ -38,7 +40,8 @@ class PatchView extends StatelessWidget {
|
||||
// The web rendered view shows hunks, not the recorded `---`/`+++`
|
||||
// file headers (they stay in the Raw view), so the dialog does not
|
||||
// show a long header clipped at its edge.
|
||||
for (final hunk in parsed.hunks) _HunkView(hunk: hunk),
|
||||
for (var index = 0; index < parsed.hunks.length; index++)
|
||||
_HunkView(hunk: parsed.hunks[index], index: index),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -46,9 +49,10 @@ class PatchView extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _HunkView extends StatelessWidget {
|
||||
const _HunkView({required this.hunk});
|
||||
const _HunkView({required this.hunk, required this.index});
|
||||
|
||||
final PatchHunk hunk;
|
||||
final int index;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -80,6 +84,8 @@ class _HunkView extends StatelessWidget {
|
||||
for (var index = 0; index < hunk.lines.length; index++)
|
||||
_LineRow(
|
||||
key: ValueKey('diff-line-$index'),
|
||||
hunk: this.index,
|
||||
index: index,
|
||||
line: hunk.lines[index],
|
||||
width: width,
|
||||
base: base,
|
||||
@@ -99,11 +105,15 @@ class _HunkView extends StatelessWidget {
|
||||
class _LineRow extends StatelessWidget {
|
||||
const _LineRow({
|
||||
super.key,
|
||||
required this.hunk,
|
||||
required this.index,
|
||||
required this.line,
|
||||
required this.width,
|
||||
required this.base,
|
||||
});
|
||||
|
||||
final int hunk;
|
||||
final int index;
|
||||
final PatchLine line;
|
||||
final int width;
|
||||
final TextStyle base;
|
||||
@@ -114,7 +124,7 @@ class _LineRow extends StatelessWidget {
|
||||
? ' ' * (width * 2 + 1)
|
||||
: '${gutterText(line.oldLine, width)} ${gutterText(line.newLine, width)}';
|
||||
final colors = diffLineColors(context, line.kind);
|
||||
return Container(
|
||||
final row = Container(
|
||||
color: colors.background,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
|
||||
child: Text.rich(
|
||||
@@ -133,15 +143,62 @@ class _LineRow extends StatelessWidget {
|
||||
: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text: ' ${line.text}',
|
||||
style: base.copyWith(color: colors.text),
|
||||
),
|
||||
const TextSpan(text: ' '),
|
||||
..._textSpans(context, base.copyWith(color: colors.text)),
|
||||
],
|
||||
),
|
||||
softWrap: false,
|
||||
overflow: TextOverflow.visible,
|
||||
),
|
||||
);
|
||||
final highlight = DiffHighlight.maybeOf(context);
|
||||
final file = DiffFileIndex.maybeOf(context);
|
||||
final current = highlight?.current;
|
||||
if (current != null && file != null && current.isOn(file, hunk, index)) {
|
||||
return KeyedSubtree(key: highlight!.currentKey, child: row);
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/// The line's text with every search match marked, the current one
|
||||
/// strongest.
|
||||
List<TextSpan> _textSpans(BuildContext context, TextStyle style) {
|
||||
final highlight = DiffHighlight.maybeOf(context);
|
||||
if (highlight == null || !highlight.active) {
|
||||
return [TextSpan(text: line.text, style: style)];
|
||||
}
|
||||
final ranges = matchRanges(line.text, highlight.query);
|
||||
if (ranges.isEmpty) return [TextSpan(text: line.text, style: style)];
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final file = DiffFileIndex.maybeOf(context);
|
||||
final current = highlight.current;
|
||||
final spans = <TextSpan>[];
|
||||
var at = 0;
|
||||
for (final (start, end) in ranges) {
|
||||
if (start > at) {
|
||||
spans.add(TextSpan(text: line.text.substring(at, start), style: style));
|
||||
}
|
||||
final isCurrent =
|
||||
current != null &&
|
||||
file != null &&
|
||||
current.isOn(file, hunk, index) &&
|
||||
current.start == start;
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: line.text.substring(start, end),
|
||||
style: style.copyWith(
|
||||
color: isCurrent ? scheme.onTertiary : scheme.onTertiaryContainer,
|
||||
backgroundColor: isCurrent
|
||||
? scheme.tertiary
|
||||
: scheme.tertiaryContainer,
|
||||
),
|
||||
),
|
||||
);
|
||||
at = end;
|
||||
}
|
||||
if (at < line.text.length) {
|
||||
spans.add(TextSpan(text: line.text.substring(at), style: style));
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user