Search in sources :

Example 96 with ITypedRegion

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

the class DocumentUtil method searchBackwardsInSamePartition.

/**
 * searches backwards 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 searchBackwardsInSamePartition(String toFind, String documentText, IDocument document, int endOffset) throws BadLocationException {
    if (endOffset < 0) {
        return null;
    }
    int length = toFind.length();
    String text = preProcessSearchString(documentText);
    ITypedRegion partition = document.getPartition(endOffset);
    int indexOf = text.lastIndexOf(toFind, endOffset - length);
    while (indexOf >= 0) {
        ITypedRegion partition2 = document.getPartition(indexOf);
        if (partition2.getType().equals(partition.getType())) {
            return new Region(indexOf, length);
        }
        indexOf = text.lastIndexOf(toFind, partition2.getOffset() - length);
    }
    String trimmed = toFind.trim();
    if (trimmed.length() > 0 && trimmed.length() != length) {
        return searchBackwardsInSamePartition(trimmed, documentText, document, endOffset);
    }
    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 97 with ITypedRegion

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

the class XtextAutoEditStrategy method getCurrentRuleUptoOffset.

protected String getCurrentRuleUptoOffset(int offset, IDocument doc) throws BadLocationException {
    ITypedRegion currentPartition = doc.getPartition(offset);
    String partitionType = currentPartition.getType();
    String currentSegment = doc.get(currentPartition.getOffset(), offset - currentPartition.getOffset());
    StringBuilder ruleAsString = new StringBuilder();
    while (currentSegment.indexOf(';') == -1) {
        ruleAsString.insert(0, currentSegment);
        do {
            if (currentPartition.getOffset() == 0) {
                return ruleAsString.toString();
            }
            currentPartition = doc.getPartition(currentPartition.getOffset() - 1);
            currentSegment = doc.get(currentPartition.getOffset(), currentPartition.getLength());
        } while (!partitionType.equals(currentPartition.getType()));
    }
    ruleAsString.insert(0, currentSegment.substring(currentSegment.lastIndexOf(';') + 1));
    return ruleAsString.toString();
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 98 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project tmdm-studio-se by Talend.

the class NonRuleBasedDamagerRepairer method getDamageRegion.

/**
 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
 */
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, boolean documentPartitioningChanged) {
    if (!documentPartitioningChanged) {
        try {
            IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
            int start = Math.max(partition.getOffset(), info.getOffset());
            int end = event.getOffset() + (event.getText() == null ? event.getLength() : event.getText().length());
            if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
                // optimize the case of the same line
                end = info.getOffset() + info.getLength();
            } else
                end = endOfLineOf(end);
            end = Math.min(partition.getOffset() + partition.getLength(), end);
            return new Region(start, end - start);
        } catch (BadLocationException x) {
        }
    }
    return partition;
}
Also used : Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 99 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project tmdm-studio-se by Talend.

the class XMLDocumentPartitioner method getPartition.

public ITypedRegion getPartition(int offset) {
    try {
        Position[] category = document.getPositions(positionCategory);
        if (category == null || category.length == 0) {
            // $NON-NLS-1$
            return new TypedRegion(0, document.getLength(), "__dftl_partition_content_type");
        }
        int index = document.computeIndexInCategory(positionCategory, offset);
        if (index < category.length) {
            TypedPosition next = (TypedPosition) category[index];
            if (offset == next.offset) {
                return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
            }
            if (index == 0) {
                // $NON-NLS-1$
                return new TypedRegion(0, next.offset, "__dftl_partition_content_type");
            }
            TypedPosition previous = (TypedPosition) category[index - 1];
            if (previous.includes(offset)) {
                return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
            }
            int endOffset = previous.getOffset() + previous.getLength();
            // $NON-NLS-1$
            return new TypedRegion(endOffset, next.getOffset() - endOffset, "__dftl_partition_content_type");
        }
        TypedPosition previous = (TypedPosition) category[category.length - 1];
        if (previous.includes(offset)) {
            return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
        }
        int endOffset = previous.getOffset() + previous.getLength();
        // $NON-NLS-1$
        return new TypedRegion(endOffset, document.getLength() - endOffset, "__dftl_partition_content_type");
    } catch (BadPositionCategoryException _ex) {
    } catch (BadLocationException _ex) {
    }
    // $NON-NLS-1$
    return new TypedRegion(0, document.getLength(), "__dftl_partition_content_type");
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 100 with ITypedRegion

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

the class XMLFindOccurencesHandler method getProcessorForCurrentSelection.

/**
 * Get the appropriate find occurrences processor
 *
 * @param document -
 *            assumes not null
 * @param textSelection
 * @return FindOccurrencesProcessor
 */
private FindOccurrencesProcessor getProcessorForCurrentSelection(IDocument document, ITextSelection textSelection) {
    // check if we have an action that's enabled on the current partition
    ITypedRegion tr = getPartition(document, textSelection);
    // $NON-NLS-1$
    String partition = tr != null ? tr.getType() : "";
    Iterator it = getProcessors().iterator();
    FindOccurrencesProcessor processor = null;
    while (it.hasNext()) {
        processor = (FindOccurrencesProcessor) it.next();
        // we just choose the first action that can handle the partition
        if (processor.enabledForParitition(partition))
            return processor;
    }
    List extendedFindOccurrencesProcessors = ExtendedConfigurationBuilder.getInstance().getConfigurations(FindOccurrencesProcessor.class.getName(), partition);
    for (int i = 0; i < extendedFindOccurrencesProcessors.size(); i++) {
        Object o = extendedFindOccurrencesProcessors.get(i);
        if (o instanceof FindOccurrencesProcessor) {
            /*
				 * We just choose the first registered processor that
				 * explicitly says it can handle the partition
				 */
            processor = (FindOccurrencesProcessor) o;
            if (processor.enabledForParitition(partition))
                return processor;
        }
    }
    return null;
}
Also used : Iterator(java.util.Iterator) XMLFindOccurrencesProcessor(org.eclipse.wst.xml.ui.internal.search.XMLFindOccurrencesProcessor) FindOccurrencesProcessor(org.eclipse.wst.sse.ui.internal.search.FindOccurrencesProcessor) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ArrayList(java.util.ArrayList) List(java.util.List)

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