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