Files
GitJournal/lib/editors/controllers/highlighting_text_controller.dart
Vishesh Handa 657721adc6 Update dart-git and stop using the Result class
Instead we're going to move back to standard exceptions.

Using a custom Result class has created far far more problems
- The Stacktraces aren't always right
- Sometimes one forgets to check the Result error
- All other exception throwing code needing to be converted to Results
- Non idiomatic Dart code

I think it's better to just go back to exceptions. They have their
problems, but overall, I think it's a better approach.
2023-11-24 14:03:30 +01:00

65 lines
1.5 KiB
Dart

/*
* SPDX-FileCopyrightText: 2021 Vishesh Handa <me@vhanda.in>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import 'package:flutter/material.dart';
class HighlightingTextController {
final String highlightText;
final int currentPos;
final Color highlightBackgroundColor;
final Color highlightCurrentBackgroundColor;
HighlightingTextController({
required this.highlightText,
required this.currentPos,
required this.highlightBackgroundColor,
required this.highlightCurrentBackgroundColor,
});
TextSpan highlight({
required TextSpan input,
// required BuildContext context,
required TextStyle? style,
required bool withComposing,
}) {
if (input.text != null) {
var text = input.text!;
var regexp = RegExp(RegExp.escape(highlightText), caseSensitive: false);
var children = <TextSpan>[];
var index = 0;
text.splitMapJoin(
regexp,
onMatch: (Match m) {
var backgroundColor = index != currentPos
? highlightBackgroundColor
: highlightCurrentBackgroundColor;
children.add(TextSpan(
text: m[0],
style: style?.copyWith(backgroundColor: backgroundColor),
));
index++;
return "";
},
onNonMatch: (String span) {
children.add(TextSpan(text: span, style: style));
return "";
},
);
if (children.length == 1) {
return children[0];
}
return TextSpan(children: children);
}
return input;
}
}