Search in sources :

Example 11 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.

the class CSSRegionContainer method getCssText.

/**
 * @return java.lang.String
 */
public String getCssText() {
    if (fFirstRegion == null || fLastRegion == null)
        return generateSource();
    ITextRegionList regions = fParentRegion.getRegions();
    StringBuffer source = new StringBuffer();
    boolean bIn = false;
    for (int i = 0; i < regions.size(); i++) {
        ITextRegion current = regions.get(i);
        if (bIn) {
            source.append(fParentRegion.getFullText(current));
            if (current == fLastRegion)
                break;
        } else {
            if (current == fFirstRegion) {
                bIn = true;
                source.append(fParentRegion.getFullText(current));
                if (current == fLastRegion)
                    break;
            }
        }
    }
    return source.toString();
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 12 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.

the class AbstractCSSSourceFormatter method appendSpaceBefore.

/**
 */
protected void appendSpaceBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) {
    if (node == null || toAppend == null || source == null)
        return;
    if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
        // for not formatting case on cleanup action
        return;
    String type = toAppend.getType();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    boolean needIndent = !(node instanceof ICSSStyleSheet);
    if (type == CSSRegionContexts.CSS_COMMENT) {
        // check whether previous region is 'S' and has CR-LF
        String delim = getLineDelimiter(node);
        RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
        it.prev();
        ITextRegion prev = it.prev();
        // bug390904
        if (prev.getType() == CSSRegionContexts.CSS_LBRACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getFullText(prev), 0)[0] > 0) {
            source.append(delim);
            source.append(getIndent(node));
            source.append(getIndentString());
        } else if (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
            source.append(delim);
            source.append(getIndent(node));
            if (needIndent)
                source.append(getIndentString());
        } else {
            appendSpaceBefore(node, toAppend.getText(), source);
        }
    } else if (type == CSSRegionContexts.CSS_LBRACE && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
        String delim = getLineDelimiter(node);
        source.append(delim);
        source.append(getIndent(node));
    // } else if (type == CSSRegionContexts.CSS_CURLY_BRACE_CLOSE) {
    // } else if (type == CSSRegionContexts.CSS_INCLUDES || type ==
    // CSSRegionContexts.CSS_DASHMATCH) {
    } else if (type == CSSRegionContexts.CSS_DECLARATION_SEPARATOR && node instanceof ICSSStyleDeclItem) {
        int n = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
        // no delimiter case
        while (n-- > 0) // $NON-NLS-1$
        source.append(" ");
    } else if (type == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR || type == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
        if (preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) {
            int length = getLastLineLength(node, source);
            int append = 1;
            if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) {
                source.append(getLineDelimiter(node));
                source.append(getIndent(node));
                if (needIndent)
                    source.append(getIndentString());
            }
        }
    } else if (CSSRegionContexts.CSS_FOREIGN_ELEMENT == type || CSSRegionContexts.CSS_DECLARATION_DELIMITER == type) {
        return;
    } else
        appendSpaceBefore(node, toAppend.getText(), source);
}
Also used : ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) Preferences(org.eclipse.core.runtime.Preferences) ICSSStyleSheet(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleSheet)

Example 13 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.

the class AbstractCSSSourceFormatter method getRegions.

/**
 */
protected CompoundRegion[] getRegions(IStructuredDocument model, IRegion reg, IRegion exceptFor, String pickupType) {
    int start = reg.getOffset();
    int end = reg.getOffset() + reg.getLength();
    int startE = (exceptFor != null) ? exceptFor.getOffset() : -1;
    int endE = (exceptFor != null) ? exceptFor.getOffset() + exceptFor.getLength() : 0;
    ArrayList list = new ArrayList();
    IStructuredDocumentRegion flatNode = model.getRegionAtCharacterOffset(start);
    boolean pickuped = false;
    while (flatNode != null && flatNode.getStartOffset() < end) {
        ITextRegionList regionList = flatNode.getRegions();
        Iterator it = regionList.iterator();
        while (it.hasNext()) {
            ITextRegion region = (ITextRegion) it.next();
            if (flatNode.getStartOffset(region) < start)
                continue;
            if (end <= flatNode.getStartOffset(region))
                break;
            if (startE >= 0 && startE <= flatNode.getStartOffset(region) && flatNode.getEndOffset(region) <= endE)
                continue;
            if (region.getType() == CSSRegionContexts.CSS_COMMENT || region.getType() == CSSRegionContexts.CSS_CDC || region.getType() == CSSRegionContexts.CSS_CDO)
                list.add(new CompoundRegion(flatNode, region));
            else if (!pickuped && region.getType() == pickupType) {
                list.add(new CompoundRegion(flatNode, region));
                pickuped = true;
            }
        }
        flatNode = flatNode.getNext();
    }
    if (list.size() > 0) {
        CompoundRegion[] regions = new CompoundRegion[list.size()];
        list.toArray(regions);
        return regions;
    }
    return new CompoundRegion[0];
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) Iterator(java.util.Iterator)

Example 14 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.

the class AbstractCSSSourceFormatter method getRegionsWithoutWhiteSpaces.

/**
 */
protected CompoundRegion[] getRegionsWithoutWhiteSpaces(IStructuredDocument model, IRegion reg, CSSCleanupStrategy stgy) {
    int start = reg.getOffset();
    int end = reg.getOffset() + reg.getLength() - 1;
    ArrayList list = new ArrayList();
    IStructuredDocumentRegion flatNode = model.getRegionAtCharacterOffset(start);
    while (flatNode != null && flatNode.getStartOffset() <= end) {
        ITextRegionList regionList = flatNode.getRegions();
        Iterator it = regionList.iterator();
        while (it.hasNext()) {
            ITextRegion region = (ITextRegion) it.next();
            if (flatNode.getStartOffset(region) < start)
                continue;
            if (end < flatNode.getStartOffset(region))
                break;
            if (// for
            region.getType() != CSSRegionContexts.CSS_S || (isCleanup() && !stgy.isFormatSource()))
                // not
                // formatting
                // case
                // on
                // cleanup
                // action
                list.add(new CompoundRegion(flatNode, region));
        }
        flatNode = flatNode.getNext();
    }
    if (list.size() > 0) {
        CompoundRegion[] regions = new CompoundRegion[list.size()];
        list.toArray(regions);
        return regions;
    }
    return new CompoundRegion[0];
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ArrayList(java.util.ArrayList) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) Iterator(java.util.Iterator)

Example 15 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.

the class CounterFormatter method getAttrInsertPos.

/**
 */
public int getAttrInsertPos(ICSSNode node, String attrName) {
    if (node == null || attrName == null || attrName.length() == 0)
        return -1;
    IndexedRegion iNode = (IndexedRegion) node;
    if (ICounter.IDENTIFIER.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.IDENTIFIER);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        if (iNode.getEndOffset() <= 0)
            return -1;
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.LISTSTYLE.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.LISTSTYLE);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR)
                return it.getStructuredDocumentRegion().getEndOffset(region);
            else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else if (ICounter.SEPARATOR.equalsIgnoreCase(attrName)) {
        ICSSAttr attr = (ICSSAttr) node.getAttributes().getNamedItem(ICounter.SEPARATOR);
        if (attr != null && ((IndexedRegion) attr).getEndOffset() > 0)
            return ((IndexedRegion) attr).getStartOffset();
        IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(iNode.getEndOffset() - 1);
        RegionIterator it = new RegionIterator(flatNode, flatNode.getRegionAtCharacterOffset(iNode.getEndOffset() - 1));
        boolean hasComma = false;
        while (it.hasPrev()) {
            ITextRegion region = it.prev();
            if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR) {
                if (!hasComma)
                    hasComma = true;
                else
                    return it.getStructuredDocumentRegion().getEndOffset(region);
            } else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                return it.getStructuredDocumentRegion().getEndOffset(region);
        }
        return ((IndexedRegion) node).getEndOffset();
    } else
        return -1;
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)447 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)179 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)174 Iterator (java.util.Iterator)75 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)58 ArrayList (java.util.ArrayList)40 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)39 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)35 List (java.util.List)33 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)31 BadLocationException (org.eclipse.jface.text.BadLocationException)30 RegionIterator (org.eclipse.wst.dtd.core.internal.text.RegionIterator)22 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)19 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)16 Node (org.w3c.dom.Node)16 ContextRegion (org.eclipse.wst.sse.core.internal.parser.ContextRegion)15 IOException (java.io.IOException)13 IRegion (org.eclipse.jface.text.IRegion)13 StyleRange (org.eclipse.swt.custom.StyleRange)13