Files
pipelines/app/lib/features/changes/widgets/diff_search_controller.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

56 lines
1.6 KiB
Dart

import 'package:flutter/foundation.dart';
import '../domain/diff_search.dart';
import '../domain/file_change.dart';
/// The diff page's search: whether it is open, its query, every match across
/// the viewed files and which one is current.
class DiffSearchController extends ChangeNotifier {
bool _open = false;
String _query = '';
List<DiffMatch> _matches = const [];
int? _current;
bool get open => _open;
String get query => _open ? _query : '';
List<DiffMatch> get matches => _matches;
/// The current match, or null when there is none.
DiffMatch? get current => _current == null ? null : _matches[_current!];
/// The current match's position counted from 1, or 0.
int get position => _current == null ? 0 : _current! + 1;
void show() {
if (_open) return;
_open = true;
notifyListeners();
}
void close() {
if (!_open) return;
_open = false;
notifyListeners();
}
/// Searches [files] for [query], starting at the first match in [fromFile]
/// or after it.
void search(List<FileChange> files, String query, {required int fromFile}) {
_open = true;
_query = query;
_matches = diffMatches(files, query);
_current = firstMatchFrom(_matches, fromFile);
notifyListeners();
}
/// Moves to the next (+1) or previous (-1) match, wrapping around. Returns
/// false when there is no match to move to.
bool step(int delta) {
if (!_open || _matches.isEmpty) return false;
final from = _current ?? (delta > 0 ? -1 : 0);
_current = (from + delta) % _matches.length;
notifyListeners();
return true;
}
}