/** * Inserts elements after the position specified by the hitIterator. * * @param operation the operation that wants to insert elements. * @param doc the document to insert elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to insert. * @param modifyAction the action that specifies what to insert. * @throws InvalidRequestException if something goes wrong. */ private void insertAfter(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int insertAt = range.getEnd(); int inserted = insertInto(operation, doc, view, insertAt, modifyAction, valueIndex); hitIterator.shift(insertAt, inserted); valueIndex++; range = hitIterator.next(); } }
/** * Inserts elements at the position specified by the hitIterator. * * @param operation the operation that wants to insert elements. * @param doc the document to insert elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to insert. * @param modifyAction the action that specifies what to insert. * @throws InvalidRequestException if something goes wrong. */ private void insert(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int insertAt = range.getStart(); int inserted = insertInto(operation, doc, view, insertAt, modifyAction, valueIndex); hitIterator.shift(insertAt, inserted); valueIndex++; range = hitIterator.next(); } }
hitIterator.shift(replaceAt, numInserted + numRemoved);
/** * Deletes ranges of elements from a document as specified by the iterator. * * @param operation the operation to execute. * @param view the view of the document. * @param hitIterator iterates over the ranges of elements to delete. * @throws InvalidRequestException if the specified range was invalid. */ private void delete(OperationRequest operation, ApiView view, DocumentHitIterator hitIterator) throws InvalidRequestException { Range range = hitIterator.next(); while (range != null) { int start = range.getStart(); int end = range.getEnd(); if (start == 0) { // Can't delete the first new line. start = 1; } if (start >= end) { throw new InvalidRequestException( "Invalid range specified, " + start + ">=" + end, operation); } // Delete using the view. view.delete(start, end); // Shift the iterator to match the updated document. hitIterator.shift(start, end - start); range = hitIterator.next(); } }
public void testTextMatcherDeletingTheFirstChar() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i + 1, r.getStart()); apiView.delete(1, 2); it.shift(1, -1); } assertNull(it.next()); }
public void testTextMatcherDeletingTheFirstChar() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i + 1, r.getStart()); apiView.delete(1, 2); it.shift(1, -1); } assertNull(it.next()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }