-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace some deprecated components with listeners/services (#524)
This migrates some of the existing project components to a more modern (and recommended) service/listener architecture. Also, some other minor deprecations were fixed along the way
- Loading branch information
1 parent
0247248
commit 3d9dc39
Showing
19 changed files
with
234 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 86 additions & 4 deletions
90
src/com/twitter/intellij/pants/components/PantsProjectCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,94 @@ | ||
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.twitter.intellij.pants.components; | ||
|
||
import com.intellij.openapi.components.ProjectComponent; | ||
import com.intellij.ProjectTopics; | ||
import com.intellij.openapi.Disposable; | ||
import com.intellij.openapi.components.ServiceManager; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.roots.ModuleRootEvent; | ||
import com.intellij.openapi.roots.ModuleRootListener; | ||
import com.intellij.openapi.roots.ProjectRootManager; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import com.intellij.openapi.vfs.VfsUtil; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.util.containers.ContainerUtil; | ||
import com.intellij.util.messages.MessageBusConnection; | ||
import com.twitter.intellij.pants.util.PantsUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.jps.model.java.JavaSourceRootType; | ||
|
||
public interface PantsProjectCache extends ProjectComponent { | ||
boolean folderContainsSourceRoot(@NotNull VirtualFile file); | ||
import java.util.Comparator; | ||
import java.util.TreeSet; | ||
|
||
public class PantsProjectCache implements Disposable { | ||
private static final Comparator<VirtualFile> VIRTUAL_FILE_COMPARATOR = (o1, o2) -> { | ||
if (o1.isDirectory() && !o2.isDirectory()) { | ||
return -1; | ||
} | ||
if (o2.isDirectory() && !o1.isDirectory()) { | ||
return 1; | ||
} | ||
|
||
return StringUtil.naturalCompare(o1.getPath().toLowerCase(), o2.getPath().toLowerCase()); | ||
}; | ||
|
||
|
||
private final Project myProject; | ||
private volatile TreeSet<VirtualFile> myProjectRoots = null; | ||
|
||
public PantsProjectCache(Project project) { | ||
myProject = project; | ||
listenForRootsChange(); | ||
} | ||
|
||
public static PantsProjectCache getInstance(@NotNull Project project) { | ||
return ServiceManager.getService(project, PantsProjectCache.class); | ||
} | ||
|
||
public boolean folderContainsSourceRoot(@NotNull VirtualFile file) { | ||
if (!file.isDirectory()) { | ||
return false; | ||
} | ||
final TreeSet<VirtualFile> allRoots = getProjectRoots(); | ||
// find this file or the next one in natural order | ||
final VirtualFile candidate = allRoots.ceiling(file); | ||
return candidate != null && VfsUtil.isAncestor(file, candidate, false); | ||
} | ||
|
||
private synchronized TreeSet<VirtualFile> getProjectRoots() { | ||
if (myProjectRoots == null) { | ||
myProjectRoots = collectRoots(); | ||
} | ||
return myProjectRoots; | ||
} | ||
|
||
@NotNull | ||
private TreeSet<VirtualFile> collectRoots() { | ||
final TreeSet<VirtualFile> result = new TreeSet<>(VIRTUAL_FILE_COMPARATOR); | ||
final ProjectRootManager rootManager = ProjectRootManager.getInstance(myProject); | ||
result.addAll(rootManager.getModuleSourceRoots(ContainerUtil.set(JavaSourceRootType.SOURCE, JavaSourceRootType.TEST_SOURCE))); | ||
return result; | ||
} | ||
|
||
private void listenForRootsChange() { | ||
if (myProject.isDefault() || !PantsUtil.isPantsProject(myProject)) { | ||
return; | ||
} | ||
final MessageBusConnection connection = myProject.getMessageBus().connect(this); | ||
connection.subscribe( | ||
ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() { | ||
@Override | ||
public void rootsChanged(@NotNull ModuleRootEvent event) { | ||
myProjectRoots = null; | ||
} | ||
} | ||
); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
myProjectRoots = null; | ||
} | ||
} |
98 changes: 0 additions & 98 deletions
98
src/com/twitter/intellij/pants/components/impl/PantsProjectCacheImpl.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.