Search in sources :

Example 86 with ITypedRegion

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

the class DefaultPartitionerZeroLengthTest method assertComputeZeroLengthPartitioning_InterleavingPartitions.

private void assertComputeZeroLengthPartitioning_InterleavingPartitions(int startOffset, int endOffset, int[] offsets, String startType) {
    ITypedRegion[] regions = fPartitioner.computePartitioning(startOffset, endOffset - startOffset, true);
    String type = startType;
    int previousOffset = startOffset;
    assertEquals(offsets.length + 1, regions.length);
    for (int i = 0; i <= offsets.length; i++) {
        int currentOffset = (i == offsets.length) ? endOffset : offsets[i];
        ITypedRegion region = regions[i];
        assertTypedRegion(region, previousOffset, currentOffset, type);
        // advance
        if (type == DEFAULT)
            type = COMMENT;
        else
            type = DEFAULT;
        previousOffset = currentOffset;
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 87 with ITypedRegion

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

the class FastPartitionerTest method assertEqualPartition.

private void assertEqualPartition(int offset, int end, String type) {
    int from = offset;
    int to = end - 1;
    for (int i = from; i <= to; i++) {
        ITypedRegion region = fPartitioner.getPartition(i);
        assertTypedRegion(region, offset, end, type);
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 88 with ITypedRegion

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

the class STPAutoEditStrategy method smartIndentAfterClosingBracket.

private void smartIndentAfterClosingBracket(IDocument d, DocumentCommand c) {
    if (c.offset == -1 || d.getLength() == 0)
        return;
    try {
        int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
        int line = d.getLineOfOffset(p);
        int start = d.getLineOffset(line);
        int whiteend = findEndOfWhiteSpace(d, start, c.offset);
        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);
        }
        STPIndenter indenter = new STPIndenter(d, scanner, fProject);
        // shift only when line does not contain any text up to the closing bracket
        if (whiteend == c.offset) {
            // evaluate the line with the opening bracket that matches out closing bracket
            int reference = indenter.findReferencePosition(c.offset, false, MatchMode.MATCH_BRACE);
            int indLine = d.getLineOfOffset(reference);
            if (indLine != -1 && indLine != line) {
                // take the indent of the found line
                StringBuilder replaceText = new StringBuilder(getIndentOfLine(d, indLine));
                // add the rest of the current line including the just added close bracket
                replaceText.append(d.get(whiteend, c.offset - whiteend));
                replaceText.append(c.text);
                // modify document command
                c.length += c.offset - start;
                c.offset = start;
                c.text = replaceText.toString();
            }
        }
    } catch (BadLocationException e) {
        IDEPlugin.log(e);
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 89 with ITypedRegion

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

the class STPAutoEditStrategy method smartIndentAfterHash.

private void smartIndentAfterHash(IDocument doc, DocumentCommand c) {
    try {
        ITypedRegion partition = TextUtilities.getPartition(doc, fPartitioning, c.offset, false);
        if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
            IRegion startLine = doc.getLineInformationOfOffset(c.offset);
            String indent = doc.get(startLine.getOffset(), c.offset - startLine.getOffset());
            if (indent.trim().length() == 0) {
                c.offset -= indent.length();
                c.length += indent.length();
            }
        }
    } catch (BadLocationException e) {
        IDEPlugin.log(e);
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 90 with ITypedRegion

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

the class IndentHandler method indentLine.

/**
 * Indents a single line using the heuristic scanner. Multiline comments are
 * indented as specified by the <code>CCommentAutoIndentStrategy</code>.
 *
 * @param document
 *            the document
 * @param line
 *            the line to be indented
 * @param caret
 *            the caret position
 * @param indenter
 *            the indenter
 * @param scanner
 *            the heuristic scanner
 * @param multiLine
 *            <code>true</code> if more than one line is being indented
 * @return <code>true</code> if <code>document</code> was modified,
 *         <code>false</code> otherwise
 * @throws BadLocationException
 *             if the document got changed concurrently
 */
private boolean indentLine(IDocument document, int line, int caret, STPIndenter indenter, STPHeuristicScanner scanner, boolean multiLine) throws BadLocationException {
    IRegion currentLine = document.getLineInformation(line);
    int offset = currentLine.getOffset();
    // where we start searching for non-WS; after the
    int wsStart = offset;
    // "//" in single line comments
    String indent = null;
    if (offset < document.getLength()) {
        ITypedRegion partition = TextUtilities.getPartition(document, STPPartitionScanner.STP_PARTITIONING, offset, true);
        ITypedRegion startingPartition = TextUtilities.getPartition(document, STPPartitionScanner.STP_PARTITIONING, offset, false);
        String type = partition.getType();
        if (type.equals(STPPartitionScanner.STP_MULTILINE_COMMENT)) {
            indent = computeCommentIndent(document, line, scanner, startingPartition);
        } else if (startingPartition.getType().equals(STPPartitionScanner.STP_CONDITIONAL)) {
            indent = computePreprocessorIndent(document, line, startingPartition);
        } else if (startingPartition.getType().equals(STPPartitionScanner.STP_STRING) && offset > startingPartition.getOffset()) {
            // don't indent inside (raw-)string
            return false;
        } else if (!fIsTabAction && startingPartition.getOffset() == offset && startingPartition.getType().equals(STPPartitionScanner.STP_COMMENT)) {
            // line comment starting at position 0 -> indent inside
            if (indentInsideLineComments()) {
                int max = document.getLength() - offset;
                int slashes = 2;
                while (slashes < max - 1 && // $NON-NLS-1$
                document.get(offset + slashes, 2).equals("//")) slashes += 2;
                wsStart = offset + slashes;
                StringBuilder computed = indenter.computeIndentation(offset);
                if (computed == null)
                    computed = new StringBuilder(0);
                int tabSize = getTabSize();
                while (slashes > 0 && computed.length() > 0) {
                    char c = computed.charAt(0);
                    if (c == '\t') {
                        if (slashes > tabSize) {
                            slashes -= tabSize;
                        } else {
                            break;
                        }
                    } else if (c == ' ') {
                        slashes--;
                    } else {
                        break;
                    }
                    computed.deleteCharAt(0);
                }
                indent = document.get(offset, wsStart - offset) + computed;
            }
        }
    }
    // standard C code indentation
    if (indent == null) {
        StringBuilder computed = indenter.computeIndentation(offset);
        if (computed != null) {
            indent = computed.toString();
        } else {
            // $NON-NLS-1$
            indent = "";
        }
    }
    // change document:
    // get current white space
    int lineLength = currentLine.getLength();
    int end = scanner.findNonWhitespaceForwardInAnyPartition(wsStart, offset + lineLength);
    if (end == STPHeuristicScanner.NOT_FOUND) {
        // an empty line
        end = offset + lineLength;
        if (multiLine && !indentEmptyLines()) {
            // $NON-NLS-1$
            indent = "";
        }
    }
    int length = end - offset;
    String currentIndent = document.get(offset, length);
    // set the caret offset so it can be used when setting the selection
    if (caret >= offset && caret <= end) {
        fCaretOffset = offset + indent.length();
    } else {
        fCaretOffset = -1;
    }
    // only change the document if it is a real change
    if (!indent.equals(currentIndent)) {
        document.replace(offset, length, indent);
        return true;
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

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