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

The following examples show how to use org.eclipse.xtext.generator.trace.LocationData. 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: GeneratorNodeTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testEmptyIndent() {
  final LocationData root = this.loc(0);
  CompositeGeneratorNode node = this.exts.appendNewLine(this.exts.append(this.exts.trace(root), "Hallo"));
  this.exts.indent(node);
  this.exts.appendNewLine(this.exts.append(node, "noindent"));
  IndentNode _indentNode = new IndentNode("  ", true, true);
  this.exts.append(node, _indentNode);
  this.exts.appendNewLine(this.exts.append(node, "noindent"));
  final GeneratorNodeProcessor processor = new GeneratorNodeProcessor();
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("Hallo");
  _builder.newLine();
  _builder.append("noindent");
  _builder.newLine();
  _builder.append("noindent");
  _builder.newLine();
  this.assertEquals(_builder.toString(), processor.process(node).toString());
}
 
Example #2
Source File: GeneratorNodePerformanceTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testBasicCreationAndProcessingPerformance() {
	LocationData root = loc(0);
	CompositeGeneratorNode node = exts.appendNewLine(exts.append(exts.trace(root), "notindented"));
	for (int i = 0; i < GeneratorNodePerformanceTest.numberOfLines; i++) {
		exts.appendNewLine(exts.append(
				exts.indent(exts.appendNewLine(exts.append(exts.trace(exts.indent(node), loc(1)), "indented1"))),
				"indented2"));
	}
	exts.append(exts.appendNewLine(node), "dedented");
	GeneratorNodeProcessor processor = new GeneratorNodeProcessor();
	GeneratorNodeProcessor.Result result = processor.process(node);
	String resultAsString = null;
	if (result != null) {
		resultAsString = result.toString();
	}
	Assert.assertFalse(Strings.isNullOrEmpty(resultAsString));
}
 
Example #3
Source File: AppendableBasedTraceRegion.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private List<ILocationData> copyLocationData(TreeAppendable delegate, boolean useOriginalLocations) {
	if (useOriginalLocations) {
		return Lists.newArrayList(delegate.getLocationData());
	} else {
		List<ILocationData> result = Lists.newArrayList();
		for(ILocationData locationData: delegate.getLocationData()) {
			result.add(new LocationData(locationData, null));
		}
		return result;
	}
}
 
Example #4
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void generateDynamicTests(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<DynamicValidationComponent> specificComponents) {
	int i = 0;
	for (final DynamicValidationComponent component : specificComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_4,
				inputFile.getName(), component.functionName() + i));
		final String actionName = toActionName("dyn_" + component.functionName(), component, i);
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_10, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"other\")").newLine();
		it.append("@").append(Tag.class).append("(\"other_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		component.generateValidationCode(it);
		it.decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example #5
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void generateTestsForFailureCode(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<ValidationComponent> failureCompilationComponents) {
	int i = 0;
	for (final ValidationComponent component : failureCompilationComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_2,
				inputFile.getName(), component.getLinenoInSourceFile(), component.getCode()));
		final String actionName = toActionName("failure", component, i); //$NON-NLS-1$
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_8, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"failure\")").newLine();
		it.append("@").append(Tag.class).append("(\"failure_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		it.append(List.class).append("<String> issues = getScriptExecutor().compile(")
			.append(str(component.getLinenoInSourceFile())).append(", \"")
			.append(str(component.getCode())).append("\");").newLine();
		it.append("assertIssues(").append(str(component.getLinenoInSourceFile()))
			.append(", issues);").decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example #6
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void generateTestsForSuccessCode(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<ValidationComponent> successCompilationComponents) {
	int i = 0;
	for (final ValidationComponent component : successCompilationComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_1,
				inputFile.getName(), component.getLinenoInSourceFile(), component.getCode()));
		final String actionName = toActionName("success", component, i); //$NON-NLS-1$
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_7, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"success\")").newLine();
		it.append("@").append(Tag.class).append("(\"success_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		it.append(List.class).append("<String> issues = getScriptExecutor().compile(")
			.append(str(component.getLinenoInSourceFile())).append(", \"")
			.append(str(component.getCode())).append("\");").newLine();
		it.append("assertNoIssue(").append(str(component.getLinenoInSourceFile()))
			.append(", issues);").decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example #7
Source File: GeneratorNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBasicCreationAndProcessing() {
  final LocationData root = this.loc(0);
  CompositeGeneratorNode node = this.exts.appendNewLine(this.exts.append(this.exts.trace(root), "notindented"));
  this.exts.append(this.exts.appendNewLine(this.exts.append(this.exts.trace(this.exts.indent(node), this.loc(1)), "indented1")), "indented2");
  this.exts.append(this.exts.appendNewLine(node), "dedented");
  final GeneratorNodeProcessor processor = new GeneratorNodeProcessor();
  final GeneratorNodeProcessor.Result result = processor.process(node);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("notindented");
  _builder.newLine();
  _builder.append("  ");
  _builder.append("indented1");
  _builder.newLine();
  _builder.append("  ");
  _builder.append("indented2");
  _builder.newLine();
  _builder.append("dedented");
  this.assertEquals(_builder.toString(), result.toString());
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("CompletableTraceRegion [myOffset=0, myLength=44, useForDebugging=false] associations={");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("LocationData [TextRegionWithLineInformation [0:100][lineNumber=0, endLineNumber=0]][path=foo/mymodel.dsl]");
  _builder_1.newLine();
  _builder_1.append("} nestedRegions={");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("CompletableTraceRegion [myOffset=14, myLength=21, useForDebugging=false] associations={");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("LocationData [TextRegionWithLineInformation [1:99][lineNumber=0, endLineNumber=0]][path=foo/mymodel.dsl]");
  _builder_1.newLine();
  _builder_1.append("  ");
  _builder_1.append("}");
  _builder_1.newLine();
  _builder_1.append("}");
  this.assertEquals(_builder_1.toString(), result.getTraceRegion().toString());
}
 
Example #8
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable generateDocumentation(final String text, final List<INode> documentationNodes, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/**");
    final StringConcatenation doc = ((StringConcatenation) _builder);
    doc.newLine();
    doc.append(" * ");
    doc.append(text, " * ");
    doc.newLine();
    doc.append(" */");
    ITreeAppendable _xifexpression = null;
    boolean _isEmpty = documentationNodes.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      ITreeAppendable _xblockexpression_1 = null;
      {
        ITextRegionWithLineInformation documentationTrace = ITextRegionWithLineInformation.EMPTY_REGION;
        for (final INode node : documentationNodes) {
          documentationTrace = documentationTrace.merge(node.getTextRegionWithLineInformation());
        }
        LocationData _locationData = new LocationData(documentationTrace, null);
        appendable.trace(_locationData).append(doc.toString());
        _xblockexpression_1 = appendable.newLine();
      }
      _xifexpression = _xblockexpression_1;
    } else {
      _xifexpression = appendable.append(doc.toString()).newLine();
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example #9
Source File: TreeAppendableUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public ITreeAppendable traceSignificant(ITreeAppendable appendable, EObject source, boolean useForDebugging) {
	if (appendable instanceof TreeAppendable) {
		return ((TreeAppendable) appendable).trace(source,
				ILocationInFileProviderExtension.RegionDescription.SIGNIFICANT, useForDebugging);
	} else {
		ITextRegionWithLineInformation it = (ITextRegionWithLineInformation) locationProvider
				.getSignificantTextRegion(source);
		if (it != null && it != ITextRegion.EMPTY_REGION) {
			return appendable.trace(new LocationData(it.getOffset(), it.getLength(), it.getLineNumber(),
					it.getEndLineNumber(), null), useForDebugging);
		} else {
			return appendable;
		}
	}
}
 
Example #10
Source File: TreeAppendable.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected static ILocationData createLocationData(ITraceURIConverter converter, EObject object, ITextRegionWithLineInformation textRegion) {
	Resource resource = object.eResource();
	SourceRelativeURI uriForTrace = null;
	if (converter != null) {
		uriForTrace = converter.getURIForTrace(resource);
	}
	ILocationData newData = new LocationData(textRegion, uriForTrace);
	return newData;
}
 
Example #11
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ILocationData toLocationData(List<INode> nodes) {
	ITextRegionWithLineInformation result = ITextRegionWithLineInformation.EMPTY_REGION;
	for (INode node : nodes) {
		if (!isHidden(node)) {
			ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
			if (region.getLength() != 0) {
				result = result.merge(new TextRegionWithLineInformation(region.getOffset(), region.getLength(), region.getLineNumber() - 1, region.getEndLineNumber() - 1));
			}
		}
	}
	if (result.getLength() == 0)
		return null;
	return new LocationData(result.getOffset(), result.getLength(), result.getLineNumber(),
			result.getEndLineNumber(), null);
}
 
Example #12
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void setCurrentAppendable(/* @Nullable */ RichStringLiteral origin) {
	if (currentAppendable == null && origin != null) {
		ITextRegionWithLineInformation region = (ITextRegionWithLineInformation) getLocationInFileProvider().getSignificantTextRegion(origin, XbasePackage.Literals.XSTRING_LITERAL__VALUE, 0);
		currentAppendable = appendable.trace(new LocationData(region, null), true);
	}
}
 
Example #13
Source File: TracingSugar.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @return ILocationData covering the <code>fullTextRegion</code> of the given EObject.
 */
public ILocationData location(EObject obj) {
	ITextRegion region = locationProvider.getFullTextRegion(obj);
	SourceRelativeURI uri = traceURIConverter.getURIForTrace(obj.eResource());
	return new LocationData((ITextRegionWithLineInformation) region, uri);
}
 
Example #14
Source File: RootTraceRegionForTesting.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public RootTraceRegionForTesting(ITextRegionWithLineInformation myLocation, ITextRegionWithLineInformation association) {
	super(myLocation, true, new LocationData(association, new SourceRelativeURI("uri")), null);
}
 
Example #15
Source File: GeneratorNodePerformanceTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private LocationData loc(int idx) {
	return new LocationData(idx, 100 - idx, 0, 0, new SourceRelativeURI("foo/mymodel.dsl"));
}
 
Example #16
Source File: DebugTraceBasedRegion.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
private LocationData convert(DebugLocationData data) {
	SourceRelativeURI uri = data.getPath() != null ? new SourceRelativeURI(data.getPath()) : null;
	return new LocationData(data.getOffset(), data.getLength(), data.getLineNumber(), data.getEndLineNumber(), uri);
}
 
Example #17
Source File: GeneratorNodeTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private LocationData loc(final int idx) {
  SourceRelativeURI _sourceRelativeURI = new SourceRelativeURI("foo/mymodel.dsl");
  return new LocationData(idx, (100 - idx), 0, 0, _sourceRelativeURI);
}
 
Example #18
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 4 votes vote down vote up
private void generateTestsForFacts(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<ValidationComponent> factualComponents) {
	int i = 0;
	for (final ValidationComponent component : factualComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_3,
				inputFile.getName(), component.getLinenoInSourceFile(), component.getCode()));
		final String actionName = toActionName("fact", component, i); //$NON-NLS-1$
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_9, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"fact\")").newLine();
		it.append("@").append(Tag.class).append("(\"fact_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		it.append("final String expected = ").append(Utils.class).append(".dump(")
			.append(Boolean.class).append(".TRUE, false) + \"\\nOR\\nObject {\\n}\\n\";").newLine();
		it.append("Object result;").newLine();
		it.append("try {").increaseIndentation().newLine();
		it.append("result = getScriptExecutor().execute(").append(str(component.getLinenoInSourceFile()))
			.append(", \"").append(str(component.getCode())).append("\");").decreaseIndentation().newLine();
		it.append("} catch (").append(Throwable.class).append(" exception) {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(exception.getLocalizedMessage() + \" [line: ")
			.append(str(component.getLinenoInSourceFile())).append("]\", expected, ")
			.append(Throwables.class).append(".getStackTraceAsString(exception));").decreaseIndentation().newLine();
		it.append("}").newLine();
		it.append("if (result instanceof ").append(Boolean.class).append(") {").increaseIndentation().newLine();
		it.append("boolean boolResult = ((").append(Boolean.class).append(") result).booleanValue();").newLine();
		it.append("if (!boolResult) {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(\"Invalid expression result [line: ").append(str(component.getLinenoInSourceFile()))
			.append("]\", expected, ").append(Utils.class).append(".dump(result, false));").decreaseIndentation().newLine();
		it.append("}").decreaseIndentation().newLine();
		it.append("} else if (result == null || result instanceof ").append(Exception.class)
			.append(") {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(\"Invalid expression result [line: ").append(str(component.getLinenoInSourceFile()))
			.append("]\", expected, ").append(Utils.class).append(".dump(result, false));").decreaseIndentation().newLine();
		it.append("}").decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example #19
Source File: TracingSugar.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * @param obj
 *            the EObject containing the feature
 * @param feature
 *            the EStructuralFeature to trace
 * @param idx
 *            the index of the value to trace, in case the feature contains a list, should be <code>-1</code>
 *            otherwise.
 * 
 * @return ILocationData covering the <code>fullTextRegion</code> of the given feature in the given EObject.
 */
public ILocationData location(EObject obj, EStructuralFeature feature, int idx) {
	ITextRegion region = locationProvider.getFullTextRegion(obj, feature, idx);
	SourceRelativeURI uri = traceURIConverter.getURIForTrace(obj.eResource());
	return new LocationData((ITextRegionWithLineInformation) region, uri);
}