Search in sources :

Example 16 with TextRegion

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

the class TokenRegionProviderTest 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 17 with TextRegion

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

the class TokenRegionProviderTest method testTokenMerge_1.

@Test
public void testTokenMerge_1() throws Exception {
    String model = " ab ";
    ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(2, 0));
    assertEquals(1, tokenRegion.getOffset());
    assertEquals(2, 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 18 with TextRegion

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

the class TokenRegionProviderTest method testTokenSplit_1.

@Test
public void testTokenSplit_1() throws Exception {
    String model = " axb ";
    ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(2, 1));
    assertEquals(1, tokenRegion.getOffset());
    assertEquals(3, 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 19 with TextRegion

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

the class TokenRegionProvider method getTokenReplaceRegion.

public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) {
    int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength();
    ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion.getText().length()));
    if (tokenRegion.getOffset() == replaceRegion.getOffset() && tokenRegion.getLength() == replaceRegion.getText().length())
        return replaceRegion;
    else
        return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta, changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength()));
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 20 with TextRegion

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

the class TokenRegionProvider method getTokenRegion.

public ITextRegion getTokenRegion(String text, ITextRegion region) {
    Lexer lexer = lexerProvider.get();
    lexer.setCharStream(new ANTLRStringStream(text));
    int currentStart = 0;
    int currentEnd = 0;
    CommonToken nextToken = (CommonToken) lexer.nextToken();
    int regionStart = region.getOffset();
    int regionEnd = regionStart + region.getLength();
    while (nextToken != Token.EOF_TOKEN && currentEnd <= regionStart) {
        currentStart = nextToken.getStartIndex();
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    // nextToken is either EOF or the first token that follows the start of the given region
    while (nextToken != Token.EOF_TOKEN && currentEnd < regionEnd) {
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    if (currentStart != regionStart || currentEnd != regionEnd)
        return new TextRegion(currentStart, currentEnd - currentStart);
    else
        return region;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.parser.antlr.Lexer) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) CommonToken(org.antlr.runtime.CommonToken)

Aggregations

TextRegion (org.eclipse.xtext.util.TextRegion)41 ITextRegion (org.eclipse.xtext.util.ITextRegion)28 Test (org.junit.Test)19 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 Collection (java.util.Collection)5 StringReader (java.io.StringReader)3 CommonToken (org.antlr.runtime.CommonToken)3 IFile (org.eclipse.core.resources.IFile)3 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)3 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)3 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)3 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)3 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)3 IEclipseTrace (org.eclipse.xtext.ui.generator.trace.IEclipseTrace)3 EObject (org.eclipse.emf.ecore.EObject)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)2 ILocationInResource (org.eclipse.xtext.generator.trace.ILocationInResource)2 TraceRegionToStringTester (org.eclipse.xtext.generator.trace.TraceRegionToStringTester)2