Search in sources :

Example 11 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class FlexTokenRegionProviderTest method testTokenSplit.

@Test
public void testTokenSplit() throws Exception {
    String model = " a b ";
    ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(2, 1));
    assertEquals(2, tokenRegion.getOffset());
    assertEquals(1, tokenRegion.getLength());
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

Example 12 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class FlexTokenRegionProvider method getTokenRegion.

@Override
public ITextRegion getTokenRegion(String text, ITextRegion region) {
    try {
        InternalFlexer flexer = flexerFactory.createFlexer(new StringReader(text));
        int token = flexer.advance();
        int prevStart = 0;
        int nextStart = flexer.getTokenLength();
        final int regionStartOffset = region.getOffset();
        final int regionEnd = regionStartOffset + region.getLength();
        while (token != Token.EOF && nextStart <= regionStartOffset) {
            prevStart = nextStart;
            token = flexer.advance();
            nextStart += flexer.getTokenLength();
        }
        while (token != Token.EOF && nextStart < regionEnd) {
            token = flexer.advance();
            nextStart += flexer.getTokenLength();
        }
        return new TextRegion(prevStart, nextStart - prevStart);
    } catch (IOException e) {
        // cannot happen since StringReader doesn't throw IOException
        throw new RuntimeException(e);
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 13 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class XtendProposalProvider method completeInRichString.

public void completeInRichString(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    INode node = context.getCurrentNode();
    ITextRegion textRegion = node.getTextRegion();
    int offset = textRegion.getOffset();
    int length = textRegion.getLength();
    String currentNodeText = node.getText();
    if (currentNodeText.startsWith("\u00BB") && offset + 1 <= context.getOffset() || currentNodeText.startsWith("'''") && offset + 3 <= context.getOffset()) {
        if (context.getOffset() > offset && context.getOffset() < offset + length)
            addGuillemotsProposal(context, acceptor);
    } else if (currentNodeText.startsWith("\u00AB\u00AB")) {
        try {
            IDocument document = context.getViewer().getDocument();
            int nodeLine = document.getLineOfOffset(offset);
            int completionLine = document.getLineOfOffset(context.getOffset());
            if (completionLine > nodeLine) {
                addGuillemotsProposal(context, acceptor);
            }
        } catch (BadLocationException e) {
        // ignore
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion) StyledString(org.eclipse.jface.viewers.StyledString) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 14 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.

the class XtendJavaDocProposalFactory method getReplacementContextLength.

@Override
protected int getReplacementContextLength(ContentAssistContext context) {
    int replacementOffset = context.getReplaceRegion().getOffset();
    INode currentNode = context.getCurrentNode();
    ITextRegion currentRegion = currentNode.getTextRegion();
    String text = currentNode.getText();
    int index = text.indexOf('}', replacementOffset - currentRegion.getOffset());
    if (index == -1) {
        index = text.indexOf('\n', replacementOffset - currentRegion.getOffset());
    }
    if (index != -1) {
        int indexFromStart = index + currentNode.getOffset();
        return indexFromStart - replacementOffset;
    } else {
        return context.getReplaceContextLength();
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 15 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class ContentAssistContext method getReplaceContextLength.

/**
 * The length of the region left to the completion offset that is part of the
 * replace region.
 */
public int getReplaceContextLength() {
    if (replaceContextLength == null) {
        int replacementOffset = getReplaceRegion().getOffset();
        ITextRegion currentRegion = getCurrentNode().getTextRegion();
        int replaceContextLength = currentRegion.getLength() - (replacementOffset - currentRegion.getOffset());
        this.replaceContextLength = replaceContextLength;
        return replaceContextLength;
    }
    return replaceContextLength.intValue();
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4