Chromium Code Reviews| 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/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 |
| 11 // TODO(rlanday): this method was created by cutting and pasting code from | 11 void DocumentMarkerListEditor::AddMarkerWithoutMergingOverlapping( |
| 12 // DocumentMarkerController::AddMarker(), it should be refactored in a future CL | 12 MarkerList* list, |
| 13 void DocumentMarkerListEditor::AddMarker(MarkerList* list, | 13 const DocumentMarker* marker) { |
| 14 const DocumentMarker* marker) { | |
| 15 RenderedDocumentMarker* rendered_marker = | 14 RenderedDocumentMarker* rendered_marker = |
| 16 RenderedDocumentMarker::Create(*marker); | 15 RenderedDocumentMarker::Create(*marker); |
| 17 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) { | 16 if (list->IsEmpty() || list->back()->EndOffset() <= marker->StartOffset()) { |
| 18 list->push_back(rendered_marker); | 17 list->push_back(rendered_marker); |
| 19 } else { | 18 return; |
| 20 if (marker->GetType() != DocumentMarker::kTextMatch && | |
| 21 marker->GetType() != DocumentMarker::kComposition) { | |
| 22 MergeOverlapping(list, rendered_marker); | |
| 23 } else { | |
| 24 MarkerList::iterator pos = std::lower_bound( | |
| 25 list->begin(), list->end(), marker, | |
| 26 [](const Member<RenderedDocumentMarker>& marker_in_list, | |
| 27 const DocumentMarker* marker_to_insert) { | |
| 28 return marker_in_list->StartOffset() < | |
| 29 marker_to_insert->StartOffset(); | |
| 30 }); | |
| 31 list->insert(pos - list->begin(), rendered_marker); | |
| 32 } | |
| 33 } | 19 } |
| 20 | |
| 21 MarkerList::iterator pos = std::lower_bound( | |
|
yosin_UTC9
2017/04/28 01:01:27
nit: s/MakerList::iterator/const MarkerList::itera
| |
| 22 list->begin(), list->end(), marker, | |
| 23 [](const Member<RenderedDocumentMarker>& marker_in_list, | |
| 24 const DocumentMarker* marker_to_insert) { | |
| 25 return marker_in_list->StartOffset() < marker_to_insert->StartOffset(); | |
| 26 }); | |
| 27 list->insert(pos - list->begin(), rendered_marker); | |
| 34 } | 28 } |
| 35 | 29 |
| 36 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list, | 30 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list, |
| 37 int length, | 31 int length, |
| 38 DocumentMarkerList* dst_list) { | 32 DocumentMarkerList* dst_list) { |
| 39 DCHECK_GT(length, 0); | 33 DCHECK_GT(length, 0); |
| 40 bool didMoveMarker = false; | 34 bool didMoveMarker = false; |
| 41 unsigned end_offset = length - 1; | 35 unsigned end_offset = length - 1; |
| 42 | 36 |
| 43 MarkerList::iterator it; | 37 MarkerList::iterator it; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 marker.EndOffset() != result.value().end_offset) { | 121 marker.EndOffset() != result.value().end_offset) { |
| 128 did_shift_marker = true; | 122 did_shift_marker = true; |
| 129 marker.SetStartOffset(result.value().start_offset); | 123 marker.SetStartOffset(result.value().start_offset); |
| 130 marker.SetEndOffset(result.value().end_offset); | 124 marker.SetEndOffset(result.value().end_offset); |
| 131 } | 125 } |
| 132 } | 126 } |
| 133 | 127 |
| 134 return did_shift_marker; | 128 return did_shift_marker; |
| 135 } | 129 } |
| 136 | 130 |
| 137 void DocumentMarkerListEditor::MergeOverlapping( | 131 void DocumentMarkerListEditor::AddMarkerAndMergeOverlapping( |
| 138 DocumentMarkerListEditor::MarkerList* list, | 132 MarkerList* list, |
| 139 RenderedDocumentMarker* to_insert) { | 133 const DocumentMarker* marker) { |
| 134 RenderedDocumentMarker* rendered_marker = | |
|
yosin_UTC9
2017/04/28 01:01:27
nit: s/RenderedDocumentMarker*/RenderedDocumentMar
| |
| 135 RenderedDocumentMarker::Create(*marker); | |
| 136 if (list->IsEmpty() || list->back()->EndOffset() < marker->StartOffset()) { | |
| 137 list->push_back(rendered_marker); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 140 auto first_overlapping = std::lower_bound( | 141 auto first_overlapping = std::lower_bound( |
| 141 list->begin(), list->end(), to_insert, | 142 list->begin(), list->end(), rendered_marker, |
| 142 [](const Member<RenderedDocumentMarker>& marker_in_list, | 143 [](const Member<RenderedDocumentMarker>& marker_in_list, |
| 143 const DocumentMarker* marker_to_insert) { | 144 const DocumentMarker* marker_to_insert) { |
| 144 return marker_in_list->EndOffset() < marker_to_insert->StartOffset(); | 145 return marker_in_list->EndOffset() < marker_to_insert->StartOffset(); |
| 145 }); | 146 }); |
| 147 | |
| 146 size_t index = first_overlapping - list->begin(); | 148 size_t index = first_overlapping - list->begin(); |
| 147 list->insert(index, to_insert); | 149 list->insert(index, rendered_marker); |
| 148 const auto inserted = list->begin() + index; | 150 const auto inserted = list->begin() + index; |
| 149 first_overlapping = inserted + 1; | 151 first_overlapping = inserted + 1; |
| 152 // TODO(rlanday): optimize this loop so it runs in O(N) time and not O(N^2) | |
| 150 for (const auto i = first_overlapping; | 153 for (const auto i = first_overlapping; |
| 151 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { | 154 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { |
| 152 (*inserted)->SetStartOffset( | 155 (*inserted)->SetStartOffset( |
| 153 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); | 156 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); |
| 154 (*inserted)->SetEndOffset( | 157 (*inserted)->SetEndOffset( |
| 155 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); | 158 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); |
| 156 list->erase(i - list->begin()); | 159 list->erase(i - list->begin()); |
| 157 } | 160 } |
| 158 } | 161 } |
| 159 | 162 |
| 160 } // namespace blink | 163 } // namespace blink |
| OLD | NEW |