use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class FindReferencesXpectMethod method findReferences.
/**
* This Xpect methods compares all computed references at a given EObject to the expected references. The expected
* references include the line number.
*/
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public void findReferences(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset) {
// When you write Xpect test methods, ALWAYS retrieve eObject via IEObjectCoveringRegion to get the right
// eObject!
// Do NOT use EObject arg1!
EObject context = offset.getEObject();
EObject argEObj = offsetHelper.resolveElementAt((XtextResource) context.eResource(), offset.getOffset());
// If not a cross-reference element, use context instead
if (argEObj == null)
argEObj = context;
EObject eObj = argEObj;
if (argEObj instanceof ParameterizedTypeRef)
eObj = ((ParameterizedTypeRef) argEObj).getDeclaredType();
List<EObject> refs = findReferenceHelper.findReferences(eObj);
ArrayList<String> result = Lists.newArrayList();
for (EObject ref : refs) {
if (ref instanceof PropertyNameOwner)
ref = ((PropertyNameOwner) ref).getDeclaredName();
ICompositeNode srcNode = NodeModelUtils.getNode(ref);
int line = srcNode.getStartLine();
String moduleName;
if (ref.eResource() instanceof N4JSResource) {
N4JSResource n4jsResource = (N4JSResource) ref.eResource();
moduleName = n4jsResource.getModule().getQualifiedName();
} else {
moduleName = "(unknown resource)";
}
String text = NodeModelUtils.getTokenText(srcNode);
if (ref instanceof GenericDeclaration)
text = ((GenericDeclaration) ref).getDefinedType().getName();
String resultText = moduleName + " - " + text + " - " + line;
result.add(resultText);
}
expectation.assertEquals(result);
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class EcmaScriptSubGenerator method internalDoGenerate.
@Override
protected void internalDoGenerate(Resource resource, GeneratorOption[] options, IFileSystemAccess fsa) {
if (!(resource instanceof N4JSResource)) {
if (IN4JSProject.N4MF_MANIFEST.equals(resource.getURI().lastSegment())) {
return;
}
throw new IllegalArgumentException("Given resource is not an N4JSResource. " + resource);
}
final N4JSResource resourceCasted = (N4JSResource) resource;
if (resourceCasted.getModule().isStaticPolyfillModule()) {
// do not transpile static polyfill modules (i.e. the fillers)
return;
}
Measurement measurement = this.collector.getMeasurement(resource.getURI().toString());
/*
* In addition to here, check for cancellation is done also on file-emit boundaries, see fsa.generateFile().
*/
CancelIndicator monitor = ciExtractor.extractCancelIndicator(fsa);
// if the transpile-conditions are all met, then transpile:
if (shouldBeCompiled(resource, monitor)) {
final String compiledFileExtension = getCompiledFileExtension(resource);
final String filename = getTargetFileName(resource, compiledFileExtension);
final String sourceMapFileExtension = getCompiledFileSourceMapExtension(resource);
final String sourceMapFileName = getTargetFileName(resource, sourceMapFileExtension);
// used within the file-content to refer to sibling-file:
final String simpleSourceMapFileName = new File(sourceMapFileName).toPath().getFileName().toString();
final String simpleCompiledFileName = new File(filename).toPath().getFileName().toString();
// the next two variables store the navigation-prefix to get to the sources
final Path relativeNavigationToSrc = calculateNavigationFromOutputToSourcePath(fsa, getCompilerID(), resourceCasted);
final Path explicitNavigationToSrc = Paths.get("/sources");
// true use explicitNavigationToSrc | false use relativeNavigationToSrc
boolean useExplicitSourceRef = true;
boolean createSourceMap = true;
if (filename != null) {
final EObject root = rootElement(resource);
if (root != null) {
final Writer buffCode = new StringWriter();
Optional<SourceMapInfo> optSourceMapData = Optional.absent();
if (createSourceMap) {
SourceMapInfo sourceMapDataInstance = getTranspiler().new SourceMapInfo();
sourceMapDataInstance.sourceMapBuff = new StringWriter();
sourceMapDataInstance.simpleSourceMapFileName = simpleSourceMapFileName;
sourceMapDataInstance.simpleCompiledFileName = simpleCompiledFileName;
sourceMapDataInstance.isExplicitSourceRef = useExplicitSourceRef;
sourceMapDataInstance.explicitNavigationToSrc = explicitNavigationToSrc;
sourceMapDataInstance.n4jsFilePath = relativeNavigationToSrc.resolve(resourceCasted.getURI().lastSegment()).toString();
sourceMapDataInstance.sourceMapFileExtension = sourceMapFileExtension;
optSourceMapData = Optional.of(sourceMapDataInstance);
}
getTranspiler().transpile(resourceCasted, options, buffCode, optSourceMapData);
fsa.generateFile(filename, COMPILER_ID, buffCode.toString());
if (createSourceMap) {
fsa.generateFile(sourceMapFileName, COMPILER_ID, optSourceMapData.get().sourceMapBuff.toString());
}
}
}
}
measurement.end();
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class N4JSDirtyStateEditorSupport method collectTransitivelyDependentResources.
private List<Resource> collectTransitivelyDependentResources(XtextResource resource, Set<URI> deltaURIs) {
List<Resource> result = Lists.newArrayList();
ResourceSet resourceSet = resource.getResourceSet();
for (Resource candidate : resourceSet.getResources()) {
if (candidate != resource) {
URI uri = candidate.getURI();
if (deltaURIs.contains(uri)) {
// the candidate is contained in the delta list
// schedule it for unloading
result.add(candidate);
} else if (candidate instanceof N4JSResource) {
// schedule it for unloading
if (canLoadFromDescriptionHelper.dependsOnAny(candidate, deltaURIs)) {
result.add(candidate);
}
}
}
}
return result;
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class MarkedProject method unloadASTAndClearCaches.
/**
* Unload the ASTs and clear the resource scope caches of all resources that belong to this marked project.
*/
public void unloadASTAndClearCaches() {
Iterables.filter(resources, resource -> resource.isLoaded()).forEach(resource -> {
if (resource instanceof N4JSResource) {
N4JSResource n4jsResource = (N4JSResource) resource;
n4jsResource.unloadAST();
}
});
}
use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.
the class N4HeadlessCompiler method postProcessResources.
/*
* ===============================================================================================================
*
* PROJECT POST-PROCESSING
*
* ===============================================================================================================
*/
/**
* Post processing on the whole project. While validations can trigger post processing in a lazy way, they do not
* guarantee that all resources will be fully processed. This is an issue in larger project graphs. Unlike in the
* IDE which can at any point load AST on demand based on the TModel, the HLC does not allow to access AST after
* unloading therefore we explicitly post process all N4JS resources in the given project.
*
* @param markedProject
* project to trigger post process.
*/
private void postProcessResources(MarkedProject markedProject) {
if (logger.isCreateDebugOutput())
logger.debug(" PostProcessing " + markedProject);
Iterables.filter(markedProject.resources, resource -> resource.isLoaded()).forEach(resource -> {
if (resource instanceof N4JSResource) {
N4JSResource n4jsResource = (N4JSResource) resource;
// Make sure the resource is fully postprocessed before unloading the AST. Otherwise, resolving
// cross references to the elements inside the resources from dependent projects will fail.
n4jsResource.performPostProcessing();
}
});
}
Aggregations