Search in sources :

Example 31 with ITypedRegion

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

the class STPAutoEditStrategy method smartIndentAfterOpeningBracket.

private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) {
    if (c.offset < 1 || d.getLength() == 0)
        return;
    int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
    try {
        STPHeuristicScanner scanner = new STPHeuristicScanner(d);
        ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, p, false);
        if (STPPartitionScanner.STP_CONDITIONAL.equals(partition.getType())) {
            scanner = new STPHeuristicScanner(d, fPartitioning, STPPartitionScanner.STP_CONDITIONAL);
        }
        // current line
        int line = d.getLineOfOffset(c.offset);
        int lineOffset = d.getLineOffset(line);
        // make sure we don't have any leading comments etc.
        if (!d.get(lineOffset, c.offset - lineOffset).trim().isEmpty())
            return;
        // Line of last C code
        int pos = scanner.findNonWhitespaceBackward(p, STPHeuristicScanner.UNBOUND);
        if (pos == -1)
            return;
        int lastLine = d.getLineOfOffset(pos);
        // Only shift if the last C line is further up and is a braceless block candidate
        if (lastLine < line) {
            STPIndenter indenter = new STPIndenter(d, scanner, fProject);
            StringBuilder indent = indenter.computeIndentation(p, true);
            String toDelete = d.get(lineOffset, c.offset - lineOffset);
            if (indent != null && !indent.toString().equals(toDelete)) {
                c.text = indent.append(c.text).toString();
                c.length += c.offset - lineOffset;
                c.offset = lineOffset;
            }
        }
    } catch (BadLocationException e) {
        IDEPlugin.log(e);
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 32 with ITypedRegion

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

the class STPAutoEditStrategy method inStringOrComment.

private boolean inStringOrComment(IDocument d, DocumentCommand c) {
    int docLength = d.getLength();
    if (c.offset == -1 || docLength == 0)
        return false;
    try {
        ITypedRegion partition = TextUtilities.getPartition(d, fPartitioning, c.offset, false);
        String partitionType = partition.getType();
        if (c.offset > 0 && (STPPartitionScanner.STP_COMMENT.equals(partitionType) || STPPartitionScanner.STP_MULTILINE_COMMENT.equals(partitionType) || STPPartitionScanner.STP_STRING.equals(partitionType))) {
            return true;
        }
        IRegion lineInfo = d.getLineInformationOfOffset(c.offset);
        int offset = lineInfo.getOffset();
        boolean inChar = false;
        boolean inString = false;
        boolean inComment = false;
        for (int i = offset; i < c.offset; ++i) {
            char ch = d.getChar(i);
            switch(ch) {
                case '\"':
                    if (!inChar)
                        inString = !inString;
                    break;
                case '\'':
                    if (!inString)
                        inChar = !inChar;
                    break;
                case '\\':
                    ++i;
                    break;
                case '/':
                    if (!inString && !inChar) {
                        ch = d.getChar(i + 1);
                        if (ch == '/')
                            // We have a line comment
                            return true;
                        else if (ch == '*')
                            inComment = true;
                    }
                    break;
                case '#':
                    if (!inString && !inChar)
                        return true;
                    break;
                case '*':
                    if (!inString && !inChar) {
                        if (inComment) {
                            ch = d.getChar(i + 1);
                            if (ch == '/')
                                inComment = false;
                        }
                    }
                    break;
            }
        }
        return inString || inChar || inComment;
    } catch (BadLocationException e) {
        IDEPlugin.log(e);
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 33 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion 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 34 with ITypedRegion

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

the class STPCompletionProcessor method getProbe.

/**
 * Returns the full name of the probe surrounding the given
 * offset. This function assumes that the given offset is inside
 * of a {@link STPPartitionScanner#STP_PROBE} section.
 * @param document
 * @param offset
 * @return the probe name
 * @throws BadLocationException
 * @throws BadPartitioningException
 */
private String getProbe(IDocument document, int offset) throws BadLocationException, BadPartitioningException {
    String probePoint = null;
    ITypedRegion partition = ((IDocumentExtension3) document).getPartition(STPProbeScanner.STP_PROBE_PARTITIONING, offset, false);
    String probe = document.get(partition.getOffset(), partition.getLength());
    // make sure that we are inside a probe
    if (probe.startsWith(PROBE_KEYWORD)) {
        probePoint = probe.substring(PROBE_KEYWORD.length(), probe.indexOf('{'));
        probePoint = probePoint.trim();
    }
    return probePoint;
}
Also used : IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 35 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion 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)

Aggregations

ITypedRegion (org.eclipse.jface.text.ITypedRegion)129 BadLocationException (org.eclipse.jface.text.BadLocationException)59 IRegion (org.eclipse.jface.text.IRegion)42 IDocument (org.eclipse.jface.text.IDocument)21 Region (org.eclipse.jface.text.Region)19 ArrayList (java.util.ArrayList)17 TypedRegion (org.eclipse.jface.text.TypedRegion)16 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)14 Position (org.eclipse.jface.text.Position)14 TypedPosition (org.eclipse.jface.text.TypedPosition)13 List (java.util.List)11 StyleRange (org.eclipse.swt.custom.StyleRange)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)6 ITextSelection (org.eclipse.jface.text.ITextSelection)5 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)4 Document (org.eclipse.jface.text.Document)4 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)4 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)4