Java Code Examples for org.eclipse.xtext.generator.trace.AbstractTraceRegion#getAssociatedSrcRelativePath()

The following examples show how to use org.eclipse.xtext.generator.trace.AbstractTraceRegion#getAssociatedSrcRelativePath() . 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: 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 2
Source File: AppendableBasedTraceRegion.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public AppendableBasedTraceRegion(/* @Nullable */ AbstractTraceRegion parent, TreeAppendable delegate, int offset, int lineNumber) {
	super(parent);
	this.offset = offset;
	this.lineNumber = lineNumber;
	this.useForDebugging = delegate.isUseForDebugging();
	boolean useLocationsFromDelegate = true;
	if (parent != null) {
		SourceRelativeURI parentPath = parent.getAssociatedSrcRelativePath();
		if (parentPath != null) {
			useLocationsFromDelegate = !allLocationsMatch(delegate, parentPath);
		}
	}
	this.locations = copyLocationData(delegate, useLocationsFromDelegate);
	int length = 0;
	int line = lineNumber;
	for (Object child : delegate.getChildren()) {
		if (child instanceof TreeAppendable) {
			TreeAppendable castedChild = (TreeAppendable) child;
			if (hasVisibleChildren(castedChild)) {
				AppendableBasedTraceRegion childRegion = new AppendableBasedTraceRegion(this, castedChild, offset + length, line);
				length += childRegion.getMyLength();
				line = childRegion.getMyEndLineNumber();
			}
		} else {
			String s = child.toString();
			length += s.length();
			line += Strings.countLineBreaks(s);
		}
	}
	this.length = length;
	this.endLineNumber = line;
	if (parent == null) {
		this.generatedText = delegate.getContent();
		compressTrace(this.generatedText);
		IParseResult result = ((XtextResource) delegate.getState().getResource()).getParseResult();
		if (result != null)
			this.sourceText = result.getRootNode().getText();
		else
			this.sourceText = null;
	} else {
		this.generatedText = null;
		this.sourceText = null;
	}
}
 
Example 3
Source File: DebugSourceInstallingCompilationParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void buildFinished(IJavaProject project) {
	StoppedTask task = Stopwatches.forTask("DebugSourceInstallingCompilationParticipant.install");
	try {
		task.start();
		super.buildFinished(project);
		if (files == null)
			return;
		for (BuildContext ctx : files) {
			try {
				IFile generatedJavaFile = ctx.getFile();

				// This may fail if there is no trace file.
				IEclipseTrace traceToSource = traceInformation.getTraceToSource(generatedJavaFile);
				if (traceToSource == null) {
					continue;
				}
				AbstractTraceRegion rootTraceRegion = findRootTraceRegion(traceToSource);
				if (rootTraceRegion == null)
					continue;

				SourceRelativeURI dslSourceFile = rootTraceRegion.getAssociatedSrcRelativePath();

				// OutputConfigurations are only available for folders targeted by Xtext's code generation.
				OutputConfiguration outputConfiguration = findOutputConfiguration(dslSourceFile, generatedJavaFile);
				if (outputConfiguration == null)
					continue;

				IJavaElement element = JavaCore.create(generatedJavaFile);
				if (element == null)
					continue;

				deleteTaskMarkers(generatedJavaFile);
				markerReflector.reflectErrorMarkerInSource(generatedJavaFile, traceToSource);

				ITraceToBytecodeInstaller installer = getInstaller(outputConfiguration);
				installer.setTrace(generatedJavaFile.getName(), rootTraceRegion);
				for (IFile javaClassFile : findGeneratedJavaClassFiles(element)) {
					InputStream contents = javaClassFile.getContents();
					try {
						byte[] byteCode = installer.installTrace(ByteStreams.toByteArray(contents));
						if (byteCode != null) {
							javaClassFile.setContents(new ByteArrayInputStream(byteCode), 0, null);
						} else {
							// we need to touch the class file to do a respin of the build
							// otherwise a needsRebuild request is ignored since no IResourceDelta is available
							javaClassFile.touch(null);
						}
					} finally {
						contents.close();
					}
				}
			} catch (Exception e) {
				String msg = "Could not process %s to install source information: %s";
				log.error(String.format(msg, ctx.getFile().getFullPath().toString(), e.getMessage()), e);
			}
		}
	} finally {
		files = null;
		task.stop();
	}
}
 
Example 4
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;
}