setAttributes — use equals

This commit is contained in:
Vladimir Krivosheev
2016-09-29 17:03:17 +02:00
parent f3233caed9
commit 2db4b63864
7 changed files with 145 additions and 105 deletions
@@ -27,7 +27,7 @@ import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
internal class InspectionSchemeTest {
class InspectionSchemeTest {
companion object {
@JvmField
@ClassRule
@@ -35,7 +35,7 @@ import org.junit.Rule
import org.junit.Test
import java.nio.file.Paths
internal class ProjectInspectionManagerTest {
class ProjectInspectionManagerTest {
companion object {
@JvmField
@ClassRule
@@ -77,10 +77,6 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
// version influences XML format and triggers migration
private int myVersion = CURR_VERSION;
/**
* The version from the original file.
*/
private int myOriginalVersion = CURR_VERSION;
protected Map<ColorKey, Color> myColorsMap = ContainerUtilRt.newHashMap();
protected Map<TextAttributesKey, TextAttributes> myAttributesMap = ContainerUtilRt.newHashMap();
@@ -133,8 +129,7 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
public AbstractColorsScheme() {
}
public void setDefaultMetaInfo(@Nullable AbstractColorsScheme parentScheme) {
myMetaInfo.setProperty(META_INFO_CREATION_TIME, META_INFO_DATE_FORMAT.format(new Date()));
myMetaInfo.setProperty(META_INFO_IDE, PlatformUtils.getPlatformPrefix());
@@ -334,7 +329,6 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
}
myVersion = readVersion;
myOriginalVersion = readVersion;
String isDefaultScheme = node.getAttributeValue(DEFAULT_SCHEME_ATTR);
boolean isDefault = isDefaultScheme != null && Boolean.parseBoolean(isDefaultScheme);
if (!isDefault) {
@@ -445,8 +439,7 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
};
private void readColors(Element childNode) {
for (final Object o : childNode.getChildren(OPTION_ELEMENT)) {
Element colorElement = (Element)o;
for (Element colorElement : childNode.getChildren(OPTION_ELEMENT)) {
Color valueColor = myValueReader.read(Color.class, colorElement);
final String colorName = colorElement.getAttributeValue(NAME_ATTR);
if (BACKGROUND_COLOR_NAME.equals(colorName)) {
@@ -459,43 +452,53 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
}
}
private void readSettings(Element childNode, boolean isDefault) {
String name = childNode.getAttributeValue(NAME_ATTR);
if (LINE_SPACING.equals(name)) {
Float value = myValueReader.read(Float.class, childNode);
if (value != null) myLineSpacing = value;
}
else if (EDITOR_FONT_SIZE.equals(name)) {
int value = readFontSize(childNode, isDefault);
if (value > 0) setEditorFontSize(value);
}
else if (EDITOR_FONT_NAME.equals(name)) {
String value = myValueReader.read(String.class, childNode);
if (value != null) setEditorFontName(value);
}
else if (CONSOLE_LINE_SPACING.equals(name)) {
Float value = myValueReader.read(Float.class, childNode);
if (value != null) setConsoleLineSpacing(value);
}
else if (CONSOLE_FONT_SIZE.equals(name)) {
int value = readFontSize(childNode, isDefault);
if (value > 0) setConsoleFontSize(value);
}
else if (CONSOLE_FONT_NAME.equals(name)) {
String value = myValueReader.read(String.class, childNode);
if (value != null) setConsoleFontName(value);
}
else if (EDITOR_QUICK_JAVADOC_FONT_SIZE.equals(name)) {
FontSize value = myValueReader.read(FontSize.class, childNode);
if (value != null) myQuickDocFontSize = value;
}
else if (EDITOR_LIGATURES.equals(name)) {
Boolean value = myValueReader.read(Boolean.class, childNode);
if (value != null) myFontPreferences.setUseLigatures(value);
}
else if (CONSOLE_LIGATURES.equals(name)) {
Boolean value = myValueReader.read(Boolean.class, childNode);
if (value != null) myConsoleFontPreferences.setUseLigatures(value);
private void readSettings(@NotNull Element childNode, boolean isDefault) {
switch (childNode.getAttributeValue(NAME_ATTR)) {
case LINE_SPACING: {
Float value = myValueReader.read(Float.class, childNode);
if (value != null) myLineSpacing = value;
break;
}
case EDITOR_FONT_SIZE: {
int value = readFontSize(childNode, isDefault);
if (value > 0) setEditorFontSize(value);
break;
}
case EDITOR_FONT_NAME: {
String value = myValueReader.read(String.class, childNode);
if (value != null) setEditorFontName(value);
break;
}
case CONSOLE_LINE_SPACING: {
Float value = myValueReader.read(Float.class, childNode);
if (value != null) setConsoleLineSpacing(value);
break;
}
case CONSOLE_FONT_SIZE: {
int value = readFontSize(childNode, isDefault);
if (value > 0) setConsoleFontSize(value);
break;
}
case CONSOLE_FONT_NAME: {
String value = myValueReader.read(String.class, childNode);
if (value != null) setConsoleFontName(value);
break;
}
case EDITOR_QUICK_JAVADOC_FONT_SIZE: {
FontSize value = myValueReader.read(FontSize.class, childNode);
if (value != null) myQuickDocFontSize = value;
break;
}
case EDITOR_LIGATURES: {
Boolean value = myValueReader.read(Boolean.class, childNode);
if (value != null) myFontPreferences.setUseLigatures(value);
break;
}
case CONSOLE_LIGATURES: {
Boolean value = myValueReader.read(Boolean.class, childNode);
if (value != null) myConsoleFontPreferences.setUseLigatures(value);
break;
}
}
}
@@ -525,6 +528,13 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
}
}
private static void addOptionTag(@NotNull Element parentNode, @NotNull String name, @NotNull String value) {
Element element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, name);
element.setAttribute(VALUE_ELEMENT, value);
parentNode.addContent(element);
}
public void writeExternal(Element parentNode) {
parentNode.setAttribute(NAME_ATTR, getName());
parentNode.setAttribute(VERSION_ATTR, Integer.toString(myVersion));
@@ -537,20 +547,16 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
parentNode.addContent(metaInfoToElement());
}
Element element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, LINE_SPACING);
element.setAttribute(VALUE_ELEMENT, String.valueOf(getLineSpacing()));
parentNode.addContent(element);
if (getLineSpacing() != 1) {
addOptionTag(parentNode, LINE_SPACING, String.valueOf(getLineSpacing()));
}
// IJ has used a 'single customizable font' mode for ages. That's why we want to support that format now, when it's possible
// to specify fonts sequence (see getFontPreferences()), there are big chances that many clients still will use a single font.
// That's why we want to use old format when zero or one font is selected and 'extended' format otherwise.
boolean useOldFontFormat = myFontPreferences.getEffectiveFontFamilies().size() <= 1;
if (useOldFontFormat) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, EDITOR_FONT_SIZE);
element.setAttribute(VALUE_ELEMENT, String.valueOf(getEditorFontSize()));
parentNode.addContent(element);
addOptionTag(parentNode, EDITOR_FONT_SIZE, String.valueOf(getEditorFontSize()));
}
else {
writeFontPreferences(EDITOR_FONT, parentNode, myFontPreferences);
@@ -559,16 +565,10 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
if (!myFontPreferences.equals(myConsoleFontPreferences)) {
if (myConsoleFontPreferences.getEffectiveFontFamilies().size() <= 1) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, CONSOLE_FONT_NAME);
element.setAttribute(VALUE_ELEMENT, getConsoleFontName());
parentNode.addContent(element);
addOptionTag(parentNode, CONSOLE_FONT_NAME, getConsoleFontName());
if (getConsoleFontSize() != getEditorFontSize()) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, CONSOLE_FONT_SIZE);
element.setAttribute(VALUE_ELEMENT, Integer.toString(getConsoleFontSize()));
parentNode.addContent(element);
addOptionTag(parentNode, CONSOLE_FONT_SIZE, Integer.toString(getConsoleFontSize()));
}
}
else {
@@ -578,24 +578,15 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
}
if (getConsoleLineSpacing() != getLineSpacing()) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, CONSOLE_LINE_SPACING);
element.setAttribute(VALUE_ELEMENT, Float.toString(getConsoleLineSpacing()));
parentNode.addContent(element);
addOptionTag(parentNode, CONSOLE_LINE_SPACING, Float.toString(getConsoleLineSpacing()));
}
if (DEFAULT_FONT_SIZE != getQuickDocFontSize()) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, EDITOR_QUICK_JAVADOC_FONT_SIZE);
element.setAttribute(VALUE_ELEMENT, getQuickDocFontSize().toString());
parentNode.addContent(element);
addOptionTag(parentNode, EDITOR_QUICK_JAVADOC_FONT_SIZE, getQuickDocFontSize().toString());
}
if (useOldFontFormat) {
element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, EDITOR_FONT_NAME);
element.setAttribute(VALUE_ELEMENT, getEditorFontName());
parentNode.addContent(element);
addOptionTag(parentNode, EDITOR_FONT_NAME, getEditorFontName());
}
Element colorElements = new Element(COLORS_ELEMENT);
@@ -616,26 +607,15 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
private static void writeLigaturesPreferences(Element parentNode, FontPreferences preferences, String optionName) {
if (preferences.useLigatures()) {
Element element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, optionName);
element.setAttribute(VALUE_ELEMENT, String.valueOf(true));
parentNode.addContent(element);
addOptionTag(parentNode, optionName, String.valueOf(true));
}
}
private static void writeFontPreferences(@NotNull String key, @NotNull Element parent, @NotNull FontPreferences preferences) {
for (String fontFamily : preferences.getRealFontFamilies()) {
Element element = new Element(key);
Element e = new Element(OPTION_ELEMENT);
e.setAttribute(NAME_ATTR, EDITOR_FONT_NAME);
e.setAttribute(VALUE_ELEMENT, fontFamily);
element.addContent(e);
e = new Element(OPTION_ELEMENT);
e.setAttribute(NAME_ATTR, EDITOR_FONT_SIZE);
e.setAttribute(VALUE_ELEMENT, String.valueOf(preferences.getSize(fontFamily)));
element.addContent(e);
addOptionTag(element, EDITOR_FONT_NAME, fontFamily);
addOptionTag(element, EDITOR_FONT_SIZE, String.valueOf(preferences.getSize(fontFamily)));
parent.addContent(element);
}
}
@@ -700,15 +680,12 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
for (ColorKey key : list) {
if (haveToWrite(key)) {
Color value = myColorsMap.get(key);
Element element = new Element(OPTION_ELEMENT);
element.setAttribute(NAME_ATTR, key.getExternalName());
element.setAttribute(VALUE_ELEMENT, value != null ? Integer.toString(value.getRGB() & 0xFFFFFF, 16) : "");
colorElements.addContent(element);
addOptionTag(colorElements, key.getExternalName(), value == null ? "" : Integer.toString(value.getRGB() & 0xFFFFFF, 16));
}
}
}
private boolean haveToWrite(final ColorKey key) {
private boolean haveToWrite(@NotNull ColorKey key) {
Color value = myColorsMap.get(key);
if (myParentScheme != null) {
if (myParentScheme instanceof AbstractColorsScheme) {
@@ -716,10 +693,8 @@ public abstract class AbstractColorsScheme implements EditorColorsScheme, Serial
return false;
}
}
else {
if (Comparing.equal((myParentScheme).getColor(key), value)) {
return false;
}
else if (Comparing.equal((myParentScheme).getColor(key), value)) {
return false;
}
}
return true;
@@ -26,9 +26,6 @@ import org.jetbrains.annotations.Nullable;
import java.awt.*;
/**
* @author Yura Cangea
*/
public class EditorColorsSchemeImpl extends AbstractColorsScheme implements ExternalizableScheme {
public EditorColorsSchemeImpl(EditorColorsScheme parentScheme) {
super(parentScheme);
@@ -36,7 +33,7 @@ public class EditorColorsSchemeImpl extends AbstractColorsScheme implements Exte
@Override
public void setAttributes(@NotNull TextAttributesKey key, TextAttributes attributes) {
if (attributes != getAttributes(key)) {
if (!attributes.equals(getAttributes(key))) {
myAttributesMap.put(key, attributes);
}
}
@@ -30,5 +30,6 @@
<orderEntry type="library" scope="TEST" name="assertJ" level="project" />
<orderEntry type="library" scope="TEST" name="KotlinTest" level="project" />
<orderEntry type="library" scope="RUNTIME" name="purejavacomm" level="project" />
<orderEntry type="module" module-name="configuration-store-impl" scope="TEST" />
</component>
</module>
@@ -0,0 +1,68 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.editor.colors
import com.intellij.configurationStore.SchemeManagerFactoryBase
import com.intellij.openapi.editor.colors.ex.DefaultColorSchemesManager
import com.intellij.openapi.editor.colors.impl.EditorColorsManagerImpl
import com.intellij.testFramework.InMemoryFsRule
import com.intellij.testFramework.ProjectRule
import com.intellij.util.io.readText
import com.intellij.util.io.write
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.hasChildren
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
class EditorColorSchemeTest {
companion object {
@JvmField
@ClassRule
val projectRule = ProjectRule()
}
@JvmField
@Rule
val fsRule = InMemoryFsRule()
@Test fun loadSchemes() {
val schemeFile = fsRule.fs.getPath("colors/Foo.icls")
val schemeData = """
<scheme name="Foo" version="142" parent_scheme="Default">
<metaInfo>
<property name="created">2016-09-29T12:13:05</property>
<property name="ide">idea</property>
<property name="ideVersion">2016.3.0.0</property>
<property name="modified">2016-09-29T12:14:54</property>
<property name="originalScheme">Default</property>
</metaInfo>
<option name="EDITOR_FONT_SIZE" value="12" />
<option name="EDITOR_FONT_NAME" value="Menlo" />
</scheme>""".trimIndent()
schemeFile.write(schemeData)
val schemeManagerFactory = SchemeManagerFactoryBase.TestSchemeManagerFactory(fsRule.fs.getPath(""))
val manager = EditorColorsManagerImpl(DefaultColorSchemesManager.getInstance(), schemeManagerFactory)
val scheme = manager.getScheme("Foo")
assertThat(scheme.name).isEqualTo("Foo")
schemeManagerFactory.save()
assertThat(schemeFile.readText()).isEqualTo(schemeData)
assertThat(schemeFile.parent).hasChildren("Foo.icls")
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -205,7 +205,6 @@ public class EditorColorsSchemeImplTest extends EditorColorSchemeTestCase {
Pair<EditorColorsScheme, TextAttributes> result = doTestWriteRead(DefaultLanguageHighlighterColors.FUNCTION_DECLARATION, declarationAttrs);
TextAttributes fallbackAttrs = result.first.getAttributes(DefaultLanguageHighlighterColors.FUNCTION_DECLARATION.getFallbackAttributeKey());
assertThat(result.second).isEqualTo(fallbackAttrs);
assertThat(result.second).isNotSameAs(fallbackAttrs);
}
public void testSaveInheritanceForEmptyAttrs() throws Exception {