Search in sources :

Example 36 with IRegion

use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.

the class DocumentTokenSource method computeDamageRegion.

protected IRegion computeDamageRegion(final DocumentEvent e) {
    // empty document -> no dirty region
    if (e.getDocument().getLength() == 0) {
        setTokens(createTokenInfos(e.fDocument.get()));
        return new Region(0, 0);
    }
    // previously empty -> full document dirty
    if (internalModifyableTokenInfos.isEmpty()) {
        setTokens(createTokenInfos(e.fDocument.get()));
        return new Region(0, e.getDocument().getLength());
    }
    try {
        RepairEntryData repairEntryData = getRepairEntryData(e);
        int tokenStartsAt = repairEntryData.offset;
        int tokenInfoIdx = repairEntryData.index;
        CommonToken token = repairEntryData.newToken;
        if (token == Token.EOF_TOKEN)
            internalModifyableTokenInfos.subList(tokenInfoIdx, internalModifyableTokenInfos.size()).clear();
        int regionOffset = tokenStartsAt;
        int regionLength = e.fDocument.getLength() - tokenStartsAt;
        int lengthDiff = e.fText.length() - e.fLength;
        // compute region length
        while (true) {
            if (token == Token.EOF_TOKEN || tokenInfoIdx >= internalModifyableTokenInfos.size())
                break;
            while (true) {
                if (tokenInfoIdx >= internalModifyableTokenInfos.size())
                    break;
                TokenInfo tokenInfo = internalModifyableTokenInfos.get(tokenInfoIdx);
                if (token.getStartIndex() >= e.fOffset + e.fText.length()) {
                    if (tokenStartsAt + lengthDiff == token.getStartIndex() && tokenInfo.type == token.getType() && token.getStopIndex() - token.getStartIndex() + 1 == tokenInfo.length) {
                        return new Region(regionOffset, token.getStartIndex() - regionOffset);
                    }
                }
                if (tokenStartsAt + lengthDiff + tokenInfo.length > token.getStopIndex() + 1)
                    break;
                internalModifyableTokenInfos.remove(tokenInfoIdx);
                tokenStartsAt += tokenInfo.length;
                if (tokenStartsAt + lengthDiff > token.getStartIndex())
                    break;
            }
            internalModifyableTokenInfos.add(tokenInfoIdx++, createTokenInfo(token));
            token = (CommonToken) repairEntryData.tokenSource.nextToken();
        }
        internalModifyableTokenInfos.subList(tokenInfoIdx, internalModifyableTokenInfos.size()).clear();
        // add subsequent tokens
        if (tokenInfoIdx >= internalModifyableTokenInfos.size()) {
            while (token != Token.EOF_TOKEN) {
                internalModifyableTokenInfos.add(createTokenInfo(token));
                token = (CommonToken) repairEntryData.tokenSource.nextToken();
            }
        }
        return new Region(regionOffset, regionLength);
    } catch (Exception exc) {
        logger.error("Error computing damaged region", exc);
        internalModifyableTokenInfos = createTokenInfos(e.fDocument.get());
        return new Region(0, e.fDocument.getLength());
    } finally {
        setTokens(internalModifyableTokenInfos);
    }
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) CommonToken(org.antlr.runtime.CommonToken)

Example 37 with IRegion

use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.

the class DocumentUtil method searchInSamePartition.

/**
 * searches for the given string within the same partition type
 *
 * @return the region of the match or <code>null</code> if no match were found
 * @since 2.4
 */
public IRegion searchInSamePartition(String toFind, String documentText, IDocument document, int startOffset) throws BadLocationException {
    if (startOffset >= document.getLength()) {
        return null;
    }
    String text = preProcessSearchString(documentText);
    ITypedRegion partition = document.getPartition(startOffset);
    int indexOf = text.indexOf(toFind, getOffset(toFind, startOffset));
    while (indexOf >= 0 && indexOf < document.getLength()) {
        ITypedRegion partition2 = document.getPartition(indexOf);
        if (partition2.getType().equals(partition.getType())) {
            return new Region(indexOf, toFind.length());
        }
        indexOf = text.indexOf(toFind, partition2.getOffset() + partition2.getLength());
    }
    String trimmed = toFind.trim();
    if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
        return searchInSamePartition(trimmed, documentText, document, startOffset);
    }
    return null;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 38 with IRegion

use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.

the class WhitespaceHelper method calculateLeadingWhitespace.

protected String calculateLeadingWhitespace(IDocument document) {
    try {
        IRegion line = document.getLineInformationOfOffset(offset);
        if (line != null) {
            String linePrefix = document.get(line.getOffset(), offset - line.getOffset());
            if (isEmpty(linePrefix.trim())) {
                int lineNr = document.getLineOfOffset(offset);
                if (lineNr > 0) {
                    IRegion lineInformation = document.getLineInformation(lineNr - 1);
                    if (isEmpty(document.get(lineInformation.getOffset(), lineInformation.getLength()).trim())) {
                        int lineDelimiterLength = document.getLineDelimiter(lineNr - 1).length();
                        int skipLeadingWhitespace = linePrefix.length() + lineDelimiterLength;
                        offset -= skipLeadingWhitespace;
                        length += skipLeadingWhitespace;
                        return lineSeparator;
                    } else {
                        return lineSeparator;
                    }
                }
            } else {
                return lineSeparator + lineSeparator;
            }
        }
    } catch (BadLocationException e) {
        LOG.error("Error calculating leading whitespace", e);
    }
    return null;
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 39 with IRegion

use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.

the class WhitespaceHelper method calculateTrailingWhitespace.

protected String calculateTrailingWhitespace(IDocument document) {
    try {
        IRegion line = document.getLineInformationOfOffset(offset + length);
        if (line != null) {
            String lineSuffix = document.get(offset + length, line.getLength() - (offset + length - line.getOffset()));
            if (isEmpty(lineSuffix.trim())) {
                length += lineSuffix.length();
                int lineNr = document.getLineOfOffset(offset + length);
                if (lineNr < document.getNumberOfLines() - 1) {
                    IRegion lineInformation = document.getLineInformation(lineNr + 1);
                    if (!isEmpty(document.get(lineInformation.getOffset(), lineInformation.getLength()).trim())) {
                        return lineSeparator;
                    }
                }
            } else {
                for (int i = 0; i < lineSuffix.length(); ++i) {
                    if (Character.isWhitespace(lineSuffix.charAt(i)))
                        ++length;
                    else
                        break;
                }
                return lineSeparator + lineSeparator;
            }
        }
    } catch (BadLocationException e) {
        LOG.error("Error calculating trailing whitespace", e);
    }
    return null;
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 40 with IRegion

use of org.eclipse.jface.text.IRegion in project xtext-eclipse by eclipse.

the class XbaseEditor method selectAndReveal.

@Override
protected void selectAndReveal(final int selectionStart, final int selectionLength, final int revealStart, final int revealLength) {
    try {
        reentrantCallFromSelf++;
        if (expectJavaSelection > 0) {
            try {
                ITrace traceToSource = getTraceStorage();
                if (traceToSource != null) {
                    IResource javaResource = typeRoot.getResource();
                    if (expectLineSelection && javaResource instanceof IStorage) {
                        if (isCompiledWithJSR45()) {
                            try {
                                String string = Files.readStreamIntoString(((IStorage) javaResource).getContents());
                                Document javaDocument = new Document(string);
                                int line = getLineInJavaDocument(javaDocument, selectionStart, selectionLength);
                                if (line != -1) {
                                    int startOffsetOfContents = getStartOffsetOfContentsInJava(javaDocument, line);
                                    if (startOffsetOfContents != -1) {
                                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(startOffsetOfContents, 0));
                                        if (bestSelection != null) {
                                            final ITextRegionWithLineInformation textRegion = bestSelection.getTextRegion();
                                            if (textRegion != null) {
                                                int lineToSelect = textRegion.getLineNumber();
                                                try {
                                                    IRegion lineInfo = getDocument().getLineInformation(lineToSelect);
                                                    super.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength(), lineInfo.getOffset(), lineInfo.getLength());
                                                    return;
                                                } catch (BadLocationException e) {
                                                    log.error(e.getMessage(), e);
                                                }
                                            }
                                        }
                                    }
                                }
                            } catch (BadLocationException e) {
                            // do nothing
                            } catch (CoreException e) {
                            // do nothing
                            }
                        }
                    } else if (selectionStart >= 0 && selectionLength >= 0) {
                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(selectionStart, selectionLength));
                        if (bestSelection != null) {
                            ILocationInResource bestReveal = bestSelection;
                            if (selectionStart != revealStart || selectionLength != revealLength) {
                                bestReveal = traceToSource.getBestAssociatedLocation(new TextRegion(revealStart, revealLength));
                                if (bestReveal == null) {
                                    bestReveal = bestSelection;
                                }
                            }
                            ITextRegion fixedSelection = bestSelection.getTextRegion();
                            if (fixedSelection != null) {
                                ITextRegion fixedReveal = bestReveal.getTextRegion();
                                if (fixedReveal == null) {
                                    fixedReveal = fixedSelection;
                                }
                                super.selectAndReveal(fixedSelection.getOffset(), fixedSelection.getLength(), fixedReveal.getOffset(), fixedReveal.getLength());
                                return;
                            }
                        }
                    }
                }
            } finally {
                expectLineSelection = false;
                expectJavaSelection--;
            }
        }
        super.selectAndReveal(selectionStart, selectionLength, revealStart, revealLength);
    } finally {
        reentrantCallFromSelf--;
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IStorage(org.eclipse.core.resources.IStorage) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IRegion(org.eclipse.jface.text.IRegion) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITrace(org.eclipse.xtext.generator.trace.ITrace) IResource(org.eclipse.core.resources.IResource) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)668 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)265 IDocument (org.eclipse.jface.text.IDocument)143 Test (org.junit.Test)108 Point (org.eclipse.swt.graphics.Point)76 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)57 ITypedRegion (org.eclipse.jface.text.ITypedRegion)55 Position (org.eclipse.jface.text.Position)50 ArrayList (java.util.ArrayList)37 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)35 IFile (org.eclipse.core.resources.IFile)33 ITextSelection (org.eclipse.jface.text.ITextSelection)32 IEditorPart (org.eclipse.ui.IEditorPart)29 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)24 StyledText (org.eclipse.swt.custom.StyledText)22 CoreException (org.eclipse.core.runtime.CoreException)19 List (java.util.List)18 Document (org.eclipse.jface.text.Document)17 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)17