74d91b476cb2c92a910b0ac53863d426329b4199
[idea/community.git] / java / java-impl / src / com / intellij / codeInsight / daemon / problems / pass / ProjectProblemFileSelectionListener.java
1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.codeInsight.daemon.problems.pass;
3
4 import com.intellij.codeInsight.daemon.problems.FileStateUpdater;
5 import com.intellij.codeInsight.hints.InlayHintsSettings;
6 import com.intellij.injected.editor.VirtualFileWindow;
7 import com.intellij.openapi.application.ApplicationManager;
8 import com.intellij.openapi.fileEditor.*;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.roots.ProjectFileIndex;
11 import com.intellij.openapi.roots.ProjectRootManager;
12 import com.intellij.openapi.startup.StartupActivity;
13 import com.intellij.openapi.util.registry.Registry;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.openapi.vfs.VirtualFileManager;
16 import com.intellij.openapi.vfs.newvfs.BulkFileListener;
17 import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent;
18 import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
19 import com.intellij.psi.PsiFile;
20 import com.intellij.psi.PsiJavaFile;
21 import com.intellij.psi.PsiManager;
22 import com.intellij.util.messages.MessageBusConnection;
23 import org.jetbrains.annotations.NotNull;
24
25 import java.util.List;
26
27 import static com.intellij.codeInsight.daemon.problems.pass.ProjectProblemInlaySettingsProvider.hintsEnabled;
28 import static com.intellij.util.ObjectUtils.tryCast;
29
30 class ProjectProblemFileSelectionListener implements FileEditorManagerListener, InlayHintsSettings.SettingsListener, BulkFileListener {
31
32   private final Project myProject;
33
34   private ProjectProblemFileSelectionListener(Project project) {
35     myProject = project;
36   }
37
38   @Override
39   public void selectionChanged(@NotNull FileEditorManagerEvent event) {
40     if (!hintsEnabled()) return;
41     VirtualFile oldFile = event.getOldFile();
42     VirtualFile newFile = event.getNewFile();
43     TextEditor oldEditor = tryCast(event.getOldEditor(), TextEditor.class);
44     if (oldFile == null || oldEditor == null || oldFile instanceof VirtualFileWindow || !oldFile.isValid() || oldFile.equals(newFile)) return;
45     PsiJavaFile psiJavaFile = tryCast(PsiManager.getInstance(myProject).findFile(oldFile), PsiJavaFile.class);
46     if (psiJavaFile == null) return;
47     ProjectProblemPassUtils.removeInlays(oldEditor.getEditor());
48     FileStateUpdater.setPreviousState(psiJavaFile);
49   }
50
51   @Override
52   public void before(@NotNull List<? extends VFileEvent> events) {
53     PsiManager psiManager = PsiManager.getInstance(myProject);
54     ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
55     for (VFileEvent e : events) {
56       if (!(e instanceof VFileDeleteEvent)) continue;
57       VirtualFile virtualFile = ((VFileDeleteEvent)e).getFile();
58       if (!fileIndex.isInContent(virtualFile)) continue;
59       PsiFile psiFile = psiManager.findFile(virtualFile);
60       if (psiFile == null) continue;
61       FileStateUpdater.removeState(psiFile);
62     }
63   }
64
65   @Override
66   public void settingsChanged() {
67     if (!hintsEnabled()) onHintsDisabled();
68   }
69
70   @Override
71   public void languageStatusChanged() {
72     if (!hintsEnabled()) onHintsDisabled();
73   }
74
75   @Override
76   public void globalEnabledStatusChanged(boolean newEnabled) {
77     if (!hintsEnabled()) onHintsDisabled();
78   }
79
80   private void onHintsDisabled() {
81     FileEditorManager editorManager = FileEditorManager.getInstance(myProject);
82     PsiManager psiManager = PsiManager.getInstance(myProject);
83     for (FileEditor selectedEditor : editorManager.getSelectedEditors()) {
84       TextEditor textEditor = tryCast(selectedEditor, TextEditor.class);
85       if (textEditor == null) continue;
86       VirtualFile virtualFile = selectedEditor.getFile();
87       if (virtualFile == null) continue;
88       PsiJavaFile psiJavaFile = tryCast(psiManager.findFile(virtualFile), PsiJavaFile.class);
89       if (psiJavaFile == null) continue;
90       ProjectProblemPassUtils.removeInlays(textEditor.getEditor());
91       FileStateUpdater.setPreviousState(psiJavaFile);
92     }
93   }
94
95   public static class MyStartupActivity implements StartupActivity {
96     @Override
97     public void runActivity(@NotNull Project project) {
98       ProjectProblemFileSelectionListener listener = new ProjectProblemFileSelectionListener(project);
99       MessageBusConnection connection = project.getMessageBus().connect();
100       connection.subscribe(FILE_EDITOR_MANAGER, listener);
101       connection.subscribe(InlayHintsSettings.getINLAY_SETTINGS_CHANGED(), listener);
102       connection.subscribe(VirtualFileManager.VFS_CHANGES, listener);
103     }
104   }
105 }