Skip to content

Commit

Permalink
Chore: dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
CatHood0 committed Aug 20, 2024
1 parent c7314ae commit 0fce2ec
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 51 deletions.
3 changes: 2 additions & 1 deletion lib/src/common/language_dic_priority_order.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@Deprecated('LanguageDicPriorityOrder is not longer used and will be removed in future releases\nPlease use StrategyLanguageSearchOrder instead')
@Deprecated(
'LanguageDicPriorityOrder is not longer used and will be removed in future releases\nPlease use StrategyLanguageSearchOrder instead')
enum LanguageDicPriorityOrder {
customFirst,
defaultFirst,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/common/strategy_language_search_order.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// This tells to the [SimpleSpellchecker] which should be the priority
/// when we need to search the dictionary for the current language
enum StrategyLanguageSearchOrder {
// Will search first if the language implementation is into the
// Will search first if the language implementation is into the
// custom languages
byUser,
// Will search first if the language implementation is into the
// default languages implemented by the package
byUser,
// Will search first if the language implementation is into the
// default languages implemented by the package
byPackage,
}
3 changes: 2 additions & 1 deletion lib/src/common/tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class Tokenizer {
Tokenizer({this.separatorRegExp});
bool canTokenizeText(String text);
List<String> tokenize(String content,
{@Deprecated('removeAllEmptyWords are no longer used and will be removed in future releases')
{@Deprecated(
'removeAllEmptyWords are no longer used and will be removed in future releases')
bool removeAllEmptyWords = false});
}
108 changes: 76 additions & 32 deletions lib/src/spell_checker.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import 'dart:async' show Future, Stream, StreamController;
import 'dart:convert' show LineSplitter;
import 'package:flutter/gestures.dart' show LongPressGestureRecognizer;
import 'package:flutter/material.dart' show Colors, TextDecoration, TextDecorationStyle, TextSpan, TextStyle;
import 'package:flutter/material.dart'
show Colors, TextDecoration, TextDecorationStyle, TextSpan, TextStyle;
import 'package:simple_spell_checker/simple_spell_checker.dart'
show LanguageIdentifier, WordTokenizer, defaultLanguages, isWordHasNumber, LanguageDicPriorityOrder;
show
LanguageIdentifier,
WordTokenizer,
defaultLanguages,
isWordHasNumber,
LanguageDicPriorityOrder;
import 'package:simple_spell_checker/src/common/extensions.dart';
import 'package:simple_spell_checker/src/common/tokenizer.dart' show Tokenizer;
import 'package:simple_spell_checker/src/utils.dart'
show defaultLanguages, defaultLanguagesMap, isWordHasNumber, notSupportedLanguages;
show
defaultLanguages,
defaultLanguagesMap,
isWordHasNumber,
notSupportedLanguages;
import 'common/cache_object.dart' show CacheObject;
import 'common/strategy_language_search_order.dart';

Expand Down Expand Up @@ -62,7 +72,8 @@ class SimpleSpellChecker {
/// If safeDictionaryLoad is true, this will be used as the default language to update
/// the state of SimpleSpellChecker and to store to a existent language with its dictionary
String safeLanguageName;
final StreamController<Object?> _simpleSpellCheckerWidgetsState = StreamController.broadcast();
final StreamController<Object?> _simpleSpellCheckerWidgetsState =
StreamController.broadcast();
final StreamController<String?> _languageState = StreamController.broadcast();
LanguageDicPriorityOrder priorityOrder;
StrategyLanguageSearchOrder strategy;
Expand All @@ -85,7 +96,8 @@ class SimpleSpellChecker {
_cacheLanguageIdentifier = null;
_wordTokenizer = wordTokenizer ?? WordTokenizer();
reloadDictionarySync();
if (autoAddLanguagesFromCustomDictionaries) _addLanguagesFromCustomDictionaries();
if (autoAddLanguagesFromCustomDictionaries)
_addLanguagesFromCustomDictionaries();
}

/// Check if your line wrong words
Expand All @@ -95,9 +107,11 @@ class SimpleSpellChecker {
/// or make some custom action for wrong words
List<TextSpan>? check(
String text, {
@Deprecated('removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
@Deprecated(
'removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
bool removeEmptyWordsOnTokenize = false,
LongPressGestureRecognizer Function(String)? customLongPressRecognizerOnWrongSpan,
LongPressGestureRecognizer Function(String)?
customLongPressRecognizerOnWrongSpan,
}) {
_addNewEventToWidgetsState(null);
// verify if the current language is a non supported one
Expand All @@ -117,7 +131,10 @@ class SimpleSpellChecker {
for (int i = 0; i < words.length; i++) {
final word = words.elementAt(i);
final nextIndex = (i + 1) < words.length - 1 ? i + 1 : -1;
if (isWordHasNumber(word) || !hasWrongWords(word) || word.contains(' ') || word.noWords) {
if (isWordHasNumber(word) ||
!hasWrongWords(word) ||
word.contains(' ') ||
word.noWords) {
if (nextIndex != -1) {
final nextWord = words.elementAt(nextIndex);
if (nextWord.contains(' ')) {
Expand Down Expand Up @@ -154,9 +171,11 @@ class SimpleSpellChecker {
/// or make some custom action for wrong words
Stream<List<TextSpan>> checkStream(
String text, {
@Deprecated('removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
@Deprecated(
'removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
bool removeEmptyWordsOnTokenize = false,
LongPressGestureRecognizer Function(String)? customLongPressRecognizerOnWrongSpan,
LongPressGestureRecognizer Function(String)?
customLongPressRecognizerOnWrongSpan,
}) async* {
// verify if the current language is a non supported one
if (notSupportedLanguages.contains(_language)) {
Expand All @@ -175,7 +194,10 @@ class SimpleSpellChecker {
for (int i = 0; i < words.length; i++) {
final word = words.elementAt(i);
final nextIndex = (i + 1) < words.length - 1 ? i + 1 : -1;
if (isWordHasNumber(word) || !hasWrongWords(word) || word.contains(' ') || word.noWords) {
if (isWordHasNumber(word) ||
!hasWrongWords(word) ||
word.contains(' ') ||
word.noWords) {
if (nextIndex != -1) {
final nextWord = words.elementAt(nextIndex);
if (nextWord.contains(' ')) {
Expand Down Expand Up @@ -213,7 +235,8 @@ class SimpleSpellChecker {
List<T>? checkBuilder<T>(
String text, {
required T Function(String, bool) builder,
@Deprecated('removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
@Deprecated(
'removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
bool removeEmptyWordsOnTokenize = false,
}) {
_addNewEventToWidgetsState(null);
Expand All @@ -235,7 +258,10 @@ class SimpleSpellChecker {
for (int i = 0; i < words.length; i++) {
final word = words.elementAt(i);
final nextIndex = (i + 1) < words.length - 1 ? i + 1 : -1;
if (isWordHasNumber(word) || !hasWrongWords(word) || word.contains(' ') || word.noWords) {
if (isWordHasNumber(word) ||
!hasWrongWords(word) ||
word.contains(' ') ||
word.noWords) {
if (nextIndex != -1) {
final nextWord = words.elementAt(nextIndex);
if (nextWord.contains(' ')) {
Expand Down Expand Up @@ -277,7 +303,8 @@ class SimpleSpellChecker {
Stream<List<T>> checkBuilderStream<T>(
String text, {
required T Function(String, bool) builder,
@Deprecated('removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
@Deprecated(
'removeEmptyWordsOnTokenize are no longer used and will be removed in future releases')
bool removeEmptyWordsOnTokenize = false,
}) async* {
_verifyState();
Expand All @@ -297,7 +324,10 @@ class SimpleSpellChecker {
for (int i = 0; i < words.length; i++) {
final word = words.elementAt(i);
final nextIndex = (i + 1) < words.length - 1 ? i + 1 : -1;
if (isWordHasNumber(word) || !hasWrongWords(word) || word.contains(' ') || word.noWords) {
if (isWordHasNumber(word) ||
!hasWrongWords(word) ||
word.contains(' ') ||
word.noWords) {
if (nextIndex != -1) {
final nextWord = words.elementAt(nextIndex);
if (nextWord.contains(' ')) {
Expand Down Expand Up @@ -328,7 +358,8 @@ class SimpleSpellChecker {
if (word.trim().isEmpty) return false;
_verifyState(alsoCache: true);
final wordsMap = _cacheWordDictionary?.get ?? {};
final newWordWithCaseSensitive = caseSensitive ? word.toLowerCaseFirst() : word.trim().toLowerCase();
final newWordWithCaseSensitive =
caseSensitive ? word.toLowerCaseFirst() : word.trim().toLowerCase();
final int? validWord = wordsMap[newWordWithCaseSensitive];
return validWord == null;
}
Expand All @@ -340,11 +371,14 @@ class SimpleSpellChecker {

void setNewLanguageToState(String language) {
_verifyState();
assert(language.isNotEmpty, 'The country code of your language cannot be empty');
assert(language.isNotEmpty,
'The country code of your language cannot be empty');
_language = language;
_addNewEventToLanguageState(_language);
}
@Deprecated('SetNewPriorityOrder is no longer used. Please, use setNewStrategy instead')

@Deprecated(
'SetNewPriorityOrder is no longer used. Please, use setNewStrategy instead')
void setNewPriorityOrder(LanguageDicPriorityOrder priorityOrder) {
_verifyState();
this.priorityOrder = priorityOrder;
Expand Down Expand Up @@ -404,15 +438,17 @@ class SimpleSpellChecker {
throw StateError(
'The identifier ${language.language} is not into customLanguages. Please consider add before use update operations');
}
int indexOf = customLanguages!.indexWhere((element) => element.language == language.language);
int indexOf = customLanguages!
.indexWhere((element) => element.language == language.language);
if (indexOf != -1) {
customLanguages![indexOf] = language;
}
}

/// Use disposeControllers is just never will be use the StreamControllers
void disposeControllers() {
if (!_simpleSpellCheckerWidgetsState.isClosed) _simpleSpellCheckerWidgetsState.close();
if (!_simpleSpellCheckerWidgetsState.isClosed)
_simpleSpellCheckerWidgetsState.close();
if (!_languageState.isClosed) _languageState.close();
_disposedControllers = true;
}
Expand All @@ -428,9 +464,11 @@ class SimpleSpellChecker {
_verifyState();
if (_cacheLanguageIdentifier?.get.language == _language) return;
// check if the current language is not registered already
if ((strategy == StrategyLanguageSearchOrder.byUser || !defaultLanguages.contains(_language)) &&
if ((strategy == StrategyLanguageSearchOrder.byUser ||
!defaultLanguages.contains(_language)) &&
_intoCount <= 2) {
final indexOf = customLanguages?.indexWhere((element) => element.language == _language);
final indexOf = customLanguages
?.indexWhere((element) => element.language == _language);
final invalidIndex = (indexOf == null || indexOf == -1);
if (invalidIndex && !safeDictionaryLoad) {
throw UnsupportedError(
Expand All @@ -442,7 +480,8 @@ class SimpleSpellChecker {
reloadDictionarySync();
return;
}
final LanguageIdentifier identifier = customLanguages!.elementAt(indexOf!);
final LanguageIdentifier identifier =
customLanguages!.elementAt(indexOf!);
_initDictionary(identifier);
return;
}
Expand All @@ -457,12 +496,13 @@ class SimpleSpellChecker {
} else {
_cacheLanguageIdentifier!.set = identifier;
}
final Iterable<MapEntry<String, int>> entries = const LineSplitter().convert(identifier.words).map(
(element) => MapEntry(
element.trim().toLowerCase(),
1,
),
);
final Iterable<MapEntry<String, int>> entries =
const LineSplitter().convert(identifier.words).map(
(element) => MapEntry(
element.trim().toLowerCase(),
1,
),
);
final Map<String, int> wordsMap = {};
wordsMap.addEntries(entries);
_cacheWordDictionary ??= CacheObject(object: {});
Expand Down Expand Up @@ -501,12 +541,15 @@ class SimpleSpellChecker {
void _verifyState({bool alsoCache = false}) {
if (!_disposedControllers) {
assert(
!_disposed && !_simpleSpellCheckerWidgetsState.isClosed && !_languageState.isClosed,
!_disposed &&
!_simpleSpellCheckerWidgetsState.isClosed &&
!_languageState.isClosed,
'You cannot reuse this SimpleSpellchecker since you dispose it before',
);
return;
}
assert(!_disposed, 'You cannot reuse this SimpleSpellchecker since you dispose it before');
assert(!_disposed,
'You cannot reuse this SimpleSpellchecker since you dispose it before');
if (alsoCache) {
assert(_cacheWordDictionary != null);
assert(_cacheLanguageIdentifier != null);
Expand All @@ -517,7 +560,8 @@ class SimpleSpellChecker {
void dispose({bool closeDirectionary = true}) {
if (closeDirectionary) _cacheWordDictionary = null;
_cacheLanguageIdentifier = null;
if (!_simpleSpellCheckerWidgetsState.isClosed) _simpleSpellCheckerWidgetsState.close();
if (!_simpleSpellCheckerWidgetsState.isClosed)
_simpleSpellCheckerWidgetsState.close();
if (!_languageState.isClosed) _languageState.close();
_disposed = true;
_disposedControllers = true;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/word_tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class WordTokenizer extends Tokenizer {
@override
List<String> tokenize(
String content, {
@Deprecated('removeAllEmptyWords are no longer used and will be removed in future releases')
@Deprecated(
'removeAllEmptyWords are no longer used and will be removed in future releases')
bool removeAllEmptyWords = false,
}) {
final List<String> words = (separatorRegExp ?? defaultSeparatorRegExp)
Expand Down
Loading

0 comments on commit 0fce2ec

Please sign in to comment.