Search in sources :

Example 91 with IndexedRegion

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

the class ThreadedModelReadEditTest method domInsertElement.

public void domInsertElement(IFile file, String tagName, HashMap attributes, int insertionPoint) {
    IStructuredModel sModel = null;
    try {
        sModel = StructuredModelManager.getModelManager().getModelForEdit(file);
        if (sModel != null) {
            Document domDoc = sModel.getAdapter(Document.class);
            IndexedRegion r = sModel.getIndexedRegion(insertionPoint);
            if (r instanceof Node) {
                Element e = domDoc.createElement(tagName);
                Iterator iter = attributes.keySet().iterator();
                // set attributes
                while (iter.hasNext()) {
                    String attr = (String) iter.next();
                    String val = (String) attributes.get(attr);
                    Attr attrNode = domDoc.createAttribute(attr);
                    attrNode.setValue(val);
                    e.setAttributeNode(attrNode);
                }
                domDoc.insertBefore(e, (Node) r);
                domDoc.insertBefore(domDoc.createTextNode("\n"), (Node) r);
            }
            sModel.save();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    } finally {
        if (sModel != null)
            sModel.releaseFromEdit();
    }
// System.out.println("inserted element, dom style");
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Iterator(java.util.Iterator) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IDocument(org.eclipse.jface.text.IDocument) Document(org.w3c.dom.Document) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Attr(org.w3c.dom.Attr)

Example 92 with IndexedRegion

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

the class JSONHoverProcessor method computeHoverHelp.

/**
 * Retrieves documentation to display in the hover help popup.
 *
 * @return String any documentation information to display <code>null</code>
 *         if there is nothing to display.
 */
protected String computeHoverHelp(ITextViewer textViewer, int documentPosition) {
    String result = null;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
    if (treeNode == null) {
        return null;
    }
    IJSONNode node = (IJSONNode) treeNode;
    IJSONNode parentNode = node.getParentNode();
    IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(documentPosition);
    if (flatNode != null) {
        ITextRegion region = flatNode.getRegionAtCharacterOffset(documentPosition);
        if (region != null) {
            result = computeRegionHelp(treeNode, parentNode, flatNode, region);
        }
    }
    return result;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 93 with IndexedRegion

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

the class PrimitiveValueFormatter method formatPre.

/**
 */
protected void formatPre(ICSSNode node, StringBuffer source) {
    int start = ((IndexedRegion) node).getStartOffset();
    int end = ((IndexedRegion) node).getEndOffset();
    Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
    CSSCleanupStrategy stgy = getCleanupStrategy(node);
    if (end > 0) {
        // format source
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        boolean appendQuote = regions.length > 1 && node.getParentNode() instanceof ICSSStyleDeclItem && isCleanup() && getCleanupStrategy(node).isQuoteValues() && (((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_FONT_FAMILY) || ((ICSSStyleDeclItem) node.getParentNode()).getPropertyName().equals(PropCMProperty.P_VOICE_FAMILY));
        String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
        StringBuffer strBuf = new StringBuffer();
        boolean skipSpace = false;
        for (int i = 0; i < regions.length; i++) {
            if (i != 0 && !skipSpace)
                appendSpaceBefore(node, regions[i], strBuf);
            skipSpace = false;
            strBuf.append(decoratedPropValueRegion(regions[i], stgy));
            if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
                skipSpace = true;
        }
        if (appendQuote) {
            source.append(quote);
            String str = strBuf.toString();
            str = str.replace('\'', ' ');
            str = str.replace('\"', ' ');
            str = str.trim();
            source.append(str);
            source.append(quote);
        } else {
            source.append(strBuf);
        }
    } else {
        // generate source
        ICSSPrimitiveValue value = (ICSSPrimitiveValue) node;
        short type = value.getPrimitiveType();
        String quote = preferences.getString(CSSCorePreferenceNames.FORMAT_QUOTE);
        String str = null;
        switch(type) {
            case CSSPrimitiveValue.CSS_NUMBER:
            case CSSPrimitiveValue.CSS_PERCENTAGE:
            case CSSPrimitiveValue.CSS_EMS:
            case CSSPrimitiveValue.CSS_EXS:
            case CSSPrimitiveValue.CSS_PX:
            case CSSPrimitiveValue.CSS_CM:
            case CSSPrimitiveValue.CSS_MM:
            case CSSPrimitiveValue.CSS_IN:
            case CSSPrimitiveValue.CSS_PT:
            case CSSPrimitiveValue.CSS_PC:
            case CSSPrimitiveValue.CSS_DEG:
            case CSSPrimitiveValue.CSS_RAD:
            case CSSPrimitiveValue.CSS_GRAD:
            case CSSPrimitiveValue.CSS_MS:
            case CSSPrimitiveValue.CSS_S:
            case CSSPrimitiveValue.CSS_HZ:
            case CSSPrimitiveValue.CSS_KHZ:
            case CSSPrimitiveValue.CSS_DIMENSION:
            case ICSSPrimitiveValue.CSS_INTEGER:
                if (value.getFloatValue(type) == ((int) value.getFloatValue(type))) {
                    str = Integer.toString((int) value.getFloatValue(type));
                } else {
                    str = Float.toString(value.getFloatValue(type));
                }
                break;
            case CSSPrimitiveValue.CSS_IDENT:
            case ICSSPrimitiveValue.CSS_HASH:
            case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
                str = value.getStringValue();
                if (str != null) {
                    if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
                        str = str.toUpperCase();
                    else
                        str.toLowerCase();
                }
                break;
            case CSSPrimitiveValue.CSS_STRING:
            case CSSPrimitiveValue.CSS_URI:
            case CSSPrimitiveValue.CSS_ATTR:
            case ICSSPrimitiveValue.CSS_URANGE:
            case ICSSPrimitiveValue.CSS_FORMAT:
            case ICSSPrimitiveValue.CSS_LOCAL:
            case ICSSPrimitiveValue.CSS_COMMA:
            case ICSSPrimitiveValue.CSS_SLASH:
                str = value.getStringValue();
        }
        String preStr = null, postStr = null;
        switch(type) {
            case CSSPrimitiveValue.CSS_ATTR:
                // $NON-NLS-1$
                preStr = "attr(";
                // $NON-NLS-1$
                postStr = ")";
                break;
            case CSSPrimitiveValue.CSS_CM:
                // $NON-NLS-1$
                postStr = "cm";
                break;
            // case ICSSPrimitiveValue.CSS_COUNTER:
            case CSSPrimitiveValue.CSS_DEG:
                // $NON-NLS-1$
                postStr = "deg";
                break;
            case CSSPrimitiveValue.CSS_EMS:
                // $NON-NLS-1$
                postStr = "em";
                break;
            case CSSPrimitiveValue.CSS_EXS:
                // $NON-NLS-1$
                postStr = "ex";
                break;
            case CSSPrimitiveValue.CSS_GRAD:
                // $NON-NLS-1$
                postStr = "grad";
                break;
            case CSSPrimitiveValue.CSS_HZ:
                // $NON-NLS-1$
                postStr = "Hz";
                break;
            case CSSPrimitiveValue.CSS_IN:
                // $NON-NLS-1$
                postStr = "in";
                break;
            case CSSPrimitiveValue.CSS_KHZ:
                // $NON-NLS-1$
                postStr = "kHz";
                break;
            case CSSPrimitiveValue.CSS_MM:
                // $NON-NLS-1$
                postStr = "mm";
                break;
            case CSSPrimitiveValue.CSS_MS:
                // $NON-NLS-1$
                postStr = "ms";
                break;
            case CSSPrimitiveValue.CSS_PC:
                // $NON-NLS-1$
                postStr = "pc";
                break;
            case CSSPrimitiveValue.CSS_PERCENTAGE:
                // $NON-NLS-1$
                postStr = "%";
                break;
            case CSSPrimitiveValue.CSS_PT:
                // $NON-NLS-1$
                postStr = "pt";
                break;
            case CSSPrimitiveValue.CSS_PX:
                // $NON-NLS-1$
                postStr = "px";
                break;
            case CSSPrimitiveValue.CSS_RAD:
                // $NON-NLS-1$
                postStr = "rad";
                break;
            // case ICSSPrimitiveValue.CSS_RGBCOLOR:
            case CSSPrimitiveValue.CSS_S:
                // $NON-NLS-1$
                postStr = "s";
                break;
            case CSSPrimitiveValue.CSS_STRING:
                quote = CSSUtil.detectQuote(str, quote);
                preStr = quote;
                postStr = quote;
                break;
            case CSSPrimitiveValue.CSS_URI:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "url(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
            // the followings are original primitive values
            case CSSPrimitiveValue.CSS_IDENT:
            case CSSPrimitiveValue.CSS_UNKNOWN:
            // use str
            case ICSSPrimitiveValue.CSS_INTEGER:
            case ICSSPrimitiveValue.CSS_HASH:
            case ICSSPrimitiveValue.CSS_SLASH:
            case ICSSPrimitiveValue.CSS_COMMA:
            case CSSPrimitiveValue.CSS_DIMENSION:
            case CSSPrimitiveValue.CSS_NUMBER:
            case ICSSPrimitiveValue.CSS_INHERIT_PRIMITIVE:
                break;
            case ICSSPrimitiveValue.CSS_URANGE:
                // $NON-NLS-1$
                preStr = "U+";
                break;
            case ICSSPrimitiveValue.CSS_FORMAT:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "format(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
            case ICSSPrimitiveValue.CSS_LOCAL:
                quote = CSSUtil.detectQuote(str, quote);
                // $NON-NLS-1$
                preStr = "local(" + quote;
                // $NON-NLS-1$
                postStr = quote + ")";
                break;
        }
        if (preferences.getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER) {
            if (preStr != null)
                preStr = preStr.toUpperCase();
            if (postStr != null)
                postStr = postStr.toUpperCase();
        }
        if (preStr != null)
            source.append(preStr);
        if (str != null)
            source.append(str);
        if (postStr != null)
            source.append(postStr);
    }
}
Also used : ICSSPrimitiveValue(org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Preferences(org.eclipse.core.runtime.Preferences)

Example 94 with IndexedRegion

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

the class StyleSheetFormatter method formatPost.

/**
 */
protected void formatPost(ICSSNode node, StringBuffer source) {
    int end = ((IndexedRegion) node).getEndOffset();
    int start = (node.getLastChild() != null && ((IndexedRegion) node.getLastChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getLastChild()).getEndOffset() : getChildInsertPos(node);
    if (end > 0 && start < end) {
        CSSCleanupStrategy stgy = getCleanupStrategy(node);
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
        for (int i = 0; i < regions.length; i++) {
            appendDelimBefore(node, regions[i], source);
            source.append(decoratedRegion(regions[i], 0, stgy));
        }
        if ((regions != null && regions.length > 0 && regions[regions.length - 1].getType() == CSSRegionContexts.CSS_CDC) || node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
            appendDelimBefore(node, null, source);
    } else {
        // source generation
        if (node.getOwnerDocument().getModel() != null && node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
            appendDelimBefore(node, null, source);
        // nothing
        return;
    }
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CSSCleanupStrategy(org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)

Example 95 with IndexedRegion

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

the class StyleSheetFormatter method getChildInsertPos.

/**
 */
public int getChildInsertPos(ICSSNode node) {
    int n = ((IndexedRegion) node).getEndOffset();
    if (n > 0) {
        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
        IStructuredDocumentRegion flatNode = structuredDocument.getRegionAtCharacterOffset(n - 1);
        ITextRegion region = flatNode.getRegionAtCharacterOffset(n - 1);
        RegionIterator it = new RegionIterator(flatNode, region);
        while (it.hasPrev()) {
            ITextRegion reg = it.prev();
            if (reg.getType() == CSSRegionContexts.CSS_CDC)
                return it.getStructuredDocumentRegion().getStartOffset(reg);
        }
        return n;
    }
    return -1;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) RegionIterator(org.eclipse.wst.css.core.internal.util.RegionIterator) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7