use of org.eclipse.n4js.transpiler.print.SourceMapAwareAppendable.SourceOutputMapping in project n4js by eclipse.
the class PrettyPrinter method print.
/**
* Serialize the intermediate model in the given transpiler state to <code>outCode</code> and emit source maps to
* <code>optSourceMapData</code>.
*
* @param optPreamble
* an optional preamble that will be prepended to the output code. Use '\n' as line separator. If absent,
* no preamble will be prepended.<br>
* If present, a single line break will be used to separate the preamble from the main output, but
* additional line feed characters may be added at the end of the preamble string if empty lines are
* desired between preamble and main output.
*/
public void print(TranspilerState state, Writer outCode, Optional<String> optPreamble, Optional<SourceMapInfo> optSourceMapInfo) throws IOException {
final boolean emitSourceMaps = optSourceMapInfo.isPresent();
final SourceMapAwareAppendable out = new SourceMapAwareAppendable(outCode, INDENT, emitSourceMaps);
if (optPreamble.isPresent()) {
// #append(CharSequence) will convert '\n' to correct line separator
out.append(optPreamble.get());
out.newLine();
}
PrettyPrinterSwitch.append(out, state);
if (emitSourceMaps) {
final SourceMapInfo sourceMapInfo = optSourceMapInfo.get();
final SourceMapGenerator generator = new SourceMapGeneratorDummy();
// append link to source maps to outCode
out.newLine();
out.append("//# sourceMappingURL=" + sourceMapInfo.simpleSourceMapFileName);
out.newLine();
// get the mappings collected by SourceMapAwareAppendable
final List<SourceOutputMapping> mappings = new ArrayList<>(out.getSourceMapData());
// perform some tweaks on the mappings (TEMPORARY)
removeCatchAllMapping(mappings);
sortMappings(mappings);
// Convert the source/output mappings produced by SourceMapAwareAppendable to the API of the
// Google Closure compiler source map library and add them to our SourceMapGenerator 'generator'
final PositionProvider positionProvider = PositionProvider.from(state.resource);
for (SourceOutputMapping m : mappings) {
final EObject originalASTNode = state.tracer.getOriginalASTNode(m.elementInIM);
if (// it's ok if this is null
originalASTNode != null && originalASTNode.eResource() instanceof N4JSResource) {
final ITextRegion region = locationInFileProvider.getSignificantTextRegion(originalASTNode);
// get the resource and compute the path.
final String path = sourceMapInfo.resolve((N4JSResource) originalASTNode.eResource());
final FilePosition sourceStartPosition = positionProvider.toPosition(region.getOffset());
generator.addMapping(path, // TODO source maps: support for original symbol name
null, sourceStartPosition, m.outputStart, m.outputEnd);
}
}
// append actual source maps to the buffer passed in via 'sourceMapInfo'
generator.appendTo(sourceMapInfo.sourceMapBuff, sourceMapInfo.simpleCompiledFileName);
}
}
Aggregations