eclipse-xtext / eclipse-xtext/xtext

Bad behaviour of AbstractTraceRegion.getAssociatedSrcRelativePath()

Open
#2,558 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug confirmed
Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Description

we have a grammar like

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	"model" name=ID;

and a inferrer like

class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
 		acceptor.accept(element.toClass(element.name)) [
		]
 		acceptor.accept(element.toClass(element.name+"Impl")) [
		]
	}
}

and do (more or less) something like the following handler code

package org.xtext.example.mydsl.ui.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.xtext.generator.trace.AbsoluteURI;
import org.eclipse.xtext.ui.generator.trace.IEclipseTrace;
import org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource;
import org.eclipse.xtext.ui.generator.trace.ITraceForStorageProvider;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.eclipse.xtext.util.TextRegion;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;

@SuppressWarnings("restriction")
public class GenerationHandler extends AbstractHandler {

	@Inject
	private IResourceSetProvider resourceSetProvider;

	@Inject
	private ITraceForStorageProvider traceProvider;

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			Object firstElement = structuredSelection.getFirstElement();
			if (firstElement instanceof IFile) {
				IFile file = (IFile) firstElement;
				IProject project = file.getProject();
				URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
				ResourceSet rs = resourceSetProvider.get(project);
				Resource r = rs.getResource(uri, true);
				IEclipseTrace traceToTarget = traceProvider.getTraceToTarget(file);
				String uriAsString = uri.toString();
				uriAsString = uriAsString.replaceAll("/src/", "/src-gen/");
				uriAsString = uriAsString.replaceAll("\\.mydsl", ".java");
				Iterable<? extends ILocationInEclipseResource> allAssociatedLocations = traceToTarget.getAllAssociatedLocations(new TextRegion(7, 2), new AbsoluteURI(uriAsString) );
				System.out.println(Iterables.size(allAssociatedLocations));
			}
		}
		return null;
	}

}

although there are locations there

org.eclipse.xtext.generator.trace.AbstractTraceRegion.getAssociatedSrcRelativePath()

fails cause there are multiple locations with different targets

TraceRegion [myOffset=6, myLength=4, useForDebugging=true] associations={
  LocationData [TextRegionWithLineInformation [38:8][lineNumber=1, endLineNumber=1]][path=demoImpl.java]
  LocationData [TextRegionWithLineInformation [38:4][lineNumber=1, endLineNumber=1]][path=demo.java]
}

and it sorts these out.

StorageAwareTrace(AbstractTrace).isAssociatedWith(AbstractTraceRegion, AbsoluteURI, IProjectConfig) line: 321	
StorageAwareTrace(AbstractTrace).findParentByURI(AbstractTraceRegion, AbsoluteURI, IProjectConfig) line: 325	
AbstractTrace$TraceRegionsByURI$1.apply(AbstractTraceRegion) line: 68	
AbstractTrace$TraceRegionsByURI$1.apply(Object) line: 65	
Iterators$7.transform(F) line: 750	
Iterators$7(TransformedIterator<F,T>).next() line: 47	
Iterators$6.computeNext() line: 616	
Iterators$6(AbstractIterator<T>).tryToComputeNext() line: 145	
Iterators$6(AbstractIterator<T>).hasNext() line: 140	
AbstractTrace$1$1.computeNext() line: 376	
AbstractTrace$1$1.computeNext() line: 366	
AbstractTrace$1$1(AbstractIterator<T>).tryToComputeNext() line: 145	
AbstractTrace$1$1(AbstractIterator<T>).hasNext() line: 140	
Iterators.size(Iterator<?>) line: 197	
Iterables.size(Iterable<?>) line: 127	
GenerationHandler.execute(ExecutionEvent) line: 50	

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with org.eclipse.xtext.generator.trace.AbstractTraceRegion.getAssociatedSrcRelativePath(), then follow the calls through StorageAwareTrace and the GenerationHandler reproduction in the issue. Inspect how the trace handles the two associated locations, demoImpl.java and demo.java, and verify the resulting behavior with the provided handler and trace data.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.