Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/editing/markers/DocumentMarkerList.h" | |
| 6 | |
| 7 namespace blink { | |
| 8 | |
| 9 DocumentMarkerList::DocumentMarkerList() {} | |
| 10 | |
| 11 bool DocumentMarkerList::isEditingMarkerList() const { | |
| 12 return false; | |
| 13 } | |
| 14 | |
| 15 bool DocumentMarkerList::isSpellCheckMarkerList() const { | |
| 16 return false; | |
| 17 } | |
| 18 | |
| 19 void DocumentMarkerList::add(DocumentMarker* marker) { | |
| 20 m_markers.push_back(marker); | |
| 21 } | |
| 22 | |
| 23 void DocumentMarkerList::clear() { | |
| 24 m_markers.clear(); | |
| 25 } | |
| 26 | |
| 27 void DocumentMarkerList::appendMarkersToInputList( | |
| 28 DocumentMarkerVector* list) const { | |
| 29 for (Member<DocumentMarker> marker : m_markers) { | |
| 30 list->push_back(marker); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 // TODO(rlanday): this method leaves pointers to the same DocumentMarkers in the | |
|
Xiaocheng
2017/03/29 19:41:43
nit: Let's put this note in the header file. TODOs
| |
| 35 // source and destination lists (and shifts their offsets if delta != 0), this | |
| 36 // should probably be cleaned up eventually to not do that | |
| 37 DocumentMarkerList::DidCopyMarkerOrNot DocumentMarkerList::copyMarkers( | |
| 38 unsigned startOffset, | |
| 39 int length, | |
| 40 DocumentMarkerList* dstList, | |
| 41 int delta) const { | |
| 42 DidCopyMarkerOrNot didCopyMarker = DidCopyMarkerOrNot::DidNotCopyMarker; | |
| 43 unsigned endOffset = startOffset + length - 1; | |
| 44 | |
| 45 for (Member<DocumentMarker> marker : m_markers) { | |
| 46 // pin the marker to the specified range and apply the shift delta | |
| 47 if (marker->endOffset() >= startOffset && | |
| 48 marker->startOffset() <= endOffset) { | |
| 49 didCopyMarker = DidCopyMarkerOrNot::DidCopyMarker; | |
| 50 if (marker->startOffset() < startOffset) | |
| 51 marker->setStartOffset(startOffset); | |
| 52 if (marker->endOffset() > endOffset) | |
| 53 marker->setEndOffset(endOffset); | |
| 54 marker->shiftOffsets(delta); | |
| 55 | |
| 56 dstList->add(marker); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 return didCopyMarker; | |
| 61 } | |
| 62 | |
| 63 DocumentMarkerList::DidRemoveMarkerOrNot DocumentMarkerList::removeMarkers( | |
| 64 unsigned startOffset, | |
| 65 int length, | |
| 66 bool shouldRemovePartiallyOverlappingMarkers) { | |
| 67 unsigned endOffset = startOffset + length; | |
| 68 HeapVector<Member<DocumentMarker>> newMarkerList; | |
| 69 DidRemoveMarkerOrNot didRemoveMarker = | |
| 70 DidRemoveMarkerOrNot::DidNotRemoveMarker; | |
| 71 | |
| 72 for (Member<DocumentMarker> marker : m_markers) { | |
| 73 if (marker->endOffset() <= startOffset) { | |
| 74 newMarkerList.push_back(marker); | |
| 75 continue; | |
| 76 } | |
| 77 | |
| 78 if (marker->startOffset() >= endOffset) { | |
| 79 newMarkerList.push_back(marker); | |
| 80 continue; | |
| 81 } | |
| 82 | |
| 83 didRemoveMarker = DidRemoveMarkerOrNot::DidRemoveMarker; | |
| 84 | |
| 85 if (shouldRemovePartiallyOverlappingMarkers) { | |
| 86 // Stop here. Don't add resulting slices back. | |
| 87 continue; | |
| 88 } | |
| 89 | |
| 90 // add either of the resulting slices that are left after removing target | |
| 91 if (startOffset > marker->startOffset()) { | |
| 92 DocumentMarker* newLeft = new DocumentMarker(*marker); | |
| 93 newLeft->setEndOffset(startOffset); | |
| 94 newMarkerList.push_back(newLeft); | |
| 95 } | |
| 96 | |
| 97 if (marker->endOffset() > endOffset) { | |
| 98 DocumentMarker* newRight = new DocumentMarker(*marker); | |
| 99 newRight->setStartOffset(endOffset); | |
| 100 newMarkerList.push_back(newRight); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 swap(m_markers, newMarkerList); | |
| 105 | |
|
Xiaocheng
2017/03/29 19:41:43
nit: Remove the extra blank line.
| |
| 106 return didRemoveMarker; | |
| 107 } | |
| 108 | |
| 109 DocumentMarkerList::DidShiftMarkerOrNot DocumentMarkerList::shiftMarkers( | |
| 110 unsigned offset, | |
| 111 unsigned oldLength, | |
| 112 unsigned newLength) { | |
| 113 HeapVector<Member<DocumentMarker>> newMarkerList; | |
| 114 DocumentMarkerList::DidShiftMarkerOrNot didShift = | |
| 115 DidShiftMarkerOrNot::DidNotShiftMarker; | |
| 116 for (Member<DocumentMarker> marker : m_markers) { | |
| 117 DocumentMarker::ShiftMarkerResult result = | |
| 118 marker->getShiftedMarkerPosition(offset, oldLength, newLength); | |
| 119 | |
| 120 if (result.shouldRemoveMarker) { | |
| 121 didShift = DidShiftMarkerOrNot::DidShiftMarker; | |
|
Xiaocheng
2017/03/29 19:41:43
nit: We prefer the early-continue style:
if (resu
| |
| 122 } else { | |
| 123 if (result.newStartOffset != marker->startOffset() || | |
| 124 result.newEndOffset != marker->endOffset()) { | |
| 125 marker->setStartOffset(result.newStartOffset); | |
| 126 marker->setEndOffset(result.newEndOffset); | |
| 127 | |
| 128 didShift = DidShiftMarkerOrNot::DidShiftMarker; | |
| 129 } | |
| 130 newMarkerList.push_back(marker); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 swap(m_markers, newMarkerList); | |
| 135 | |
|
Xiaocheng
2017/03/29 19:41:43
nit: Remove the extra blank line.
| |
| 136 return didShift; | |
| 137 } | |
| 138 | |
| 139 HeapVector<Member<DocumentMarker>>::iterator | |
| 140 DocumentMarkerList::getPosOfFirstMarkerNotEndingBefore(size_t startOffset) { | |
| 141 DCHECK(markerListIsSorted()); | |
| 142 return std::upper_bound( | |
| 143 m_markers.begin(), m_markers.end(), startOffset, | |
| 144 [](size_t startOffset, const Member<DocumentMarker>& rhv) -> bool { | |
| 145 return startOffset < rhv->endOffset(); | |
| 146 }); | |
| 147 } | |
| 148 | |
| 149 DEFINE_TRACE(DocumentMarkerList) { | |
| 150 visitor->trace(m_markers); | |
| 151 } | |
| 152 | |
| 153 } // namespace blink | |
| OLD | NEW |