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/SpellCheckMarkerListImpl.h" | 7 #include "core/editing/markers/SpellCheckMarkerListImpl.h" |
| 8 | 8 |
| 9 namespace blink { | 9 namespace blink { |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 if (marker.StartOffset() >= end_offset) | 73 if (marker.StartOffset() >= end_offset) |
| 74 break; | 74 break; |
| 75 | 75 |
| 76 list->erase(i - list->begin()); | 76 list->erase(i - list->begin()); |
| 77 doc_dirty = true; | 77 doc_dirty = true; |
| 78 } | 78 } |
| 79 | 79 |
| 80 return doc_dirty; | 80 return doc_dirty; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // TODO(rlanday): make this not take O(n^2) time when all the markers are | |
| 84 // removed | |
| 85 bool DocumentMarkerListEditor::ShiftMarkersContentDependent( | 83 bool DocumentMarkerListEditor::ShiftMarkersContentDependent( |
| 86 MarkerList* list, | 84 MarkerList* list, |
| 87 unsigned offset, | 85 unsigned offset, |
| 88 unsigned old_length, | 86 unsigned old_length, |
| 89 unsigned new_length) { | 87 unsigned new_length) { |
| 88 // Construct new list and swap so this method can run in O(n) time | |
| 89 MarkerList new_list; | |
| 90 bool did_shift_marker = false; | 90 bool did_shift_marker = false; |
| 91 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { | 91 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { |
| 92 DocumentMarker& marker = **it; | 92 DocumentMarker& marker = **it; |
| 93 | 93 |
| 94 // marked text is neither changed nor shifted | 94 // marked text is neither changed nor shifted |
| 95 if (marker.EndOffset() <= offset) | 95 if (marker.EndOffset() <= offset) { |
| 96 new_list.push_back(marker); | |
| 96 continue; | 97 continue; |
| 98 } | |
| 97 | 99 |
| 98 // marked text is (potentially) changed by edit, remove marker | 100 // marked text is (potentially) changed by edit, remove marker |
| 99 if (marker.StartOffset() < offset + old_length) { | 101 if (marker.StartOffset() < offset + old_length) { |
| 100 list->erase(it - list->begin()); | |
| 101 --it; | |
| 102 did_shift_marker = true; | 102 did_shift_marker = true; |
| 103 continue; | 103 continue; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // marked text is shifted but not changed | 106 // marked text is shifted but not changed |
| 107 marker.ShiftOffsets(new_length - old_length); | 107 marker.ShiftOffsets(new_length - old_length); |
| 108 new_list.push_back(marker); | |
| 108 did_shift_marker = true; | 109 did_shift_marker = true; |
| 109 } | 110 } |
| 110 | 111 |
| 112 std::swap(*list, new_list); | |
|
yosin_UTC9
2017/06/02 04:28:56
nit: |*list = std::move(new_list);|
Let's do C++11
| |
| 111 return did_shift_marker; | 113 return did_shift_marker; |
|
yosin_UTC9
2017/06/02 04:28:56
Could you get rid of |did_shift_marker|?
Once we
rlanday
2017/06/02 15:23:01
I don't think this is quite right because we want
| |
| 112 } | 114 } |
| 113 | 115 |
| 114 // TODO(rlanday): make this not take O(n^2) time when all the markers are | |
| 115 // removed | |
| 116 bool DocumentMarkerListEditor::ShiftMarkersContentIndependent( | 116 bool DocumentMarkerListEditor::ShiftMarkersContentIndependent( |
| 117 MarkerList* list, | 117 MarkerList* list, |
| 118 unsigned offset, | 118 unsigned offset, |
| 119 unsigned old_length, | 119 unsigned old_length, |
| 120 unsigned new_length) { | 120 unsigned new_length) { |
| 121 // Construct new list and swap so this method can run in O(n) time | |
| 122 MarkerList new_list; | |
| 121 bool did_shift_marker = false; | 123 bool did_shift_marker = false; |
| 122 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { | 124 for (MarkerList::iterator it = list->begin(); it != list->end(); ++it) { |
| 123 DocumentMarker& marker = **it; | 125 DocumentMarker& marker = **it; |
| 124 Optional<DocumentMarker::MarkerOffsets> result = | 126 Optional<DocumentMarker::MarkerOffsets> result = |
| 125 marker.ComputeOffsetsAfterShift(offset, old_length, new_length); | 127 marker.ComputeOffsetsAfterShift(offset, old_length, new_length); |
| 126 if (result == WTF::nullopt) { | 128 if (result == WTF::nullopt) { |
| 127 list->erase(it - list->begin()); | |
| 128 --it; | |
| 129 did_shift_marker = true; | 129 did_shift_marker = true; |
| 130 continue; | 130 continue; |
| 131 } | 131 } |
| 132 | 132 |
| 133 if (marker.StartOffset() != result.value().start_offset || | 133 if (marker.StartOffset() != result.value().start_offset || |
| 134 marker.EndOffset() != result.value().end_offset) { | 134 marker.EndOffset() != result.value().end_offset) { |
| 135 did_shift_marker = true; | 135 did_shift_marker = true; |
| 136 marker.SetStartOffset(result.value().start_offset); | 136 marker.SetStartOffset(result.value().start_offset); |
| 137 marker.SetEndOffset(result.value().end_offset); | 137 marker.SetEndOffset(result.value().end_offset); |
| 138 } | 138 } |
| 139 | |
| 140 new_list.push_back(marker); | |
| 139 } | 141 } |
| 140 | 142 |
| 143 std::swap(*list, new_list); | |
|
yosin_UTC9
2017/06/02 04:28:56
nit: |*list = std::move(new_list);|
Let's do C++11
| |
| 141 return did_shift_marker; | 144 return did_shift_marker; |
|
yosin_UTC9
2017/06/02 04:28:56
Could you get rid of |did_shift_marker|?
Once we
| |
| 142 } | 145 } |
| 143 | 146 |
| 144 } // namespace blink | 147 } // namespace blink |
| OLD | NEW |