Search in sources :

Example 6 with TextRegion

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

the class FlexTokenRegionProviderTest method testTokenRegionContainsRegion.

@Test
public void testTokenRegionContainsRegion() throws Exception {
    String modelAsString = "a1 / /* comment */ b2";
    List<CommonToken> tokens = getTokens(modelAsString);
    for (int length = 0; length < modelAsString.length(); ++length) {
        for (int offset = 0; offset + length < modelAsString.length(); ++offset) {
            ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(modelAsString, new TextRegion(offset, length));
            // System.out.println(offset + ":" + length + " -> " + tokenRegion);
            CommonToken firstToken = findTokenStartingAt(tokenRegion.getOffset(), tokens);
            assertTrue(firstToken.getStartIndex() <= offset);
            if (tokenRegion.getLength() != 0) {
                CommonToken lastToken = findTokenStopingAt(tokenRegion.getOffset() + tokenRegion.getLength() - 1, tokens);
                assertTrue(lastToken.getStopIndex() >= offset + length - 1);
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 7 with TextRegion

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

the class FlexTokenRegionProviderTest method testTokenMerge.

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

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

the class DerivedSourceView method selectAndReveal.

@Override
protected void selectAndReveal(IWorkbenchPartSelection workbenchPartSelection) {
    if (selectedSource != null) {
        IAnnotationModel annotationModel = getSourceViewer().getAnnotationModel();
        TextRegion localRegion = mapTextRegion(workbenchPartSelection);
        IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
        if (trace != null) {
            Iterable<? extends ILocationInEclipseResource> allAssociatedLocations = trace.getAllAssociatedLocations(localRegion, selectedSource);
            ILocationInResource firstLocationInResource = Iterables.getFirst(allAssociatedLocations, null);
            if (firstLocationInResource != null) {
                ITextRegion textRegion = firstLocationInResource.getTextRegion();
                if (textRegion != null) {
                    openEditorAction.setSelectedRegion(textRegion);
                    getSourceViewer().revealRange(textRegion.getOffset(), textRegion.getLength());
                }
            }
            for (ILocationInResource locationInResource : allAssociatedLocations) {
                ITextRegion textRegion = locationInResource.getTextRegion();
                if (textRegion != null) {
                    annotationModel.addAnnotation(new Annotation(SEARCH_ANNOTATION_TYPE, true, null), new Position(textRegion.getOffset(), textRegion.getLength()));
                }
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Position(org.eclipse.jface.text.Position) ITextRegion(org.eclipse.xtext.util.ITextRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) Annotation(org.eclipse.jface.text.source.Annotation)

Example 9 with TextRegion

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

the class DerivedSourceView method mapTextRegion.

private TextRegion mapTextRegion(IWorkbenchPartSelection workbenchPartSelection) {
    ITextSelection textSelection = (ITextSelection) workbenchPartSelection.getSelection();
    TextRegion localRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
    return localRegion;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 10 with TextRegion

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

the class DerivedSourceView method computeInput.

@Override
protected String computeInput(IWorkbenchPartSelection workbenchPartSelection) {
    openEditorAction.setInputFile(null);
    openEditorAction.setSelectedRegion(null);
    IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
    if (trace != null) {
        if (workbenchPartSelection instanceof DerivedSourceSelection) {
            DerivedSourceSelection derivedSourceSelection = (DerivedSourceSelection) workbenchPartSelection;
            selectedSource = derivedSourceSelection.getStorage();
        } else {
            derivedSources = Sets.newHashSet();
            TextRegion localRegion = mapTextRegion(workbenchPartSelection);
            Iterable<IStorage> transform = Iterables.filter(transform(trace.getAllAssociatedLocations(localRegion), new Function<ILocationInEclipseResource, IStorage>() {

                @Override
                public IStorage apply(ILocationInEclipseResource input) {
                    return input.getPlatformResource();
                }
            }), Predicates.notNull());
            addAll(derivedSources, transform);
            ILocationInEclipseResource bestAssociatedLocation = trace.getBestAssociatedLocation(localRegion);
            if (bestAssociatedLocation != null) {
                selectedSource = bestAssociatedLocation.getPlatformResource();
            } else if (!derivedSources.isEmpty()) {
                selectedSource = derivedSources.iterator().next();
            }
        }
    }
    IFile file = getSelectedFile();
    if (file != null) {
        try {
            file.refreshLocal(1, new NullProgressMonitor());
            if (file.exists()) {
                openEditorAction.setInputFile(file);
                return Files.readStreamIntoString(file.getContents());
            }
        } catch (CoreException e) {
            throw new WrappedRuntimeException(e);
        }
    }
    return null;
}
Also used : ILocationInEclipseResource(org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource) Function(com.google.common.base.Function) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) CoreException(org.eclipse.core.runtime.CoreException) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) IStorage(org.eclipse.core.resources.IStorage) WrappedRuntimeException(org.eclipse.core.internal.utils.WrappedRuntimeException)

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