Search in sources :

Example 1 with ITextRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project liferay-ide by liferay.

the class AlloyJsTranslator method translateInlineJSNode.

public void translateInlineJSNode(IStructuredDocumentRegion container) {
    /* start a function header.. will amend later */
    ITextRegionList t = container.getRegions();
    ITextRegion r;
    Iterator<?> regionIterator = t.iterator();
    while (regionIterator.hasNext() && !isCanceled()) {
        r = (ITextRegion) regionIterator.next();
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            r.getStart();
            r.getTextEnd();
            String tagAttrname = container.getText(r);
            /*
				 * Attribute values aren't case sensative, also make sure next region is attrib
				 * value
				 */
            if (NodeHelper.isInArray(JsDataTypes.HTMLATREVENTS, tagAttrname) || NodeHelper.isInArray(ALLOYATTREVENTS, tagAttrname)) {
                if (regionIterator.hasNext()) {
                    regionIterator.next();
                }
                if (regionIterator.hasNext()) {
                    r = (ITextRegion) regionIterator.next();
                }
                if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
                    int valStartOffset = container.getStartOffset(r);
                    String rawText = container.getText(r);
                    if ((rawText == null) || (rawText.length() == 0)) {
                        continue;
                    }
                    /* Strip quotes */
                    switch(rawText.charAt(0)) {
                        case '\'':
                        case '"':
                            rawText = rawText.substring(1);
                            valStartOffset++;
                    }
                    if ((rawText == null) || (rawText.length() == 0)) {
                        continue;
                    }
                    switch(rawText.charAt(rawText.length() - 1)) {
                        case '\'':
                        case '"':
                            rawText = rawText.substring(0, rawText.length() - 1);
                    }
                    new Position(valStartOffset, rawText.length());
                    char[] spaces = Util.getPad(Math.max(0, valStartOffset - scriptOffset - _EVENT_HANDLER_PRE_LENGTH));
                    for (int i = 0; i < spaces.length; i++) {
                        try {
                            char c = fStructuredDocument.getChar(scriptOffset + i);
                            if ((c == '\n') || (c == '\r') || (c == '\t')) {
                                spaces[i] = c;
                            }
                        } catch (BadLocationException ble) {
                            Logger.logException(ble);
                        }
                    }
                    fScriptText.append(spaces);
                    fScriptText.append(_EVENT_HANDLER_PRE);
                    appendAndTrack(rawText, valStartOffset);
                    if (ADD_SEMICOLON_AT_INLINE) {
                        fScriptText.append(";");
                    }
                    if (r.getLength() > rawText.length()) {
                        fScriptText.append(_EVENT_HANDLER_POST);
                        spaces = Util.getPad(Math.max(0, r.getLength() - rawText.length() - _EVENT_HANDLER_POST_LENGTH));
                        fScriptText.append(spaces);
                    }
                    scriptOffset = container.getEndOffset(r);
                }
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) Position(org.eclipse.jface.text.Position) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with ITextRegion

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

the class AbstractLineStyleProvider method prepareTextRegion.

/**
 * @param region
 * @param start
 * @param length
 * @param holdResults
 * @return
 */
private boolean prepareTextRegion(ITextRegionCollection blockedRegion, int partitionStartOffset, int partitionLength, Collection holdResults) {
    boolean handled = false;
    final int partitionEndOffset = partitionStartOffset + partitionLength - 1;
    ITextRegion region = null;
    ITextRegionList regions = blockedRegion.getRegions();
    int nRegions = regions.size();
    StyleRange styleRange = null;
    for (int i = 0; i < nRegions; i++) {
        region = regions.get(i);
        TextAttribute attr = null;
        TextAttribute previousAttr = null;
        if (blockedRegion.getStartOffset(region) > partitionEndOffset)
            break;
        if (blockedRegion.getEndOffset(region) <= partitionStartOffset)
            continue;
        if (region instanceof ITextRegionCollection) {
            handled = prepareTextRegion((ITextRegionCollection) region, partitionStartOffset, partitionLength, holdResults);
        } else {
            attr = getAttributeFor(blockedRegion, region);
            if (attr != null) {
                handled = true;
                // regions correctly
                if ((styleRange != null) && (previousAttr != null) && (previousAttr.equals(attr))) {
                    styleRange.length += region.getLength();
                } else {
                    styleRange = createStyleRange(blockedRegion, region, attr, partitionStartOffset, partitionLength);
                    holdResults.add(styleRange);
                    // technically speaking, we don't need to update
                    // previousAttr
                    // in the other case, because the other case is when
                    // it hasn't changed
                    previousAttr = attr;
                }
            } else {
                previousAttr = null;
            }
        }
    }
    return handled;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)

Example 3 with ITextRegion

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

the class StyledTextColorPicker method getNamedStyleAtOffset.

protected String getNamedStyleAtOffset(int offset) {
    // ensure the offset is clean
    if (offset >= fInput.length())
        return getNamedStyleAtOffset(fInput.length() - 1);
    else if (offset < 0)
        return getNamedStyleAtOffset(0);
    // find the ITextRegion at this offset
    if (fNodes == null)
        return null;
    IStructuredDocumentRegion aNode = fNodes;
    while (aNode != null && !aNode.containsOffset(offset)) aNode = aNode.getNext();
    if (aNode != null) {
        // find the ITextRegion's Context at this offset
        ITextRegion interest = aNode.getRegionAtCharacterOffset(offset);
        if (interest == null)
            return null;
        if (offset > aNode.getTextEndOffset(interest))
            return null;
        String regionContext = interest.getType();
        if (regionContext == null)
            return null;
        // find the named style (internal/selectable name) for that
        // context
        String namedStyle = (String) getContextStyleMap().get(regionContext);
        if (namedStyle != null) {
            return namedStyle;
        }
    }
    return null;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 4 with ITextRegion

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

the class StyledTextColorPicker method applyStyles.

protected void applyStyles() {
    if (fText == null || fText.isDisposed() || fInput == null || fInput.length() == 0)
        return;
    // List regions = fParser.getRegions();
    IStructuredDocumentRegion node = fNodes;
    while (node != null) {
        ITextRegionList regions = node.getRegions();
        for (int i = 0; i < regions.size(); i++) {
            ITextRegion currentRegion = regions.get(i);
            // lookup the local coloring type and apply it
            String namedStyle = (String) getContextStyleMap().get(currentRegion.getType());
            if (namedStyle == null)
                continue;
            TextAttribute attribute = getAttribute(namedStyle);
            if (attribute == null)
                continue;
            StyleRange style = new StyleRange(node.getStartOffset(currentRegion), currentRegion.getLength(), attribute.getForeground(), attribute.getBackground(), attribute.getStyle());
            fText.setStyleRange(style);
        }
        node = node.getNext();
    }
}
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) TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange)

Example 5 with ITextRegion

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

the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.

@Override
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer textViewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    setErrorMessage(null);
    fTextViewer = textViewer;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition <= 0 ? 0 : documentPosition - 1);
    IJSONNode node = (IJSONNode) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    // Fix completion region in case of JSON_OBJECT_CLOSE
    if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
        completionRegion = getCompletionRegion(documentPosition, node);
    }
    String matchString = EMPTY;
    if (completionRegion != null) {
        if (isPairValue(context, node)) {
            try {
                String nodeText = getNodeText(node);
                int colonIndex = nodeText.indexOf(COLON);
                int offset = documentPosition - node.getStartOffset();
                if (colonIndex >= 0 && offset >= 0) {
                    String str = nodeText.substring(colonIndex + 1, offset);
                    // $NON-NLS-1$
                    str = str.replaceAll(",", BLANK);
                    matchString = str;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        } else {
            matchString = getMatchString(sdRegion, completionRegion, documentPosition);
        }
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
    if (contentAssistRequest == null) {
        contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    /*
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892 Only set this
		 * error message if nothing else was already set
		 */
    if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
    return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

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