Search in sources :

Example 11 with ITypedRegion

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

the class AbstractEditStrategy method getDocumentContent.

protected String getDocumentContent(IDocument document, DocumentCommand command) throws BadLocationException {
    final ITypedRegion partition = document.getPartition(command.offset);
    ITypedRegion[] partitions = document.getDocumentPartitioner().computePartitioning(0, document.getLength());
    Iterable<ITypedRegion> partitionsOfCurrentType = Iterables.filter(Arrays.asList(partitions), new Predicate<ITypedRegion>() {

        @Override
        public boolean apply(ITypedRegion input) {
            return input.getType().equals(partition.getType());
        }
    });
    StringBuilder builder = new StringBuilder();
    for (ITypedRegion position : partitionsOfCurrentType) {
        builder.append(document.get(position.getOffset(), position.getLength()));
    }
    return builder.toString();
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 12 with ITypedRegion

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

the class PresentationDamager method getDamageRegion.

@Override
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {
    if (!(e.getDocument() instanceof IXtextDocument)) {
        return new Region(0, 0);
    }
    XtextDocument document = (XtextDocument) e.getDocument();
    IRegion lastDamage = document.getLastDamage();
    // check whether this is just a presentation invalidation not based on a real document change
    if (lastDamage == null || !isEventMatchingLastDamage(e, lastDamage)) {
        IRegion result = computeInterSection(partition, e, document);
        return result;
    }
    if (!TextUtilities.overlaps(partition, lastDamage) && lastDamage.getOffset() < e.getDocument().getLength()) {
        if (documentPartitioningChanged)
            return partition;
        return lastDamage;
    }
    int offset = Math.max(lastDamage.getOffset(), partition.getOffset());
    int endOffset = Math.min(lastDamage.getOffset() + lastDamage.getLength(), partition.getOffset() + partition.getLength());
    IRegion result = new Region(offset, endOffset - offset);
    return result;
}
Also used : Region(org.eclipse.jface.text.Region) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) IRegion(org.eclipse.jface.text.IRegion) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 13 with ITypedRegion

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

the class PresentationDamager method computeInterSection.

/**
 * @return the common region of the given partition and the changed region in the DocumentEvent based on the underlying tokens.
 */
protected IRegion computeInterSection(ITypedRegion partition, DocumentEvent e, XtextDocument document) {
    Iterable<ILexerTokenRegion> tokensInPartition = Iterables.filter(document.getTokens(), Regions.overlaps(partition.getOffset(), partition.getLength()));
    Iterator<ILexerTokenRegion> tokens = Iterables.filter(tokensInPartition, Regions.overlaps(e.getOffset(), e.getLength())).iterator();
    if (tokens.hasNext()) {
        ILexerTokenRegion first = tokens.next();
        ILexerTokenRegion last = first;
        while (tokens.hasNext()) last = tokens.next();
        return new Region(first.getOffset(), last.getOffset() + last.getLength() - first.getOffset());
    }
    // this shouldn't happen, but just in case return the whole partition
    return partition;
}
Also used : Region(org.eclipse.jface.text.Region) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion)

Example 14 with ITypedRegion

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

the class PartitionEndSkippingEditStrategy method internalCustomizeDocumentCommand.

@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
    if (command.length == 0 && command.text.length() > 0) {
        ITypedRegion partition = document.getPartition(command.offset);
        String part = document.get(partition.getOffset(), partition.getLength());
        if (end != null) {
            int relativeOffset = command.offset - partition.getOffset();
            if (relativeOffset < partition.getLength() - end.length())
                return;
            if (!part.endsWith(end))
                return;
            String text = command.text;
            if (part.length() - relativeOffset < text.length()) {
                text = text.substring(0, part.length() - relativeOffset);
            }
            if (part.substring(relativeOffset, relativeOffset + text.length()).equals(text))
                command.length = text.length();
        } else {
            if (part.substring(command.offset - partition.getOffset()).equals(command.text))
                command.length = command.text.length();
        }
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 15 with ITypedRegion

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

the class PartitionInsertEditStrategy method internalCustomizeDocumentCommand.

@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
    if (left.length() >= command.text.length() && command.text.length() > 0 && left.endsWith(command.text)) {
        ITypedRegion partition = document.getPartition(command.offset);
        if (command.offset != 0 && partition.getLength() == 0 && document.getLength() != 0) {
            ITypedRegion precedingPartition = document.getPartition(command.offset - 1);
            partition = precedingPartition;
        }
        if (partition.getOffset() + partition.getLength() >= command.offset + right.length()) {
            if (!left.equals(right) && right.equals(document.get(command.offset, right.length())))
                return;
        }
        if (isIdentifierPart(document, command.offset + command.length))
            return;
        if (left.length() > 1) {
            int minDocumentLength = left.length() - command.text.length();
            if (minDocumentLength > document.getLength()) {
                return;
            }
            if (command.offset - minDocumentLength < 0)
                return;
            String existingLeftPart = document.get(command.offset - minDocumentLength, minDocumentLength);
            if (!left.equals(existingLeftPart + command.text))
                return;
        }
        if (left.equals(right)) {
            String partitionContent = document.get(partition.getOffset(), partition.getLength());
            if (count(left, partitionContent) % 2 != 0)
                return;
            IRegion currentLine = document.getLineInformationOfOffset(command.offset);
            if (partition.getOffset() == command.offset && partition.getOffset() + partition.getLength() > currentLine.getOffset() + currentLine.getLength()) {
                String trailingLine = document.get(command.offset, currentLine.getLength() - (command.offset - currentLine.getOffset()));
                if (count(left, trailingLine) % 2 != 0)
                    return;
            }
        }
        command.caretOffset = command.offset + command.text.length();
        command.shiftsCaret = false;
        command.text += right;
    }
}
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