Search in sources :

Example 41 with IStructuredDocumentRegion

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

the class CSSStructuredDocumentReParser method checkForCrossStructuredDocumentRegionSyntax.

/**
 */
// public StructuredDocumentEvent
// checkForCrossStructuredDocumentRegionBoundryCases2() {
// StructuredDocumentEvent result = specialPositionUpdate(fStart, fStart +
// fLengthToReplace - 1);
// if (result == null) {
// result = checkInsideBrace();
// }
// return result;
// }
protected StructuredDocumentEvent checkForCrossStructuredDocumentRegionSyntax() {
    int checkStart = fStart;
    int checkEnd = fStart + fLengthToReplace - 1;
    IStructuredDocumentRegion endRegion = fStructuredDocument.getRegionAtCharacterOffset(checkEnd);
    if (endRegion != null) {
        checkEnd = endRegion.getEndOffset();
    }
    ReparseRange range = new ReparseRange(checkStart, checkEnd);
    range.expand(getUpdateRangeForDelimiter(range.getStart(), range.getEnd()));
    range.expand(getUpdateRangeForUnknownRegion(range.getStart(), range.getEnd()));
    range.expand(getUpdateRangeForQuotes(range.getStart(), range.getEnd()));
    range.expand(getUpdateRangeForComments(range.getStart(), range.getEnd()));
    range.expand(getUpdateRangeForBraces(range.getStart(), range.getEnd()));
    StructuredDocumentEvent result;
    result = checkInsideBrace(range.getStart(), range.getEnd());
    if (result == null) {
        result = reparse(range.getStart(), range.getEnd());
    }
    return result;
}
Also used : StructuredDocumentEvent(org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)

Example 42 with IStructuredDocumentRegion

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

the class JsTranslator method translateJSNode.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator#translateJSNode(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)
	 */
public void translateJSNode(IStructuredDocumentRegion container) {
    if (container == null)
        return;
    ITextRegionCollection containerRegion = container;
    Iterator regions = containerRegion.getRegions().iterator();
    ITextRegion region = null;
    char[] spaces = Util.getPad(container.getStartOffset() - scriptOffset);
    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 e) {
            Logger.logException(e);
        }
    }
    fScriptText.append(spaces);
    scriptOffset = container.getStartOffset();
    if (container.getType() != DOMRegionContext.BLOCK_TEXT && container.getType() != DOMRegionContext.XML_CDATA_TEXT) {
        // add place holder in position map (for formatting)
        appendAndTrack("", scriptOffset);
        return;
    }
    while (regions.hasNext() && !isCanceled()) {
        region = (ITextRegion) regions.next();
        String type = region.getType();
        // content assist was not showing up in JSP inside a javascript
        // region
        // System.out.println("Region text: " + container.getText().substring(region.getStart(), region.getEnd()));
        boolean isContainerRegion = region instanceof ITextRegionContainer;
        /* make sure its not a sub container region, probably JSP */
        if (type == DOMRegionContext.BLOCK_TEXT) {
            int scriptStartOffset = container.getStartOffset(region);
            int scriptTextLength = container.getLength();
            String regionText = container.getFullText(region);
            // regionText = StringUtils.replace(regionText, CDATA_START, CDATA_START_PAD);
            // regionText = StringUtils.replace(regionText, CDATA_END, CDATA_END_PAD);
            int regionLength = region.getLength();
            spaces = Util.getPad(scriptStartOffset - scriptOffset);
            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 e) {
                    Logger.logException(e);
                }
            }
            fScriptText.append(spaces);
            // skip over XML/HTML comment starts
            if (regionText.indexOf(XML_COMMENT_START) >= 0) {
                int index = regionText.indexOf(XML_COMMENT_START);
                boolean replaceCommentStart = true;
                for (int i = 0; i < index; i++) {
                    /*
						 * replace the comment start in the translation when
						 * it's preceded only by white space or '/'
						 */
                    replaceCommentStart = replaceCommentStart && (Character.isWhitespace(regionText.charAt(i)) || '/' == regionText.charAt(i));
                }
                if (replaceCommentStart) {
                    IRegion line;
                    int end;
                    int length;
                    try {
                        /*
							 * try to find where the line with the comment
							 * ends (it is the end of what we'll replace)
							 */
                        line = container.getParentDocument().getLineInformationOfOffset(index + scriptStartOffset);
                        end = line.getOffset() + line.getLength() - scriptStartOffset;
                        if (end > regionText.length()) {
                            end = regionText.length();
                        }
                        length = end - index;
                    } catch (BadLocationException e) {
                        // $NON-NLS-1$
                        Logger.logException("Could not get web page's comment line information", e);
                        end = index + XML_COMMENT_START.length();
                        length = XML_COMMENT_START.length();
                    }
                    scriptStartOffset += end;
                    regionText = regionText.substring(end);
                    appendAndTrack(new String(Util.getPad(end)), fScriptText.length());
                }
            }
            /*
				 * Fix for
				 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=284774
				 * end of last valid JS source, start of next content to
				 * skip
				 */
            // last offset of valid JS source, after which there's server-side stuff
            int validJSend = 0;
            // start of next valid JS source, last offset of content that was skipped
            int validJSstart = 0;
            Matcher matcher = fClientSideTagPattern.matcher(regionText);
            // note the start of a HTML tag if one's present
            int clientMatchStart = matcher.find() ? matcher.start() : -1;
            StringBuffer generatedContent = new StringBuffer();
            int serverSideStart = -1;
            int serverSideDelimiter = 0;
            // find any instance of server code blocks in the region text
            for (int i = 0; i < fServerSideDelimiters.length; i++) {
                int index = regionText.indexOf(fServerSideDelimiters[i][0]);
                if (serverSideStart < 0) {
                    serverSideStart = index;
                    serverSideDelimiter = i;
                } else if (index >= 0) {
                    serverSideStart = Math.min(serverSideStart, index);
                    if (serverSideStart == index) {
                        serverSideDelimiter = i;
                    }
                }
            }
            // contains something other than pure JavaScript
            while (serverSideStart > -1 || clientMatchStart > -1) {
                // $NON-NLS-1$
                validJSend = validJSstart;
                boolean biasClient = false;
                boolean biasServer = false;
                // update the start of content to skip
                if (clientMatchStart > -1 && serverSideStart > -1) {
                    validJSend = Math.min(clientMatchStart, serverSideStart);
                    biasClient = validJSend == clientMatchStart;
                    biasServer = validJSend == serverSideStart;
                } else if (clientMatchStart > -1 && serverSideStart < 0) {
                    validJSend = clientMatchStart;
                    biasClient = true;
                } else if (clientMatchStart < 0 && serverSideStart > -1) {
                    validJSend = serverSideStart;
                    biasServer = true;
                }
                // append if there's something we want to include
                if (-1 < validJSstart && -1 < validJSend) {
                    // append what we want to include
                    appendAndTrack(regionText.substring(validJSstart, validJSend), scriptStartOffset + validJSstart);
                    // change the skipped content to a valid variable name and append it as a placeholder
                    int startOffset = scriptStartOffset + validJSend;
                    String serverEnd = fServerSideDelimiters[serverSideDelimiter][1];
                    int serverSideEnd = (regionLength > validJSend + serverEnd.length()) ? regionText.indexOf(serverEnd, validJSend + fServerSideDelimiters[serverSideDelimiter][1].length()) : -1;
                    if (serverSideEnd > -1)
                        serverSideEnd += serverEnd.length();
                    int clientMatchEnd = matcher.find(validJSend) ? matcher.end() : -1;
                    // update end of what we skipped
                    validJSstart = -1;
                    if (clientMatchEnd > validJSend && serverSideEnd > validJSend) {
                        if (biasClient)
                            validJSstart = clientMatchEnd;
                        else if (biasServer)
                            validJSstart = serverSideEnd;
                        else
                            validJSstart = Math.min(clientMatchEnd, serverSideEnd);
                    }
                    if (clientMatchEnd >= validJSend && serverSideEnd < 0)
                        validJSstart = matcher.end();
                    if (clientMatchEnd < 0 && serverSideEnd >= validJSend)
                        validJSstart = serverSideEnd;
                    /*
						 * the substitution text length much match original
						 * length exactly, generate text of the right length
						 */
                    int start = scriptStartOffset + validJSend;
                    int end = scriptStartOffset + validJSstart;
                    generatedContent.append('_');
                    for (int i = validJSend + 1; i < validJSstart; i++) {
                        switch(i - validJSend) {
                            case 1:
                                generatedContent.append('$');
                                break;
                            case 2:
                                generatedContent.append('t');
                                break;
                            case 3:
                                generatedContent.append('a');
                                break;
                            case 4:
                                generatedContent.append('g');
                                break;
                            default:
                                generatedContent.append('_');
                        }
                    }
                    /*
						 * Remember this source range, it may be needed to
						 * find the original contents for which we're
						 * placeholding
						 */
                    fGeneratedRanges.add(new Region(start, end - start));
                    appendAndTrack(generatedContent.toString(), start);
                    // reset now that it's been appended
                    generatedContent.setLength(0);
                }
                // set up to end while if no end for valid
                if (validJSstart > 0) {
                    int serverSideStartGuess = -1;
                    for (int i = 0; i < fServerSideDelimiters.length; i++) {
                        int index = regionText.indexOf(fServerSideDelimiters[i][0], validJSstart);
                        if (serverSideStartGuess < 0) {
                            serverSideStartGuess = index;
                            serverSideDelimiter = i;
                        } else if (index >= 0) {
                            serverSideStartGuess = Math.min(serverSideStartGuess, index);
                            if (serverSideStartGuess == index) {
                                serverSideDelimiter = i;
                            }
                        }
                    }
                    serverSideStart = validJSstart < regionLength - fShortestServerSideDelimiterPairLength ? serverSideStartGuess : -1;
                    clientMatchStart = validJSstart < regionLength ? (matcher.find(validJSstart) ? matcher.start() : -1) : -1;
                } else {
                    serverSideStart = clientMatchStart = -1;
                }
            }
            if (validJSstart >= 0) {
                appendAndTrack(regionText.substring(validJSstart), scriptStartOffset + validJSstart);
                Position inHtml = new Position(scriptStartOffset + validJSstart, regionText.length() - validJSstart);
                scriptLocationInHtml.add(inHtml);
            } else {
                appendAndTrack(regionText, scriptStartOffset);
                Position inHtml = new Position(scriptStartOffset, regionText.length());
                scriptLocationInHtml.add(inHtml);
            }
            scriptOffset = fScriptText.length();
        }
    }
    IStructuredDocumentRegion endTag = container.getNext();
    if (endTag == null) {
        missingEndTagRegionStart = container.getStartOffset();
    } else if (endTag != null) {
        NodeHelper nh = new NodeHelper(endTag);
        String name = nh.getTagName();
        if (name == null || !name.trim().equalsIgnoreCase("script") || !nh.isEndTag()) {
            // $NON-NLS-1$
            missingEndTagRegionStart = container.getStartOffset();
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IRegion(org.eclipse.jface.text.IRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 43 with IStructuredDocumentRegion

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

the class HTMLSyntaxColoringPage method getNamedStyleAtOffset.

private String getNamedStyleAtOffset(int offset) {
    // ensure the offset is clean
    if (offset >= fDocument.getLength())
        return getNamedStyleAtOffset(fDocument.getLength() - 1);
    else if (offset < 0)
        return getNamedStyleAtOffset(0);
    IStructuredDocumentRegion documentRegion = fDocument.getFirstStructuredDocumentRegion();
    while (documentRegion != null && !documentRegion.containsOffset(offset)) {
        documentRegion = documentRegion.getNext();
    }
    if (documentRegion != null) {
        // find the ITextRegion's Context at this offset
        ITextRegion interest = documentRegion.getRegionAtCharacterOffset(offset);
        if (interest == null)
            return null;
        if (offset > documentRegion.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) fContextToStyleMap.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 44 with IStructuredDocumentRegion

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

the class TestRegionChangedEvent method testGetRegion.

public void testGetRegion() {
    RegionChangedEvent event = getBasicEvent();
    IStructuredDocumentRegion structuredDocumentRegion = event.getStructuredDocumentRegion();
    assertEquals(null, structuredDocumentRegion);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) RegionChangedEvent(org.eclipse.wst.sse.core.internal.provisional.events.RegionChangedEvent)

Example 45 with IStructuredDocumentRegion

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

the class JSONHoverProcessor method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    if ((textViewer == null) || (textViewer.getDocument() == null)) {
        return null;
    }
    IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(offset);
    ITextRegion region = null;
    if (flatNode != null) {
        region = flatNode.getRegionAtCharacterOffset(offset);
    }
    if (region != null) {
        // only supply hoverhelp for object key, or simple JSON value
        String regionType = region.getType();
        if ((regionType == JSONRegionContexts.JSON_OBJECT_KEY) || JSONUtil.isJSONSimpleValue(regionType)) {
            try {
                // check if we are at whitespace before or after line
                IRegion line = textViewer.getDocument().getLineInformationOfOffset(offset);
                if ((offset > (line.getOffset())) && (offset < (line.getOffset() + line.getLength()))) {
                    // (whitespace after relevant info)
                    if (offset < flatNode.getTextEndOffset(region)) {
                        return new Region(flatNode.getStartOffset(region), region.getTextLength());
                    }
                }
            } catch (BadLocationException e) {
                Logger.logException(e);
            }
        }
    }
    return null;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)439 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)174 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)99 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)87 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)70 List (java.util.List)40 BadLocationException (org.eclipse.jface.text.BadLocationException)39 ArrayList (java.util.ArrayList)38 Iterator (java.util.Iterator)35 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 Node (org.w3c.dom.Node)30 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)26 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)19 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)15 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)14 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)13 NodeList (org.w3c.dom.NodeList)13