Search in sources :

Example 1 with TextRegion

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);
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 2 with TextRegion

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());
}
Also used : IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) Test(org.junit.Test)

Example 3 with TextRegion

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));
}
Also used : IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) ITrace(org.eclipse.xtext.generator.trace.ITrace) Test(org.junit.Test)

Example 4 with TextRegion

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);
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ImportsAcceptor(org.eclipse.xtext.xbase.imports.ImportsAcceptor) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 5 with TextRegion

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());
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

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