Refactor FacetPointerManger. Convert from component to service
authorAlex Plate <aleksei.plate@jetbrains.com>
Mon, 26 Jul 2021 14:20:55 +0000 (17:20 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Tue, 27 Jul 2021 17:05:30 +0000 (17:05 +0000)
GitOrigin-RevId: 5d50100ce41cfdc2639d57bb07a03951b220fad0

platform/lang-api/src/com/intellij/facet/pointers/FacetPointersManager.java
platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointerModuleListener.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersFacetManagerListener.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersManagerImpl.java
platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersPostStartupActivity.java [new file with mode: 0644]
platform/platform-resources/src/META-INF/PlatformLangComponents.xml

index bdca20ac37e623f55ef68466225880289c575cca..ecb7401a35a27961102b4ec2324c74f35c4ddaf2 100644 (file)
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
 @ApiStatus.NonExtendable
 public abstract class FacetPointersManager {
   public static FacetPointersManager getInstance(Project project) {
-    return project.getComponent(FacetPointersManager.class);
+    return project.getService(FacetPointersManager.class);
   }
 
   public abstract <F extends Facet> FacetPointer<F> create(F facet);
diff --git a/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointerModuleListener.java b/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointerModuleListener.java
new file mode 100644 (file)
index 0000000..b3d88fd
--- /dev/null
@@ -0,0 +1,38 @@
+// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+package com.intellij.facet.impl.pointers;
+
+import com.intellij.facet.pointers.FacetPointersManager;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.project.ModuleListener;
+import com.intellij.openapi.project.Project;
+import com.intellij.util.Function;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.List;
+
+public class FacetPointerModuleListener implements ModuleListener {
+
+  private final Project myProject;
+
+  public FacetPointerModuleListener(Project project) { myProject = project; }
+
+  @Override
+  public void moduleAdded(@NotNull Project project, @NotNull Module module) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(myProject);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      managerImpl.refreshPointers();
+    }
+  }
+
+  @Override
+  public void modulesRenamed(@NotNull Project project,
+                             @NotNull List<? extends Module> modules,
+                             @NotNull Function<? super Module, String> oldNameProvider) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(myProject);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      managerImpl.refreshPointers();
+    }
+  }
+}
diff --git a/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersFacetManagerListener.java b/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersFacetManagerListener.java
new file mode 100644 (file)
index 0000000..9072e4e
--- /dev/null
@@ -0,0 +1,45 @@
+// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+package com.intellij.facet.impl.pointers;
+
+import com.intellij.facet.Facet;
+import com.intellij.facet.FacetManagerListener;
+import com.intellij.facet.pointers.FacetPointersManager;
+import com.intellij.openapi.project.Project;
+import org.jetbrains.annotations.NotNull;
+
+public class FacetPointersFacetManagerListener implements FacetManagerListener {
+
+  private final Project myProject;
+
+  public FacetPointersFacetManagerListener(Project project) { myProject = project; }
+
+  @Override
+  public void facetAdded(@NotNull Facet facet) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(myProject);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      managerImpl.refreshPointers();
+    }
+  }
+
+  @Override
+  public void beforeFacetRenamed(@NotNull Facet facet) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(myProject);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      final FacetPointerImpl pointer = managerImpl.get(FacetPointersManager.constructId(facet));
+      if (pointer != null) {
+        pointer.refresh();
+      }
+    }
+  }
+
+  @Override
+  public void facetRenamed(@NotNull final Facet facet, @NotNull final String oldName) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(myProject);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      managerImpl.refreshPointers();
+    }
+  }
+}
index 585694236ff36474cdd07110274f0ae24d06e5e3..076600322c5b4acc2ce5a818bf2525dfe76abaaa 100644 (file)
@@ -1,24 +1,16 @@
 // Copyright 2000-2019 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.
 package com.intellij.facet.impl.pointers;
 
-import com.intellij.ProjectTopics;
 import com.intellij.facet.Facet;
-import com.intellij.facet.FacetManager;
-import com.intellij.facet.FacetManagerAdapter;
 import com.intellij.facet.impl.FacetUtil;
 import com.intellij.facet.pointers.FacetPointer;
 import com.intellij.facet.pointers.FacetPointerListener;
 import com.intellij.facet.pointers.FacetPointersManager;
 import com.intellij.openapi.Disposable;
-import com.intellij.openapi.module.Module;
-import com.intellij.openapi.module.ModuleManager;
-import com.intellij.openapi.project.ModuleListener;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.Pair;
 import com.intellij.util.EventDispatcher;
-import com.intellij.util.Function;
-import com.intellij.util.messages.MessageBusConnection;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.ArrayList;
@@ -33,7 +25,6 @@ public final class FacetPointersManagerImpl extends FacetPointersManager {
 
   public FacetPointersManagerImpl(@NotNull Project project) {
     myProject = project;
-    initComponent();
   }
 
   @Override
@@ -66,46 +57,7 @@ public final class FacetPointersManagerImpl extends FacetPointersManager {
     myPointers.remove(pointer.getId());
   }
 
-  private void initComponent() {
-    MessageBusConnection connection = myProject.getMessageBus().connect();
-    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {
-      @Override
-      public void moduleAdded(@NotNull Project project, @NotNull Module module) {
-        refreshPointers(module);
-      }
-
-      @Override
-      public void modulesRenamed(@NotNull Project project, @NotNull List<? extends Module> modules, @NotNull Function<? super Module, String> oldNameProvider) {
-        for (Module module : modules) {
-          refreshPointers(module);
-        }
-      }
-    });
-    connection.subscribe(FacetManager.FACETS_TOPIC, new FacetManagerAdapter() {
-      @Override
-      public void facetAdded(@NotNull Facet facet) {
-        refreshPointers(facet.getModule());
-      }
-
-      @Override
-      public void beforeFacetRenamed(@NotNull Facet facet) {
-        final FacetPointerImpl pointer = myPointers.get(constructId(facet));
-        if (pointer != null) {
-          pointer.refresh();
-        }
-      }
-
-      @Override
-      public void facetRenamed(@NotNull final Facet facet, @NotNull final String oldName) {
-        refreshPointers(facet.getModule());
-      }
-    });
-    for (Module module : ModuleManager.getInstance(myProject).getModules()) {
-      refreshPointers(module);
-    }
-  }
-
-  private void refreshPointers(@NotNull final Module module) {
+  void refreshPointers() {
     //todo[nik] refresh only pointers related to renamed module/facet?
     List<Pair<FacetPointerImpl, String>> changed = new ArrayList<>();
 
@@ -136,6 +88,10 @@ public final class FacetPointersManagerImpl extends FacetPointersManager {
     return myPointers.containsKey(pointer.getId());
   }
 
+  FacetPointerImpl get(String id) {
+    return myPointers.get(id);
+  }
+
   @Override
   public void addListener(final FacetPointerListener<Facet> listener) {
     addListener(Facet.class, listener);
diff --git a/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersPostStartupActivity.java b/platform/lang-impl/src/com/intellij/facet/impl/pointers/FacetPointersPostStartupActivity.java
new file mode 100644 (file)
index 0000000..0aa86ed
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+package com.intellij.facet.impl.pointers;
+
+import com.intellij.facet.pointers.FacetPointersManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.startup.StartupActivity;
+import org.jetbrains.annotations.NotNull;
+
+public class FacetPointersPostStartupActivity implements StartupActivity {
+  @Override
+  public void runActivity(@NotNull Project project) {
+    FacetPointersManager manager = FacetPointersManager.getInstance(project);
+    if (manager instanceof FacetPointersManagerImpl) {
+      FacetPointersManagerImpl managerImpl = (FacetPointersManagerImpl)manager;
+      managerImpl.refreshPointers();
+    }
+  }
+}
index bc6a4d4183f49574cee89ff3cbf1fb733ec92934..fdbfbf6523c6e0b12678b6a72a147192e4837988 100644 (file)
       <interface-class>com.intellij.openapi.roots.impl.PushedFilePropertiesUpdater</interface-class>
       <implementation-class>com.intellij.openapi.roots.impl.PushedFilePropertiesUpdaterImpl</implementation-class>
     </component>
-
-    <component>
-      <interface-class>com.intellij.facet.pointers.FacetPointersManager</interface-class>
-      <implementation-class>com.intellij.facet.impl.pointers.FacetPointersManagerImpl</implementation-class>
-    </component>
   </project-components>
 
   <extensionPoints>
     <projectService serviceImplementation="com.intellij.psi.search.scope.packageSet.UpdatingScopeOnProjectStructureChangeListener"
                     preload="await"/>
     <startupActivity implementation="com.intellij.codeInsight.actions.ReaderModeEditorSettingsListener"/>
+
+    <projectService serviceImplementation="com.intellij.facet.impl.pointers.FacetPointersManagerImpl"
+                    serviceInterface="com.intellij.facet.pointers.FacetPointersManager"/>
+    <postStartupActivity implementation="com.intellij.facet.impl.pointers.FacetPointersPostStartupActivity"/>
   </extensions>
 
   <!--suppress PluginXmlDynamicPlugin -->
               topic="com.intellij.openapi.fileEditor.FileEditorManagerListener" activeInHeadlessMode="false"/>
     <listener class="com.intellij.codeInsight.actions.ReaderModeSettingsListener"
               topic="com.intellij.codeInsight.actions.ReaderModeListener" activeInHeadlessMode="false"/>
+
+    <listener class="com.intellij.facet.impl.pointers.FacetPointerModuleListener" topic="com.intellij.openapi.project.ModuleListener"/>
+    <listener class="com.intellij.facet.impl.pointers.FacetPointersFacetManagerListener" topic="com.intellij.facet.FacetManagerListener"/>
   </projectListeners>
 </idea-plugin>