Search in sources :

Example 21 with StructuredFormatPreferencesXML

use of org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML in project webtools.sourceediting by eclipse.

the class FormatTester method testFormatTags.

public void testFormatTags() {
    // get model
    IStructuredModel structuredModel = getModel("tags.xml");
    // init FormatPreferences
    IStructuredFormatPreferences formatPreferences = fFormatProcessor.getFormatPreferences();
    ((StructuredFormatPreferencesXML) formatPreferences).setSplitMultiAttrs(false);
    formatPreferences.setLineWidth(999);
    formatPreferences.setIndent("\t");
    formatPreferences.setClearAllBlankLines(false);
    // format
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = false;
    fFormatProcessor.formatModel(structuredModel);
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = true;
    // compare
    String formatted = structuredModel.getStructuredDocument().get();
    String expectedFileName = "tags.afterDefaultFormat.xml";
    String expected = getFile(expectedFileName);
    compare(expectedFileName, expected, formatted);
    // release model
    structuredModel.releaseFromRead();
}
Also used : StructuredFormatPreferencesXML(org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IStructuredFormatPreferences(org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)

Example 22 with StructuredFormatPreferencesXML

use of org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML in project webtools.sourceediting by eclipse.

the class FormatTester method testFormatTagOpen.

public void testFormatTagOpen() {
    // get model
    IStructuredModel structuredModel = getModel("tagOpen.xml");
    // init FormatPreferences
    IStructuredFormatPreferences formatPreferences = fFormatProcessor.getFormatPreferences();
    ((StructuredFormatPreferencesXML) formatPreferences).setSplitMultiAttrs(false);
    formatPreferences.setLineWidth(999);
    formatPreferences.setIndent("\t");
    formatPreferences.setClearAllBlankLines(false);
    // format
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = false;
    fFormatProcessor.formatModel(structuredModel);
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = true;
    // compare
    String formatted = structuredModel.getStructuredDocument().get();
    String expectedFileName = "tagOpen.xml";
    String expected = getFile(expectedFileName);
    compare(expectedFileName, expected, formatted);
    // release model
    structuredModel.releaseFromRead();
}
Also used : StructuredFormatPreferencesXML(org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IStructuredFormatPreferences(org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)

Example 23 with StructuredFormatPreferencesXML

use of org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML in project webtools.sourceediting by eclipse.

the class FormatTester method testFormatSplitLinesSplitMultiAttrs.

public void testFormatSplitLinesSplitMultiAttrs() {
    // get model
    IStructuredModel structuredModel = getModel("small.xml");
    // init FormatPreferences
    IStructuredFormatPreferences formatPreferences = fFormatProcessor.getFormatPreferences();
    ((StructuredFormatPreferencesXML) formatPreferences).setSplitMultiAttrs(true);
    formatPreferences.setLineWidth(72);
    formatPreferences.setIndent("\t");
    formatPreferences.setClearAllBlankLines(false);
    // format
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = false;
    fFormatProcessor.formatModel(structuredModel);
    ((AbstractStructuredFormatProcessor) fFormatProcessor).refreshFormatPreferences = true;
    // compare
    String formatted = structuredModel.getStructuredDocument().get();
    String expectedFileName = "small.afterSplitLinesSplitMultiAttrsFormat.xml";
    String expected = getFile(expectedFileName);
    compare(expectedFileName, expected, formatted);
    // release model
    structuredModel.releaseFromRead();
}
Also used : StructuredFormatPreferencesXML(org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IStructuredFormatPreferences(org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)

Example 24 with StructuredFormatPreferencesXML

use of org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML in project webtools.sourceediting by eclipse.

the class HTMLElementFormatter method formatStartTag.

/**
 */
private void formatStartTag(IDOMElement element, HTMLFormatContraints contraints) {
    if (element.getNodeName().equals("jsp:scriptlet")) {
        // $NON-NLS-1$
        insertBreakBefore(element, contraints);
        return;
    }
    IStructuredDocumentRegion startStructuredDocumentRegion = element.getStartStructuredDocumentRegion();
    if (startStructuredDocumentRegion == null)
        return;
    // if (element.isJSPTag() || element.isCommentTag()) {
    if (element.isCommentTag()) {
        String startTag = startStructuredDocumentRegion.getText();
        if (startTag != null && startTag.length() > 0) {
            setWidth(contraints, startTag);
        }
        return;
    }
    // first process style attribute
    if (element.isGlobalTag()) {
        // $NON-NLS-1$
        Attr attr = element.getAttributeNode("style");
        if (attr != null)
            formatStyleAttr(attr);
    }
    boolean insertBreak = false;
    insertBreak = ((StructuredFormatPreferencesXML) getFormatPreferences()).getSplitMultiAttrs();
    boolean alignEndBracket = ((StructuredFormatPreferencesXML) getFormatPreferences()).isAlignEndBracket();
    boolean attributesSplitted = false;
    if (insertBreak) {
        NamedNodeMap attributes = element.getAttributes();
        if (attributes == null || attributes.getLength() < 2)
            insertBreak = false;
    }
    String breakSpaces = getBreakSpaces(element);
    String originalBreakSpaces = breakSpaces;
    String indent = getIndent();
    if (indent != null && indent.length() > 0) {
        breakSpaces += indent;
    }
    ITextRegion lastBreakRegion = null;
    ITextRegion prevRegion = null;
    ITextRegionList regions = startStructuredDocumentRegion.getRegions();
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        if (region == null)
            continue;
        ITextRegion breakRegion = null;
        String regionType = region.getType();
        if (regionType == DOMRegionContext.XML_TAG_NAME || isNestedTag(regionType)) {
            if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_OPEN) {
                removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
            }
            breakRegion = region;
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
                // attribute name without value
                breakRegion = prevRegion;
            }
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
            if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
                removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
            }
        } else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
            if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
                removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
            }
            breakRegion = region;
        } else if (regionType == DOMRegionContext.XML_TAG_CLOSE || regionType == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
            if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
                // attribute name without value
                breakRegion = prevRegion;
            }
        }
        if (breakRegion != null) {
            int end = breakRegion.getTextEnd();
            if (lastBreakRegion != null) {
                int offset = lastBreakRegion.getEnd();
                int count = end - offset;
                if (insertBreak || !isWidthAvailable(contraints, count + 1)) {
                    replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, breakSpaces);
                    setWidth(contraints, breakSpaces);
                    attributesSplitted = true;
                } else {
                    compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
                    addWidth(contraints, 1);
                }
                addWidth(contraints, count);
            } else {
                addWidth(contraints, end);
            }
            lastBreakRegion = breakRegion;
        }
        prevRegion = region;
    }
    if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
        // attribute name without value
        int end = prevRegion.getTextEnd();
        if (lastBreakRegion != null) {
            int offset = lastBreakRegion.getEnd();
            int count = end - offset;
            if (insertBreak || !isWidthAvailable(contraints, count + 1)) {
                replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, breakSpaces);
                setWidth(contraints, breakSpaces);
                attributesSplitted = true;
            } else {
                compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
                addWidth(contraints, 1);
            }
            addWidth(contraints, count);
        } else {
            addWidth(contraints, end);
        }
        lastBreakRegion = prevRegion;
    }
    if (lastBreakRegion != null) {
        int offset = lastBreakRegion.getTextEnd();
        int count = startStructuredDocumentRegion.getLength() - offset;
        if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
            compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
            count++;
        } else {
            removeTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
            // BUG123890 (pre-factor in end tag)
            count += element.getTagName().length() + 3;
        }
        addWidth(contraints, count);
    } else {
        addWidth(contraints, startStructuredDocumentRegion.getLength());
    }
    // BUG113584 - align last bracket
    if (alignEndBracket && attributesSplitted) {
        removeTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
        replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, originalBreakSpaces);
        contraints.setAvailableLineWidth(getLineWidth() - originalBreakSpaces.length() - 1);
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) NamedNodeMap(org.w3c.dom.NamedNodeMap) StructuredFormatPreferencesXML(org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Attr(org.w3c.dom.Attr)

Example 25 with StructuredFormatPreferencesXML

use of org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML in project webtools.sourceediting by eclipse.

the class TestFormatProcessorXML method testPreservePCDATAFormat.

public void testPreservePCDATAFormat() throws UnsupportedEncodingException, IOException, CoreException {
    // BUG84688
    IStructuredFormatPreferences formatPreferences = formatProcessor.getFormatPreferences();
    ((StructuredFormatPreferencesXML) formatPreferences).setPreservePCDATAContent(true);
    formatAndAssertEquals("testfiles/xml/xml-preservepcdata.xml", "testfiles/xml/xml-preservepcdata-yes-fmt.xml", false);
    ((StructuredFormatPreferencesXML) formatPreferences).setPreservePCDATAContent(false);
    formatAndAssertEquals("testfiles/xml/xml-preservepcdata.xml", "testfiles/xml/xml-preservepcdata-no-fmt.xml", false);
}
Also used : StructuredFormatPreferencesXML(org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML) IStructuredFormatPreferences(org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)

Aggregations

StructuredFormatPreferencesXML (org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML)28 IStructuredFormatPreferences (org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences)27 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)17 Preferences (org.eclipse.core.runtime.Preferences)3 Iterator (java.util.Iterator)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)1 Attr (org.w3c.dom.Attr)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1