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());
}
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());
}
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());
}
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()));
}
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;
}
Aggregations