fix project inspection scheme reloading
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Fri, 8 Jul 2016 13:40:00 +0000 (15:40 +0200)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Fri, 8 Jul 2016 13:40:00 +0000 (15:40 +0200)
14 files changed:
platform/analysis-api/src/com/intellij/profile/ProfileChangeAdapter.java
platform/analysis-impl/src/com/intellij/profile/codeInspection/BaseInspectionProfileManager.kt
platform/analysis-impl/src/com/intellij/profile/codeInspection/InspectionProfileManager.java
platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt
platform/configuration-store-impl/src/SchemeManagerImpl.kt
platform/core-api/src/com/intellij/openapi/options/scheme.kt
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/LocalInspectionsPassFactory.java
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/WholeFileLocalInspectionsPassFactory.java
platform/lang-impl/src/com/intellij/codeInspection/ex/ApplicationInspectionProfileManager.java
platform/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java
platform/platform-impl/src/com/intellij/openapi/keymap/impl/KeymapManagerImpl.java
platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java
xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementAnnotationsManagerImpl.java

index 319f86da9d1c4c59a5ca49d6252dbdaf78ef01ea..026237adca819babbc0b076e810c19ce402bbde9 100644 (file)
@@ -21,7 +21,7 @@ public interface ProfileChangeAdapter {
   default void profileChanged(Profile profile) {
   }
 
-  default void profileActivated(@Nullable Profile oldProfile, Profile profile) {
+  default void profileActivated(@Nullable Profile oldProfile, @Nullable Profile profile) {
   }
 
   default void profilesInitialized() {
index 5b19c7963fffd621c3fba481e06c41f44f9d9632..75227d744d048065c36465b45e276ab7e3903da7 100644 (file)
@@ -33,7 +33,7 @@ import com.intellij.util.messages.MessageBus
 internal val LOG = Logger.getInstance(BaseInspectionProfileManager::class.java)
 
 abstract class BaseInspectionProfileManager(messageBus: MessageBus) :  InspectionProfileManager {
-  protected abstract val schemeManager: SchemeManager<InspectionProfile>
+  protected abstract val schemeManager: SchemeManager<InspectionProfileImpl>
 
   protected val profileListeners: MutableList<ProfileChangeAdapter> = ContainerUtil.createLockFreeCopyOnWriteList<ProfileChangeAdapter>()
   private val severityRegistrar = SeverityRegistrar(messageBus)
@@ -79,8 +79,8 @@ abstract class BaseInspectionProfileManager(messageBus: MessageBus) :  Inspectio
     }
   }
 
-  fun addProfile(profile: Profile) {
-    schemeManager.addScheme(profile as InspectionProfile)
+  fun addProfile(profile: InspectionProfileImpl) {
+    schemeManager.addScheme(profile)
   }
 
   override final fun deleteProfile(name: String) {
@@ -94,13 +94,13 @@ abstract class BaseInspectionProfileManager(messageBus: MessageBus) :  Inspectio
   }
 
   override fun updateProfile(profile: Profile) {
-    schemeManager.addScheme(profile as InspectionProfile)
+    schemeManager.addScheme(profile as InspectionProfileImpl)
     fireProfileChanged(profile)
   }
 }
 
-abstract class InspectionProfileProcessor : LazySchemeProcessor<InspectionProfile, InspectionProfileImpl>() {
-  override final fun getState(scheme: InspectionProfile): SchemeState {
+abstract class InspectionProfileProcessor : LazySchemeProcessor<InspectionProfileImpl, InspectionProfileImpl>() {
+  override final fun getState(scheme: InspectionProfileImpl): SchemeState {
     if (scheme is InspectionProfileImpl) {
       return if (scheme.wasInitialized()) SchemeState.POSSIBLY_CHANGED else SchemeState.UNCHANGED
     }
index d30519a73db3e3a45ebcf97701eccbe9b5c8e68d..6b8187a7ced27f17e8f2ecc97a1657a1533313ec 100644 (file)
@@ -48,7 +48,7 @@ public interface InspectionProfileManager extends ProfileManager, SeverityProvid
 
   void fireProfileChanged(@Nullable Profile oldProfile, @NotNull Profile profile);
 
-  void setRootProfile(@Nullable String profileName);
+  void setRootProfile(@Nullable String name);
 
   @SuppressWarnings("unused")
   @NotNull
index a48d38ddfb7604e09791d27b5f2b36c525c82ed3..68949aaa4605e4bb51d78e8f7fd3158ac2c2e89d 100644 (file)
@@ -77,7 +77,7 @@ class ProjectInspectionProfileManager(val project: Project,
     }
   }
 
-  override val schemeManager: SchemeManager<InspectionProfile>
+  override val schemeManager: SchemeManager<InspectionProfileImpl>
 
   private data class State(@field:com.intellij.util.xmlb.annotations.OptionTag("PROJECT_PROFILE") var projectProfile: String? = PROJECT_DEFAULT_PROFILE_NAME,
                            @field:com.intellij.util.xmlb.annotations.OptionTag("USE_PROJECT_PROFILE") var useProjectProfile: Boolean = true)
@@ -97,7 +97,15 @@ class ProjectInspectionProfileManager(val project: Project,
       }
 
       override fun onSchemeAdded(scheme: InspectionProfileImpl) {
-        fireProfileChanged(scheme)
+        if (scheme.wasInitialized()) {
+          fireProfileChanged(scheme)
+        }
+      }
+
+      override fun onCurrentSchemeSwitched(oldScheme: InspectionProfileImpl?, newScheme: InspectionProfileImpl?) {
+        for (adapter in profileListeners) {
+          adapter.profileActivated(oldScheme, newScheme)
+        }
       }
     }, isUseOldFileNameSanitize = true)
 
@@ -206,11 +214,17 @@ class ProjectInspectionProfileManager(val project: Project,
         }
       }
     }
+
+    if (newState.useProjectProfile) {
+      schemeManager.currentSchemeName = newState.projectProfile
+    }
   }
 
   @Synchronized override fun getState(): Element? {
     val result = Element("settings")
-    XmlSerializer.serializeInto(this.state, result, skipDefaultsSerializationFilter)
+    val state = this.state
+    state.projectProfile = schemeManager.currentSchemeName
+    XmlSerializer.serializeInto(state, result, skipDefaultsSerializationFilter)
     if (!result.children.isEmpty()) {
       result.addContent(Element("version").setAttribute("value", VERSION))
     }
@@ -235,20 +249,12 @@ class ProjectInspectionProfileManager(val project: Project,
   @Synchronized override fun getAvailableProfileNames(): Array<String> = schemeManager.allSchemeNames.toTypedArray()
 
   val projectProfile: String?
-    get() = state.projectProfile
+    get() = schemeManager.currentSchemeName
 
-  @Synchronized override fun setRootProfile(newProfile: String?) {
-    if (newProfile == state.projectProfile) {
-      return
-    }
-
-    val oldProfile = state.projectProfile
-    state.projectProfile = newProfile
-    state.useProjectProfile = newProfile != null
-    oldProfile?.let {
-      for (adapter in profileListeners) {
-        adapter.profileActivated(getProfile(oldProfile), newProfile?.let { getProfile(it) })
-      }
+  @Synchronized override fun setRootProfile(name: String?) {
+    if (name != schemeManager.currentSchemeName) {
+      schemeManager.currentSchemeName = name
+      state.useProjectProfile = name != null
     }
   }
 
@@ -257,23 +263,20 @@ class ProjectInspectionProfileManager(val project: Project,
       return applicationProfileManager.currentProfile as InspectionProfileImpl
     }
 
-    val currentName = state.projectProfile
-    if (currentName == null || schemeManager.isEmpty) {
-      state.projectProfile = PROJECT_DEFAULT_PROFILE_NAME
-      val projectProfile = InspectionProfileImpl(PROJECT_DEFAULT_PROFILE_NAME, InspectionToolRegistrar.getInstance(), this, InspectionProfileImpl.getDefaultProfile(), null)
-      projectProfile.copyFrom(applicationProfileManager.currentProfile)
-      projectProfile.isProjectLevel = true
-      projectProfile.setName(PROJECT_DEFAULT_PROFILE_NAME)
-      schemeManager.addScheme(projectProfile)
-      return projectProfile
-    }
-
-    var profile = schemeManager.findSchemeByName(currentName)
-    if (profile == null) {
-      profile = schemeManager.allSchemes.get(0)!!
-      state.projectProfile = profile.name
+    var currentScheme = schemeManager.currentScheme
+    if (currentScheme == null) {
+      currentScheme = schemeManager.allSchemes.firstOrNull()
+      if (currentScheme == null) {
+        currentScheme = InspectionProfileImpl(PROJECT_DEFAULT_PROFILE_NAME, InspectionToolRegistrar.getInstance(), this,
+                                            InspectionProfileImpl.getDefaultProfile(), null)
+        currentScheme.copyFrom(applicationProfileManager.currentProfile)
+        currentScheme.isProjectLevel = true
+        currentScheme.setName(PROJECT_DEFAULT_PROFILE_NAME)
+        schemeManager.addScheme(currentScheme)
+      }
+      schemeManager.setCurrent(currentScheme, false)
     }
-    return profile as InspectionProfileImpl
+    return currentScheme
   }
 
   private fun fireProfilesInitialized() {
index 0c1d47cc4d1870333036fbd8db3f8284a8c055e2..fbcbe28c5c8b92f5a59505ea8dab01e068dce234 100644 (file)
@@ -133,13 +133,11 @@ class SchemeManagerImpl<T : Scheme, MUTABLE_SCHEME : T>(val fileSpec: String,
               processor.onSchemeDeleted(it)
             }
 
-            val newScheme = readSchemeFromFile(event.file, false)?.let {
+            updateCurrentScheme(oldCurrentScheme, readSchemeFromFile(event.file, false)?.let {
               processor.initScheme(it)
               processor.onSchemeAdded(it)
               it
-            }
-
-            updateCurrentScheme(oldCurrentScheme, newScheme)
+            })
           }
 
           is VFileCreateEvent -> {
@@ -188,17 +186,22 @@ class SchemeManagerImpl<T : Scheme, MUTABLE_SCHEME : T>(val fileSpec: String,
       }
     }
 
-    private fun updateCurrentScheme(oldCurrentScheme: T?, newCurrentScheme: T? = null) {
+    private fun updateCurrentScheme(oldScheme: T?, newScheme: T? = null) {
       if (currentScheme != null) {
         return
       }
 
-      if (oldCurrentScheme != currentScheme) {
-        @Suppress("UNCHECKED_CAST")
-        setCurrent(newCurrentScheme ?: schemes.firstOrNull())
+      if (oldScheme != currentScheme) {
+        val scheme = newScheme ?: schemes.firstOrNull()
+        currentPendingSchemeName = null
+        currentScheme = scheme
+        // must be equals by reference
+        if (oldScheme !== scheme) {
+          processor.onCurrentSchemeSwitched(oldScheme, scheme)
+        }
       }
-      else if (newCurrentScheme != null) {
-        processPendingCurrentSchemeName(newCurrentScheme)
+      else if (newScheme != null) {
+        processPendingCurrentSchemeName(newScheme)
       }
     }
   }
@@ -770,15 +773,21 @@ class SchemeManagerImpl<T : Scheme, MUTABLE_SCHEME : T>(val fileSpec: String,
     schemes.addAll(newSchemes)
 
     if (oldCurrentScheme != newCurrentScheme) {
+      val newScheme: T?
       if (newCurrentScheme != null) {
         currentScheme = newCurrentScheme
+        newScheme = newCurrentScheme
       }
       else if (oldCurrentScheme != null && !schemes.contains(oldCurrentScheme)) {
-        currentScheme = schemes.firstOrNull()
+        newScheme = schemes.firstOrNull()
+        currentScheme = newScheme
+      }
+      else {
+        newScheme = null
       }
 
-      if (oldCurrentScheme != currentScheme) {
-        processor.onCurrentSchemeChanged(oldCurrentScheme)
+      if (oldCurrentScheme != newScheme) {
+        processor.onCurrentSchemeSwitched(oldCurrentScheme, newScheme)
       }
     }
   }
@@ -885,8 +894,8 @@ class SchemeManagerImpl<T : Scheme, MUTABLE_SCHEME : T>(val fileSpec: String,
 
     val oldCurrent = currentScheme
     currentScheme = scheme
-    if (notify && oldCurrent != scheme) {
-      processor.onCurrentSchemeChanged(oldCurrent)
+    if (notify && oldCurrent !== scheme) {
+      processor.onCurrentSchemeSwitched(oldCurrent, scheme)
     }
   }
 
index f52fb9a68ece47117f3cbe149499d0ad83babb29..e26317c0fa424391efa933727210f9f5eaf4e52b 100644 (file)
@@ -69,10 +69,7 @@ abstract class SchemeProcessor<SCHEME : Scheme, in MUTABLE_SCHEME: SCHEME> {
   open fun onSchemeDeleted(scheme: MUTABLE_SCHEME) {
   }
 
-  /**
-   * Scheme switched.
-   */
-  open fun onCurrentSchemeChanged(oldScheme: Scheme?) {
+  open fun onCurrentSchemeSwitched(oldScheme: SCHEME?, newScheme: SCHEME?) {
   }
 
   open fun getState(scheme: SCHEME): SchemeState = SchemeState.POSSIBLY_CHANGED
index df653d76f566127359237d1f0026e4473d4bd30f..fe7073c2c57009df03e9051c6201fdf0d57e0b46 100644 (file)
@@ -496,7 +496,7 @@ public class DaemonListeners implements Disposable {
     }
 
     @Override
-    public void profileActivated(Profile oldProfile, Profile profile) {
+    public void profileActivated(Profile oldProfile, @Nullable Profile profile) {
       stopDaemonAndRestartAllFiles("Profile activated");
     }
 
index d49cb875b2841d2ca676276e35f3705be41a3941..b942b8e02423b8fec18633985397415f24f88c43 100644 (file)
@@ -28,7 +28,6 @@ import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.TextRange;
-import com.intellij.profile.codeInspection.ProjectInspectionProfileManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.util.PsiUtilCore;
 import com.intellij.util.ArrayUtil;
@@ -60,7 +59,7 @@ public class LocalInspectionsPassFactory extends AbstractProjectComponent implem
   @Nullable
   public TextEditorHighlightingPass createHighlightingPass(@NotNull PsiFile file, @NotNull final Editor editor) {
     TextRange textRange = calculateRangeToProcess(editor);
-    if (textRange == null || !ProjectInspectionProfileManager.getInstanceImpl(file.getProject()).isCurrentProfileInitialized()){
+    if (textRange == null){
       return new ProgressableTextEditorHighlightingPass.EmptyPass(myProject, editor.getDocument());
     }
     TextRange visibleRange = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
index 784ca7d9358917016f2a260b03c5b3c02697735e..23bd04a88b5ce97b60099f49a8f33ce7266bca62 100644 (file)
@@ -32,7 +32,6 @@ import com.intellij.openapi.util.Disposer;
 import com.intellij.profile.Profile;
 import com.intellij.profile.ProfileChangeAdapter;
 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
-import com.intellij.profile.codeInspection.ProjectInspectionProfileManager;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiManager;
@@ -77,7 +76,7 @@ public class WholeFileLocalInspectionsPassFactory extends AbstractProjectCompone
       }
 
       @Override
-      public void profileActivated(Profile oldProfile, Profile profile) {
+      public void profileActivated(Profile oldProfile, @Nullable Profile profile) {
         myFileToolsCache.clear();
       }
     };
@@ -93,8 +92,7 @@ public class WholeFileLocalInspectionsPassFactory extends AbstractProjectCompone
       return null; //optimization
     }
 
-    if (!ProjectInspectionProfileManager.getInstanceImpl(file.getProject()).isCurrentProfileInitialized() ||
-        myFileToolsCache.containsKey(file) && !myFileToolsCache.get(file)) {
+    if (myFileToolsCache.containsKey(file) && !myFileToolsCache.get(file)) {
       return null;
     }
 
index 08864638c5f4ebc6510d19af2f3d11956b12806f..aae4c5ae460d6ed9ced378576f97bb69aedde2eb 100644 (file)
@@ -34,7 +34,6 @@ import com.intellij.openapi.components.State;
 import com.intellij.openapi.components.Storage;
 import com.intellij.openapi.editor.colors.TextAttributesKey;
 import com.intellij.openapi.extensions.Extensions;
-import com.intellij.openapi.options.Scheme;
 import com.intellij.openapi.options.SchemeManager;
 import com.intellij.openapi.options.SchemeManagerFactory;
 import com.intellij.openapi.project.Project;
@@ -74,7 +73,7 @@ public class ApplicationInspectionProfileManager extends BaseInspectionProfileMa
                                                                                                  SeverityProvider,
                                                                                                  PersistentStateComponent<Element> {
   private final InspectionToolRegistrar myRegistrar;
-  private final SchemeManager<InspectionProfile> mySchemeManager;
+  private final SchemeManager<InspectionProfileImpl> mySchemeManager;
   private final AtomicBoolean myProfilesAreInitialized = new AtomicBoolean(false);
 
   public static ApplicationInspectionProfileManager getInstanceImpl() {
@@ -114,10 +113,9 @@ public class ApplicationInspectionProfileManager extends BaseInspectionProfileMa
       }
 
       @Override
-      public void onCurrentSchemeChanged(@Nullable Scheme oldScheme) {
-        Profile current = mySchemeManager.getCurrentScheme();
-        if (current != null) {
-          fireProfileChanged((Profile)oldScheme, current);
+      public void onCurrentSchemeSwitched(@Nullable InspectionProfileImpl oldScheme, @Nullable InspectionProfileImpl newScheme) {
+        if (newScheme != null) {
+          fireProfileChanged(oldScheme, newScheme);
         }
         onProfilesChanged();
       }
@@ -126,7 +124,7 @@ public class ApplicationInspectionProfileManager extends BaseInspectionProfileMa
 
   @NotNull
   @Override
-  protected SchemeManager<InspectionProfile> getSchemeManager() {
+  protected SchemeManager<InspectionProfileImpl> getSchemeManager() {
     return mySchemeManager;
   }
 
index 38a6d707fadf1873fbcb1ca32bc84d68fbeddd87..e59bd140953e4f03bfffd33cfe7461976bd4c7d5 100644 (file)
@@ -31,7 +31,10 @@ import com.intellij.openapi.editor.colors.EditorColorsScheme;
 import com.intellij.openapi.editor.colors.TextAttributesKey;
 import com.intellij.openapi.editor.colors.ex.DefaultColorSchemesManager;
 import com.intellij.openapi.editor.markup.TextAttributes;
-import com.intellij.openapi.options.*;
+import com.intellij.openapi.options.BaseSchemeProcessor;
+import com.intellij.openapi.options.SchemeManager;
+import com.intellij.openapi.options.SchemeManagerFactory;
+import com.intellij.openapi.options.SchemeState;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.util.text.StringUtil;
@@ -99,11 +102,11 @@ public class EditorColorsManagerImpl extends EditorColorsManager implements Pers
       }
 
       @Override
-      public void onCurrentSchemeChanged(@Nullable Scheme oldScheme) {
+      public void onCurrentSchemeSwitched(@Nullable EditorColorsScheme oldScheme, @Nullable EditorColorsScheme newScheme) {
         LafManager.getInstance().updateUI();
         schemeChangedOrSwitched();
 
-        fireChanges(mySchemeManager.getCurrentScheme());
+        fireChanges(newScheme);
       }
 
       @NotNull
index 9fc03f3f66d23ca2d7bb1bd15cd86a07cf7c12fc..32303862b906c366024c8c6ab4f210865a9ef13a 100644 (file)
@@ -72,10 +72,9 @@ public class KeymapManagerImpl extends KeymapManagerEx implements PersistentStat
       }
 
       @Override
-      public void onCurrentSchemeChanged(@Nullable Scheme oldScheme) {
-        Keymap keymap = mySchemeManager.getCurrentScheme();
+      public void onCurrentSchemeSwitched(@Nullable Keymap oldScheme, @Nullable Keymap newScheme) {
         for (KeymapManagerListener listener : myListeners) {
-          listener.activeKeymapChanged(keymap);
+          listener.activeKeymapChanged(newScheme);
         }
       }
     };
index 156a2a12261ea27ade8a20678d8b55db4fd8e946..daba1ab3e726f5b931aac4e956085fffb46caf59 100644 (file)
@@ -196,13 +196,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
                                           @NotNull final Project project,
                                           @NotNull final Collection<String> disabledInspections,
                                           @NotNull Disposable parentDisposable) {
-    InspectionToolWrapper[] wrapped =
-      ContainerUtil.map2Array(tools, InspectionToolWrapper.class, new Function<InspectionProfileEntry, InspectionToolWrapper>() {
-        @Override
-        public InspectionToolWrapper fun(InspectionProfileEntry tool) {
-          return InspectionToolRegistrar.wrapTool(tool);
-        }
-      });
+    InspectionToolWrapper[] wrapped = ContainerUtil.map2Array(tools, InspectionToolWrapper.class, InspectionToolRegistrar::wrapTool);
 
     final InspectionProfileImpl profile = InspectionProfileImpl.createSimple(LightPlatformTestCase.PROFILE, project, wrapped);
     profile.disableToolByDefault(new ArrayList<String>(disabledInspections), project);
index 1bd43cc1ab4216f36b706288a256e109f0083417..16e961c697fcf16918376595f201dfc6c3cb01d0 100644 (file)
@@ -111,7 +111,7 @@ public class DomElementAnnotationsManagerImpl extends DomElementAnnotationsManag
     myProject = project;
     final ProfileChangeAdapter profileChangeAdapter = new ProfileChangeAdapter() {
       @Override
-      public void profileActivated(Profile oldProfile, Profile profile) {
+      public void profileActivated(Profile oldProfile, @Nullable Profile profile) {
         dropAnnotationsCache();
       }