org.eclipse.xtext.generator.trace.ILocationInResource Java Examples

The following examples show how to use org.eclipse.xtext.generator.trace.ILocationInResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassFileBasedOpenerContributor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean collectSourceFileOpeners(IEditorPart editor, IAcceptor<FileOpener> acceptor) {
	if (!(editor instanceof XtextEditor) && editor.getEditorInput() != null) {
		try {
			IClassFile classFile = Adapters.adapt(editor, IClassFile.class);
			if (classFile == null) {
				return false;
			}
			ITrace trace = traceForTypeRootProvider.getTraceToSource(classFile);
			if (trace == null) {
				return false;
			}
			for (ILocationInResource location : trace.getAllAssociatedLocations()) {
				String name = location.getAbsoluteResourceURI().getURI().lastSegment();
				IEditorDescriptor editorDescriptor = IDE.getEditorDescriptor(name);
				acceptor.accept(createEditorOpener(editor.getEditorInput(), editorDescriptor.getId()));
				return true;
			}
		} catch (PartInitException e) {
			LOG.error(e.getMessage(), e);
		}
	}
	return false;
}
 
Example #2
Source File: JvmOutlineNodeElementOpener.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void openEObject(EObject state) {
	try {
		if (state instanceof JvmIdentifiableElement && state.eResource() instanceof TypeResource) {
			IJavaElement javaElement = javaElementFinder.findElementFor((JvmIdentifiableElement) state);
			if (javaElement instanceof IMember) {
				IResource resource = javaElement.getResource();
				if (resource instanceof IStorage) {
					ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource);
					if (traceToSource != null) {
						ISourceRange sourceRange = ((IMember) javaElement).getSourceRange();
						ILocationInResource sourceInformation = traceToSource.getBestAssociatedLocation(new TextRegion(sourceRange.getOffset(), sourceRange.getLength()));
						if (sourceInformation != null) {
							globalURIEditorOpener.open(sourceInformation.getAbsoluteResourceURI().getURI(), javaElement, true);
							return;
						}
					}
				}
				globalURIEditorOpener.open(null, javaElement, true);
				return;
			}
		}
	} catch (Exception exc) {
		LOG.error("Error opening java editor", exc);
	}
	super.openEObject(state);
}
 
Example #3
Source File: XbaseResourceForEditorInputFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected URI getClassFileSourceURI(IClassFile classFile) {
	ITrace traceToSource = typeForTypeRootProvider.getTraceToSource(classFile);
	if (traceToSource != null) {
		for (ILocationInResource loc : traceToSource.getAllAssociatedLocations())
			return loc.getAbsoluteResourceURI().getURI();
	}
	return null;
}
 
Example #4
Source File: XbaseEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected ILocationInResource getLocationInResource(ITrace traceToSource) {
	if (traceToSource == null) {
		return null;
	}
	for (ILocationInResource locationInResource : traceToSource.getAllAssociatedLocations()) {
		return locationInResource;
	}
	return null;
}
 
Example #5
Source File: XbaseBreakpointUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public SourceRelativeURI getBreakpointURI(IEditorInput input) {
	IResource resource = Adapters.adapt(input, IResource.class);
	if (resource != null)
		return null;
	if (input instanceof IStorageEditorInput) {
		IStorage storage;
		try {
			storage = ((IStorageEditorInput) input).getStorage();
			if (storage instanceof IResource)
				return null;
			if (storage instanceof IJarEntryResource) {
				IJarEntryResource jarEntryResource = (IJarEntryResource) storage;
				if (!jarEntryResource.getPackageFragmentRoot().isArchive())
					return null;
				Object parent = jarEntryResource.getParent();
				if (parent instanceof IPackageFragment) {
					String path = ((IPackageFragment) parent).getElementName().replace('.', '/');
					return new SourceRelativeURI(path + "/" + storage.getName());
				} else if (parent instanceof IPackageFragmentRoot) {
					return new SourceRelativeURI(storage.getName());
				}
			}
		} catch (CoreException e) {
			logger.error("Error finding breakpoint URI", e);
			return null;
		}
	} else {
		IClassFile classFile = Adapters.adapt(input, IClassFile.class);
		if (classFile != null) {
			ITrace traceToSource = traceForTypeRootProvider.getTraceToSource(classFile);
			if (traceToSource == null)
				return null;
			for (ILocationInResource loc : traceToSource.getAllAssociatedLocations())
				return loc.getSrcRelativeResourceURI();
			return null;
		}
	}
	return null;
}
 
Example #6
Source File: LinkToOriginProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public LinkToOrigin createLinkToOrigin(ILocationInResource source, IRegion selectedWord, IMember selectedMember, ICompilationUnit compilationUnitOfSelection, List<LinkToOrigin> alreadyCreatedLinks) {
	LinkToOrigin hyperlink = hyperlinkProvider.get();
	hyperlink.setHyperlinkRegion(new Region(selectedWord.getOffset(), selectedWord.getLength()));
	hyperlink.setURI(source.getAbsoluteResourceURI().getURI());
	hyperlink.setMember(selectedMember);
	hyperlink.setHyperlinkText("Open Original Declaration");
	hyperlink.setTypeLabel("Navigate to source artifact");
	return hyperlink;
}
 
Example #7
Source File: XtendTraceTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #8
Source File: CompilerTraceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertTrace(final String javaCodeWithMarker, String xbaseCodeWithMarker) throws Exception {
	xbaseCodeWithMarker = " " + xbaseCodeWithMarker + " ";
	Matcher xbaseMatcher = p.matcher(xbaseCodeWithMarker);
	assertTrue(xbaseMatcher.matches());
	String xbaseGroup1 = xbaseMatcher.group(1);
	String xbaseGroup2 = xbaseMatcher.group(2);
	String xbaseGroup3 = xbaseMatcher.group(3);
	String actualCode = xbaseGroup1 + xbaseGroup2 + xbaseGroup3; 
	XExpression model = expression(actualCode,true);
	TreeAppendable appendable = new TreeAppendable(new ImportManager(true), converter, locationProvider, jvmModelAssociations, model, "  ", "\n");
	XbaseCompiler compiler = get(XbaseCompiler.class);
	LightweightTypeReference returnType = typeResolver.resolveTypes(model).getReturnType(model);
	if (returnType == null) {
		throw new IllegalStateException();
	}
	compiler.compile(model, appendable, returnType);
	String compiledJavaCode = appendable.getContent();
	Matcher javaMatcher = p.matcher(javaCodeWithMarker);
	assertTrue(javaMatcher.matches());
	String javaGroup1 = javaMatcher.group(1);
	String javaGroup2 = javaMatcher.group(2);
	String javaGroup3 = javaMatcher.group(3);
	String actualExpectation = javaGroup1 + javaGroup2 + javaGroup3;
	assertEquals(actualExpectation, compiledJavaCode);
	ITrace trace = new SimpleTrace(appendable.getTraceRegion());
	ILocationInResource location = trace.getBestAssociatedLocation(new TextRegion(javaGroup1.length(), javaGroup2.length()));
	if (location == null) {
		throw new IllegalStateException("location may not be null");
	}
	assertEquals(new TextRegion(xbaseGroup1.length(), xbaseGroup2.length()), location.getTextRegion());
}
 
Example #9
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ILocationInResource getBestAssociatedLocation(ITextRegion region) {
	AbstractTraceRegion right = findTraceRegionAtRightOffset(region.getOffset() + region.getLength());
	if (right != null && encloses(right, region.getOffset(), true)) {
		if (right.getMyOffset() + right.getMyLength() == region.getOffset() + region.getLength())
			return getMergedLocationInResource(right);
	}
	
	AbstractTraceRegion left = findTraceRegionAtLeftOffset(region.getOffset());
	return mergeRegions(left, right);
}
 
Example #10
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Iterable<? extends ILocationInResource> toLocations(final Iterable<AbstractTraceRegion> allTraceRegions) {
	return new Iterable<ILocationInResource>() {

		@Override
		public Iterator<ILocationInResource> iterator() {
			return new AbstractIterator<ILocationInResource>() {

				private Iterator<AbstractTraceRegion> delegate = allTraceRegions.iterator();
				private AbstractTraceRegion region;
				private Iterator<ILocationData> locationDelegate;
				
				@Override
				protected ILocationInResource computeNext() {
					while(true) {
						if (locationDelegate == null || !locationDelegate.hasNext()) {
							if (delegate.hasNext()) {
								region = delegate.next();
								locationDelegate = region.getAssociatedLocations().iterator();
								if (!locationDelegate.hasNext()) {
									continue;
								}
							}
						}
						if (locationDelegate != null && locationDelegate.hasNext()) {
							ILocationData locationData = locationDelegate.next();
							ILocationInResource result = createLocationInResourceFor(locationData, region);
							if (result != null) {
								return result;
							}
							continue;
						}
						return endOfData();
					}
				}
			};
		}
		
	};
}
 
Example #11
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a new location for a target resource that matches the given {@code location}.
 * @param location the location
 * @return the location in resource, <code>null</code> detecting a path fails.
 */
protected ILocationInResource createLocationInResourceFor(ILocationData location, AbstractTraceRegion traceRegion) {
	SourceRelativeURI path = location.getSrcRelativePath();
	if (path == null)
		path = traceRegion.getAssociatedSrcRelativePath();
	if (path == null)
		return null;
	return createLocationInResource(location, path);
}
 
Example #12
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ILocationInResource getBestAssociatedLocation(ITextRegion localRegion, AbsoluteURI uri) {
	IProjectConfig projectConfig = getLocalProjectConfig();
	AbstractTraceRegion left = findTraceRegionAtLeftOffset(localRegion.getOffset());
	left = findParentByURI(left, uri, projectConfig);
	AbstractTraceRegion right = findTraceRegionAtRightOffset(localRegion.getOffset() + localRegion.getLength());
	right = findParentByURI(right, uri, projectConfig);
	return mergeRegions(left, right);
}
 
Example #13
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends ILocationInResource> getAllAssociatedLocations() {
	Iterable<AbstractTraceRegion> allTraceRegions = getAllTraceRegions();
	return toLocations(allTraceRegions);		
}
 
Example #14
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends ILocationInResource> getAllAssociatedLocations(AbsoluteURI uri) {
	final Iterable<AbstractTraceRegion> allTraceRegions = getAllTraceRegions();
	Iterable<AbstractTraceRegion> filteredByURI = new TraceRegionsByURI(allTraceRegions, uri, getLocalProjectConfig());
	return toLocations(filteredByURI);
}
 
Example #15
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends ILocationInResource> getAllAssociatedLocations(ITextRegion localRegion, AbsoluteURI uri) {
	final Iterable<AbstractTraceRegion> allTraceRegions = getAllTraceRegions(localRegion);
	Iterable<AbstractTraceRegion> filteredByURI = new TraceRegionsByURI(allTraceRegions, uri, getLocalProjectConfig());
	return toLocations(filteredByURI);
}
 
Example #16
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends ILocationInResource> getAllAssociatedLocations(ITextRegion localRegion) {
	Iterable<AbstractTraceRegion> allTraceRegions = getAllTraceRegions(localRegion);
	return toLocations(allTraceRegions);
}
 
Example #17
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ILocationInResource getMergedLocationInResource(AbstractTraceRegion region) {
	ILocationData locationData = region.getMergedAssociatedLocation();
	if (locationData != null)
		return createLocationInResourceFor(locationData, region);
	return null;
}
 
Example #18
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ILocationInResource createLocationInResource(ITextRegionWithLineInformation region, SourceRelativeURI srcRelativePath) {
	return new LocationInResource(region.getOffset(), region.getLength(), region.getLineNumber(), region.getEndLineNumber(), srcRelativePath, this);
}
 
Example #19
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ILocationInResource mergeRegions(AbstractTraceRegion left, AbstractTraceRegion right) {
	if (left == null) {
		if (right != null) {
			return getMergedLocationInResource(right);
		}
		return null;
	}
	if (right == null || left.equals(right)) {
		return getMergedLocationInResource(left);
	} else {
		SourceRelativeURI leftToPath = left.getAssociatedSrcRelativePath();
		SourceRelativeURI rightToPath = right.getAssociatedSrcRelativePath();
		if (ObjectExtensions.operator_equals(leftToPath, rightToPath)) {
			ITextRegionWithLineInformation leftRegion = left.getMyRegion();
			ITextRegionWithLineInformation rightRegion = right.getMyRegion();
			if (leftRegion.contains(rightRegion)) {
				return getMergedLocationInResource(left);
			} else if (rightRegion.contains(leftRegion)) {
				return getMergedLocationInResource(right);
			} else {
				AbstractTraceRegion parent = left.getParent();
				AbstractTraceRegion leftChild = left;
				while(parent != null) {
					if (parent.getMyRegion().contains(rightRegion)) {
						break;
					}
					leftChild = parent;
					parent = parent.getParent();
				}
				if (parent != null) {
					AbstractTraceRegion rightChild = right;
					while(!parent.equals(rightChild.getParent())) {
						rightChild = rightChild.getParent();
						if (rightChild == null) {
							return getMergedLocationInResource(leftChild);
						}
					}
					SourceRelativeURI path = leftToPath;
					if (path == null) {
						path = leftChild.getAssociatedSrcRelativePath();
					}
					ITextRegionWithLineInformation merged = parent.getMergedAssociatedLocation();
					if (merged != null) {
						return createLocationInResource(merged, path);
					}
				}
			}
		}
	} 
	// TODO the remaining cases have yet to be implemented
	return null;
}
 
Example #20
Source File: CompilerTraceTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public void tracesTo(final CharSequence xtend, final CharSequence java) {
  try {
    final String xtendWithSpaces = ((" " + xtend) + " ");
    final Matcher xtendMatcher = this.p.matcher(xtendWithSpaces);
    Assert.assertTrue("xtendMatcher.matches", xtendMatcher.matches());
    final String xtendGroup1 = xtendMatcher.group(1);
    final String xtendGroup2 = xtendMatcher.group(2);
    final String xtendGroup3 = xtendMatcher.group(3);
    final String actualXtendCode = ((xtendGroup1 + xtendGroup2) + xtendGroup3);
    final XtendFile file = this.file(actualXtendCode, true);
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
    final JvmGenericType inferredType = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
    CharSequence compiledCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
    compiledCode = this.postProcessor.postProcess(null, compiledCode);
    final Matcher javaMatcher = this.p.matcher(java.toString());
    Assert.assertTrue("javaMatcher.matches", javaMatcher.matches());
    final String javaGroup1 = javaMatcher.group(1);
    final String javaGroup2 = javaMatcher.group(2);
    final String javaGroup3 = javaMatcher.group(3);
    final String actualJavaExpectation = ((javaGroup1 + javaGroup2) + javaGroup3);
    Assert.assertEquals(actualJavaExpectation, compiledCode.toString());
    AbstractTraceRegion _traceRegion = ((ITraceRegionProvider) compiledCode).getTraceRegion();
    URI _createURI = URI.createURI(file.eResource().getURI().path());
    SourceRelativeURI _sourceRelativeURI = new SourceRelativeURI(_createURI);
    URI _createURI_1 = URI.createURI(file.eResource().getURI().path());
    SourceRelativeURI _sourceRelativeURI_1 = new SourceRelativeURI(_createURI_1);
    AbstractTraceRegion _merge = this.merge(_traceRegion.invertFor(_sourceRelativeURI, _sourceRelativeURI_1));
    final SimpleTrace trace = new SimpleTrace(_merge);
    int _length = xtendGroup1.length();
    int _length_1 = xtendGroup2.length();
    TextRegion _textRegion = new TextRegion(_length, _length_1);
    final Iterable<? extends ILocationInResource> locations = trace.getAllAssociatedLocations(_textRegion);
    int _length_2 = javaGroup1.length();
    int _length_3 = javaGroup2.length();
    final TextRegion expectedRegion = new TextRegion(_length_2, _length_3);
    Assert.assertFalse(IterableExtensions.isEmpty(locations));
    for (final ILocationInResource location : locations) {
      ITextRegionWithLineInformation _textRegion_1 = location.getTextRegion();
      boolean _equals = Objects.equal(_textRegion_1, expectedRegion);
      if (_equals) {
        return;
      }
    }
    Assert.fail(((("locations did not match expectation: " + locations) + " / ") + expectedRegion));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}