Java Code Examples for org.eclipse.xtext.generator.trace.ITrace#getBestAssociatedLocation()

The following examples show how to use org.eclipse.xtext.generator.trace.ITrace#getBestAssociatedLocation() . 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: XtendTraceTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@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));
}
 
Example 2
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 3
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);
}