Load kotlin formatter settings with obsolete defaults for restore (KT-23400)

This commit is contained in:
Nikolay Krasko
2018-08-07 13:26:07 +03:00
parent a22f4232c0
commit a1759fe2ab
5 changed files with 87 additions and 8 deletions
+2 -3
View File
@@ -55,9 +55,6 @@
<inspection_tool class="DoubleCheckedLocking" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreOnVolatileVariables" value="true" />
</inspection_tool>
<inspection_tool class="DuplicateCondition" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreSideEffectConditions" value="true" />
</inspection_tool>
<inspection_tool class="EmptyStatementBody" enabled="true" level="WARNING" enabled_by_default="true">
<option name="m_reportEmptyBlocks" value="false" />
</inspection_tool>
@@ -106,6 +103,8 @@
<Problem reference="com.intellij.testFramework.PlatformTestCase#doCreateRealModuleIn" reason="Not static anymore in 181 after 7dacf096c47d2125e17031c71a037b63ab00ec53" />
<Problem reference="com.intellij.openapi.progress.ProgressManager#getProgressIndicator" reason="Nullable in 181. Temporary use progressIndicatorNullable instead." />
<Problem reference="org.jetbrains.java.decompiler.main.decompiler.BaseDecompiler#addSpace" reason="Method was replaced with outher methods in 182. Use addSpaceEx instead." />
<Problem reference="com.intellij.psi.codeStyle.CommonCodeStyleSettings#copyFrom" reason="Absent in 173. Use CompatibilityKt.copyFromEx instead." />
<Problem reference="com.intellij.psi.codeStyle.CommonCodeStyleSettingsManager#copy" reason="Removed since 181. Use CompatibilityKt.copyFromEx instead." />
</list>
</option>
</inspection_tool>
@@ -23,8 +23,10 @@ import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.codeStyle.*;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.formatter.KotlinObsoleteCodeStyle;
import org.jetbrains.kotlin.idea.formatter.KotlinStyleGuideCodeStyle;
import org.jetbrains.kotlin.idea.util.FormatterUtilKt;
import org.jetbrains.kotlin.idea.util.ReflectionUtil;
import static com.intellij.util.ReflectionUtil.copyFields;
@@ -63,6 +65,12 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
@ReflectionUtil.SkipInEquals
public String CODE_STYLE_DEFAULTS = null;
/**
* Load settings with previous IDEA defaults to have an ability to restore them.
*/
@Nullable
private KotlinCodeStyleSettings settingsAgainstPreviousDefaults = null;
private final boolean isTempForDeserialize;
public KotlinCodeStyleSettings(CodeStyleSettings container) {
@@ -95,6 +103,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public KotlinCodeStyleSettings cloneSettings() {
KotlinCodeStyleSettings clone = new KotlinCodeStyleSettings(getContainer());
clone.copyFrom(this);
clone.settingsAgainstPreviousDefaults = this.settingsAgainstPreviousDefaults;
return clone;
}
@@ -136,10 +145,17 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
}
KotlinCodeStyleSettings tempSettings = readExternalToTemp(parentElement);
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(tempSettings.CODE_STYLE_DEFAULTS)) {
String customDefaults = tempSettings.CODE_STYLE_DEFAULTS;
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(customDefaults)) {
KotlinStyleGuideCodeStyle.Companion.applyToKotlinCustomSettings(this, true);
} else if (KotlinObsoleteCodeStyle.CODE_STYLE_ID.equals(tempSettings.CODE_STYLE_DEFAULTS)) {
} else if (KotlinObsoleteCodeStyle.CODE_STYLE_ID.equals(customDefaults)) {
KotlinObsoleteCodeStyle.Companion.applyToKotlinCustomSettings(this, true);
} else if (customDefaults == null && FormatterUtilKt.isDefaultOfficialCodeStyle()) {
// Temporary load settings against previous defaults
settingsAgainstPreviousDefaults = new KotlinCodeStyleSettings(null, true);
KotlinObsoleteCodeStyle.Companion.applyToKotlinCustomSettings(settingsAgainstPreviousDefaults, true);
settingsAgainstPreviousDefaults.readExternal(parentElement);
}
// Actual read
@@ -153,4 +169,10 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
return tempSettings;
}
public void restore() {
if (settingsAgainstPreviousDefaults != null) {
copyFrom(settingsAgainstPreviousDefaults);
}
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.core.formatter
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
/**
* Method copyFrom is absent in 173.
* BUNCH: 181
*/
fun CommonCodeStyleSettings.copyFromEx(source: CommonCodeStyleSettings) {
@Suppress("IncompatibleAPI")
copyFrom(source)
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.core.formatter
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.codeStyle.CommonCodeStyleSettingsManager
/**
* Method copyFrom is absent in 173.
* BUNCH: 181
*/
fun CommonCodeStyleSettings.copyFromEx(source: CommonCodeStyleSettings) {
@Suppress("IncompatibleAPI")
CommonCodeStyleSettingsManager.copy(source, this)
}
@@ -21,6 +21,8 @@ import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.idea.core.formatter.CompatibilityKt;
import org.jetbrains.kotlin.idea.util.FormatterUtilKt;
import org.jetbrains.kotlin.idea.util.ReflectionUtil;
import java.lang.reflect.Field;
@@ -34,6 +36,12 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
@ReflectionUtil.SkipInEquals
public String CODE_STYLE_DEFAULTS = null;
/**
* Load settings with previous IDEA defaults to have an ability to restore them.
*/
@Nullable
private KotlinCommonCodeStyleSettings settingsAgainstPreviousDefaults = null;
private final boolean isTempForDeserialize;
public KotlinCommonCodeStyleSettings() {
@@ -59,10 +67,16 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
KotlinCommonCodeStyleSettings tempDeserialize = createForTempDeserialize();
tempDeserialize.readExternal(element);
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(tempDeserialize.CODE_STYLE_DEFAULTS)) {
String customDefaults = tempDeserialize.CODE_STYLE_DEFAULTS;
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(customDefaults)) {
KotlinStyleGuideCodeStyle.Companion.applyToCommonSettings(this, true);
} else if (KotlinObsoleteCodeStyle.CODE_STYLE_ID.equals(tempDeserialize.CODE_STYLE_DEFAULTS)) {
} else if (KotlinObsoleteCodeStyle.CODE_STYLE_ID.equals(customDefaults)) {
KotlinObsoleteCodeStyle.Companion.applyToCommonSettings(this, true);
} else if (customDefaults == null && FormatterUtilKt.isDefaultOfficialCodeStyle()) {
// Temporary load settings against previous defaults
settingsAgainstPreviousDefaults = createForTempDeserialize();
KotlinObsoleteCodeStyle.Companion.applyToCommonSettings(settingsAgainstPreviousDefaults, true);
settingsAgainstPreviousDefaults.readExternal(element);
}
readExternalBase(element);
@@ -121,7 +135,10 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
@Override
public CommonCodeStyleSettings clone(@NotNull CodeStyleSettings rootSettings) {
CommonCodeStyleSettings commonSettings = new KotlinCommonCodeStyleSettings();
KotlinCommonCodeStyleSettings commonSettings = new KotlinCommonCodeStyleSettings();
commonSettings.settingsAgainstPreviousDefaults = settingsAgainstPreviousDefaults;
copyPublicFieldsOwn(this, commonSettings);
try {
@@ -214,6 +231,12 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
return provider == null ? null : provider.getSupportedFields();
}
public void restore() {
if (settingsAgainstPreviousDefaults != null) {
CompatibilityKt.copyFromEx(this, settingsAgainstPreviousDefaults);
}
}
private static class SupportedFieldsDiffFilter extends DifferenceFilter<CommonCodeStyleSettings> {
private final Set<String> mySupportedFieldNames;