Search in sources :

Example 1 with SourceFolderSnapshot

use of org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot in project n4js by eclipse.

the class N4JSClusteringStorageAwareResourceLoader method initDependencyMaps.

private void initDependencyMaps(List<LoadResult> results, ProjectConfigSnapshot pcs, Set<LoadResult> noDeps, Multimap<LoadResult, LoadResult> dependsOn, Multimap<LoadResult, LoadResult> dependsOnInverse) {
    Multimap<String, LoadResult> moduleName2Result = HashMultimap.create();
    for (LoadResult result : results) {
        URI uri = result.resource.getURI();
        SourceFolderSnapshot srcFolder = pcs.findSourceFolderContaining(uri);
        URI srcFolderUri = srcFolder.getPath();
        URI relUri = uri.deresolve(srcFolderUri);
        String moduleName = URIUtils.trimFileExtension(relUri).toFileString();
        moduleName2Result.put(moduleName, result);
        noDeps.add(result);
    }
    String prjName = pcs.getName();
    for (LoadResult result : results) {
        for (EObject eobj : result.resource.getContents()) {
            if (eobj instanceof Script) {
                Script script = (Script) eobj;
                for (EObject topLevelStmt : script.getScriptElements()) {
                    if (topLevelStmt instanceof ImportDeclaration) {
                        ImportDeclaration impDecl = (ImportDeclaration) topLevelStmt;
                        String moduleSpecifier = impDecl.getModuleSpecifierAsText();
                        boolean hasInnerProjectDependency = false;
                        if (moduleName2Result.containsKey(moduleSpecifier)) {
                            hasInnerProjectDependency = true;
                        } else if (moduleSpecifier.startsWith(prjName)) {
                            moduleSpecifier = moduleSpecifier.substring(prjName.length() + 1);
                            if (moduleName2Result.containsKey(moduleSpecifier)) {
                                hasInnerProjectDependency = true;
                            }
                        }
                        if (hasInnerProjectDependency) {
                            for (LoadResult dependsOnResult : moduleName2Result.get(moduleSpecifier)) {
                                dependsOn.put(result, dependsOnResult);
                                dependsOnInverse.put(dependsOnResult, result);
                            }
                            noDeps.remove(result);
                        }
                    } else {
                        // we know that all import statements are at the beginning of a file
                        break;
                    }
                }
            }
        }
    }
}
Also used : Script(org.eclipse.n4js.n4JS.Script) SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) EObject(org.eclipse.emf.ecore.EObject) ImportDeclaration(org.eclipse.n4js.n4JS.ImportDeclaration) URI(org.eclipse.emf.common.util.URI)

Example 2 with SourceFolderSnapshot

use of org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot in project n4js by eclipse.

the class XWorkspaceBuilder method scanAddedSourceFoldersForNewSourceFiles.

/**
 * @return list of URIs from newly added source folders (including source folders of added projects).
 */
private List<URI> scanAddedSourceFoldersForNewSourceFiles(WorkspaceChanges changes, IFileSystemScanner scanner) {
    List<URI> added = new ArrayList<>();
    for (SourceFolderSnapshot sourceFolder : changes.getAllAddedSourceFolders()) {
        List<URI> sourceFilesOnDisk = sourceFolderScanner.findAllSourceFiles(sourceFolder, scanner);
        added.addAll(sourceFilesOnDisk);
    }
    return added;
}
Also used : SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI)

Example 3 with SourceFolderSnapshot

use of org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot in project n4js by eclipse.

the class N4JSProjectConfig method computeChanges.

/**
 * Given two project configuration snapshots representing the old and new state of the receiving project, this
 * method computes the corresponding workspace changes. If no changes occurred, an empty {@link WorkspaceChanges}
 * instance will be returned.
 */
public static WorkspaceChanges computeChanges(ProjectConfigSnapshot oldProjectConfig, ProjectConfigSnapshot newProjectConfig) {
    Set<? extends SourceFolderSnapshot> oldSourceFolders = oldProjectConfig.getSourceFolders();
    Set<? extends SourceFolderSnapshot> newSourceFolders = newProjectConfig.getSourceFolders();
    // detect added/removed source folders
    Map<URI, SourceFolderSnapshot> oldSFs = new HashMap<>();
    Map<URI, SourceFolderSnapshot> newSFs = new HashMap<>();
    for (SourceFolderSnapshot sourceFolder : oldSourceFolders) {
        oldSFs.put(sourceFolder.getPath(), sourceFolder);
    }
    for (SourceFolderSnapshot sourceFolder : newSourceFolders) {
        newSFs.put(sourceFolder.getPath(), sourceFolder);
    }
    List<SourceFolderSnapshot> addedSourceFolders = new ArrayList<>();
    List<SourceFolderSnapshot> removedSourceFolders = new ArrayList<>();
    for (URI sfUri : Iterables.concat(oldSFs.keySet(), newSFs.keySet())) {
        boolean isOld = oldSFs.containsKey(sfUri);
        boolean isNew = newSFs.containsKey(sfUri);
        if (isOld && isNew) {
        // unchanged
        } else if (isOld && !isNew) {
            removedSourceFolders.add(oldSFs.get(sfUri));
        } else if (!isOld && isNew) {
            addedSourceFolders.add(newSFs.get(sfUri));
        }
    }
    if (Objects.equals(oldProjectConfig.getName(), newProjectConfig.getName())) {
        // detect changes in project properties
        boolean propertiesChanged = !addedSourceFolders.isEmpty() || !removedSourceFolders.isEmpty() || !Objects.equals(oldProjectConfig, newProjectConfig);
        return new WorkspaceChanges(ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.copyOf(removedSourceFolders), ImmutableList.copyOf(addedSourceFolders), ImmutableList.of(), ImmutableList.of(), propertiesChanged ? ImmutableList.of(newProjectConfig) : ImmutableList.of());
    } else {
        return new WorkspaceChanges(ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.copyOf(removedSourceFolders), ImmutableList.copyOf(addedSourceFolders), ImmutableList.of(oldProjectConfig), ImmutableList.of(newProjectConfig), ImmutableList.of());
    }
}
Also used : WorkspaceChanges(org.eclipse.n4js.xtext.workspace.WorkspaceChanges) SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI) FileURI(org.eclipse.n4js.workspace.locations.FileURI)

Example 4 with SourceFolderSnapshot

use of org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot in project n4js by eclipse.

the class N4JSProjectBuilder method scanForSourceFiles.

/**
 * Overridden to exclude all ts files (yet still include d.ts files)
 */
@Override
protected Set<URI> scanForSourceFiles() {
    Set<URI> result = new HashSet<>();
    for (SourceFolderSnapshot srcFolder : getProjectConfig().getSourceFolders()) {
        List<URI> allSourceFileUris = sourceFolderScanner.findAllSourceFiles(srcFolder, fileSystemScanner);
        for (URI srcFileUri : allSourceFileUris) {
            if (!srcFileUri.hasTrailingPathSeparator()) {
                IResourceServiceProvider rsp = resourceServiceProviders.getResourceServiceProvider(srcFileUri);
                if (rsp != null) {
                    String fileExtension = URIUtils.fileExtension(srcFileUri);
                    if (N4JSGlobals.TS_FILE_EXTENSION.equals(fileExtension)) {
                        // ignore ts files (yet consider d.ts files)
                        continue;
                    }
                    result.add(srcFileUri);
                }
            }
        }
    }
    return result;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) URI(org.eclipse.emf.common.util.URI) HashSet(java.util.HashSet)

Example 5 with SourceFolderSnapshot

use of org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot in project n4js by eclipse.

the class XWorkspaceBuilder method getURIsFromRemovedSourceFolders.

/**
 * @return list of URIs from removed source folders (<em>not</em> including URIs of removed projects).
 */
private List<URI> getURIsFromRemovedSourceFolders(WorkspaceChanges workspaceChanges) {
    List<URI> deleted = new ArrayList<>(workspaceChanges.getRemovedURIs());
    for (SourceFolderSnapshot sourceFolder : workspaceChanges.getRemovedSourceFolders()) {
        URI prefix = sourceFolder.getPath();
        ProjectBuilder projectBuilder = workspaceManager.getProjectBuilder(prefix);
        if (projectBuilder != null) {
            List<URI> matchedURIs = projectBuilder.findResourcesStartingWithPrefix(prefix);
            deleted.addAll(matchedURIs);
        }
    }
    return deleted;
}
Also used : SourceFolderSnapshot(org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI)

Aggregations

URI (org.eclipse.emf.common.util.URI)5 SourceFolderSnapshot (org.eclipse.n4js.xtext.workspace.SourceFolderSnapshot)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EObject (org.eclipse.emf.ecore.EObject)1 ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)1 Script (org.eclipse.n4js.n4JS.Script)1 FileURI (org.eclipse.n4js.workspace.locations.FileURI)1 WorkspaceChanges (org.eclipse.n4js.xtext.workspace.WorkspaceChanges)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1