spell_check.dart 15.6 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart' show TargetPlatform, defaultTargetPlatform;
6 7 8 9
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart'
    show SpellCheckResults, SpellCheckService, SuggestionSpan, TextEditingValue;

10 11 12
import 'editable_text.dart' show EditableTextContextMenuBuilder;
import 'framework.dart' show immutable;

13 14 15 16 17 18 19 20 21 22 23
/// Controls how spell check is performed for text input.
///
/// This configuration determines the [SpellCheckService] used to fetch the
/// [List<SuggestionSpan>] spell check results and the [TextStyle] used to
/// mark misspelled words within text input.
@immutable
class SpellCheckConfiguration {
  /// Creates a configuration that specifies the service and suggestions handler
  /// for spell check.
  const SpellCheckConfiguration({
    this.spellCheckService,
24
    this.misspelledSelectionColor,
25
    this.misspelledTextStyle,
26
    this.spellCheckSuggestionsToolbarBuilder,
27 28 29 30 31 32
  }) : _spellCheckEnabled = true;

  /// Creates a configuration that disables spell check.
  const SpellCheckConfiguration.disabled()
    :  _spellCheckEnabled = false,
       spellCheckService = null,
33
       spellCheckSuggestionsToolbarBuilder = null,
34 35
       misspelledTextStyle = null,
       misspelledSelectionColor = null;
36 37 38 39

  /// The service used to fetch spell check results for text input.
  final SpellCheckService? spellCheckService;

40 41 42 43 44 45 46
  /// The color the paint the selection highlight when spell check is showing
  /// suggestions for a misspelled word.
  ///
  /// For example, on iOS, the selection appears red while the spell check menu
  /// is showing.
  final Color? misspelledSelectionColor;

47 48 49 50 51 52 53 54
  /// Style used to indicate misspelled words.
  ///
  /// This is nullable to allow style-specific wrappers of [EditableText]
  /// to infer this, but this must be specified if this configuration is
  /// provided directly to [EditableText] or its construction will fail with an
  /// assertion error.
  final TextStyle? misspelledTextStyle;

55 56 57 58
  /// Builds the toolbar used to display spell check suggestions for misspelled
  /// words.
  final EditableTextContextMenuBuilder? spellCheckSuggestionsToolbarBuilder;

59 60 61 62 63 64 65 66 67
  final bool _spellCheckEnabled;

  /// Whether or not the configuration should enable or disable spell check.
  bool get spellCheckEnabled => _spellCheckEnabled;

  /// Returns a copy of the current [SpellCheckConfiguration] instance with
  /// specified overrides.
  SpellCheckConfiguration copyWith({
    SpellCheckService? spellCheckService,
68
    Color? misspelledSelectionColor,
69 70
    TextStyle? misspelledTextStyle,
    EditableTextContextMenuBuilder? spellCheckSuggestionsToolbarBuilder}) {
71 72 73 74 75 76 77
    if (!_spellCheckEnabled) {
      // A new configuration should be constructed to enable spell check.
      return const SpellCheckConfiguration.disabled();
    }

    return SpellCheckConfiguration(
      spellCheckService: spellCheckService ?? this.spellCheckService,
78
      misspelledSelectionColor: misspelledSelectionColor ?? this.misspelledSelectionColor,
79
      misspelledTextStyle: misspelledTextStyle ?? this.misspelledTextStyle,
80
      spellCheckSuggestionsToolbarBuilder : spellCheckSuggestionsToolbarBuilder ?? this.spellCheckSuggestionsToolbarBuilder,
81 82 83 84 85 86 87 88 89
    );
  }

  @override
  String toString() {
    return '''
  spell check enabled   : $_spellCheckEnabled
  spell check service   : $spellCheckService
  misspelled text style : $misspelledTextStyle
Lioness100's avatar
Lioness100 committed
90
  spell check suggestions toolbar builder: $spellCheckSuggestionsToolbarBuilder
91 92 93 94 95 96 97 98 99 100 101 102 103
'''
        .trim();
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
        return true;
    }

    return other is SpellCheckConfiguration
      && other.spellCheckService == spellCheckService
      && other.misspelledTextStyle == misspelledTextStyle
104
      && other.spellCheckSuggestionsToolbarBuilder == spellCheckSuggestionsToolbarBuilder
105 106 107 108
      && other._spellCheckEnabled == _spellCheckEnabled;
  }

  @override
109
  int get hashCode => Object.hash(spellCheckService, misspelledTextStyle, spellCheckSuggestionsToolbarBuilder, _spellCheckEnabled);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

// Methods for displaying spell check results:

/// Adjusts spell check results to correspond to [newText] if the only results
/// that the handler has access to are the [results] corresponding to
/// [resultsText].
///
/// Used in the case where the request for the spell check results of the
/// [newText] is lagging in order to avoid display of incorrect results.
List<SuggestionSpan> _correctSpellCheckResults(
    String newText, String resultsText, List<SuggestionSpan> results) {
  final List<SuggestionSpan> correctedSpellCheckResults = <SuggestionSpan>[];
  int spanPointer = 0;
  int offset = 0;

  // Assumes that the order of spans has not been jumbled for optimization
  // purposes, and will only search since the previously found span.
  int searchStart = 0;

  while (spanPointer < results.length) {
131 132
    final SuggestionSpan currentSpan = results[spanPointer];
    final String currentSpanText =
133
        resultsText.substring(currentSpan.range.start, currentSpan.range.end);
134
    final int spanLength = currentSpan.range.end - currentSpan.range.start;
135

136 137 138 139 140 141 142 143
    // Try finding SuggestionSpan from resultsText in new text.
    final RegExp currentSpanTextRegexp = RegExp('\\b$currentSpanText\\b');
    final int foundIndex = newText.substring(searchStart).indexOf(currentSpanTextRegexp);

    // Check whether word was found exactly where expected or elsewhere in the newText.
    final bool currentSpanFoundExactly = currentSpan.range.start == foundIndex + searchStart;
    final bool currentSpanFoundExactlyWithOffset = currentSpan.range.start + offset == foundIndex + searchStart;
    final bool currentSpanFoundElsewhere = foundIndex >= 0;
144

145 146 147 148
    if (currentSpanFoundExactly || currentSpanFoundExactlyWithOffset) {
      // currentSpan was found at the same index in newText and resutsText
      // or at the same index with the previously calculated adjustment by
      // the offset value, so apply it to new text by adding it to the list of
149
      // corrected results.
150 151 152 153 154 155
      final SuggestionSpan adjustedSpan = SuggestionSpan(
        TextRange(
          start: currentSpan.range.start + offset,
          end: currentSpan.range.end + offset,
        ),
        currentSpan.suggestions,
156
      );
157 158 159

      // Start search for the next misspelled word at the end of currentSpan.
      searchStart = currentSpan.range.end + 1 + offset;
160
      correctedSpellCheckResults.add(adjustedSpan);
161 162 163 164 165 166 167 168
    } else if (currentSpanFoundElsewhere) {
      // Word was pushed forward but not modified.
      final int adjustedSpanStart = searchStart + foundIndex;
      final int adjustedSpanEnd = adjustedSpanStart + spanLength;
      final SuggestionSpan adjustedSpan = SuggestionSpan(
        TextRange(start: adjustedSpanStart, end: adjustedSpanEnd),
        currentSpan.suggestions,
      );
169

170 171 172 173 174 175 176
      // Start search for the next misspelled word at the end of the
      // adjusted currentSpan.
      searchStart = adjustedSpanEnd + 1;
      // Adjust offset to reflect the difference between where currentSpan
      // was positioned in resultsText versus in newText.
      offset = adjustedSpanStart - currentSpan.range.start;
      correctedSpellCheckResults.add(adjustedSpan);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    }
    spanPointer++;
  }
  return correctedSpellCheckResults;
}

/// Builds the [TextSpan] tree given the current state of the text input and
/// spell check results.
///
/// The [value] is the current [TextEditingValue] requested to be rendered
/// by a text input widget. The [composingWithinCurrentTextRange] value
/// represents whether or not there is a valid composing region in the
/// [value]. The [style] is the [TextStyle] to render the [value]'s text with,
/// and the [misspelledTextStyle] is the [TextStyle] to render misspelled
/// words within the [value]'s text with. The [spellCheckResults] are the
/// results of spell checking the [value]'s text.
TextSpan buildTextSpanWithSpellCheckSuggestions(
    TextEditingValue value,
    bool composingWithinCurrentTextRange,
    TextStyle? style,
    TextStyle misspelledTextStyle,
    SpellCheckResults spellCheckResults) {
  List<SuggestionSpan> spellCheckResultsSpans =
      spellCheckResults.suggestionSpans;
  final String spellCheckResultsText = spellCheckResults.spellCheckedText;

  if (spellCheckResultsText != value.text) {
    spellCheckResultsSpans = _correctSpellCheckResults(
        value.text, spellCheckResultsText, spellCheckResultsSpans);
  }

208 209 210 211 212 213 214 215
  // We will draw the TextSpan tree based on the composing region, if it is
  // available.
  // TODO(camsim99): The two separate stratgies for building TextSpan trees
  // based on the availability of a composing region should be merged:
  // https://github.com/flutter/flutter/issues/124142.
  final bool shouldConsiderComposingRegion = defaultTargetPlatform == TargetPlatform.android;
  if (shouldConsiderComposingRegion) {
    return TextSpan(
216
      style: style,
217
      children: _buildSubtreesWithComposingRegion(
218 219 220 221
          spellCheckResultsSpans,
          value,
          style,
          misspelledTextStyle,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
          composingWithinCurrentTextRange,
      ),
    );
  }

  return TextSpan(
    style: style,
    children: _buildSubtreesWithoutComposingRegion(
      spellCheckResultsSpans,
      value,
      style,
      misspelledTextStyle,
      value.selection.baseOffset,
    ),
  );
}

/// Builds the [TextSpan] tree for spell check without considering the composing
/// region. Instead, uses the cursor to identify the word that's actively being
/// edited and shouldn't be spell checked. This is useful for platforms and IMEs
/// that don't use the composing region for the active word.
List<TextSpan> _buildSubtreesWithoutComposingRegion(
    List<SuggestionSpan>? spellCheckSuggestions,
    TextEditingValue value,
    TextStyle? style,
    TextStyle misspelledStyle,
    int cursorIndex,
) {
  final List<TextSpan> textSpanTreeChildren = <TextSpan>[];

  int textPointer = 0;
  int currentSpanPointer = 0;
  int endIndex;
  final String text = value.text;
  final TextStyle misspelledJointStyle =
      style?.merge(misspelledStyle) ?? misspelledStyle;
  bool cursorInCurrentSpan = false;

  // Add text interwoven with any misspelled words to the tree.
  if (spellCheckSuggestions != null) {
    while (textPointer < text.length &&
      currentSpanPointer < spellCheckSuggestions.length) {
      final SuggestionSpan currentSpan = spellCheckSuggestions[currentSpanPointer];

      if (currentSpan.range.start > textPointer) {
        endIndex = currentSpan.range.start < text.length
            ? currentSpan.range.start
            : text.length;
        textSpanTreeChildren.add(
          TextSpan(
            style: style,
            text: text.substring(textPointer, endIndex),
          )
        );
        textPointer = endIndex;
      } else {
        endIndex =
            currentSpan.range.end < text.length ? currentSpan.range.end : text.length;
        cursorInCurrentSpan = currentSpan.range.start <= cursorIndex && currentSpan.range.end >= cursorIndex;
        textSpanTreeChildren.add(
          TextSpan(
            style: cursorInCurrentSpan
                ? style
                : misspelledJointStyle,
            text: text.substring(currentSpan.range.start, endIndex),
          )
        );

        textPointer = endIndex;
        currentSpanPointer++;
      }
    }
  }

  // Add any remaining text to the tree if applicable.
  if (textPointer < text.length) {
    textSpanTreeChildren.add(
      TextSpan(
        style: style,
        text: text.substring(textPointer, text.length),
302 303
      )
    );
304 305 306
  }

  return textSpanTreeChildren;
307 308
}

309 310 311
/// Builds [TextSpan] subtree for text with misspelled words with logic based on
/// a valid composing region.
List<TextSpan> _buildSubtreesWithComposingRegion(
312 313 314 315 316
    List<SuggestionSpan>? spellCheckSuggestions,
    TextEditingValue value,
    TextStyle? style,
    TextStyle misspelledStyle,
    bool composingWithinCurrentTextRange) {
317
  final List<TextSpan> textSpanTreeChildren = <TextSpan>[];
318 319

  int textPointer = 0;
320
  int currentSpanPointer = 0;
321
  int endIndex;
322
  SuggestionSpan currentSpan;
323 324 325 326 327 328 329 330
  final String text = value.text;
  final TextRange composingRegion = value.composing;
  final TextStyle composingTextStyle =
      style?.merge(const TextStyle(decoration: TextDecoration.underline)) ??
          const TextStyle(decoration: TextDecoration.underline);
  final TextStyle misspelledJointStyle =
      style?.merge(misspelledStyle) ?? misspelledStyle;
  bool textPointerWithinComposingRegion = false;
331
  bool currentSpanIsComposingRegion = false;
332 333 334 335

  // Add text interwoven with any misspelled words to the tree.
  if (spellCheckSuggestions != null) {
    while (textPointer < text.length &&
336 337
      currentSpanPointer < spellCheckSuggestions.length) {
      currentSpan = spellCheckSuggestions[currentSpanPointer];
338

339 340 341
      if (currentSpan.range.start > textPointer) {
        endIndex = currentSpan.range.start < text.length
            ? currentSpan.range.start
342 343 344 345 346 347 348
            : text.length;
        textPointerWithinComposingRegion =
            composingRegion.start >= textPointer &&
                composingRegion.end <= endIndex &&
                !composingWithinCurrentTextRange;

        if (textPointerWithinComposingRegion) {
349
          _addComposingRegionTextSpans(textSpanTreeChildren, text, textPointer,
350
              composingRegion, style, composingTextStyle);
351
          textSpanTreeChildren.add(
352 353
            TextSpan(
              style: style,
354
              text: text.substring(composingRegion.end, endIndex),
355 356 357
            )
          );
        } else {
358
          textSpanTreeChildren.add(
359 360
            TextSpan(
              style: style,
361
              text: text.substring(textPointer, endIndex),
362 363 364 365 366 367 368
            )
          );
        }

        textPointer = endIndex;
      } else {
        endIndex =
369 370
            currentSpan.range.end < text.length ? currentSpan.range.end : text.length;
        currentSpanIsComposingRegion = textPointer >= composingRegion.start &&
371 372
            endIndex <= composingRegion.end &&
            !composingWithinCurrentTextRange;
373
        textSpanTreeChildren.add(
374
          TextSpan(
375
            style: currentSpanIsComposingRegion
376 377
                ? composingTextStyle
                : misspelledJointStyle,
378
            text: text.substring(currentSpan.range.start, endIndex),
379 380 381 382
          )
        );

        textPointer = endIndex;
383
        currentSpanPointer++;
384 385 386 387 388 389 390 391
      }
    }
  }

  // Add any remaining text to the tree if applicable.
  if (textPointer < text.length) {
    if (textPointer < composingRegion.start &&
        !composingWithinCurrentTextRange) {
392
      _addComposingRegionTextSpans(textSpanTreeChildren, text, textPointer,
393 394 395
          composingRegion, style, composingTextStyle);

      if (composingRegion.end != text.length) {
396
        textSpanTreeChildren.add(
397 398
          TextSpan(
            style: style,
399
            text: text.substring(composingRegion.end, text.length),
400 401 402 403
          )
        );
      }
    } else {
404
      textSpanTreeChildren.add(
405
        TextSpan(
406
          style: style, text: text.substring(textPointer, text.length),
407 408 409 410 411
        )
      );
    }
  }

412
  return textSpanTreeChildren;
413 414 415 416 417 418 419 420 421 422 423 424 425 426
}

/// Helper method to create [TextSpan] tree children for specified range of
/// text up to and including the composing region.
void _addComposingRegionTextSpans(
    List<TextSpan> treeChildren,
    String text,
    int start,
    TextRange composingRegion,
    TextStyle? style,
    TextStyle composingTextStyle) {
  treeChildren.add(
    TextSpan(
      style: style,
427
      text: text.substring(start, composingRegion.start),
428 429 430 431 432
    )
  );
  treeChildren.add(
    TextSpan(
      style: composingTextStyle,
433
      text: text.substring(composingRegion.start, composingRegion.end),
434 435 436
    )
  );
}