Search in sources :

Example 76 with ITypedRegion

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

the class TypedRegionMerger method merge.

public ITypedRegion[] merge(ITypedRegion[] original) {
    if (original == null || original.length == 0)
        return original;
    ITypedRegion[] result = new ITypedRegion[original.length];
    String contentType = original[0].getType();
    result[0] = original[0];
    for (int i = 1; i < original.length; i++) {
        ITypedRegion copyMe = original[i];
        result[i] = new TypedRegion(copyMe.getOffset(), copyMe.getLength(), contentType);
    }
    return result;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 77 with ITypedRegion

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

the class XtendDoubleClickStrategyProvider method getStrategy.

@Override
public ITextDoubleClickStrategy getStrategy(ISourceViewer sourceViewer, String contentType, String documentPartitioning) {
    if (TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION.equals(contentType)) {
        return new AbstractPartitionDoubleClickSelector(documentPartitioning) {

            @Override
            protected IRegion getSelectedRegion(IDocument document, ITypedRegion completePartition) throws BadLocationException {
                String content = document.get(completePartition.getOffset(), completePartition.getLength());
                // assume � as start character
                int trimLeft = 1;
                if (content.startsWith("'''")) {
                    trimLeft = 3;
                }
                int trimRight = 0;
                if (content.endsWith("'''")) {
                    trimRight = 3;
                } else if (content.endsWith("''")) {
                    trimRight = 2;
                } else if (content.endsWith("'") || content.endsWith("\u00AB")) {
                    trimRight = 1;
                }
                return new Region(completePartition.getOffset() + trimLeft, completePartition.getLength() - trimLeft - trimRight);
            }
        };
    }
    if (TokenTypeToPartitionMapper.JAVA_DOC_PARTITION.equals(contentType)) {
        return new AbstractPartitionDoubleClickSelector(documentPartitioning) {

            /**
             * Allows to select the complete <code>@param</code> instead of just the literal <code>param</code>.
             * Copied from org.eclipse.jdt.internal.ui.text.java.JavadocDoubleClickStrategy.
             */
            @Override
            protected IRegion findExtendedDoubleClickSelection(IDocument document, int position) {
                try {
                    IRegion match = super.findExtendedDoubleClickSelection(document, position);
                    if (match != null)
                        return match;
                    IRegion word = findWord(document, position);
                    IRegion line = document.getLineInformationOfOffset(position);
                    if (position == line.getOffset() + line.getLength())
                        return null;
                    int start = word.getOffset();
                    int end = start + word.getLength();
                    if (start > 0 && document.getChar(start - 1) == '@' && Character.isJavaIdentifierPart(document.getChar(start)) && (start == 1 || Character.isWhitespace(document.getChar(start - 2)) || document.getChar(start - 2) == '{')) {
                        // double click after @ident
                        start--;
                    } else if (end == position && end == start + 1 && end < line.getOffset() + line.getLength() && document.getChar(end) == '@') {
                        // double click before " @ident"
                        return findExtendedDoubleClickSelection(document, position + 1);
                    }
                    if (start == end)
                        return null;
                    return new Region(start, end - start);
                } catch (BadLocationException x) {
                    return null;
                }
            }

            @Override
            protected CommonBreakIterator createBreakIterator() {
                return new CommonBreakIterator(false) {

                    class Braces extends Other {

                        @Override
                        protected boolean isValid(char ch) {
                            return ch == '{' || ch == '}';
                        }
                    }

                    class Parentheses extends Other {

                        @Override
                        protected boolean isValid(char ch) {
                            return ch == '(' || ch == ')';
                        }
                    }

                    Braces braces = new Braces();

                    Parentheses parentheses = new Parentheses();

                    @Override
                    protected Run getRun(char ch) {
                        if (braces.isValid(ch)) {
                            return braces;
                        }
                        if (parentheses.isValid(ch)) {
                            return parentheses;
                        }
                        return super.getRun(ch);
                    }
                };
            }
        };
    }
    return super.getStrategy(sourceViewer, contentType, documentPartitioning);
}
Also used : IRegion(org.eclipse.jface.text.IRegion) AbstractPartitionDoubleClickSelector(org.eclipse.xtext.ui.editor.doubleClicking.AbstractPartitionDoubleClickSelector) ITypedRegion(org.eclipse.jface.text.ITypedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) CommonBreakIterator(org.eclipse.xtext.ui.editor.model.CommonBreakIterator) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 78 with ITypedRegion

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

the class PartitionTest method testPartitioningAfterModify_02.

@Test
public void testPartitioningAfterModify_02() throws BadLocationException {
    String input = "class SomeType {\n" + "	def someOperation() '''\n" + "		Dear,\n" + "		bla bla foo\n" + "		\n" + "		Yours sincerely,\n" + "		\n" + "		Joe Developer\n" + "		\n" + "	'''	\n" + "}";
    document.set(input);
    document.replace(input.indexOf("\t\tYours"), 0, "���");
    document.replace(input.indexOf("Joe") + 3, /*���*/
    0, "\t");
    ITypedRegion[] partitions = document.getDocumentPartitioner().computePartitioning(0, input.length() + 4);
    assertEquals(4, partitions.length);
    ITypedRegion first = partitions[0];
    assertEquals(0, first.getOffset());
    assertEquals(input.indexOf("'"), first.getLength());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, first.getType());
    ITypedRegion second = partitions[1];
    assertEquals(input.indexOf("'"), second.getOffset());
    assertEquals(input.indexOf("\t\tYours") + 1, second.getLength() + second.getOffset());
    assertEquals(TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION, second.getType());
    ITypedRegion third = partitions[2];
    assertEquals(input.indexOf("\t\tYours") + 1, third.getOffset());
    assertEquals(input.lastIndexOf("'") + 1 + 4, /*��� + \t*/
    third.getLength() + third.getOffset());
    assertEquals(TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION, third.getType());
    ITypedRegion forth = partitions[3];
    assertEquals(input.lastIndexOf("'") + 1 + 4, /*��� + \t*/
    forth.getOffset());
    assertEquals(document.getLength(), forth.getLength() + forth.getOffset());
    assertEquals(input.length() + 4, /*��� + \t*/
    forth.getLength() + forth.getOffset());
    assertEquals(IDocument.DEFAULT_CONTENT_TYPE, forth.getType());
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) Test(org.junit.Test)

Example 79 with ITypedRegion

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

the class NonRuleBasedDamagerRepairer method getDamageRegion.

@Override
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 80 with ITypedRegion

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

the class PresentationReconciler method createPresentation.

/**
 * Constructs a "repair description" for the given damage and returns this
 * description as a text presentation. For this, it queries the partitioning
 * of the damage region and asks the appropriate presentation repairer for
 * each partition to construct the "repair description" for this partition.
 *
 * @param damage the damage to be repaired
 * @param document the document whose presentation must be repaired
 * @return the presentation repair description as text presentation or
 *         <code>null</code> if the partitioning could not be computed
 */
protected TextPresentation createPresentation(IRegion damage, IDocument document) {
    try {
        if (fRepairers == null || fRepairers.isEmpty()) {
            TextPresentation presentation = new TextPresentation(damage, 100);
            presentation.setDefaultStyleRange(new StyleRange(damage.getOffset(), damage.getLength(), null, null));
            return presentation;
        }
        TextPresentation presentation = new TextPresentation(damage, 1000);
        ITypedRegion[] partitioning = TextUtilities.computePartitioning(document, getDocumentPartitioning(), damage.getOffset(), damage.getLength(), false);
        for (ITypedRegion r : partitioning) {
            IPresentationRepairer repairer = getRepairer(r.getType());
            if (repairer != null)
                repairer.createPresentation(presentation, r);
        }
        return presentation;
    } catch (BadLocationException x) {
        return null;
    }
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TextPresentation(org.eclipse.jface.text.TextPresentation) 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