use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class EcmaScriptSubGenerator method getCompileResultAsText.
// note: following method is only used for testing
@Override
public String getCompileResultAsText(Script root, GeneratorOption[] options) {
final Resource resource = root.eResource();
if (!(resource instanceof N4JSResource)) {
throw new IllegalArgumentException("given script must be contained in an N4JSResource");
}
final N4JSResource resourceCasted = (N4JSResource) resource;
final Writer buffCode = new StringWriter();
getTranspiler().transpile(resourceCasted, options, buffCode, Optional.absent());
return buffCode.toString();
}
use of org.eclipse.n4js.resource.N4JSResource 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);
}
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class PreparationStep method prepare.
/**
* Creates and initializes the transpiler state. In particular, this will create the intermediate model as a copy of
* the original AST with some modifications (esp. rewiring of cross-references to ensure intermediate model is
* self-contained).
*
* @param options
* the {@link GeneratorOption generator options} to use during generation.
*/
public TranspilerState prepare(Script script, GeneratorOption[] options) {
final N4JSResource resource = (N4JSResource) script.eResource();
final ContainerTypesHelper.MemberCollector memberCollector = containerTypesHelper.fromContext(resource);
final Tracer tracer = new Tracer();
final InformationRegistry info = new InformationRegistry();
final STECache steCache = createIM(script, tracer, info);
return new TranspilerState(resource, options, memberCollector, steCache.im, steCache, tracer, info);
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class N4IDEXpectFileSetup method createThisResource.
/**
* Creates {@link N4JSResource} in new {@link ResourceSet}. Created resource has uri of processed xt file and its
* context. During creation resource factory is obtained dynamically to preserve bindings created by XPECT (see
* {@link org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup#configure})
*/
@Creates(ThisResource.class)
public XtextResource createThisResource() throws IOException, CoreException {
Entry<IFile, IProject> file2project = findTestResources();
IFile xpectFile = file2project.getKey();
IProject userProject = file2project.getValue();
ResourceSet resourceSet = resourceSetProvider.get(userProject);
URI xpectFilePlatformURI = URI.createPlatformResourceURI(xpectFile.getFullPath().toString(), ENCODE_PLATFORM_RESOURCE_URIS);
Injector injector = IXtInjectorProvider.INSTANCE.getInjector(ctx.get(XpectJavaModel.class), xpectFilePlatformURI);
Resource resource = injector.getInstance(IResourceFactory.class).createResource(xpectFilePlatformURI);
resourceSet.getResources().add(resource);
InputStream input = xpectFile.getContents();
try {
resource.load(input, null);
} finally {
if (input != null)
input.close();
}
return (XtextResource) resource;
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class ConcreteSyntaxAwareReferenceFinder method findReferences.
@Override
public void findReferences(Predicate<URI> targetURIs, Resource resource, Acceptor acceptor, IProgressMonitor monitor) {
// make sure data is present
keys.getData((TargetURIs) targetURIs, new SimpleResourceAccess(resource.getResourceSet()));
EList<EObject> astContents;
if (resource instanceof N4JSResource) {
// In case of N4JSResource, we search only in the AST but NOT in TModule tree!
Script script = (Script) ((N4JSResource) resource).getContents().get(0);
astContents = new BasicEList<>();
astContents.add(script);
} else {
astContents = resource.getContents();
}
for (EObject content : astContents) {
findReferences(targetURIs, content, acceptor, monitor);
}
}
Aggregations