OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/editing/spellcheck/HotModeSpellCheckRequester.h" | 5 #include "core/editing/spellcheck/HotModeSpellCheckRequester.h" |
6 | 6 |
7 #include "core/editing/EditingUtilities.h" | 7 #include "core/editing/EditingUtilities.h" |
8 #include "core/editing/Editor.h" | 8 #include "core/editing/Editor.h" |
9 #include "core/editing/VisiblePosition.h" | 9 #include "core/editing/VisiblePosition.h" |
10 #include "core/editing/commands/CompositeEditCommand.h" | 10 #include "core/editing/commands/CompositeEditCommand.h" |
11 #include "core/editing/commands/TypingCommand.h" | 11 #include "core/editing/commands/TypingCommand.h" |
12 #include "core/editing/iterators/BackwardsCharacterIterator.h" | 12 #include "core/editing/iterators/BackwardsCharacterIterator.h" |
13 #include "core/editing/iterators/CharacterIterator.h" | 13 #include "core/editing/iterators/CharacterIterator.h" |
14 #include "core/editing/markers/DocumentMarkerController.h" | 14 #include "core/editing/markers/DocumentMarkerController.h" |
15 #include "core/editing/spellcheck/SpellCheckRequester.h" | 15 #include "core/editing/spellcheck/SpellCheckRequester.h" |
16 #include "core/editing/spellcheck/SpellChecker.h" | 16 #include "core/editing/spellcheck/SpellChecker.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
| 22 const int kHotModeCheckAllThreshold = 128; |
22 const int kHotModeChunkSize = 1024; | 23 const int kHotModeChunkSize = 1024; |
23 | 24 |
24 EphemeralRange AdjacentWordIfExists(const Position& pos) { | 25 EphemeralRange AdjacentWordIfExists(const Position& pos) { |
25 const VisiblePosition& visible_pos = CreateVisiblePosition(pos); | 26 const VisiblePosition& visible_pos = CreateVisiblePosition(pos); |
26 const VisiblePosition& word_start = PreviousWordPosition(visible_pos); | 27 const VisiblePosition& word_start = PreviousWordPosition(visible_pos); |
27 if (word_start.IsNull()) | 28 if (word_start.IsNull()) |
28 return EphemeralRange(); | 29 return EphemeralRange(); |
29 const VisiblePosition& word_end = EndOfWord(word_start); | 30 const VisiblePosition& word_end = EndOfWord(word_start); |
30 if (word_end.IsNull()) | 31 if (word_end.IsNull()) |
31 return EphemeralRange(); | 32 return EphemeralRange(); |
(...skipping 26 matching lines...) Expand all Loading... |
58 return false; | 59 return false; |
59 if (editable.VisibleBoundsInVisualViewport().IsEmpty()) | 60 if (editable.VisibleBoundsInVisualViewport().IsEmpty()) |
60 return false; | 61 return false; |
61 // TODO(xiaochengh): Design more aggressive strategies to reduce checking when | 62 // TODO(xiaochengh): Design more aggressive strategies to reduce checking when |
62 // we are just moving selection around without modifying anything. | 63 // we are just moving selection around without modifying anything. |
63 return true; | 64 return true; |
64 } | 65 } |
65 | 66 |
66 EphemeralRange CalculateHotModeCheckingRange(const Element& editable, | 67 EphemeralRange CalculateHotModeCheckingRange(const Element& editable, |
67 const Position& position) { | 68 const Position& position) { |
| 69 // Check everything in |editable| if its total length is short. |
68 const EphemeralRange& full_range = EphemeralRange::RangeOfContents(editable); | 70 const EphemeralRange& full_range = EphemeralRange::RangeOfContents(editable); |
69 const int full_length = TextIterator::RangeLength(full_range.StartPosition(), | 71 const int full_length = TextIterator::RangeLength(full_range.StartPosition(), |
70 full_range.EndPosition()); | 72 full_range.EndPosition()); |
71 if (full_length <= kHotModeChunkSize) | 73 // TODO(xiaochengh): There is no need to check if |full_length <= 2|, since |
| 74 // we don't consider two characters as misspelled. However, a lot of layout |
| 75 // tests depend on "zz" as misspelled, which should be changed. |
| 76 if (full_length <= kHotModeCheckAllThreshold) |
72 return full_range; | 77 return full_range; |
73 | 78 |
| 79 // Otherwise, if |position| is in a short paragraph, check the paragraph. |
| 80 const EphemeralRange& paragraph_range = |
| 81 ExpandToParagraphBoundary(EphemeralRange(position)); |
| 82 const int paragraph_length = TextIterator::RangeLength( |
| 83 paragraph_range.StartPosition(), paragraph_range.EndPosition()); |
| 84 if (paragraph_length <= kHotModeChunkSize) |
| 85 return paragraph_range; |
| 86 |
| 87 // Otherwise, check a chunk of text centered at |position|. |
74 TextIteratorBehavior behavior = TextIteratorBehavior::Builder() | 88 TextIteratorBehavior behavior = TextIteratorBehavior::Builder() |
75 .SetEmitsObjectReplacementCharacter(true) | 89 .SetEmitsObjectReplacementCharacter(true) |
76 .Build(); | 90 .Build(); |
77 BackwardsCharacterIterator backward_iterator(full_range.StartPosition(), | 91 BackwardsCharacterIterator backward_iterator(full_range.StartPosition(), |
78 position, behavior); | 92 position, behavior); |
79 if (!backward_iterator.AtEnd()) | 93 if (!backward_iterator.AtEnd()) |
80 backward_iterator.Advance(kHotModeChunkSize / 2); | 94 backward_iterator.Advance(kHotModeChunkSize / 2); |
81 const Position& chunk_start = backward_iterator.EndPosition(); | 95 const Position& chunk_start = backward_iterator.EndPosition(); |
82 CharacterIterator forward_iterator(position, full_range.EndPosition(), | 96 CharacterIterator forward_iterator(position, full_range.EndPosition(), |
83 behavior); | 97 behavior); |
(...skipping 28 matching lines...) Expand all Loading... |
112 current_word, DocumentMarker::MisspellingMarkers()); | 126 current_word, DocumentMarker::MisspellingMarkers()); |
113 return; | 127 return; |
114 } | 128 } |
115 | 129 |
116 const EphemeralRange& checking_range = | 130 const EphemeralRange& checking_range = |
117 CalculateHotModeCheckingRange(*root_editable, position); | 131 CalculateHotModeCheckingRange(*root_editable, position); |
118 requester_->RequestCheckingFor(checking_range); | 132 requester_->RequestCheckingFor(checking_range); |
119 } | 133 } |
120 | 134 |
121 } // namespace blink | 135 } // namespace blink |
OLD | NEW |