use of org.eclipse.xtext.util.TextRegion 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);
}
}
use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.
the class XtendTraceTests method testTraceFound.
@Test
public void testTraceFound() throws Exception {
IFile file = testHelper.createFile("test/Test", "package test\nclass Test {}");
waitForBuild();
IEclipseTrace traceToTarget = traceInformation.getTraceToTarget(file);
assertNotNull(traceToTarget);
Iterable<? extends ILocationInResource> locations = traceToTarget.getAllAssociatedLocations(new TextRegion(20, 0));
assertTrue(locations.iterator().hasNext());
IFile generatedFile = testHelper.getProject().getFile("/xtend-gen/test/Test.java");
assertTrue(generatedFile.exists());
Iterable<? extends ILocationInResource> locationsByURI = traceToTarget.getAllAssociatedLocations(new TextRegion(20, 0), generatedFile);
assertTrue(locationsByURI.iterator().hasNext());
}
use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.
the class XtendTraceTests method testNoEmptyTraces.
@Test
public void testNoEmptyTraces() throws Exception {
testHelper.createFile("test/Foo.java", "package test;\n" + "public class Foo {\n" + " public void foo(Object it) {}\n" + " public void _foo(Object it) {}\n" + "}");
IFile sourceFile = testHelper.createFile("test/Zar", "package test\n" + "class Bar extends Foo {\n" + " def dispatch foo(String it) {print(it)}\n" + "}");
waitForBuild();
ITrace trace = traceInformation.getTraceToTarget(sourceFile);
assertNotNull(trace);
trace.getBestAssociatedLocation(new TextRegion(65, 0));
}
use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.
the class ImportsCollectorTests method collect.
private ImportsAcceptor.DefaultImportsAcceptor collect(final CharSequence xtendFile) {
try {
String contentAsString = xtendFile.toString();
int start = 0;
int end = contentAsString.length();
int selector = contentAsString.indexOf("|");
if ((selector != (-1))) {
start = selector;
contentAsString = contentAsString.replaceFirst("\\|", "");
selector = contentAsString.indexOf("|");
end = contentAsString.length();
if ((selector != (-1))) {
end = selector;
contentAsString = contentAsString.replaceFirst("\\|", "");
}
}
final Resource resource = this.file(contentAsString).eResource();
final ImportsAcceptor.DefaultImportsAcceptor acceptor = new ImportsAcceptor.DefaultImportsAcceptor();
TextRegion _textRegion = new TextRegion(start, (end - start));
this.importsCollector.collectImports(((XtextResource) resource), _textRegion, acceptor);
return acceptor;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.util.TextRegion 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());
}
Aggregations