Skip to content

Commit

Permalink
When we highlight a suggestion, we can't rely on the `removeDiacritic…
Browse files Browse the repository at this point in the history
…s`, as it replaces some letters of 1 word into 2 or +
  • Loading branch information
g123k committed Sep 15, 2023
1 parent 3894656 commit bd40471
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/smooth_app/lib/widgets/smooth_text.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:diacritic/diacritic.dart';
import 'package:flutter/material.dart';

/// An extension on [TextStyle] that allows to have "well spaced" variant
Expand Down Expand Up @@ -87,8 +86,8 @@ class TextHighlighter extends StatelessWidget {
required TextStyle? highlightedStyle,
}) {
final Iterable<RegExpMatch> highlightedParts =
RegExp(removeDiacritics(filter).toLowerCase().trim()).allMatches(
removeDiacritics(text).toLowerCase(),
RegExp(_removeAccents(filter).toLowerCase().trim()).allMatches(
_removeAccents(text).toLowerCase(),
);

final List<(String, TextStyle?)> parts = <(String, TextStyle?)>[];
Expand Down Expand Up @@ -118,4 +117,20 @@ class TextHighlighter extends StatelessWidget {
}
return parts;
}

/// We can't rely on [removeDiacritics] here, as it replaces some characters
/// of 1 letter into 2 or more.
/// That's why this simpler algorithm is used instead.
String _removeAccents(String str) {
const String withAccents =
'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
const String withoutAccents =
'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz';

for (int i = 0; i < withAccents.length; i++) {
str = str.replaceAll(withAccents[i], withoutAccents[i]);
}

return str;
}
}

0 comments on commit bd40471

Please sign in to comment.