Search in sources :

Example 6 with ITypedRegion

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

the class DocumentUtil method findNextOffSetInPartition.

public int findNextOffSetInPartition(IDocument doc, int partitionOffSet, int minIndex) throws BadLocationException {
    ITypedRegion partition = doc.getPartition(partitionOffSet);
    ITypedRegion partition2 = doc.getPartition(minIndex);
    if (partition.getType().equals(partition2.getType()) || partition2.getLength() == 0) {
        return minIndex;
    } else {
        return findNextOffSetInPartition(doc, partitionOffSet, minIndex + partition2.getLength());
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 7 with ITypedRegion

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

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

the class DocumentPartitionerTest method testBug401433.

@Test
public void testBug401433() throws Exception {
    XtextDocument document = getDocument("     /* */ ");
    document.replace(10, 1, "");
    ITypedRegion partition = document.getPartition(9);
    assertEquals(5, partition.getOffset());
    assertEquals(5, partition.getLength());
    assertEquals(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION, partition.getType());
    document.replace(9, 1, "");
    partition = document.getPartition(8);
    assertEquals(5, partition.getOffset());
    assertEquals(4, partition.getLength());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
    document.replace(8, 1, "");
    partition = document.getPartition(7);
    assertEquals(5, partition.getOffset());
    assertEquals(3, partition.getLength());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
    document.replace(7, 1, "");
    partition = document.getPartition(6);
    assertEquals(5, partition.getOffset());
    assertEquals(2, partition.getLength());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
    document.replace(6, 1, "");
    partition = document.getPartition(5);
    assertEquals(5, partition.getOffset());
    assertEquals(1, partition.getLength());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) Test(org.junit.Test)

Example 9 with ITypedRegion

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

the class DocumentPartitionerTest method testModification.

@Test
public void testModification() throws Exception {
    XtextDocument document = getDocument("bar 345 grammar : so 'baz & so'");
    document.replace(8, 7, "/*grammar*/");
    ITypedRegion partition = document.getPartition(9);
    assertEquals(8, partition.getOffset());
    assertEquals(11, partition.getLength());
    assertEquals(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION, partition.getType());
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) Test(org.junit.Test)

Example 10 with ITypedRegion

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

the class ToggleSLCommentAction method isSelectionCommented.

/**
 * Is the given selection single-line commented?
 *
 * @param selection Selection to check
 * @return <code>true</code> iff all selected lines are commented
 * @since 2.1
 */
protected boolean isSelectionCommented(ISelection selection) {
    if (!(selection instanceof ITextSelection))
        return false;
    ITextSelection textSelection = (ITextSelection) selection;
    if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
        return false;
    IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
    try {
        IRegion block = getTextBlockFromSelection(textSelection, document);
        ITypedRegion[] regions = TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
        // [startline, endline, startline, endline, ...]
        int[] lines = new int[regions.length * 2];
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            // start line of region
            lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
            // end line of region
            int length = regions[i].getLength();
            int offset = regions[i].getOffset() + length;
            if (length > 0)
                offset--;
            lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
        }
        // Perform the check
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            String[] prefixes = fPrefixesMap.get(regions[i].getType());
            if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
                if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
                    return false;
        }
        return true;
    } catch (BadLocationException x) {
        // should not happen
        log.error(x.getMessage(), x);
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) 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