/** * Deletes from offset in this to endOffset in delTo. Uses ModelList.collapse to perform quick * deletion. * * @param delTo iterator whose endpoint is the last character to delete * @return new offset */ int deleteRight(Iterator delTo) { collapse(delTo); // if both pointing to same item, and it's a gap if (eq(delTo) && current().isGap()) { // inside gap current().shrink(delTo.getBlockOffset() - getBlockOffset()); return getBlockOffset(); } // if brace is multiple char it must be a comment because the above if // test guarrantees it can't be a gap. if (!eq(delTo)) clipLeft(); delTo.clipRight(); if (!atStart()) prev(); int delToSizeCurr; String delToTypeCurr; if (delTo.atEnd()) { setTo(delTo); return 0; } else { delToSizeCurr = delTo.current().getSize(); delToTypeCurr = delTo.current().getType(); } // get info on previous item. delTo.prev(); // get stats on previous item int delToSizePrev; String delToTypePrev; if (delTo.atStart()) { // no previous item, can't be at end delTo.next(); setTo(delTo); return 0; } else { delToSizePrev = delTo.current().getSize(); delToTypePrev = delTo.current().getType(); } delTo.next(); // put delTo back on original node int temp = _calculateOffset(delToSizePrev, delToTypePrev, delToSizeCurr, delToTypeCurr, delTo); this.setTo(delTo); return temp; }
/** * By comparing the delTo token after the walk to what it was before the walk we can see how it * has changed and where the offset should go. Prev is the item previous to the current cursor. * Curr is the current token. delTo is where current is pointing at this moment in time. * * @param delToSizePrev delToSize for the prev token * @param delToTypePrev delToType for the prev token * @param delToSizeCurr delToSize for the current token * @param delToTypeCurr delToType for the current token * @param delTo where current is pointing at this moment in time * @return calculated offset */ private int _calculateOffset( int delToSizePrev, String delToTypePrev, int delToSizeCurr, String delToTypeCurr, Iterator delTo) { int offset; int delToSizeChange = delTo.current().getSize(); // String delToTypeChange = delTo.current().getType(); // 1)if there was a gap previous to the gap at delTo delTo should be // augmented by its size, and that size is the offset. // 2)if the gap was not preceeded by a gap then it would not need to // be shrunk if (delTo.atEnd()) throw new IllegalArgumentException("Shouldn't happen"); if (delTo.current().isGap()) return delToSizeChange - delToSizeCurr; // this means that the item at the end formed a double brace with the // item that the delete left preceeding it. /dddddd* // the final item shrunk. This can only happen if the starting item // stole one of its braces: /ddddd*/ // or if it was a double brace that had to get broken because it was // now commented or no longer has an open block // EXAMPLES: /*___*/ becoming */ // /*___*/ delete the first star, through the spaces to get // /*/ // //*__\n// becoming //*__//, the // is broken // //*__\n// becoming //// , the // is broken // THIS MUST HAVE THE previous items size and type passed in from // before the update. This way we know how it's changing too. // In this if clause, special characters are initially separated by some text // (represented here as ellipses), and when the text is deleted, the special // characters come together. Sometimes, this breaks up the second token if // it is a multiple character brace. Each in-line comment demonstrates // the individual case that occurs and for which we check with this if. // In this branch, both the cursor is off and the offset is also not correct. if ((delToTypePrev.equals("/") && // /.../* => //-* ((delToTypeCurr.equals("/*") && _checkPrevEquals(delTo, "//")) || // /...// => //-/ (delToTypeCurr.equals("//") && _checkPrevEquals(delTo, "//")))) || (delToTypePrev.equals("*") && // *.../* => */-* ((delToTypeCurr.equals("/*") && _checkPrevEquals(delTo, "*/")) || // *...// => */-/ (delToTypeCurr.equals("//") && _checkPrevEquals(delTo, "*/")))) || (delToTypePrev.equals("\\") && // \...\\ => \\-\ ((delToTypeCurr.equals("\\\\") && _checkPrevEquals(delTo, "\\")) || // \...\' => \\-' (delToTypeCurr.equals("\\'") && _checkPrevEquals(delTo, "'")) || // \...\" => \\-" (delToTypeCurr.equals("\\\"") && _checkPrevEquals(delTo, "\""))))) { delTo.prev(); offset = 1; } // In this branch, the cursor is on the right token, but the offset is not correct. else if ((delToTypePrev.equals("/") && // /-*/ ((delToTypeCurr.equals("*/") && delTo.current().getType().equals("/*")) || (delToTypeCurr.equals("*") && delTo.current().getType().equals("/*")) || (delToTypeCurr.equals("/") && delTo.current().getType().equals("//")))) || (delToTypePrev.equals("*") && delToTypeCurr.equals("/") && delTo.current().getType().equals("*/")) || (delToTypePrev.equals("\\") && ((delToTypeCurr.equals("\\") && delTo.current().getType().equals("\\\\")) || (delToTypeCurr.equals("'") && delTo.current().getType().equals("\\'")) || (delToTypeCurr.equals("\"") && delTo.current().getType().equals("\\\""))))) { offset = 1; } // Otherwise, we're on the right token and our offset is correct because no recombinations // occurred else offset = 0; return offset; }