Migrate text attributes to color scheme version 142

This commit is contained in:
Rustam Vishnyakov
2015-08-13 18:45:20 +03:00
parent d639a9e696
commit 8e779e25bd
5 changed files with 42 additions and 41 deletions
@@ -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);
@@ -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();
}
}
@@ -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);
@@ -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<Element, EditorColorsScheme, Throwable>() {
@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<Element, EditorColorsScheme, Throwable>() {
@Override
public EditorColorsScheme convert(Element element) throws Throwable {
return new ReadOnlyColorsSchemeImpl(element);
}
});
}
}
}
@@ -245,6 +245,14 @@ public class EditorColorsSchemeImplTest extends LightPlatformCodeInsightTestCase
EditorColorsScheme scheme = loadScheme(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<scheme name=\"Test\" version=\"141\" parent_scheme=\"Default\">\n" +
// Some 'attributes' section is required for the upgrade procedure to work.
"<attributes>" +
" <option name=\"TEXT\">\n" +
" <value>\n" +
" option name=\"FOREGROUND\" value=\"ffaaaa\" />\n" +
" </value>\n" +
" </option>" +
"</attributes>" +
"</scheme>\n"
);
@@ -264,7 +272,6 @@ public class EditorColorsSchemeImplTest extends LightPlatformCodeInsightTestCase
EditorColorsScheme targetScheme = new EditorColorsSchemeImpl(defaultScheme);
targetScheme.readExternal(root);
((AbstractColorsScheme)targetScheme).upgradeSchemeFromPreviousVersion();
return targetScheme;
}