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

The following examples show how to use org.eclipse.xtext.generator.trace.ITraceRegionProvider. 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: AbstractTraceForURIProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public Trace getTraceToSource(final SomeFile generatedFile) {
	final PersistedTrace persistedTrace = findPersistedTrace(generatedFile);
	if (persistedTrace == null || !persistedTrace.exists()) {
		return null;
	}
	Trace result = newAbstractTrace(generatedFile);
	result.setTraceToSource(true);
	result.setTraceRegionProvider(new ITraceRegionProvider() {
		
		@Override
		public AbstractTraceRegion getTraceRegion() {
			AbstractTraceRegion traceRegion = cachedTraces.getTraceRegion(persistedTrace);
			if (traceRegion == null) {
				throw new TraceNotFoundException();
			}
			return traceRegion;
		}
	});
	return result;
}
 
Example #2
Source File: TraceEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void updateText(EObject obj) {
	DebugTraceRegion region = EcoreUtil2.getContainerOfType(obj, DebugTraceRegion.class);
	if (region == null) {
		text.setText("No " + DebugTraceRegion.class.getSimpleName() + " found for " + obj.eClass().getName());
		return;
	}
	final DebugTraceBasedRegion trace = new DebugTraceBasedRegion(null, region);
	SourceRelativeURI sourceURI = trace.getAssociatedSrcRelativePath();
	if (sourceURI == null) {
		text.setText("Could not find associated URI");
		return;
	}
	Registry registry = IResourceServiceProvider.Registry.INSTANCE;
	IResourceServiceProvider serviceProvider = registry.getResourceServiceProvider(sourceURI.getURI());
	URI traceURI = obj.eResource().getURI();
	IStorage localStorage = getLocalStorage(serviceProvider, traceURI);

	StorageAwareTrace traceProvider = serviceProvider.get(StorageAwareTrace.class);
	traceProvider.setLocalStorage(localStorage);
	traceProvider.setTraceToSource(true);
	traceProvider.setTraceRegionProvider(new ITraceRegionProvider() {
		@Override
		public AbstractTraceRegion getTraceRegion() throws TraceNotFoundException {
			return trace;
		}
	});
	String newText = traceProvider.toString();
	text.setText(newText);
}
 
Example #3
Source File: LineNumberMappingTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractTraceRegion getTraceRegion(final CharSequence xtendCode) {
  try {
    final XtendClass clazz = super.clazz(xtendCode.toString());
    final TraceRegionFSAForTest fsa = new TraceRegionFSAForTest();
    this.generator.doGenerate(clazz.eResource(), fsa);
    final AbstractTraceRegion result = ((ITraceRegionProvider) fsa.charSequence).getTraceRegion();
    this.print(result, fsa.charSequence.toString());
    return result;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #4
Source File: LineSeparatorHarmonizer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public CharSequence postProcess(URI fileURI, CharSequence content) {
	// if the content is trace region aware, we should not simply treat it as a string.
	if (content instanceof ITraceRegionProvider) {
		return content;
	}
	String lineSeparator = whitespaceInformationProvider.getLineSeparatorInformation(fileURI).getLineSeparator();
	return replaceLineSeparators(content, lineSeparator);
}
 
Example #5
Source File: EclipseResourceFileSystemAccess2.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @throws CoreException
 *             if something unexpected happens during resource access
 * @throws IOException
 *             if serialization of the trace data fails
 * @since 2.3
 */
protected void updateTraceInformation(IFile traceFile, CharSequence contents, boolean derived)
		throws CoreException, IOException {
	if (contents instanceof ITraceRegionProvider) {
		try {
			AbstractTraceRegion traceRegion = ((ITraceRegionProvider) contents).getTraceRegion();
			if (sourceTraces == null) {
				sourceTraces = HashMultimap.create();
			}
			IPath tracePath = traceFile.getFullPath();
			Iterator<AbstractTraceRegion> iterator = traceRegion.treeIterator();
			while (iterator.hasNext()) {
				AbstractTraceRegion region = iterator.next();
				for (ILocationData location : region.getAssociatedLocations()) {
					SourceRelativeURI path = location.getSrcRelativePath();
					if (path != null) {
						sourceTraces.put(path, tracePath);
					}
				}
			}
			class AccessibleOutputStream extends ByteArrayOutputStream {
				byte[] internalBuffer() {
					return buf;
				}

				int internalLength() {
					return count;
				}
			}
			AccessibleOutputStream data = new AccessibleOutputStream();
			traceSerializer.writeTraceRegionTo(traceRegion, data);
			// avoid copying the byte array
			InputStream input = new ByteArrayInputStream(data.internalBuffer(), 0, data.internalLength());
			if (traceFile.exists()) {
				traceFile.setContents(input, true, false, monitor);
			} else {
				traceFile.create(input, true, monitor);
			}
			setDerived(traceFile, derived);
			return;
		} catch (TraceNotFoundException e) {
			// ok
		}
	}
	if (traceFile.exists()) {
		traceFile.delete(IResource.FORCE, monitor);
	}
}
 
Example #6
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);
  }
}
 
Example #7
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public void setTraceRegionProvider(ITraceRegionProvider traceRegionProvider) {
	this.traceRegionProvider = traceRegionProvider;
}
 
Example #8
Source File: AbstractTrace.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ITraceRegionProvider getTraceRegionProvider() {
	return traceRegionProvider;
}