WEB-25679 NodeModulesDirectoryManager - Recursion prevented when computing FileIndex extension

This commit is contained in:
Sergey Simonchik
2017-03-01 00:55:51 +03:00
parent 79dbf72a53
commit cf01c4d5a1
2 changed files with 20 additions and 0 deletions
@@ -20,6 +20,8 @@
package com.intellij.openapi.project;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -28,6 +30,8 @@ import java.util.Collection;
public abstract class ProjectLocator {
static final Key<Project> PREFERRED_PROJECT_KEY = Key.create("PREFERRED_PROJECT_KEY");
public static ProjectLocator getInstance() {
return ServiceManager.getService(ProjectLocator.class);
}
@@ -50,4 +54,16 @@ public abstract class ProjectLocator {
*/
@NotNull
public abstract Collection<Project> getProjectsForFile(VirtualFile file);
public static <T, E extends Throwable> T computeWithPreferredProject(@NotNull VirtualFile file,
@NotNull Project preferredProject,
@NotNull ThrowableComputable<T, E> action) throws E {
file.putUserData(PREFERRED_PROJECT_KEY, preferredProject);
try {
return action.compute();
}
finally {
file.putUserData(PREFERRED_PROJECT_KEY, null);
}
}
}
@@ -41,6 +41,10 @@ public class ProjectLocatorImpl extends ProjectLocator {
if (projects.length == 1 && !projects[0].isDisposed()) return projects[0];
if (file != null) {
Project preferredProject = file.getUserData(PREFERRED_PROJECT_KEY);
if (preferredProject != null) {
return preferredProject;
}
for (Project project : projects) {
if (project.isInitialized() && !project.isDisposed() && ProjectRootManager.getInstance(project).getFileIndex().isInContent(file)) {
return project;