/** format a CDATA region, preserving indenting within the CDATA */ private void formatCDATA(TypedPosition region, StringBuffer buffer) throws BadLocationException { LineWalker lineWalker = new LineWalker(fDocument, region, fPositions); LineInfo info = lineWalker.nextLine(); int firstIndent = getIndent(info.offset); info.trimData(); int writeOffset = writeLine(info.data, fInitialIndent, fIndentLevel, buffer); info.setWriteOffset(writeOffset); fLineInfos.add(info); while (lineWalker.hasMoreLines()) { info = lineWalker.nextLine(); int indentDelta = getIndent(info.data) - firstIndent; String line = info.trimData(); if (line.length() > 0) { int indent = indentDelta > 0 ? fInitialIndent + indentDelta : fInitialIndent; writeOffset = writeLine(info.data, indent, fIndentLevel, buffer); info.setWriteOffset(writeOffset); fLineInfos.add(info); } else { buffer.append(fLineDelimiter); } } }
/** * default formatting. Everything is aligned, one indent level above the nearest enclosing opening * element, if any */ protected void formatDefault(TypedPosition region, StringBuffer buffer) throws BadLocationException { LineWalker lineWalker = new LineWalker(fDocument, region, fPositions); boolean alreadyKept = false; while (lineWalker.hasMoreLines()) { LineInfo info = lineWalker.nextLine(); if (info.isEmpty()) { info.setWriteOffset(writeEmpty(buffer)); fLineInfos.add(info); } else { String line = info.trimData(); if (line.length() > 0) { int off = writeLine(info.data, fInitialIndent, fIndentLevel, buffer); info.setWriteOffset(off); fLineInfos.add(info); } else if (fPreferences.preserveBlankLines() && !alreadyKept) { int off = writeLine("", fInitialIndent, fIndentLevel, buffer); info.setWriteOffset(off); fLineInfos.add(info); // buffer.append(fLineDelimiter); alreadyKept = true; } else { int off = writeEmpty(buffer); info.setWriteOffset(off); fLineInfos.add(info); } } } }
/** * format a tag. Attributes, if starting on a new line, are given an additional indent TODO update * this comment for the new magical multiline handline */ protected void formatTag(TypedPosition tposition, StringBuffer buffer, boolean collapseAlways) throws BadLocationException { LineWalker lineWalker = new LineWalker(fDocument, tposition, fPositions); if (lineWalker.isMultiline() && (fPreferences.wrapLongTags() || collapseAlways)) // if (false) { formatMultiLineStartTag(lineWalker, buffer); return; } int count = 0; boolean first = true; while (lineWalker.hasMoreLines()) { LineInfo info = lineWalker.nextLine(); String line = info.trimData(); if (line.length() > 0) { int indentLevel = count > 0 ? fIndentLevel + 1 : fIndentLevel; int writeOffset = writeLine(info.data, fInitialIndent, indentLevel, buffer); info.setWriteOffset(writeOffset); fLineInfos.add(info); count++; } } }
protected void formatMultiLineStartTag(LineWalker lineWalker, StringBuffer buffer) throws BadLocationException { LineInfo info = lineWalker.nextLine(); String line = info.trimDataLeadingAndTrailing(); int count = 0; // get the first non empty line while (line.length() <= 0 && lineWalker.hasMoreLines()) { info = lineWalker.nextLine(); line = info.trimDataLeadingAndTrailing(); } if (line.length() > 0) { // format the first line int indentLevel = fIndentLevel; int writeOffset = writeLine(info.data, fInitialIndent, indentLevel, buffer, false); info.setWriteOffset(writeOffset); fLineInfos.add(info); count++; } while (lineWalker.hasMoreLines()) { info = lineWalker.nextLine(); line = info.trimDataLeadingAndTrailing(); if (line.length() > 0) { int writeOffset = append(info.data, buffer); info.setWriteOffset(writeOffset); fLineInfos.add(info); count++; } } if (count > 0) buffer.append(fLineDelimiter); }