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::AddMarkerAndMergeOverlapping( |
|
Xiaocheng
2017/04/26 22:12:07
Could you put DocumentMarkerListEditor::AddMarkerW
| |
| 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 auto first_overlapping = | |
| 22 std::lower_bound(list->begin(), list->end(), rendered_marker, | |
| 23 [](const Member<RenderedDocumentMarker>& marker_in_list, | |
| 24 const DocumentMarker* marker_rendered_marker) { | |
|
Xiaocheng
2017/04/26 22:12:07
No need to rename this parameter.
rlanday
2017/04/26 22:14:46
Oops, bad find-and-replace :)
| |
| 25 return marker_in_list->EndOffset() < | |
| 26 marker_rendered_marker->StartOffset(); | |
| 27 }); | |
| 28 | |
| 29 size_t index = first_overlapping - list->begin(); | |
| 30 list->insert(index, rendered_marker); | |
| 31 const auto inserted = list->begin() + index; | |
| 32 first_overlapping = inserted + 1; | |
| 33 // TODO(rlanday): optimize this loop so it runs in O(N) time and not O(N^2) | |
| 34 for (const auto i = first_overlapping; | |
| 35 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { | |
| 36 (*inserted)->SetStartOffset( | |
| 37 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); | |
| 38 (*inserted)->SetEndOffset( | |
| 39 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); | |
| 40 list->erase(i - list->begin()); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void DocumentMarkerListEditor::AddMarkerWithoutMergingOverlapping( | |
| 45 MarkerList* list, | |
| 46 const DocumentMarker* marker) { | |
| 47 RenderedDocumentMarker* rendered_marker = | |
| 48 RenderedDocumentMarker::Create(*marker); | |
| 49 if (list->IsEmpty() || list->back()->EndOffset() <= marker->StartOffset()) { | |
| 50 list->push_back(rendered_marker); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 MarkerList::iterator pos = std::lower_bound( | |
| 55 list->begin(), list->end(), marker, | |
| 56 [](const Member<RenderedDocumentMarker>& marker_in_list, | |
| 57 const DocumentMarker* marker_to_insert) { | |
| 58 return marker_in_list->StartOffset() < marker_to_insert->StartOffset(); | |
| 59 }); | |
| 60 list->insert(pos - list->begin(), rendered_marker); | |
| 34 } | 61 } |
| 35 | 62 |
| 36 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list, | 63 bool DocumentMarkerListEditor::MoveMarkers(MarkerList* src_list, |
| 37 int length, | 64 int length, |
| 38 DocumentMarkerList* dst_list) { | 65 DocumentMarkerList* dst_list) { |
| 39 DCHECK_GT(length, 0); | 66 DCHECK_GT(length, 0); |
| 40 bool didMoveMarker = false; | 67 bool didMoveMarker = false; |
| 41 unsigned end_offset = length - 1; | 68 unsigned end_offset = length - 1; |
| 42 | 69 |
| 43 MarkerList::iterator it; | 70 MarkerList::iterator it; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 marker.EndOffset() != result.value().end_offset) { | 154 marker.EndOffset() != result.value().end_offset) { |
| 128 did_shift_marker = true; | 155 did_shift_marker = true; |
| 129 marker.SetStartOffset(result.value().start_offset); | 156 marker.SetStartOffset(result.value().start_offset); |
| 130 marker.SetEndOffset(result.value().end_offset); | 157 marker.SetEndOffset(result.value().end_offset); |
| 131 } | 158 } |
| 132 } | 159 } |
| 133 | 160 |
| 134 return did_shift_marker; | 161 return did_shift_marker; |
| 135 } | 162 } |
| 136 | 163 |
| 137 void DocumentMarkerListEditor::MergeOverlapping( | |
|
Xiaocheng
2017/04/26 22:12:07
Could you put DocumentMarkerListEditor::AddMarkerA
| |
| 138 DocumentMarkerListEditor::MarkerList* list, | |
| 139 RenderedDocumentMarker* to_insert) { | |
| 140 auto first_overlapping = std::lower_bound( | |
| 141 list->begin(), list->end(), to_insert, | |
| 142 [](const Member<RenderedDocumentMarker>& marker_in_list, | |
| 143 const DocumentMarker* marker_to_insert) { | |
| 144 return marker_in_list->EndOffset() < marker_to_insert->StartOffset(); | |
| 145 }); | |
| 146 size_t index = first_overlapping - list->begin(); | |
| 147 list->insert(index, to_insert); | |
| 148 const auto inserted = list->begin() + index; | |
| 149 first_overlapping = inserted + 1; | |
| 150 for (const auto i = first_overlapping; | |
| 151 i != list->end() && (*i)->StartOffset() <= (*inserted)->EndOffset();) { | |
| 152 (*inserted)->SetStartOffset( | |
| 153 std::min((*inserted)->StartOffset(), (*i)->StartOffset())); | |
| 154 (*inserted)->SetEndOffset( | |
| 155 std::max((*inserted)->EndOffset(), (*i)->EndOffset())); | |
| 156 list->erase(i - list->begin()); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 } // namespace blink | 164 } // namespace blink |
| OLD | NEW |