IStructuredDocument doc = node.getStructuredDocument(); int lineWidth = getFormatPreferences().getLineWidth(); int currentAvailableLineWidth = computeAvailableLineWidth(doc, node.getStartOffset(), lineWidth); String nodeText = getNodeText(node); String compressedText = compressSpaces(nodeText, formatContraints); if (((enoughSpace(parentNode, currentAvailableLineWidth, compressedText)) && (noSiblingsAndNoFollowingComment(node)) && !firstStructuredDocumentRegionContainsLineDelimiters(parentNode)) || node.getStartOffset() == 0) { handleNoReflow(node, doc, compressedText, parentNode); currentAvailableLineWidth = lineWidth - getIndentationLength(nodeIndentation); List vector = reflowText(compressedText, currentAvailableLineWidth); int vectorSize = vector.size(); StringBuffer reflowedTextBuffer = new StringBuffer(); String lineDelimiter = getLineDelimiter(doc, node.getStartOffset()); if (isEndTagMissing(parentNode)) { nodeIndentation = getNodeIndent(parentNode); if (!reflowedText.endsWith(lineDelimiter + nodeIndentation)) { reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter); replaceNodeValue(node, reflowedText);
/** * Keeps text inline with its parent (no reflow necessary) * * @param node * @param doc * @param compressedText * @param parentNode */ private void handleNoReflow(IDOMNode node, IStructuredDocument doc, String compressedText, IDOMNode parentNode) { String nodeIndentation; // enough space and text has no line delimiters and (node has no // siblings or followed by inline comment) and // parentFirstStructuredDocumentRegionContainsLineDelimiters if (isEndTagMissing(parentNode)) { parentNode = (IDOMNode) parentNode.getParentNode(); while (isEndTagMissing(parentNode)) parentNode = (IDOMNode) parentNode.getParentNode(); // add parent's indentation to end nodeIndentation = getNodeIndent(parentNode); String lineDelimiter = getLineDelimiter(doc, node.getStartOffset()); if (!compressedText.endsWith(lineDelimiter + nodeIndentation)) { compressedText = StringUtils.appendIfNotEndWith(compressedText, lineDelimiter); compressedText = StringUtils.appendIfNotEndWith(compressedText, nodeIndentation); } } if ((parentNode != null) && (parentNode.getNodeType() == Node.DOCUMENT_NODE) && (node.getNodeValue().length() > 0) && (node.getNodeValue().trim().length() == 0) && ((node.getPreviousSibling() == null) || (node.getNextSibling() == null))) // delete spaces at the beginning or end of the document compressedText = EMPTY_STRING; replaceNodeValue(node, compressedText); }
formatter = new TextNodeFormatter(); break;
protected Vector reflowText(String text, int availableWidth) { String[] stringArray = null; boolean clearAllBlankLines = getFormatPreferences().getClearAllBlankLines();
private int computeAvailableLineWidth(IStructuredDocument doc, int nodeOffset, int lineWidth) { // compute current available line width int currentAvailableLineWidth = 0; try { int lineOffset = doc.getLineInformationOfOffset(nodeOffset).getOffset(); String text = doc.get(lineOffset, nodeOffset - lineOffset); int usedWidth = getIndentationLength(text); currentAvailableLineWidth = lineWidth - usedWidth; } catch (BadLocationException e) { // log for now, unless we find reason not to Logger.log(Logger.INFO, e.getMessage()); } return currentAvailableLineWidth; }
formatter = new TextNodeFormatter(); break;