org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer Java Examples

The following examples show how to use org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer. 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: Serializer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String serialize(EObject obj, SaveOptions options) {
	checkNotNull(obj, "obj must not be null.");
	checkNotNull(options, "options must not be null.");
	try {
		if (formatter2Provider != null) {
			StringBuilder builder = new StringBuilder();
			serialize(obj, builder, options);
			return builder.toString();
		} else {
			TokenStringBuffer tokenStringBuffer = new TokenStringBuffer();
			serialize(obj, tokenStringBuffer, options);
			return tokenStringBuffer.toString();
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #2
Source File: DefaultNodeModelFormatter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IFormattedRegion format(ICompositeNode root, int offset, int length) {
	String indent = getIndentation(root, offset);
	TokenStringBuffer buf = new TokenStringBuffer();
	ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf);
	ITokenStream fmt;
	if (formatter instanceof IFormatterExtension) {
		EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root);
		if (semanticElement != null)
			fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false);
		else {
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406
			ITextRegion rootRegion = root.getTextRegion();
			return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText());
		}
	} else
		fmt = formatter.createFormatterStream(indent, out, false);
	try {
		ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
		return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
	} catch (IOException e) {
		// this should never happen since TokenStringBuffer doesn't throw IOEs.
		throw new RuntimeException(e);
	}
}
 
Example #3
Source File: RegionNodeModelFormatter.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IFormattedRegion format(final ICompositeNode root, final int offset, final int length) {
  final TokenStringBuffer buf = new TokenStringBuffer();
  final ITokenStream out = offset == 0 ? buf : new FilterStream(buf);
  final ITokenStream formatStream = formatter.createFormatterStream(null, out, false);
  if (!(formatStream instanceof IDelegatingTokenStream)) {
    return super.format(root, offset, length);
  }
  try {
    ITextRegion range;
    if (offset > 0) {
      int noffset = root.getText().lastIndexOf('\n', offset) + 1; // Keep the new line
      int nlength = length + (offset - noffset); // Always start in the beginning of the line
      range = nodeModelStreamer.feedTokenStream(formatStream, root, noffset, nlength);
    } else {
      range = nodeModelStreamer.feedTokenStream(formatStream, root, offset, length);
    }
    return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
  } catch (IOException e) {
    // this should never happen since TokenStringBuffer doesn't throw IOEs.
    throw new RuntimeException(e); // NOPMD
  }
}
 
Example #4
Source File: DotNodeModelStreamer.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private void writeHTMLStringSemantic(AbstractRule rule,
		DotFormattingConfigBasedStream out, ICompositeNode node)
		throws IOException {
	Injector htmlLabelInjector = new DotHtmlLabelStandaloneSetup()
			.createInjectorAndDoEMFRegistration();
	IFormatter dotHtmlLabelFormatter = htmlLabelInjector
			.getInstance(IFormatter.class);
	ITokenStream htmlLabelOut = new TokenStringBuffer();
	// TODO: calculate initial indentation properly
	ITokenStream fmt = dotHtmlLabelFormatter.createFormatterStream("\t\t",
			htmlLabelOut, false);
	INodeModelStreamer dothtmlLabelNodeModelStreamer = htmlLabelInjector
			.getInstance(INodeModelStreamer.class);

	IParser dotHtmlLabelParser = htmlLabelInjector
			.getInstance(IParser.class);
	// cut off the leading and the trailing white spaces
	String trimmedNodeText = node.getText().trim();
	String htmlLabelText = trimmedNodeText.substring(1,
			trimmedNodeText.length() - 1);
	IParseResult parseResult = dotHtmlLabelParser
			.parse(new StringReader(htmlLabelText));
	ICompositeNode htmlLabelRootNode = parseResult.getRootNode();

	dothtmlLabelNodeModelStreamer.feedTokenStream(fmt, htmlLabelRootNode, 0,
			htmlLabelText.length());
	out.writeSemantic(null, "<");
	out.addNewLine();
	out.addLineEntry(node.getGrammarElement(), htmlLabelOut.toString(),
			false);
	out.addNewLine();
	out.writeSemantic(null, ">");
}
 
Example #5
Source File: Serializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String serialize(EObject obj, SaveOptions options) {
	TokenStringBuffer tokenStringBuffer = new TokenStringBuffer();
	try {
		serialize(obj, tokenStringBuffer, options);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	return tokenStringBuffer.toString();
}
 
Example #6
Source File: Serializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ReplaceRegion serializeReplacement(EObject obj, SaveOptions options) {
	TokenStringBuffer tokenStringBuffer = new TokenStringBuffer();
	try {
		TreeConstructionReport report = serialize(obj, tokenStringBuffer, options);
		return new ReplaceRegion(report.getPreviousLocation(), tokenStringBuffer.toString());
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #7
Source File: FormattingConfigBasedStream.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toString() {
	TokenStringBuffer result = new TokenStringBuffer();
	try {
		flush(result, entries.size());
	} catch (IOException e) {
		e.printStackTrace();
		return "Error: " + e.getMessage();
	}
	return result.toString();
}
 
Example #8
Source File: IndentingSerializer.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method implementation is copied from {@link Serializer#serialize(EObject, SaveOptions)} with the addition of the indentation parameter. {@inheritDoc}
 */
@Override
public String serialize(final EObject obj, final SaveOptions options, final String initialIndentation) {
  TokenStringBuffer tokenStringBuffer = new TokenStringBuffer();
  try {
    serialize(obj, tokenStringBuffer, options, initialIndentation);
  } catch (IOException e) {
    throw new RuntimeException(e);// NOPMD
  }
  return tokenStringBuffer.toString();
}