Search in sources :

Example 1 with BadPartitioningException

use of org.eclipse.jface.text.BadPartitioningException in project linuxtools by eclipse.

the class STPCompletionProcessor method getHoverInfo.

@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    String documentation = null;
    try {
        String keyword = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
        int offset = hoverRegion.getOffset();
        IDocument document = textViewer.getDocument();
        if (getPrecedingToken(document, offset - 1).tokenString.equals(PROBE_KEYWORD.trim())) {
            documentation = ManpageCacher.getDocumentation(TapsetItemType.PROBE, keyword);
        } else {
            ITypedRegion partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
            if (partition.getType() == STPProbeScanner.STP_PROBE) {
                if (isFunctionRegion(document, hoverRegion)) {
                    documentation = ManpageCacher.getDocumentation(TapsetItemType.FUNCTION, keyword);
                } else {
                    String probe = getProbe(document, offset);
                    if (stpMetadataSingleton.isVariableInProbe(probe, keyword)) {
                        documentation = ManpageCacher.getDocumentation(TapsetItemType.PROBEVAR, probe, keyword);
                    }
                }
            }
        }
    } catch (BadLocationException | BadPartitioningException e) {
    // Bad hover location/scenario; just ignore it.
    }
    return documentation;
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with BadPartitioningException

use of org.eclipse.jface.text.BadPartitioningException in project linuxtools by eclipse.

the class STPCompletionProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(IDocument document, int offset) {
    ITypedRegion partition = null;
    boolean useGlobal = false;
    try {
        partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
        if (partition.getOffset() == offset) {
            if (partition.getType() != IDocument.DEFAULT_CONTENT_TYPE && partition.getType() != STPProbeScanner.STP_PROBE) {
                if (offset > 0) {
                    ITypedRegion prevPartition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset - 1, false);
                    useGlobal = prevPartition.getType() == IDocument.DEFAULT_CONTENT_TYPE;
                } else {
                    useGlobal = true;
                }
            }
        }
    } catch (BadLocationException | BadPartitioningException e) {
        return NO_COMPLETIONS;
    }
    // $NON-NLS-1$
    String prefix = "";
    // $NON-NLS-1$
    String prePrefix = "";
    // Get completion hint from document
    try {
        prefix = getPrefix(document, offset);
        Token previousToken = getPrecedingToken(document, offset - prefix.length() - 1);
        while (// $NON-NLS-1$
        previousToken.tokenString.equals("=") || previousToken.tokenString.equals(",")) {
            // $NON-NLS-1$
            previousToken = getPrecedingToken(document, previousToken.offset - 1);
            previousToken = getPrecedingToken(document, previousToken.offset - 1);
        }
        prePrefix = previousToken.tokenString;
    } catch (BadLocationException e) {
        return NO_COMPLETIONS;
    }
    if (prePrefix.startsWith("probe")) {
        // $NON-NLS-1$
        return getProbeCompletionList(prefix, offset);
    }
    // which can be called.
    if (partition.getType() == STPProbeScanner.STP_PROBE) {
        ICompletionProposal[] variableCompletions = getProbeVariableCompletions(document, offset, prefix);
        ICompletionProposal[] functionCompletions = getFunctionCompletions(offset, prefix);
        ArrayList<ICompletionProposal> completions = new ArrayList<>(variableCompletions.length + functionCompletions.length);
        completions.addAll(Arrays.asList(variableCompletions));
        completions.addAll(Arrays.asList(functionCompletions));
        return completions.toArray(new ICompletionProposal[0]);
    } else if (partition.getType() == IDocument.DEFAULT_CONTENT_TYPE || useGlobal) {
        // In the global scope return global keyword completion.
        return getGlobalKeywordCompletion(prefix, offset);
    }
    return NO_COMPLETIONS;
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with BadPartitioningException

use of org.eclipse.jface.text.BadPartitioningException in project linuxtools by eclipse.

the class STPIndenter method findReferencePosition.

/**
 * Returns the reference position regarding to indentation for <code>position</code>,
 * or {@link STPHeuristicScanner#NOT_FOUND NOT_FOUND}. <code>fIndent</code> will contain
 * the relative indentation (in indentation units, not characters) after the call. If there is
 * a special alignment (e.g. for a method declaration where parameters should be aligned),
 * <code>fAlign</code> will contain the absolute position of the alignment reference
 * in <code>fDocument</code>, otherwise <code>fAlign</code> is set to
 * {@link STPHeuristicScanner#NOT_FOUND}.
 *
 * @param offset the offset for which the reference is computed
 * @param danglingElse whether a dangling else should be assumed at <code>position</code>
 * @param matchMode determines what kind of reference position should be returned.
 *     See {@link MatchMode}.
 * @return the reference statement relative to which <code>position</code>
 *         should be indented, or {@link STPHeuristicScanner#NOT_FOUND}
 */
public int findReferencePosition(int offset, boolean danglingElse, MatchMode matchMode) {
    // The indentation modification
    fIndent = 0;
    fExtraSpaces = 0;
    fAlign = NOT_FOUND;
    fPosition = offset;
    // declaration start instead of the opening brace.
    switch(matchMode) {
        case MATCH_BRACE:
            if (skipScope(STPSymbols.TokenLBRACE, STPSymbols.TokenRBRACE)) {
                try {
                    // Align with the opening brace that is on a line by its own
                    int lineOffset = fDocument.getLineOffset(fLine);
                    if (lineOffset <= fPosition && fDocument.get(lineOffset, fPosition - lineOffset).trim().isEmpty()) {
                        return fPosition;
                    }
                } catch (BadLocationException e) {
                // Concurrent modification - walk default path
                }
                // If the opening brace is not on the start of the line, skip to the start.
                int pos = skipToStatementStart(true, true);
                // indent is aligned with reference position
                fIndent = 0;
                return pos;
            } else {
                // If we can't find the matching brace, the heuristic is to unindent
                // by one against the normal position
                int pos = findReferencePosition(offset, danglingElse, MatchMode.REGULAR);
                fIndent--;
                return pos;
            }
        case MATCH_PAREN:
            // Align parentheses.
            if (skipScope(STPSymbols.TokenLPAREN, STPSymbols.TokenRPAREN)) {
                return fPosition;
            } else {
                // If we can't find the matching paren, the heuristic is to unindent by one
                // against the normal position.
                int pos = findReferencePosition(offset, danglingElse, MatchMode.REGULAR);
                fIndent--;
                return pos;
            }
        case MATCH_CASE:
            // brace.
            return matchCaseAlignment();
        case MATCH_ACCESS_SPECIFIER:
            // opening brace.
            return matchAccessSpecifierAlignment();
        case MATCH_TYPE_DECLARATION:
            return matchTypeDeclaration();
        case REGULAR:
            break;
    }
    if (peekToken(offset) == STPSymbols.TokenCOLON) {
        int pos = fPosition;
        if (looksLikeTypeInheritanceDecl()) {
            fIndent = fPrefs.prefContinuationIndent;
            return fPosition;
        }
        fPosition = pos;
    }
    nextToken();
    // Skip access specifiers
    while (fToken == STPSymbols.TokenCOLON && isAccessSpecifier()) {
        nextToken();
    }
    int line = fLine;
    switch(fToken) {
        case STPSymbols.TokenGREATERTHAN:
        case STPSymbols.TokenRBRACE:
            // Skip the block and fall through.
            // If we can't complete the scope, reset the scan position
            int pos = fPosition;
            if (!skipScope())
                fPosition = pos;
            return skipToStatementStart(danglingElse, false);
        case STPSymbols.TokenSEMICOLON:
            // the token just after that is previous.start
            return skipToStatementStart(danglingElse, false);
        // Scope introduction: special treat who special is
        case STPSymbols.TokenLPAREN:
        case STPSymbols.TokenLBRACE:
        case STPSymbols.TokenLBRACKET:
            return handleScopeIntroduction(Math.min(offset + 1, fDocument.getLength()), true);
        case STPSymbols.TokenEOF:
            // trap when hitting start of document
            return NOT_FOUND;
        case STPSymbols.TokenEQUAL:
            // indent assignments, but don't do so if there is a String
            // after the assignment because SystemTap doesn't require
            // semi-colons to end lines and so this should be treated as
            // a complete assignment.
            pos = fPosition;
            while (pos < offset) {
                try {
                    ITypedRegion partition = ((IDocumentExtension3) fDocument).getPartition(STPPartitionScanner.STP_PARTITIONING, pos, danglingElse);
                    if (STPPartitionScanner.STP_STRING.equals(partition.getType()))
                        return skipToStatementStart(danglingElse, false);
                    pos = partition.getOffset() + partition.getLength();
                } catch (BadLocationException e) {
                    break;
                } catch (BadPartitioningException e) {
                    break;
                }
            }
            fIndent = fPrefs.prefAssignmentIndent;
            return fPosition;
        case STPSymbols.TokenCOLON:
            pos = fPosition;
            if (looksLikeCaseStatement()) {
                fIndent = fPrefs.prefCaseBlockIndent;
                return pos;
            }
            fPosition = pos;
            if (looksLikeTypeInheritanceDecl()) {
                fIndent = fPrefs.prefContinuationIndent;
                return pos;
            }
            fPosition = pos;
            if (looksLikeConstructorInitializer()) {
                fIndent = fPrefs.prefBlockIndent;
                return pos;
            }
            fPosition = pos;
            if (isConditional()) {
                fPosition = offset;
                fLine = line;
                return skipToPreviousListItemOrListStart();
            }
            fPosition = pos;
            return skipToPreviousListItemOrListStart();
        case STPSymbols.TokenQUESTIONMARK:
            if (fPrefs.prefTernaryDeepAlign) {
                setFirstElementAlignment(fPosition, offset + 1);
            } else {
                fIndent = fPrefs.prefTernaryIndent;
            }
            return fPosition;
        // Indentation for blockless introducers:
        case STPSymbols.TokenDO:
        case STPSymbols.TokenWHILE:
        case STPSymbols.TokenELSE:
            fIndent = fPrefs.prefSimpleIndent;
            return fPosition;
        case STPSymbols.TokenTRY:
            return skipToStatementStart(danglingElse, false);
        case STPSymbols.TokenRETURN:
        case STPSymbols.TokenTYPEDEF:
        case STPSymbols.TokenUSING:
            fIndent = fPrefs.prefContinuationIndent;
            return fPosition;
        case STPSymbols.TokenCONST:
            nextToken();
            if (fToken != STPSymbols.TokenRPAREN) {
                return skipToPreviousListItemOrListStart();
            }
        // $FALL-THROUGH$
        case STPSymbols.TokenRPAREN:
            if (skipScope(STPSymbols.TokenLPAREN, STPSymbols.TokenRPAREN)) {
                int scope = fPosition;
                nextToken();
                if (fToken == STPSymbols.TokenIF || fToken == STPSymbols.TokenWHILE || fToken == STPSymbols.TokenFOR || fToken == STPSymbols.TokenFOREACH) {
                    fIndent = fPrefs.prefSimpleIndent;
                    return fPosition;
                }
                if (fToken == STPSymbols.TokenSWITCH) {
                    return fPosition;
                }
                fPosition = scope;
                if (looksLikeMethodDecl()) {
                    return skipToStatementStart(danglingElse, false);
                }
                if (fToken == STPSymbols.TokenCATCH) {
                    return skipToStatementStart(danglingElse, false);
                }
                fPosition = scope;
                if (looksLikeAnonymousTypeDecl()) {
                    return skipToStatementStart(danglingElse, false);
                }
            }
            // restore
            fPosition = offset;
            fLine = line;
            // else: fall through to default
            return skipToPreviousListItemOrListStart();
        case STPSymbols.TokenCOMMA:
            // indent by list-indent.
            return skipToPreviousListItemOrListStart();
        case STPSymbols.TokenPLUS:
        case STPSymbols.TokenMINUS:
        case STPSymbols.TokenLESSTHAN:
        case STPSymbols.TokenAGGREGATE:
        case STPSymbols.TokenSHIFTRIGHT:
        case STPSymbols.TokenSHIFTLEFT:
        case STPSymbols.TokenOTHER:
            // Math symbol, use skipToPreviousListItemOrListStart.
            return skipToPreviousListItemOrListStart();
        // Otherwise, fall-through
        default:
            // end in a semi-colon.  We want to indent to the same level as the statement.
            return skipToStatementStart(danglingElse, false);
    }
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 4 with BadPartitioningException

use of org.eclipse.jface.text.BadPartitioningException in project webtools.sourceediting by eclipse.

the class BasicStructuredDocument method getPartition.

public ITypedRegion getPartition(String partitioning, int offset, boolean preferOpenPartitions) throws BadLocationException, BadPartitioningException {
    if ((0 > offset) || (offset > getLength()))
        throw new BadLocationException();
    ITypedRegion result = null;
    IDocumentPartitioner partitioner = getDocumentPartitioner(partitioning);
    if (partitioner instanceof IDocumentPartitionerExtension2) {
        result = ((IDocumentPartitionerExtension2) partitioner).getPartition(offset, preferOpenPartitions);
    } else if (partitioner != null) {
        result = partitioner.getPartition(offset);
    } else if (IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING.equals(partitioning)) {
        result = new TypedRegion(0, getLength(), DEFAULT_CONTENT_TYPE);
    } else
        throw new BadPartitioningException();
    return result;
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentPartitionerExtension2(org.eclipse.jface.text.IDocumentPartitionerExtension2) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with BadPartitioningException

use of org.eclipse.jface.text.BadPartitioningException in project linuxtools by eclipse.

the class STPCompletionProcessor method getProbeVariableCompletions.

private ICompletionProposal[] getProbeVariableCompletions(IDocument document, int offset, String prefix) {
    try {
        String probeName = getProbe(document, offset);
        TreeNode[] completionData = stpMetadataSingleton.getProbeVariableCompletions(probeName, prefix);
        ICompletionProposal[] result = new ICompletionProposal[completionData.length];
        for (int i = 0; i < completionData.length; i++) {
            result[i] = new STPProbevarCompletionProposal(completionData[i], prefix.length(), offset, probeName);
        }
        return result;
    } catch (BadLocationException | BadPartitioningException e) {
        return NO_COMPLETIONS;
    }
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) TreeNode(org.eclipse.linuxtools.systemtap.structures.TreeNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) STPProbevarCompletionProposal(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.proposals.STPProbevarCompletionProposal) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)5 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)5 ITypedRegion (org.eclipse.jface.text.ITypedRegion)4 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)3 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 ArrayList (java.util.ArrayList)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 IDocumentPartitionerExtension2 (org.eclipse.jface.text.IDocumentPartitionerExtension2)1 TypedRegion (org.eclipse.jface.text.TypedRegion)1 STPProbevarCompletionProposal (org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.proposals.STPProbevarCompletionProposal)1 TreeNode (org.eclipse.linuxtools.systemtap.structures.TreeNode)1