Store selected set of defaults in code style settings (KT-22252)

#KT-22252 In Progress
This commit is contained in:
Nikolay Krasko
2018-02-02 17:58:56 +03:00
parent 5d25b8b476
commit 1e567438a2
8 changed files with 272 additions and 13 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ apply { plugin("kotlin") }
dependencies {
compile(project(":compiler:util"))
compile(project(":compiler:frontend"))
compileOnly(intellijDep()) { includeJars("idea", "openapi", "util") }
compileOnly(intellijDep()) { includeJars("idea", "openapi", "util", "jdom") }
}
sourceSets {
@@ -18,9 +18,15 @@ package org.jetbrains.kotlin.idea.core.formatter;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.codeStyle.*;
import com.intellij.util.ReflectionUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.formatter.KotlinStyleGuideCodeStyle;
import org.jetbrains.kotlin.idea.util.ReflectionUtil;
import static com.intellij.util.ReflectionUtil.copyFields;
public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public static final KotlinCodeStyleSettings DEFAULT = new KotlinCodeStyleSettings(new CodeStyleSettings());
@@ -52,9 +58,20 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public int WRAP_ELVIS_EXPRESSIONS = 1;
public boolean IF_RPAREN_ON_NEW_LINE = false;
@ReflectionUtil.SkipInEquals
public String CODE_STYLE_DEFAULTS = null;
private final boolean isTempForDeserialize;
public KotlinCodeStyleSettings(CodeStyleSettings container) {
this(container, false);
}
private KotlinCodeStyleSettings(CodeStyleSettings container, boolean isTempForDeserialize) {
super("JetCodeStyleSettings", container);
this.isTempForDeserialize = isTempForDeserialize;
// defaults in IDE but not in tests
if (!ApplicationManager.getApplication().isUnitTestMode()) {
PACKAGES_TO_USE_STAR_IMPORTS.addEntry(new PackageEntry(false, "java.util", false));
@@ -74,8 +91,49 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
}
private void copyFrom(@NotNull KotlinCodeStyleSettings from) {
ReflectionUtil.copyFields(getClass().getFields(), from, this);
copyFields(getClass().getFields(), from, this);
PACKAGES_TO_USE_STAR_IMPORTS.copyFrom(from.PACKAGES_TO_USE_STAR_IMPORTS);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof KotlinCodeStyleSettings)) return false;
if (!ReflectionUtil.comparePublicNonFinalFieldsWithSkip(this, obj)) return false;
return true;
}
@Override
public void writeExternal(Element parentElement, @NotNull CustomCodeStyleSettings parentSettings) throws WriteExternalException {
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(CODE_STYLE_DEFAULTS)) {
KotlinCodeStyleSettings defaultKotlinCodeStyle = (KotlinCodeStyleSettings) parentSettings.clone();
KotlinStyleGuideCodeStyle.Companion.applyToKotlinCustomSettings(defaultKotlinCodeStyle, false);
parentSettings = defaultKotlinCodeStyle;
}
super.writeExternal(parentElement, parentSettings);
}
@Override
public void readExternal(Element parentElement) throws InvalidDataException {
if (isTempForDeserialize) {
super.readExternal(parentElement);
return;
}
KotlinCodeStyleSettings tempSettings = readExternalToTemp(parentElement);
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(tempSettings.CODE_STYLE_DEFAULTS)) {
KotlinStyleGuideCodeStyle.Companion.applyToKotlinCustomSettings(this, true);
}
// Actual read
super.readExternal(parentElement);
}
private static KotlinCodeStyleSettings readExternalToTemp(Element parentElement) {
// Read to temp
KotlinCodeStyleSettings tempSettings = new KotlinCodeStyleSettings(null, true);
tempSettings.readExternal(parentElement);
return tempSettings;
}
}
@@ -44,8 +44,8 @@ private val CODE_BLOCKS = TokenSet.create(KtNodeTypes.BLOCK, KtNodeTypes.CLASS_B
private val ALIGN_FOR_BINARY_OPERATIONS = TokenSet.create(MUL, DIV, PERC, PLUS, MINUS, ELVIS, LT, GT, LTEQ, GTEQ, ANDAND, OROR)
private val ANNOTATIONS = TokenSet.create(KtNodeTypes.ANNOTATION_ENTRY, KtNodeTypes.ANNOTATION)
val CodeStyleSettings.kotlinCommonSettings: CommonCodeStyleSettings
get() = getCommonSettings(KotlinLanguage.INSTANCE)
val CodeStyleSettings.kotlinCommonSettings: KotlinCommonCodeStyleSettings
get() = getCommonSettings(KotlinLanguage.INSTANCE) as KotlinCommonCodeStyleSettings
val CodeStyleSettings.kotlinCustomSettings: KotlinCodeStyleSettings
get() = getCustomSettings(KotlinCodeStyleSettings::class.java)!!
@@ -15,13 +15,13 @@ import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
import com.intellij.psi.codeStyle.arrangement.ArrangementSettings;
import com.intellij.psi.codeStyle.arrangement.ArrangementUtil;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.XmlSerializer;
import kotlin.collections.ArraysKt;
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.util.ReflectionUtil;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -31,22 +31,62 @@ import java.util.Set;
@SuppressWarnings("UnnecessaryFinalOnLocalVariableOrParameter")
public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
@ReflectionUtil.SkipInEquals
public String CODE_STYLE_DEFAULTS = null;
private final boolean isTempForDeserialize;
public KotlinCommonCodeStyleSettings() {
super(KotlinLanguage.INSTANCE);
this(false);
}
private KotlinCommonCodeStyleSettings(boolean isTempForDeserialize) {
super(KotlinLanguage.INSTANCE);
this.isTempForDeserialize = isTempForDeserialize;
}
private static KotlinCommonCodeStyleSettings createForTempDeserialize() {
return new KotlinCommonCodeStyleSettings(true);
}
//<editor-fold desc="Copied and adapted from CommonCodeStyleSettings ">
@Override
public void readExternal(Element element) throws InvalidDataException {
super.readExternal(element);
if (isTempForDeserialize) {
super.readExternal(element);
return;
}
KotlinCommonCodeStyleSettings tempDeserialize = createForTempDeserialize();
tempDeserialize.readExternal(element);
if (KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(tempDeserialize.CODE_STYLE_DEFAULTS)) {
KotlinStyleGuideCodeStyle.Companion.applyToCommonSettings(this, true);
}
readExternalBase(element);
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
CommonCodeStyleSettings defaultSettings = getDefaultSettings();
if (defaultSettings != null && KotlinStyleGuideCodeStyle.CODE_STYLE_ID.equals(CODE_STYLE_DEFAULTS)) {
KotlinStyleGuideCodeStyle.Companion.applyToCommonSettings(defaultSettings, false);
}
writeExternalBase(element, defaultSettings);
}
//<editor-fold desc="Copied and adapted from CommonCodeStyleSettings ">
private void readExternalBase(Element element) throws InvalidDataException {
super.readExternal(element);
}
private void writeExternalBase(Element element, CommonCodeStyleSettings defaultSettings) throws WriteExternalException {
Set<String> supportedFields = getSupportedFields();
if (supportedFields != null) {
supportedFields.add("FORCE_REARRANGE_MODE");
supportedFields.add("CODE_STYLE_DEFAULTS");
}
//noinspection deprecation
DefaultJDOMExternalizer.writeExternal(this, element, new SupportedFieldsDiffFilter(this, supportedFields, defaultSettings));
@@ -124,7 +164,7 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
return false;
}
if (!ReflectionUtil.comparePublicNonFinalFields(this, obj)) {
if (!ReflectionUtil.comparePublicNonFinalFieldsWithSkip(this, obj)) {
return false;
}
@@ -28,13 +28,20 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl
}
companion object {
const val CODE_STYLE_ID = "KOTLIN_OFFICIAL"
const val CODE_STYLE_TITLE = "Kotlin Coding Conventions"
fun apply(settings: CodeStyleSettings) {
applyToKotlinCustomSettings(settings.kotlinCustomSettings)
applyToCommonSettings(settings.kotlinCommonSettings)
}
fun applyToKotlinCustomSettings(kotlinCustomSettings: KotlinCodeStyleSettings) {
fun applyToKotlinCustomSettings(kotlinCustomSettings: KotlinCodeStyleSettings, modifyCodeStyle: Boolean = true) {
kotlinCustomSettings.apply {
if (modifyCodeStyle) {
CODE_STYLE_DEFAULTS = CODE_STYLE_ID
}
CONTINUATION_INDENT_IN_PARAMETER_LISTS = false
CONTINUATION_INDENT_IN_ARGUMENT_LISTS = false
CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = false
@@ -46,7 +53,7 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl
}
}
fun applyToCommonSettings(commonSettings: CommonCodeStyleSettings) {
fun applyToCommonSettings(commonSettings: CommonCodeStyleSettings, modifyCodeStyle: Boolean = true) {
commonSettings.apply {
WHILE_ON_NEW_LINE = false
ELSE_ON_NEW_LINE = false
@@ -67,6 +74,10 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl
ALIGN_MULTILINE_BINARY_OPERATION = false
}
if (modifyCodeStyle && commonSettings is KotlinCommonCodeStyleSettings) {
commonSettings.CODE_STYLE_DEFAULTS = CODE_STYLE_ID
}
}
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2000-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.util;
import com.intellij.openapi.util.Comparing;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Predicate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Set;
public class ReflectionUtil {
@Retention(RetentionPolicy.RUNTIME)
public @interface SkipInEquals {}
public static boolean comparePublicNonFinalFieldsWithSkip(@NotNull Object first, @NotNull Object second) {
return comparePublicNonFinalFields(first, second, field -> field.getAnnotation(SkipInEquals.class) == null);
}
private static boolean comparePublicNonFinalFields(@NotNull Object first, @NotNull Object second, @Nullable Predicate<Field> acceptPredicate) {
Set<Field> firstFields = ContainerUtil.newHashSet(first.getClass().getFields());
for (Field field : second.getClass().getFields()) {
if (firstFields.contains(field)) {
if (isPublic(field) && !isFinal(field) && (acceptPredicate == null || acceptPredicate.apply(field))) {
try {
if (!Comparing.equal(field.get(first), field.get(second))) {
return false;
}
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
return true;
}
private static boolean isPublic(Field field) {
return (field.getModifiers() & Modifier.PUBLIC) != 0;
}
private static boolean isFinal(Field field) {
return (field.getModifiers() & Modifier.FINAL) != 0;
}
}
@@ -60,6 +60,7 @@ public class KotlinCodeStyleSettingsProvider extends CodeStyleSettingsProvider {
addWrappingAndBracesTab(settings);
addBlankLinesTab(settings);
addTab(new ImportSettingsPanelWrapper(settings));
addTab(new KotlinSaveStylePanel(settings));
}
};
}
@@ -0,0 +1,93 @@
/*
* Copyright 2000-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.formatter
import com.intellij.application.options.CodeStyleAbstractPanel
import com.intellij.openapi.editor.colors.EditorColorsScheme
import com.intellij.openapi.ui.ComboBox
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.ui.ListCellRendererWrapper
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.panels.HorizontalLayout
import com.intellij.ui.components.panels.VerticalLayout
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import java.awt.BorderLayout
import javax.swing.BorderFactory
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JPanel
class KotlinSaveStylePanel(settings: CodeStyleSettings) : CodeStyleAbstractPanel(settings) {
override fun getRightMargin() = throw UnsupportedOperationException()
override fun createHighlighter(scheme: EditorColorsScheme) = throw UnsupportedOperationException()
override fun getFileType() = throw UnsupportedOperationException()
override fun getPreviewText(): String? = null
override fun getTabTitle(): String = "Load/Save"
private data class SaveItem(val label: String, val id: String?)
private val saveDefaultsComboBox = ComboBox<SaveItem>()
private val saveDefaultsItems = listOf(
SaveItem("<ide defaults>", null),
SaveItem("Current set of defaults", "KOTLIN_OLD_DEFAULTS"),
SaveItem(KotlinStyleGuideCodeStyle.CODE_STYLE_TITLE, KotlinStyleGuideCodeStyle.CODE_STYLE_ID)
)
var selectedId: String?
get() {
val (_, id) = saveDefaultsComboBox.selectedItem as SaveItem
return id
}
set(value) {
saveDefaultsComboBox.selectedItem = saveDefaultsItems.firstOrNull { (_, id) -> id == value } ?: saveDefaultsItems.first()
}
private val jPanel = JPanel(BorderLayout()).apply {
add(
JBScrollPane(
JPanel(VerticalLayout(JBUI.scale(5))).apply {
border = BorderFactory.createEmptyBorder(UIUtil.DEFAULT_VGAP, 10, UIUtil.DEFAULT_VGAP, 10)
add(JPanel(HorizontalLayout(JBUI.scale(5))).apply {
saveDefaultsItems.forEach {
saveDefaultsComboBox.addItem(it)
}
saveDefaultsComboBox.setRenderer(object : ListCellRendererWrapper<SaveItem>() {
override fun customize(list: JList<*>?, value: SaveItem, index: Int, selected: Boolean, hasFocus: Boolean) {
setText(value.label)
}
})
add(JLabel("Use defaults from:"))
add(saveDefaultsComboBox)
})
}
)
)
}
override fun apply(settings: CodeStyleSettings) {
settings.kotlinCustomSettings.CODE_STYLE_DEFAULTS = selectedId
settings.kotlinCommonSettings.CODE_STYLE_DEFAULTS = selectedId
}
override fun isModified(settings: CodeStyleSettings): Boolean {
return selectedId != settings.kotlinCustomSettings.CODE_STYLE_DEFAULTS ||
selectedId != settings.kotlinCommonSettings.CODE_STYLE_DEFAULTS
}
override fun getPanel() = jPanel
override fun resetImpl(settings: CodeStyleSettings) {
selectedId = settings.kotlinCustomSettings.CODE_STYLE_DEFAULTS ?: settings.kotlinCommonSettings.CODE_STYLE_DEFAULTS
}
override fun onSomethingChanged() {
// There's no way settings are going to be changed from other tabs without calling resetImpl
}
}