Control serialize/deserialize of CommonCodeStyleSettings

This commit is contained in:
Nikolay Krasko
2018-02-04 01:08:14 +03:00
parent 9e22761262
commit 5d25b8b476
3 changed files with 356 additions and 121 deletions
@@ -0,0 +1,198 @@
/*
* 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.lang.Language;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.DifferenceFilter;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.codeStyle.CodeStyleSettings;
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 java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
@SuppressWarnings("UnnecessaryFinalOnLocalVariableOrParameter")
public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
public KotlinCommonCodeStyleSettings() {
super(KotlinLanguage.INSTANCE);
}
//<editor-fold desc="Copied and adapted from CommonCodeStyleSettings ">
@Override
public void readExternal(Element element) throws InvalidDataException {
super.readExternal(element);
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
CommonCodeStyleSettings defaultSettings = getDefaultSettings();
Set<String> supportedFields = getSupportedFields();
if (supportedFields != null) {
supportedFields.add("FORCE_REARRANGE_MODE");
}
//noinspection deprecation
DefaultJDOMExternalizer.writeExternal(this, element, new SupportedFieldsDiffFilter(this, supportedFields, defaultSettings));
List<Integer> softMargins = getSoftMargins();
serializeInto(softMargins, element);
IndentOptions myIndentOptions = getIndentOptions();
if (myIndentOptions != null) {
IndentOptions defaultIndentOptions = defaultSettings != null ? defaultSettings.getIndentOptions() : null;
Element indentOptionsElement = new Element(INDENT_OPTIONS_TAG);
myIndentOptions.serialize(indentOptionsElement, defaultIndentOptions);
if (!indentOptionsElement.getChildren().isEmpty()) {
element.addContent(indentOptionsElement);
}
}
ArrangementSettings myArrangementSettings = getArrangementSettings();
if (myArrangementSettings != null) {
Element container = new Element(ARRANGEMENT_ELEMENT_NAME);
ArrangementUtil.writeExternal(container, myArrangementSettings, myLanguage);
if (!container.getChildren().isEmpty()) {
element.addContent(container);
}
}
}
@Override
public CommonCodeStyleSettings clone(@NotNull CodeStyleSettings rootSettings) {
CommonCodeStyleSettings commonSettings = new KotlinCommonCodeStyleSettings();
copyPublicFieldsOwn(this, commonSettings);
try {
Method setRootSettingsMethod = CommonCodeStyleSettings.class.getDeclaredMethod("setRootSettings", CodeStyleSettings.class);
setRootSettingsMethod.setAccessible(true);
setRootSettingsMethod.invoke(commonSettings, rootSettings);
}
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
commonSettings.setForceArrangeMenuAvailable(isForceArrangeMenuAvailable());
IndentOptions indentOptions = getIndentOptions();
if (indentOptions != null) {
IndentOptions targetIndentOptions = commonSettings.initIndentOptions();
targetIndentOptions.copyFrom(indentOptions);
}
ArrangementSettings arrangementSettings = getArrangementSettings();
if (arrangementSettings != null) {
commonSettings.setArrangementSettings(arrangementSettings.clone());
}
try {
Method setRootSettingsMethod = ArraysKt.singleOrNull(
CommonCodeStyleSettings.class.getDeclaredMethods(),
method -> "setSoftMargins".equals(method.getName()));
if (setRootSettingsMethod != null) {
// Method was introduced in 173
setRootSettingsMethod.setAccessible(true);
setRootSettingsMethod.invoke(commonSettings, getSoftMargins());
}
}
catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
return commonSettings;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof KotlinCommonCodeStyleSettings)) {
return false;
}
if (!ReflectionUtil.comparePublicNonFinalFields(this, obj)) {
return false;
}
CommonCodeStyleSettings other = (CommonCodeStyleSettings) obj;
if (!getSoftMargins().equals(other.getSoftMargins())) {
return false;
}
IndentOptions options = getIndentOptions();
if ((options == null && other.getIndentOptions() != null) ||
(options != null && !options.equals(other.getIndentOptions()))) {
return false;
}
return arrangementSettingsEqual(other);
}
// SoftMargins.serializeInfo
private void serializeInto(@NotNull List<Integer> softMargins, @NotNull Element element) {
if (softMargins.size() > 0) {
XmlSerializer.serializeInto(this, element);
}
}
//</editor-fold>
//<editor-fold desc="Copied from CommonCodeStyleSettings">
private static final String INDENT_OPTIONS_TAG = "indentOptions";
private static final String ARRANGEMENT_ELEMENT_NAME = "arrangement";
private final Language myLanguage = KotlinLanguage.INSTANCE;
@Nullable
private CommonCodeStyleSettings getDefaultSettings() {
return LanguageCodeStyleSettingsProvider.getDefaultCommonSettings(myLanguage);
}
@Nullable
private Set<String> getSupportedFields() {
final LanguageCodeStyleSettingsProvider provider = LanguageCodeStyleSettingsProvider.forLanguage(myLanguage);
return provider == null ? null : provider.getSupportedFields();
}
private static class SupportedFieldsDiffFilter extends DifferenceFilter<CommonCodeStyleSettings> {
private final Set<String> mySupportedFieldNames;
public SupportedFieldsDiffFilter(
final CommonCodeStyleSettings object,
Set<String> supportedFiledNames,
final CommonCodeStyleSettings parentObject
) {
super(object, parentObject);
mySupportedFieldNames = supportedFiledNames;
}
@Override
public boolean isAccept(@NotNull Field field) {
if (mySupportedFieldNames == null ||
mySupportedFieldNames.contains(field.getName())) {
return super.isAccept(field);
}
return false;
}
}
// Can't use super.copyPublicFields because the method is internal in 181
private static void copyPublicFieldsOwn(Object from, Object to) {
assert from != to;
com.intellij.util.ReflectionUtil.copyFields(to.getClass().getFields(), from, to);
}
//</editor-fold>
}
@@ -188,156 +188,192 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
when (settingsType) {
LanguageCodeStyleSettingsProvider.SettingsType.SPACING_SETTINGS -> {
consumer.showStandardOptions(
"SPACE_AROUND_ASSIGNMENT_OPERATORS",
"SPACE_AROUND_LOGICAL_OPERATORS",
"SPACE_AROUND_EQUALITY_OPERATORS",
"SPACE_AROUND_RELATIONAL_OPERATORS",
"SPACE_AROUND_ADDITIVE_OPERATORS",
"SPACE_AROUND_MULTIPLICATIVE_OPERATORS",
"SPACE_AROUND_UNARY_OPERATOR",
"SPACE_AFTER_COMMA",
"SPACE_BEFORE_COMMA",
"SPACE_BEFORE_IF_PARENTHESES",
"SPACE_BEFORE_WHILE_PARENTHESES",
"SPACE_BEFORE_FOR_PARENTHESES",
"SPACE_BEFORE_CATCH_PARENTHESES"
"SPACE_AROUND_ASSIGNMENT_OPERATORS",
"SPACE_AROUND_LOGICAL_OPERATORS",
"SPACE_AROUND_EQUALITY_OPERATORS",
"SPACE_AROUND_RELATIONAL_OPERATORS",
"SPACE_AROUND_ADDITIVE_OPERATORS",
"SPACE_AROUND_MULTIPLICATIVE_OPERATORS",
"SPACE_AROUND_UNARY_OPERATOR",
"SPACE_AFTER_COMMA",
"SPACE_BEFORE_COMMA",
"SPACE_BEFORE_IF_PARENTHESES",
"SPACE_BEFORE_WHILE_PARENTHESES",
"SPACE_BEFORE_FOR_PARENTHESES",
"SPACE_BEFORE_CATCH_PARENTHESES"
);
showCustomOption(KotlinCodeStyleSettings::SPACE_AROUND_RANGE,
"Range operator (..)",
CodeStyleSettingsCustomizable.SPACES_AROUND_OPERATORS)
showCustomOption(
KotlinCodeStyleSettings::SPACE_AROUND_RANGE,
"Range operator (..)",
CodeStyleSettingsCustomizable.SPACES_AROUND_OPERATORS
)
showCustomOption(KotlinCodeStyleSettings::SPACE_BEFORE_TYPE_COLON,
"Before colon, after declaration name",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_BEFORE_TYPE_COLON,
"Before colon, after declaration name",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_AFTER_TYPE_COLON,
"After colon, before declaration type",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_AFTER_TYPE_COLON,
"After colon, before declaration type",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_BEFORE_EXTEND_COLON,
"Before colon in new type definition",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_BEFORE_EXTEND_COLON,
"Before colon in new type definition",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_AFTER_EXTEND_COLON,
"After colon in new type definition",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_AFTER_EXTEND_COLON,
"After colon in new type definition",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD,
"In simple one line methods",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD,
"In simple one line methods",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_AROUND_FUNCTION_TYPE_ARROW,
"Around arrow in function types",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_AROUND_FUNCTION_TYPE_ARROW,
"Around arrow in function types",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_AROUND_WHEN_ARROW,
"Around arrow in \"when\" clause",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_AROUND_WHEN_ARROW,
"Around arrow in \"when\" clause",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_BEFORE_LAMBDA_ARROW,
"Before lambda arrow",
CodeStyleSettingsCustomizable.SPACES_OTHER)
showCustomOption(
KotlinCodeStyleSettings::SPACE_BEFORE_LAMBDA_ARROW,
"Before lambda arrow",
CodeStyleSettingsCustomizable.SPACES_OTHER
)
showCustomOption(KotlinCodeStyleSettings::SPACE_BEFORE_WHEN_PARENTHESES,
"'when' parentheses",
CodeStyleSettingsCustomizable.SPACES_BEFORE_PARENTHESES)
showCustomOption(
KotlinCodeStyleSettings::SPACE_BEFORE_WHEN_PARENTHESES,
"'when' parentheses",
CodeStyleSettingsCustomizable.SPACES_BEFORE_PARENTHESES
)
}
LanguageCodeStyleSettingsProvider.SettingsType.WRAPPING_AND_BRACES_SETTINGS -> {
consumer.showStandardOptions(
// "ALIGN_MULTILINE_CHAINED_METHODS",
"KEEP_FIRST_COLUMN_COMMENT",
"KEEP_LINE_BREAKS",
"ALIGN_MULTILINE_EXTENDS_LIST",
"ALIGN_MULTILINE_PARAMETERS",
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
"ALIGN_MULTILINE_METHOD_BRACKETS",
"ALIGN_MULTILINE_BINARY_OPERATION",
"ELSE_ON_NEW_LINE",
"WHILE_ON_NEW_LINE",
"CATCH_ON_NEW_LINE",
"FINALLY_ON_NEW_LINE",
"CALL_PARAMETERS_WRAP",
"METHOD_PARAMETERS_WRAP",
"EXTENDS_LIST_WRAP",
"METHOD_ANNOTATION_WRAP",
"CLASS_ANNOTATION_WRAP",
"PARAMETER_ANNOTATION_WRAP",
"VARIABLE_ANNOTATION_WRAP",
"FIELD_ANNOTATION_WRAP",
"METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE",
"METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE",
"CALL_PARAMETERS_LPAREN_ON_NEXT_LINE",
"CALL_PARAMETERS_RPAREN_ON_NEXT_LINE",
"ENUM_CONSTANTS_WRAP",
"METHOD_CALL_CHAIN_WRAP",
"WRAP_FIRST_METHOD_IN_CALL_CHAIN",
"ASSIGNMENT_WRAP"
LanguageCodeStyleSettingsProvider.SettingsType.WRAPPING_AND_BRACES_SETTINGS -> {
consumer.showStandardOptions(
// "ALIGN_MULTILINE_CHAINED_METHODS",
"KEEP_FIRST_COLUMN_COMMENT",
"KEEP_LINE_BREAKS",
"ALIGN_MULTILINE_EXTENDS_LIST",
"ALIGN_MULTILINE_PARAMETERS",
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
"ALIGN_MULTILINE_METHOD_BRACKETS",
"ALIGN_MULTILINE_BINARY_OPERATION",
"ELSE_ON_NEW_LINE",
"WHILE_ON_NEW_LINE",
"CATCH_ON_NEW_LINE",
"FINALLY_ON_NEW_LINE",
"CALL_PARAMETERS_WRAP",
"METHOD_PARAMETERS_WRAP",
"EXTENDS_LIST_WRAP",
"METHOD_ANNOTATION_WRAP",
"CLASS_ANNOTATION_WRAP",
"PARAMETER_ANNOTATION_WRAP",
"VARIABLE_ANNOTATION_WRAP",
"FIELD_ANNOTATION_WRAP",
"METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE",
"METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE",
"CALL_PARAMETERS_LPAREN_ON_NEXT_LINE",
"CALL_PARAMETERS_RPAREN_ON_NEXT_LINE",
"ENUM_CONSTANTS_WRAP",
"METHOD_CALL_CHAIN_WRAP",
"WRAP_FIRST_METHOD_IN_CALL_CHAIN",
"ASSIGNMENT_WRAP"
)
consumer.renameStandardOption(CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT, "'when' statements")
consumer.renameStandardOption("FIELD_ANNOTATION_WRAP", "Property annotations")
showCustomOption(KotlinCodeStyleSettings::ALIGN_IN_COLUMNS_CASE_BRANCH,
"Align 'when' branches in columns",
CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT)
showCustomOption(KotlinCodeStyleSettings::LBRACE_ON_NEXT_LINE,
"Put left brace on new line",
CodeStyleSettingsCustomizable.WRAPPING_BRACES)
showCustomOption(
KotlinCodeStyleSettings::ALIGN_IN_COLUMNS_CASE_BRANCH,
"Align 'when' branches in columns",
CodeStyleSettingsCustomizable.WRAPPING_SWITCH_STATEMENT
)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_PARAMETER_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_METHOD_PARAMETERS)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_ARGUMENT_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_METHOD_ARGUMENTS_WRAPPING)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_CHAINED_CALLS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_CALL_CHAIN)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_SUPERTYPE_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_EXTENDS_LIST)
KotlinCodeStyleSettings::LBRACE_ON_NEXT_LINE,
"Put left brace on new line",
CodeStyleSettingsCustomizable.WRAPPING_BRACES
)
showCustomOption(
KotlinCodeStyleSettings::WRAP_EXPRESSION_BODY_FUNCTIONS,
"Expression body functions",
options = *arrayOf(CodeStyleSettingsCustomizable.WRAP_OPTIONS_FOR_SINGLETON, CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON)
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_PARAMETER_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_METHOD_PARAMETERS
)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES,
"Use continuation indent",
"Expression body functions"
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_ARGUMENT_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_METHOD_ARGUMENTS_WRAPPING
)
showCustomOption(
KotlinCodeStyleSettings::WRAP_ELVIS_EXPRESSIONS,
"Elvis expressions",
options = *arrayOf(CodeStyleSettingsCustomizable.WRAP_OPTIONS_FOR_SINGLETON, CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON)
KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_CHAINED_CALLS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_CALL_CHAIN
)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_SUPERTYPE_LISTS,
"Use continuation indent",
CodeStyleSettingsCustomizable.WRAPPING_EXTENDS_LIST
)
showCustomOption(
KotlinCodeStyleSettings::WRAP_EXPRESSION_BODY_FUNCTIONS,
"Expression body functions",
options = *arrayOf(
CodeStyleSettingsCustomizable.WRAP_OPTIONS_FOR_SINGLETON,
CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON
)
)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES,
"Use continuation indent",
"Expression body functions"
)
showCustomOption(
KotlinCodeStyleSettings::WRAP_ELVIS_EXPRESSIONS,
"Elvis expressions",
options = *arrayOf(
CodeStyleSettingsCustomizable.WRAP_OPTIONS_FOR_SINGLETON,
CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON
)
)
@Suppress("InvalidBundleOrProperty")
showCustomOption(
KotlinCodeStyleSettings::IF_RPAREN_ON_NEW_LINE,
ApplicationBundle.message("wrapping.rpar.on.new.line"),
CodeStyleSettingsCustomizable.WRAPPING_IF_STATEMENT
KotlinCodeStyleSettings::IF_RPAREN_ON_NEW_LINE,
ApplicationBundle.message("wrapping.rpar.on.new.line"),
CodeStyleSettingsCustomizable.WRAPPING_IF_STATEMENT
)
showCustomOption(
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_IF_CONDITIONS,
"Use continuation indent in conditions",
CodeStyleSettingsCustomizable.WRAPPING_IF_STATEMENT
KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_IF_CONDITIONS,
"Use continuation indent in conditions",
CodeStyleSettingsCustomizable.WRAPPING_IF_STATEMENT
)
}
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> {
consumer.showStandardOptions(
"KEEP_BLANK_LINES_IN_CODE",
"KEEP_BLANK_LINES_IN_DECLARATIONS",
"KEEP_BLANK_LINES_BEFORE_RBRACE",
"BLANK_LINES_AFTER_CLASS_HEADER"
"KEEP_BLANK_LINES_IN_CODE",
"KEEP_BLANK_LINES_IN_DECLARATIONS",
"KEEP_BLANK_LINES_BEFORE_RBRACE",
"BLANK_LINES_AFTER_CLASS_HEADER"
)
showCustomOption(
KotlinCodeStyleSettings::BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES,
"Around 'when' branches with {}",
CodeStyleSettingsCustomizable.BLANK_LINES
)
showCustomOption(KotlinCodeStyleSettings::BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES,
"Around 'when' branches with {}",
CodeStyleSettingsCustomizable.BLANK_LINES)
}
else -> consumer.showStandardOptions()
}
@@ -345,8 +381,9 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
override fun getIndentOptionsEditor(): IndentOptionsEditor = SmartIndentOptionsEditor()
override fun getDefaultCommonSettings(): CommonCodeStyleSettings =
CommonCodeStyleSettings(language).apply {
override fun getDefaultCommonSettings(): CommonCodeStyleSettings {
return KotlinCommonCodeStyleSettings().apply {
initIndentOptions()
}
}
}
}
@@ -103,7 +103,7 @@ public class SettingsConfigurator {
private static boolean setSettingWithField(String settingName, Object object, Object value) {
try {
Field field = object.getClass().getDeclaredField(settingName);
Field field = object.getClass().getField(settingName);
field.set(object, value);
return true;
}