From 06c1c7e90311b3835006dcf0782092af8770e235 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Wed, 23 Jan 2019 18:26:25 +0300 Subject: [PATCH] Export comma-separated values as Json arrays --- .../properties/CommaSeparatedValues.java | 15 +++ .../CommaSeparatedValuesAccessor.java | 39 ++++++++ .../properties/PropertyAccessorFactory.java | 4 + .../testData/codeStyle/json/exportToJson.json | 97 +++++++++++++++++++ .../JsonInterchangeTest.java | 4 +- .../formatter/xml/HtmlCodeStyleSettings.java | 13 +++ 6 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValues.java create mode 100644 platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValuesAccessor.java diff --git a/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValues.java b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValues.java new file mode 100644 index 000000000000..f910a841a3ce --- /dev/null +++ b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValues.java @@ -0,0 +1,15 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.application.options.codeStyle.properties; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marker annotation for fields containing comma-separated values. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface CommaSeparatedValues { +} diff --git a/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValuesAccessor.java b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValuesAccessor.java new file mode 100644 index 000000000000..024f38cf7c59 --- /dev/null +++ b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/CommaSeparatedValuesAccessor.java @@ -0,0 +1,39 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.application.options.codeStyle.properties; + +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.util.List; + +public class CommaSeparatedValuesAccessor extends ValueListPropertyAccessor { + public CommaSeparatedValuesAccessor(@NotNull Object object, @NotNull Field field) { + super(object, field); + } + + @Nullable + @Override + protected String fromExternal(@NotNull List extVal) { + StringBuilder valueBuilder = new StringBuilder(); + for (String value : extVal) { + if (valueBuilder.length() > 0) { + valueBuilder.append(","); + } + valueBuilder.append(extVal); + } + return valueBuilder.toString(); + } + + @NotNull + @Override + protected List toExternal(@NotNull String value) { + List valueList = ContainerUtil.newArrayList(); + String[] parts = value.split(","); + for (String part : parts) { + valueList.add(part.trim()); + } + return valueList; + } +} diff --git a/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/PropertyAccessorFactory.java b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/PropertyAccessorFactory.java index e8f6639c4af0..ce596ee29433 100644 --- a/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/PropertyAccessorFactory.java +++ b/platform/lang-api/src/com/intellij/application/options/codeStyle/properties/PropertyAccessorFactory.java @@ -67,6 +67,10 @@ class PropertyAccessorFactory { case INT: return new IntegerAccessor(codeStyleObject, myField); case STRING: + CommaSeparatedValues annotation = myField.getAnnotation(CommaSeparatedValues.class); + if (annotation != null) { + return new CommaSeparatedValuesAccessor(codeStyleObject, myField); + } return new StringAccessor(codeStyleObject, myField); case WRAP: return new WrappingAccessor(codeStyleObject, myField); diff --git a/platform/platform-tests/testData/codeStyle/json/exportToJson.json b/platform/platform-tests/testData/codeStyle/json/exportToJson.json index 6ef7e6c9395c..45901472c777 100644 --- a/platform/platform-tests/testData/codeStyle/json/exportToJson.json +++ b/platform/platform-tests/testData/codeStyle/json/exportToJson.json @@ -9,5 +9,102 @@ "max_line_length": "120", "wrap_on_typing": "false" } + }, + { + "language": "html", + "options": { + "add_new_line_before_tags": [ + "body", + "div", + "p", + "form", + "h1", + "h2", + "h3" + ], + "align_attributes": "true", + "align_text": "false", + "attribute_wrap": "normal", + "block_comment_at_first_column": "true", + "continuation_indent_size": "8", + "do_not_align_children_of_min_lines": "0", + "do_not_break_if_inline_tags": [ + "title", + "h1", + "h2", + "h3", + "h4", + "h5", + "h6", + "p" + ], + "do_not_indent_children_of_tags": [ + "html", + "body", + "thead", + "tbody", + "tfoot" + ], + "enforce_quotes": "false", + "indent_size": "4", + "indent_style": "space", + "inline_tags": [ + "a", + "abbr", + "acronym", + "b", + "basefont", + "bdo", + "big", + "br", + "cite", + "cite", + "code", + "dfn", + "em", + "font", + "i", + "img", + "input", + "kbd", + "label", + "q", + "s", + "samp", + "select", + "span", + "strike", + "strong", + "sub", + "sup", + "textarea", + "tt", + "u", + "var" + ], + "keep_blank_lines": "2", + "keep_indents_on_empty_lines": "false", + "keep_line_breaks": "true", + "keep_line_breaks_in_text": "true", + "keep_whitespaces": "false", + "keep_whitespaces_inside": [ + "span", + "pre", + "textarea" + ], + "line_comment_at_first_column": "true", + "new_line_after_last_attribute": "never", + "new_line_before_first_attribute": "never", + "quote_style": "double", + "remove_new_line_before_tags": [ + "br" + ], + "smart_tabs": "false", + "space_after_tag_name": "false", + "space_around_equality_in_attribute": "false", + "space_inside_empty_tag": "false", + "tab_width": "4", + "text_wrap": "normal" + } } ] \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/JsonInterchangeTest.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/JsonInterchangeTest.java index 2b6aee88a207..18219a99e0a5 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/JsonInterchangeTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/JsonInterchangeTest.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.Nullable; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.Collections; +import java.util.Arrays; public class JsonInterchangeTest extends CodeStyleTestCase { @@ -16,7 +16,7 @@ public class JsonInterchangeTest extends CodeStyleTestCase { CodeStyleScheme testScheme = createTestScheme(); CodeStyleSchemeJsonExporter exporter = new CodeStyleSchemeJsonExporter(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - exporter.exportScheme(testScheme, outputStream, Collections.singletonList(GeneralCodeStylePropertyMapper.COMMON_DOMAIN_ID)); + exporter.exportScheme(testScheme, outputStream, Arrays.asList(GeneralCodeStylePropertyMapper.COMMON_DOMAIN_ID, "html")); String expected = loadExpected("json"); assertEquals(expected, outputStream.toString()); } diff --git a/xml/impl/src/com/intellij/psi/formatter/xml/HtmlCodeStyleSettings.java b/xml/impl/src/com/intellij/psi/formatter/xml/HtmlCodeStyleSettings.java index ca4b55d8d557..d3498ec60a81 100644 --- a/xml/impl/src/com/intellij/psi/formatter/xml/HtmlCodeStyleSettings.java +++ b/xml/impl/src/com/intellij/psi/formatter/xml/HtmlCodeStyleSettings.java @@ -1,6 +1,7 @@ // Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi.formatter.xml; +import com.intellij.application.options.codeStyle.properties.CommaSeparatedValues; import com.intellij.configurationStore.Property; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CustomCodeStyleSettings; @@ -42,21 +43,33 @@ public class HtmlCodeStyleSettings extends CustomCodeStyleSettings { public boolean HTML_SPACE_INSIDE_EMPTY_TAG; @Property(externalName = "add_new_line_before_tags") + @CommaSeparatedValues @NonNls public String HTML_ELEMENTS_TO_INSERT_NEW_LINE_BEFORE = "body,div,p,form,h1,h2,h3"; + @Property(externalName = "remove_new_line_before_tags") + @CommaSeparatedValues @NonNls public String HTML_ELEMENTS_TO_REMOVE_NEW_LINE_BEFORE = "br"; + @Property(externalName = "do_not_indent_children_of_tags") + @CommaSeparatedValues @NonNls public String HTML_DO_NOT_INDENT_CHILDREN_OF = "html,body,thead,tbody,tfoot"; + @Property(externalName = "do_not_align_children_of_min_lines") public int HTML_DO_NOT_ALIGN_CHILDREN_OF_MIN_LINES; @Property(externalName = "keep_whitespaces_inside") + @CommaSeparatedValues @NonNls public String HTML_KEEP_WHITESPACES_INSIDE = "span,pre,textarea"; + @Property(externalName = "inline_tags") + @CommaSeparatedValues @NonNls public String HTML_INLINE_ELEMENTS = "a,abbr,acronym,b,basefont,bdo,big,br,cite,cite,code,dfn,em,font,i,img,input,kbd,label,q,s,samp,select,span,strike,strong,sub,sup,textarea,tt,u,var"; + @Property(externalName = "do_not_break_if_inline_tags") + @CommaSeparatedValues @NonNls public String HTML_DONT_ADD_BREAKS_IF_INLINE_CONTENT = "title,h1,h2,h3,h4,h5,h6,p"; + @Property(externalName = "quote_style") public CodeStyleSettings.QuoteStyle HTML_QUOTE_STYLE = CodeStyleSettings.QuoteStyle.Double; @Property(externalName = "enforce_quotes")