Files
pipelines/app/lib/features/changes/widgets/patch_view.dart
T
alan d407a72a06 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>
2026-11-21 18:13:08 +00:00

205 lines
6.8 KiB
Dart

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.
/// This is the only custom drawing in the feature; everything around it is a
/// standard platform control.
class PatchView extends StatelessWidget {
const PatchView({super.key, required this.file});
final FileChange file;
@override
Widget build(BuildContext context) {
final parsed = file.parsedPatch;
final base = nativeDiffText(DefaultTextStyle.of(context).style);
return Semantics(
container: true,
label: 'Rendered diff for ${file.path}',
value: changeStat(parsed.added, parsed.removed).isEmpty
? noLineChangesMessage
: changeStat(parsed.added, parsed.removed),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (file.truncated)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: Text(
truncatedPatchNote,
style: base.copyWith(color: mutedColor(context)),
),
),
// 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 (var index = 0; index < parsed.hunks.length; index++)
_HunkView(hunk: parsed.hunks[index], index: index),
],
),
);
}
}
class _HunkView extends StatelessWidget {
const _HunkView({required this.hunk, required this.index});
final PatchHunk hunk;
final int index;
@override
Widget build(BuildContext context) {
final base = nativeDiffText(DefaultTextStyle.of(context).style);
final width = gutterWidth(hunk);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: hunkHeaderColor(context),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Text(hunk.header, style: base),
),
LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: ConstrainedBox(
// At least the dialog's width, so a short hunk still paints
// full-width bands; wider content scrolls sideways.
constraints: BoxConstraints(minWidth: constraints.maxWidth),
// The widest recorded line sets the hunk's content width, so
// every line tint is a full-width band instead of a bar as long
// as its own text.
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
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,
),
],
),
),
),
);
},
),
],
);
}
}
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;
@override
Widget build(BuildContext context) {
final gutter = line.noNewlineMarker
? ' ' * (width * 2 + 1)
: '${gutterText(line.oldLine, width)} ${gutterText(line.newLine, width)}';
final colors = diffLineColors(context, line.kind);
final row = Container(
color: colors.background,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
child: Text.rich(
TextSpan(
children: [
TextSpan(
text: gutter,
style: base.copyWith(color: mutedColor(context)),
),
TextSpan(
text: diffLineSymbol(line.kind),
style: base.copyWith(
color: colors.marker,
fontWeight: line.kind == ChangeLineKind.context
? null
: FontWeight.w700,
),
),
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;
}
}