From 8e779e25bd9193f98175e53eead18357cebb3ed3 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Thu, 13 Aug 2015 17:30:16 +0300 Subject: [PATCH] Migrate text attributes to color scheme version 142 --- .../openapi/editor/markup/TextAttributes.java | 4 ++ .../colors/impl/AbstractColorsScheme.java | 22 ++++------ .../colors/impl/EditorColorsSchemeImpl.java | 6 +-- .../colors/impl/EditorColorsManagerImpl.java | 42 ++++++++----------- .../impl/EditorColorsSchemeImplTest.java | 9 +++- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java b/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java index c60cb7245f71..8d356e5bea48 100644 --- a/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java +++ b/platform/core-api/src/com/intellij/openapi/editor/markup/TextAttributes.java @@ -100,6 +100,10 @@ public class TextAttributes implements Cloneable { return isEmpty() && !myEnforcedDefaults; } + public boolean containsValue() { + return !isEmpty() || myEnforcedDefaults; + } + public void reset() { setForegroundColor(null); setBackgroundColor(null); diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java index 47d503a8b1f6..b6b5633f615d 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/AbstractColorsScheme.java @@ -30,7 +30,6 @@ import com.intellij.openapi.options.FontSize; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Couple; import com.intellij.openapi.util.WriteExternalException; -import com.intellij.openapi.util.registry.Registry; import com.intellij.util.containers.ContainerUtilRt; import com.intellij.util.containers.HashMap; import com.intellij.util.ui.JBUI; @@ -353,6 +352,7 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme { myAttributesMap.put(name, attr); migrateErrorStripeColorFrom14(name, attr); } + setMissingUndefinedAttributesForVersion142(); } private void migrateErrorStripeColorFrom14(@NotNull TextAttributesKey name, @NotNull TextAttributes attr) { @@ -364,25 +364,16 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme { } } - /** - * The method is called for the scheme when it is fully loaded including additional text attributes from providers. - */ - public void upgradeSchemeFromPreviousVersion() { - setUndefinedAttributesAsInheritedForVersion142(); - } - - /** * Defines empty attributes with fallback (inheritance) enabled for all the attributes explicitly defined in the parent scheme since * previously undefined attributes were treated as inherited, not taken from the parent scheme. */ - private void setUndefinedAttributesAsInheritedForVersion142() { + private void setMissingUndefinedAttributesForVersion142() { if (myOriginalVersion >= 142 || myParentScheme == null) return; if (myParentScheme instanceof AbstractColorsScheme) { for (TextAttributesKey key : ((AbstractColorsScheme)myParentScheme).myAttributesMap.keySet()) { TextAttributes parentAttributes = ((AbstractColorsScheme)myParentScheme).getDirectlyDefinedAttributes(key); - if (key.getFallbackAttributeKey() != null && - parentAttributes != null && + if (parentAttributes != null && !parentAttributes.isFallbackEnabled() && !myAttributesMap.containsKey(key)) { myAttributesMap.put(key, new TextAttributes()); @@ -599,7 +590,7 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme { } } else { - if (!value.equals(defaultAttr) || defaultAttr == defaultFallbackAttr) { + if (value.containsValue() && !value.equals(defaultAttr) || defaultAttr == defaultFallbackAttr) { Element valueElement = new Element(VALUE_ELEMENT); value.writeExternal(valueElement); element.addContent(valueElement); @@ -726,4 +717,9 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme { } return myParentScheme instanceof AbstractColorsScheme ? ((AbstractColorsScheme)myParentScheme).getDirectlyDefinedAttributes(key) : null; } + + + protected static boolean containsValue(@Nullable TextAttributes attributes) { + return attributes != null && attributes.containsValue(); + } } diff --git a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImpl.java b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImpl.java index 5629767d090f..0503c7aa5d2f 100644 --- a/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImpl.java +++ b/platform/editor-ui-ex/src/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImpl.java @@ -54,12 +54,12 @@ public class EditorColorsSchemeImpl extends AbstractColorsScheme implements Exte TextAttributesKey fallbackKey = key.getFallbackAttributeKey(); TextAttributes attributes = getDirectlyDefinedAttributes(key); if (fallbackKey == null) { - if (attributes != null) return attributes; + if (containsValue(attributes)) return attributes; } else { - if (attributes != null && !attributes.isFallbackEnabled()) return attributes; + if (containsValue(attributes) && !attributes.isFallbackEnabled()) return attributes; attributes = getFallbackAttributes(fallbackKey); - if (attributes != null) return attributes; + if (containsValue(attributes)) return attributes; } } return myParentScheme.getAttributes(key); diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java index e68fddb7745e..c5db992f656d 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/colors/impl/EditorColorsManagerImpl.java @@ -124,28 +124,10 @@ public class EditorColorsManagerImpl extends EditorColorsManager implements Pers } }, RoamingType.PER_USER); - for (DefaultColorsScheme defaultScheme : myDefaultColorSchemeManager.getAllSchemes()) { - mySchemeManager.addScheme(defaultScheme); - } - - // Load default schemes from providers - if (!isUnitTestOrHeadlessMode()) { - for (BundledColorSchemeEP ep : BundledColorSchemeEP.EP_NAME.getExtensions()) { - mySchemeManager.loadBundledScheme(ep.path + ".xml", ep, new ThrowableConvertor() { - @Override - public EditorColorsScheme convert(Element element) throws Throwable { - return new ReadOnlyColorsSchemeImpl(element); - } - }); - } - } - + initDefaultSchemes(); + loadBundledSchemes(); mySchemeManager.loadSchemes(); - loadAdditionalTextAttributes(); - - upgradeSchemesFromPreviousVersion(); - String wizardEditorScheme = WelcomeWizardUtil.getWizardEditorScheme(); EditorColorsScheme scheme = null; if (wizardEditorScheme != null) { @@ -155,10 +137,22 @@ public class EditorColorsManagerImpl extends EditorColorsManager implements Pers setGlobalSchemeInner(scheme == null ? getDefaultScheme() : scheme); } - private void upgradeSchemesFromPreviousVersion() { - for (EditorColorsScheme scheme : mySchemeManager.getAllSchemes()) { - if (scheme instanceof AbstractColorsScheme && !(scheme instanceof ReadOnlyColorsScheme)) { - ((AbstractColorsScheme)scheme).upgradeSchemeFromPreviousVersion(); + private void initDefaultSchemes() { + for (DefaultColorsScheme defaultScheme : myDefaultColorSchemeManager.getAllSchemes()) { + mySchemeManager.addScheme(defaultScheme); + } + loadAdditionalTextAttributes(); + } + + private void loadBundledSchemes() { + if (!isUnitTestOrHeadlessMode()) { + for (BundledColorSchemeEP ep : BundledColorSchemeEP.EP_NAME.getExtensions()) { + mySchemeManager.loadBundledScheme(ep.path + ".xml", ep, new ThrowableConvertor() { + @Override + public EditorColorsScheme convert(Element element) throws Throwable { + return new ReadOnlyColorsSchemeImpl(element); + } + }); } } } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImplTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImplTest.java index 0a2a04ab1503..d98143036530 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImplTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/colors/impl/EditorColorsSchemeImplTest.java @@ -245,6 +245,14 @@ public class EditorColorsSchemeImplTest extends LightPlatformCodeInsightTestCase EditorColorsScheme scheme = loadScheme( "\n" + "\n" + + // Some 'attributes' section is required for the upgrade procedure to work. + "" + + " " + + "" + "\n" ); @@ -264,7 +272,6 @@ public class EditorColorsSchemeImplTest extends LightPlatformCodeInsightTestCase EditorColorsScheme targetScheme = new EditorColorsSchemeImpl(defaultScheme); targetScheme.readExternal(root); - ((AbstractColorsScheme)targetScheme).upgradeSchemeFromPreviousVersion(); return targetScheme; }