Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: third_party/WebKit/Source/core/editing/markers/DocumentMarkerListEditor.cpp

Issue 2829543002: [DMC #5] Add SpellCheckMarkerListImpl (Closed)
Patch Set: Clean up DocumentMarkerList/GenericDMLImpl Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/markers/DocumentMarkerListEditor.h" 5 #include "core/editing/markers/DocumentMarkerListEditor.h"
6 6
7 #include "core/editing/markers/RenderedDocumentMarker.h" 7 #include "core/editing/markers/RenderedDocumentMarker.h"
8 8
9 namespace blink { 9 namespace blink {
10 10
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 if (marker.StartOffset() >= end_offset) 75 if (marker.StartOffset() >= end_offset)
76 break; 76 break;
77 77
78 list->erase(i - list->begin()); 78 list->erase(i - list->begin());
79 doc_dirty = true; 79 doc_dirty = true;
80 } 80 }
81 81
82 return doc_dirty; 82 return doc_dirty;
83 } 83 }
84 84
85 bool DocumentMarkerListEditor::RemoveMarkersUnderWords(
Xiaocheng 2017/04/27 19:04:01 Could you modify this function in-place, and move
86 MarkerList* list,
87 const String& node_text,
88 const Vector<String>& words) {
89 bool removed_markers = false;
90 for (size_t j = list->size(); j > 0; --j) {
91 const DocumentMarker& marker = *(*list)[j - 1];
92 const unsigned start = marker.StartOffset();
93 const unsigned length = marker.EndOffset() - marker.StartOffset();
94 const String& marker_text = node_text.Substring(start, length);
95 if (words.Contains(marker_text)) {
96 list->erase(j - 1);
97 removed_markers = true;
98 }
99 }
100
101 return removed_markers;
102 }
103
104 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list, 85 bool DocumentMarkerListEditor::ShiftMarkers(MarkerList* list,
105 unsigned offset, 86 unsigned offset,
106 unsigned old_length, 87 unsigned old_length,
107 unsigned new_length) { 88 unsigned new_length) {
108 bool did_shift_marker = false; 89 bool did_shift_marker = false;
109 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { 90 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) {
110 RenderedDocumentMarker& marker = **it; 91 RenderedDocumentMarker& marker = **it;
111 Optional<DocumentMarker::MarkerOffsets> result = 92 Optional<DocumentMarker::MarkerOffsets> result =
112 marker.ComputeOffsetsAfterShift(offset, old_length, new_length); 93 marker.ComputeOffsetsAfterShift(offset, old_length, new_length);
113 if (result == WTF::kNullopt) { 94 if (result == WTF::kNullopt) {
114 list->erase(it - list->begin()); 95 list->erase(it - list->begin());
115 --it; 96 --it;
116 did_shift_marker = true; 97 did_shift_marker = true;
117 continue; 98 continue;
118 } 99 }
119 100
120 if (marker.StartOffset() != result.value().start_offset || 101 if (marker.StartOffset() != result.value().start_offset ||
121 marker.EndOffset() != result.value().end_offset) { 102 marker.EndOffset() != result.value().end_offset) {
122 did_shift_marker = true; 103 did_shift_marker = true;
123 marker.SetStartOffset(result.value().start_offset); 104 marker.SetStartOffset(result.value().start_offset);
124 marker.SetEndOffset(result.value().end_offset); 105 marker.SetEndOffset(result.value().end_offset);
125 } 106 }
126 } 107 }
127 108
128 return did_shift_marker; 109 return did_shift_marker;
129 } 110 }
130 111
131 void DocumentMarkerListEditor::AddMarkerAndMergeOverlapping(
Xiaocheng 2017/04/27 19:04:01 Could you modify this function in-place, and move
rlanday 2017/04/27 20:30:13 I don't think I'm modifying either of these method
132 MarkerList* list,
133 const DocumentMarker* marker) {
134 RenderedDocumentMarker* rendered_marker =
135 RenderedDocumentMarker::Create(*marker);
136 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) {
137 list->push_back(rendered_marker);
138 return;
139 }
140
141 auto first_overlapping = std::lower_bound(
142 list->begin(), list->end(), rendered_marker,
143 [](const Member<RenderedDocumentMarker>& marker_in_list,
144 const DocumentMarker* marker_to_insert) {
145 return marker_in_list->EndOffset() < marker_to_insert->StartOffset();
146 });
147
148 size_t index = first_overlapping - list->begin();
149 list->insert(index, rendered_marker);
150 const auto inserted = list->begin() + index;
151 first_overlapping = inserted + 1;
152 // TODO(rlanday): optimize this loop so it runs in O(N) time and not O(N^2)
153 for (const auto i = first_overlapping;
154 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) {
155 (*inserted)->SetStartOffset(
156 std::min((*inserted)->StartOffset(), (*i)->StartOffset()));
157 (*inserted)->SetEndOffset(
158 std::max((*inserted)->EndOffset(), (*i)->EndOffset()));
159 list->erase(i - list->begin());
160 }
161 }
162
163 } // namespace blink 112 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698