Created
March 5, 2020 12:55
-
-
Save jonnyzzz/b5ddefcc77d57d4ec520186a2db940bb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import com.intellij.codeInsight.daemon.ProblemHighlightFilter | |
import com.intellij.concurrency.JobLauncher | |
import com.intellij.openapi.application.ReadAction | |
import com.intellij.openapi.module.Module | |
import com.intellij.openapi.module.ModuleManager | |
import com.intellij.openapi.progress.ProgressIndicator | |
import com.intellij.openapi.progress.ProgressManager | |
import com.intellij.openapi.progress.Task | |
import com.intellij.openapi.project.Project | |
import com.intellij.openapi.project.ProjectCoreUtil | |
import com.intellij.openapi.project.ProjectManager | |
import com.intellij.openapi.project.ProjectUtil | |
import com.intellij.openapi.roots.* | |
import com.intellij.openapi.ui.Messages | |
import com.intellij.openapi.util.io.FileUtil | |
import com.intellij.openapi.vfs.VirtualFile | |
import com.intellij.psi.* | |
import com.intellij.psi.util.InheritanceUtil | |
import com.intellij.util.Processor | |
import org.jetbrains.annotations.NotNull | |
import org.jetbrains.annotations.Nullable | |
import java.util.concurrent.ConcurrentHashMap | |
import java.util.concurrent.atomic.AtomicInteger | |
import static com.intellij.psi.PsiReferenceService.Hints.NO_HINTS | |
final Project project = ProjectManager.getInstance().getOpenProjects()[0] | |
ProgressManager.getInstance().run(new Task.Backgroundable(project, "script", true) { | |
@Override | |
void run(@NotNull ProgressIndicator indicator) { | |
final PsiManager psiManager = PsiManager.getInstance(project) | |
final ProjectFileIndex projectIndex = ProjectRootManager.getInstance(project).getFileIndex() | |
Module module = null | |
ReadAction.run { | |
for (Module m : ModuleManager.getInstance(project).getModules()) { | |
if (m.name == "intellij.indexing.shared") { | |
module = m | |
} | |
} | |
} | |
indicator.text = "MOdule: " + module | |
final ModuleRootManager roots = ModuleRootManager.getInstance(module) | |
final ModuleFileIndex moduleIndex = roots.getFileIndex() | |
final List<VirtualFile> allFiles = new ArrayList<VirtualFile>() | |
ReadAction.run { | |
moduleIndex.iterateContent(new ContentIterator() { | |
boolean processFile(@NotNull final VirtualFile fileOrDir) { | |
indicator.checkCanceled() | |
if (fileOrDir.isDirectory()) return true | |
if (ProjectCoreUtil.isProjectOrWorkspaceFile(fileOrDir)) return true | |
if (!moduleIndex.isInContent(fileOrDir)) return true | |
if (moduleIndex.isInSourceContent(fileOrDir) || moduleIndex.isInTestSourceContent(fileOrDir)) { | |
allFiles.add(fileOrDir) | |
} | |
return true | |
} | |
}) | |
} | |
final def current = new AtomicInteger() | |
final def total = allFiles.size() | |
indicator.text = "Files: " + total | |
final def usages = new ConcurrentHashMap<Object, String>() | |
indicator.setIndeterminate(false) | |
JobLauncher.getInstance().invokeConcurrentlyUnderProgress( | |
allFiles, | |
indicator, | |
new Processor<VirtualFile>() { | |
boolean process(@NotNull final VirtualFile file) { | |
try { | |
indicator.checkCanceled() | |
indicator.setFraction(current.incrementAndGet() * 1.0 / total) | |
indicator.setText2(ProjectUtil.calcRelativeToProjectPath(file, project).toString()) | |
ReadAction.run { | |
final PsiFile psiFile = psiManager.findFile(file) | |
if (psiFile == null) return | |
if (!psiFile.isValid()) return | |
if (!ProblemHighlightFilter.shouldProcessFileInBatch(psiFile)) return | |
psiFile.accept(new PsiRecursiveElementVisitor() { | |
@Override | |
void visitElement(final PsiElement element) { | |
super.visitElement(element) | |
for (final PsiReference ref : PsiReferenceService.getService().getReferences(element, NO_HINTS)) { | |
processResolvedElement(ref.resolve()) | |
} | |
} | |
private void processResolvedElement(@Nullable PsiElement resolved) { | |
if (resolved == null) return | |
if (resolved.getProject().isDefault()) return | |
if (!resolved.isValid()) return | |
if (resolved instanceof PsiClass) { | |
InheritanceUtil.processSupers((PsiClass)resolved, true, new Processor<PsiClass>() { | |
boolean process(@NotNull PsiClass psiClass) { | |
registerUsage(psiClass) | |
return true | |
} | |
}) | |
return | |
} | |
registerUsage(resolved) | |
} | |
private void registerUsage(@NotNull PsiElement resolved) { | |
final PsiFile cFile = resolved.getContainingFile() | |
if (cFile == null) return | |
if (!cFile.isValid()) return | |
final VirtualFile virtual = cFile.getVirtualFile() | |
if (virtual == null) return | |
if (!virtual.isValid()) return | |
def entries = projectIndex.getOrderEntriesForFile(virtual) | |
for (OrderEntry entry : entries) { | |
if (entry instanceof ModuleOrderEntry) { | |
usages.put(virtual, virtual.path) | |
} | |
} | |
} | |
}) | |
} | |
} catch (Exception e) { | |
Messages.showInfoMessage("aaa", e.toString()) | |
return false | |
} | |
return true | |
} | |
}) | |
StringBuilder sb = new StringBuilder() | |
for (String value : new TreeSet<>(usages.values())) { | |
sb.append(value + "\n"); | |
} | |
FileUtil.writeToFile(new File("/Users/jonnyzzz/Work/intellij/dump.txt"), sb.toString()) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment