Skip to content

Commit

Permalink
fix: catch regex FormatException and show a corresponding error mes…
Browse files Browse the repository at this point in the history
…sage (#573)

* add error message

* restore and reformat

* update localization and comments

* chore: reformat files
  • Loading branch information
sun-jiao authored Nov 6, 2023
1 parent aa90288 commit 77e5d87
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
"replaceAll": "Replace all",
"regex": "Regex",
"caseSensitive": "Case sensitive",
"regexError": "Regex Error",
"noFindResult": "No result",
"emptySearchBoxHint": "Enter a pattern",
"uploadImage": "Upload Image",
"urlImage": "URL Image",
"incorrectLink": "Incorrect Link",
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/intl_zh_CN.arb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
"replaceAll": "替换全部",
"regex": "正则表达式",
"caseSensitive": "区分大小写",
"regexError": "正则错误",
"noFindResult": "无匹配项",
"emptySearchBoxHint": "输入查找内容",
"uploadImage": "上传图片",
"urlImage": "网络图片",
"incorrectLink": "链接错误",
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/intl_zh_TW.arb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
"replaceAll": "取代全部",
"regex": "正規表示式",
"caseSensitive": "區分大小寫",
"regexError": "正規錯誤",
"noFindResult": "無相符項",
"emptySearchBoxHint": "鍵入尋找内容",
"uploadImage": "上載圖片",
"urlImage": "網路圖片",
"incorrectLink": "連結錯誤",
Expand Down
28 changes: 20 additions & 8 deletions lib/src/editor/find_replace_menu/find_replace_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class _FindMenuState extends State<FindMenu> {

final findTextEditingController = TextEditingController();

String queriedPattern = '';
String message = AppFlowyEditorLocalizations.current.emptySearchBoxHint;

bool showReplaceMenu = false;
bool caseSensitive = false;
Expand Down Expand Up @@ -250,9 +250,7 @@ class _FindMenuState extends State<FindMenu> {
padding: const EdgeInsets.symmetric(horizontal: 8.0),
alignment: Alignment.centerLeft,
child: Text(
matches.isEmpty
? widget.localizations?.noResult ?? 'No Result'
: '$selectedIndex of ${matches.length}',
matches.isEmpty ? message : '$selectedIndex of ${matches.length}',
),
),
// previous match button
Expand Down Expand Up @@ -322,11 +320,25 @@ class _FindMenuState extends State<FindMenu> {
}

void _searchPattern() {
if (findTextEditingController.text.isEmpty) {
return;
String error;

// the following line needs to be executed even if
// findTextEditingController.text.isEmpty, otherwise the previous
// matches will persist
error =
widget.searchService.findAndHighlight(findTextEditingController.text);

switch (error) {
case 'Regex':
message = AppFlowyEditorLocalizations.current.regexError;
case 'Empty':
message = AppFlowyEditorLocalizations.current.emptySearchBoxHint;
default:
message = widget.localizations?.noResult ??
AppFlowyEditorLocalizations.current.noFindResult;
}
widget.searchService.findAndHighlight(findTextEditingController.text);
setState(() => queriedPattern = findTextEditingController.text);

_setState();
}

void _setState() {
Expand Down
35 changes: 28 additions & 7 deletions lib/src/editor/find_replace_menu/search_service_v3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class SearchServiceV3 {

final EditorState editorState;

//matchedPositions.value will contain a list of positions of the matched patterns
//the position here consists of the node and the starting offset of the
//matchWrappers.value will contain a list of matchWrappers of the matched patterns
//the position here consists of the match and the node path of
//matched pattern. We will use this to traverse between the matched patterns.
ValueNotifier<List<MatchWrapper>> matchWrappers = ValueNotifier([]);
SearchAlgorithm searchAlgorithm = DartBuiltIn();
Expand Down Expand Up @@ -75,8 +75,15 @@ class SearchServiceV3 {

// Public entry method for _findAndHighlight, do necessary checks
// and clear previous highlights before calling the private method
void findAndHighlight(String target, {bool unHighlight = false}) {
Pattern pattern = _getPattern(target);
String findAndHighlight(String target, {bool unHighlight = false}) {
Pattern pattern;

try {
pattern = _getPattern(target);
} on FormatException {
matchWrappers.value.clear();
return 'Regex';
}

if (queriedPattern != pattern) {
// this means we have a new pattern, but before we highlight the new matches,
Expand All @@ -87,9 +94,11 @@ class SearchServiceV3 {
targetString = target;
}

if (target.isEmpty) return;
if (target.isEmpty) return 'Empty';

_findAndHighlight(pattern, unHighlight: unHighlight);

return '';
}

/// Finds the pattern in editorState.document and stores it in matchedPositions.
Expand Down Expand Up @@ -186,7 +195,7 @@ class SearchServiceV3 {
/// matched word if that exists.
Future<void> replaceSelectedWord(String replaceText) async {
if (replaceText.isEmpty ||
queriedPattern.toString().isEmpty ||
queriedPattern.isEmpty ||
matchWrappers.value.isEmpty) {
return;
}
Expand Down Expand Up @@ -217,7 +226,7 @@ class SearchServiceV3 {
/// Replaces all the found occurrences of pattern with replaceText
void replaceAllMatches(String replaceText) {
if (replaceText.isEmpty ||
queriedPattern.toString().isEmpty ||
queriedPattern.isEmpty ||
matchWrappers.value.isEmpty) {
return;
}
Expand Down Expand Up @@ -257,3 +266,15 @@ class MatchWrapper {
end: Position(path: path, offset: match.end),
);
}

extension on Pattern {
bool get isEmpty {
if (this is String) {
return (this as String).isEmpty;
} else if (this is RegExp) {
return (this as RegExp).pattern.isEmpty;
} else {
return toString().isEmpty;
}
}
}
4 changes: 4 additions & 0 deletions lib/src/l10n/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class MessageLookup extends MessageLookupByLibrary {
"done": MessageLookupByLibrary.simpleMessage("Done"),
"editLink": MessageLookupByLibrary.simpleMessage("Edit link"),
"embedCode": MessageLookupByLibrary.simpleMessage("Embed Code"),
"emptySearchBoxHint":
MessageLookupByLibrary.simpleMessage("Enter a pattern"),
"find": MessageLookupByLibrary.simpleMessage("Find"),
"fontColorBlue": MessageLookupByLibrary.simpleMessage("Blue"),
"fontColorBrown": MessageLookupByLibrary.simpleMessage("Brown"),
Expand Down Expand Up @@ -113,13 +115,15 @@ class MessageLookup extends MessageLookupByLibrary {
"mobileHeading2": MessageLookupByLibrary.simpleMessage("Heading 2"),
"mobileHeading3": MessageLookupByLibrary.simpleMessage("Heading 3"),
"nextMatch": MessageLookupByLibrary.simpleMessage("Next match"),
"noFindResult": MessageLookupByLibrary.simpleMessage("No result"),
"numberedList": MessageLookupByLibrary.simpleMessage("Numbered List"),
"opacity": MessageLookupByLibrary.simpleMessage("Opacity"),
"openLink": MessageLookupByLibrary.simpleMessage("Open link"),
"paste": MessageLookupByLibrary.simpleMessage("Paste"),
"previousMatch": MessageLookupByLibrary.simpleMessage("Previous match"),
"quote": MessageLookupByLibrary.simpleMessage("Quote"),
"regex": MessageLookupByLibrary.simpleMessage("Regex"),
"regexError": MessageLookupByLibrary.simpleMessage("Regex Error"),
"removeLink": MessageLookupByLibrary.simpleMessage("Remove link"),
"replace": MessageLookupByLibrary.simpleMessage("Replace"),
"replaceAll": MessageLookupByLibrary.simpleMessage("Replace all"),
Expand Down
3 changes: 3 additions & 0 deletions lib/src/l10n/intl/messages_zh-CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MessageLookup extends MessageLookupByLibrary {
"done": MessageLookupByLibrary.simpleMessage("完成"),
"editLink": MessageLookupByLibrary.simpleMessage("修改链接"),
"embedCode": MessageLookupByLibrary.simpleMessage("代码块"),
"emptySearchBoxHint": MessageLookupByLibrary.simpleMessage("输入查找内容"),
"find": MessageLookupByLibrary.simpleMessage("查找"),
"fontColorBlue": MessageLookupByLibrary.simpleMessage("蓝色"),
"fontColorBrown": MessageLookupByLibrary.simpleMessage("棕色"),
Expand Down Expand Up @@ -97,13 +98,15 @@ class MessageLookup extends MessageLookupByLibrary {
"mobileHeading2": MessageLookupByLibrary.simpleMessage("二级标题"),
"mobileHeading3": MessageLookupByLibrary.simpleMessage("三级标题"),
"nextMatch": MessageLookupByLibrary.simpleMessage("下一匹配项"),
"noFindResult": MessageLookupByLibrary.simpleMessage("无匹配项"),
"numberedList": MessageLookupByLibrary.simpleMessage("有序列表"),
"opacity": MessageLookupByLibrary.simpleMessage("透明度"),
"openLink": MessageLookupByLibrary.simpleMessage("打开链接"),
"paste": MessageLookupByLibrary.simpleMessage("粘贴"),
"previousMatch": MessageLookupByLibrary.simpleMessage("上一匹配项"),
"quote": MessageLookupByLibrary.simpleMessage("引文"),
"regex": MessageLookupByLibrary.simpleMessage("正则表达式"),
"regexError": MessageLookupByLibrary.simpleMessage("正则错误"),
"removeLink": MessageLookupByLibrary.simpleMessage("移除链接"),
"replace": MessageLookupByLibrary.simpleMessage("替换"),
"replaceAll": MessageLookupByLibrary.simpleMessage("替换全部"),
Expand Down
3 changes: 3 additions & 0 deletions lib/src/l10n/intl/messages_zh-TW.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MessageLookup extends MessageLookupByLibrary {
"done": MessageLookupByLibrary.simpleMessage("完成"),
"editLink": MessageLookupByLibrary.simpleMessage("修改連結"),
"embedCode": MessageLookupByLibrary.simpleMessage("代碼塊"),
"emptySearchBoxHint": MessageLookupByLibrary.simpleMessage("鍵入尋找内容"),
"find": MessageLookupByLibrary.simpleMessage("尋找"),
"fontColorBlue": MessageLookupByLibrary.simpleMessage("藍色"),
"fontColorBrown": MessageLookupByLibrary.simpleMessage("棕色"),
Expand Down Expand Up @@ -97,13 +98,15 @@ class MessageLookup extends MessageLookupByLibrary {
"mobileHeading2": MessageLookupByLibrary.simpleMessage("二級標題"),
"mobileHeading3": MessageLookupByLibrary.simpleMessage("三級標題"),
"nextMatch": MessageLookupByLibrary.simpleMessage("下一相符項"),
"noFindResult": MessageLookupByLibrary.simpleMessage("無相符項"),
"numberedList": MessageLookupByLibrary.simpleMessage("有序列表"),
"opacity": MessageLookupByLibrary.simpleMessage("透明度"),
"openLink": MessageLookupByLibrary.simpleMessage("打開連結"),
"paste": MessageLookupByLibrary.simpleMessage("貼上"),
"previousMatch": MessageLookupByLibrary.simpleMessage("上一相符項"),
"quote": MessageLookupByLibrary.simpleMessage("引文"),
"regex": MessageLookupByLibrary.simpleMessage("正規表示式"),
"regexError": MessageLookupByLibrary.simpleMessage("正規錯誤"),
"removeLink": MessageLookupByLibrary.simpleMessage("移除連結"),
"replace": MessageLookupByLibrary.simpleMessage("取代"),
"replaceAll": MessageLookupByLibrary.simpleMessage("取代全部"),
Expand Down
30 changes: 30 additions & 0 deletions lib/src/l10n/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77e5d87

Please sign in to comment.