Search in sources :

Example 6 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class AbstractN4JSCore method loadModuleFromIndex.

@Override
public TModule loadModuleFromIndex(final ResourceSet resourceSet, final IResourceDescription resourceDescription, boolean allowFullLoad) {
    final URI resourceURI = resourceDescription.getURI();
    Resource resource = resourceSet.getResource(resourceURI, false);
    if (resource instanceof N4JSResource) {
        final N4JSResource resourceCasted = (N4JSResource) resource;
        final Script existingScript = resourceCasted.getScript();
        final TModule existingModule = resourceCasted.getModule();
        if (existingModule != null) {
            // -> simply return that
            return existingModule;
        } else if (existingScript != null && !existingScript.eIsProxy()) {
            // resource exists already and it already has its AST loaded (though no TModule yet)
            // -> we have to create the TModule from that AST instead of loading it from index
            // trigger installation of derived state (i.e. types builder)
            resourceCasted.installDerivedState(false);
            return resourceCasted.getModule();
        }
    }
    if (resource == null) {
        resource = resourceSet.createResource(resourceURI);
    }
    if (resource instanceof N4JSResource) {
        if (resource.getContents().isEmpty()) {
            final N4JSResource casted = (N4JSResource) resource;
            try {
                if (casted.loadFromDescription(resourceDescription)) {
                    casted.performPostProcessing();
                    return casted.getModule();
                } else if (allowFullLoad) {
                    casted.unload();
                    casted.load(resourceSet.getLoadOptions());
                    casted.installDerivedState(false);
                    return casted.getModule();
                }
            } catch (final Exception e) {
                casted.unload();
                return null;
            }
        }
    }
    return null;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI)

Example 7 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class N4JSPostProcessor method performPostProcessing.

@Override
public void performPostProcessing(PostProcessingAwareResource resource, CancelIndicator cancelIndicator) {
    final N4JSResource resourceCasted = (N4JSResource) resource;
    final ASTMetaInfoCache cache = createASTMetaInfoCache(resourceCasted);
    try {
        postProcessN4JSResource(resourceCasted, cancelIndicator);
    } catch (Throwable th) {
        operationCanceledManager.propagateIfCancelException(th);
        if (cache.hasBrokenAST()) {
        // swallow exception, AST is broken due to parse error anyway
        } else {
            // make sure this error is being reported, even if exception will be suppressed by calling code!
            UtilN4.reportError("exception while post-processing resource " + resource.getURI(), th);
            throw th;
        }
    } finally {
        cache.clearTemporaryData();
    }
}
Also used : N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Example 8 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class ResourceLoadingStatistics method countN4JSResourcesLoadedFromAST.

private int countN4JSResourcesLoadedFromAST(ResourceSet resSet) {
    int n = 0;
    for (Resource res : resSet.getResources()) {
        if (!isBuiltInResource(res)) {
            if (res instanceof N4JSResource) {
                final N4JSResource resCasted = (N4JSResource) res;
                final Script script = resCasted.getScript();
                if (script != null && !script.eIsProxy()) {
                    n++;
                }
            }
        }
    }
    return n;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Example 9 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class ResourceLoadingStatistics method investigate.

private FileLoadInfo investigate(IN4JSProject project, URI fileURI, IProgressMonitor monitor) {
    final CancelIndicator cancelIndicator = new MonitorBasedCancelIndicator(monitor);
    operationCanceledManager.checkCanceled(cancelIndicator);
    final ResourceSet resSet = n4jsCore.createResourceSet(Optional.of(project));
    final N4JSResource res = (N4JSResource) resSet.createResource(fileURI);
    try {
        res.load(Collections.emptyMap());
    } catch (IOException e) {
        e.printStackTrace();
    }
    // trigger loading of AST
    res.getContents();
    res.resolveLazyCrossReferences(cancelIndicator);
    // now start counting what was loaded incidentally ...
    return investigate(resSet);
}
Also used : N4JSResource(org.eclipse.n4js.resource.N4JSResource) MonitorBasedCancelIndicator(org.eclipse.xtext.builder.MonitorBasedCancelIndicator) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) MonitorBasedCancelIndicator(org.eclipse.xtext.builder.MonitorBasedCancelIndicator)

Example 10 with N4JSResource

use of org.eclipse.n4js.resource.N4JSResource in project n4js by eclipse.

the class ResourceLoadingStatistics method countN4JSResourcesLoadedFromIndex.

private int countN4JSResourcesLoadedFromIndex(ResourceSet resSet) {
    int n = 0;
    for (Resource res : resSet.getResources()) {
        if (!isBuiltInResource(res)) {
            if (res instanceof N4JSResource) {
                final N4JSResource resCasted = (N4JSResource) res;
                final Script script = resCasted.getScript();
                final TModule module = resCasted.getModule();
                if (script != null && script.eIsProxy() && module != null && !module.eIsProxy()) {
                    n++;
                }
            }
        }
    }
    return n;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) TModule(org.eclipse.n4js.ts.types.TModule)

Aggregations

N4JSResource (org.eclipse.n4js.resource.N4JSResource)21 Resource (org.eclipse.emf.ecore.resource.Resource)10 URI (org.eclipse.emf.common.util.URI)8 EObject (org.eclipse.emf.ecore.EObject)6 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 Script (org.eclipse.n4js.n4JS.Script)4 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)3 TModule (org.eclipse.n4js.ts.types.TModule)3 Optional (com.google.common.base.Optional)2 Iterables (com.google.common.collect.Iterables)2 File (java.io.File)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 QualifiedName (org.eclipse.xtext.naming.QualifiedName)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2