mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
WEB-25072: get rid of 'other' code style tab for JSON
This commit is contained in:
@@ -25,9 +25,7 @@ inspection.duplicate.keys.name=Duplicate keys in object literals
|
||||
inspection.duplicate.keys.msg.duplicate.keys=Object contains duplicate keys "{0}"
|
||||
|
||||
# Formatter
|
||||
formatter.other.tab.group.title=Formatting options
|
||||
formatter.other.tab.label.align.properties=Align object &properties:
|
||||
formatter.other.tab.remove.trailing.comma=Remove trailing &comma
|
||||
formatter.align.properties.caption=Align
|
||||
|
||||
formatter.align.properties.none=Do not align
|
||||
formatter.align.properties.on.colon=On colon
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.json.formatter.JsonCodeStylePanel">
|
||||
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="10" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="4b80e" binding="myPreviewPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<vspacer id="4c4f1">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="12c8e" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<clientProperties>
|
||||
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
|
||||
</clientProperties>
|
||||
<border type="line" title-resource-bundle="com/intellij/json/JsonBundle" title-key="formatter.other.tab.group.title">
|
||||
<font/>
|
||||
<title-color color="-16777216"/>
|
||||
</border>
|
||||
<children>
|
||||
<component id="4b4e5" class="javax.swing.JLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<labelFor value="9b7e4"/>
|
||||
<text resource-bundle="com/intellij/json/JsonBundle" key="formatter.other.tab.label.align.properties"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="9b7e4" class="javax.swing.JComboBox" binding="myPropertiesAlignmentCombo">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="b44e6" class="com.intellij.ui.components.JBCheckBox" binding="myRemoveTrailingCommaCheckbox">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<margin top="1" left="0" bottom="0" right="1"/>
|
||||
<text resource-bundle="com/intellij/json/JsonBundle" key="formatter.other.tab.remove.trailing.comma"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="c9980">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
</children>
|
||||
</grid>
|
||||
<hspacer id="88c58">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="2fada">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
@@ -1,133 +0,0 @@
|
||||
package com.intellij.json.formatter;
|
||||
|
||||
import com.intellij.application.options.CodeStyleAbstractPanel;
|
||||
import com.intellij.json.JsonFileType;
|
||||
import com.intellij.json.JsonLanguage;
|
||||
import com.intellij.json.formatter.JsonCodeStyleSettings.PropertyAlignment;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsScheme;
|
||||
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
|
||||
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.ui.ListCellRendererWrapper;
|
||||
import com.intellij.ui.components.JBCheckBox;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public class JsonCodeStylePanel extends CodeStyleAbstractPanel {
|
||||
public static final String ALIGNMENT_SAMPLE = "{\n" +
|
||||
" \"foo\": {\n" +
|
||||
" \"bar\": true,\n" +
|
||||
" \"baz\": false\n" +
|
||||
" },\n" +
|
||||
" \"quux\": [\n" +
|
||||
" 1, 2.0, 3e0, 4.0e0\n" +
|
||||
" ],\n" +
|
||||
" \"longPropertyName\": null\n" +
|
||||
"}";
|
||||
|
||||
private JComboBox myPropertiesAlignmentCombo;
|
||||
private JPanel myPreviewPanel;
|
||||
private JPanel myPanel;
|
||||
private JBCheckBox myRemoveTrailingCommaCheckbox;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public JsonCodeStylePanel(@NotNull CodeStyleSettings settings) {
|
||||
super(JsonLanguage.INSTANCE, null, settings);
|
||||
addPanelToWatch(myPanel);
|
||||
installPreviewPanel(myPreviewPanel);
|
||||
|
||||
// Initialize combo box with property value alignment types
|
||||
for (PropertyAlignment alignment : PropertyAlignment.values()) {
|
||||
myPropertiesAlignmentCombo.addItem(alignment);
|
||||
}
|
||||
myPropertiesAlignmentCombo.setRenderer(new ListCellRendererWrapper<PropertyAlignment>() {
|
||||
@Override
|
||||
public void customize(JList list, PropertyAlignment value, int index, boolean selected, boolean hasFocus) {
|
||||
setText(value.getDescription());
|
||||
}
|
||||
});
|
||||
myPropertiesAlignmentCombo.addItemListener(new ItemListener() {
|
||||
@Override
|
||||
public void itemStateChanged(ItemEvent e) {
|
||||
if (e.getStateChange() == ItemEvent.SELECTED) {
|
||||
somethingChanged();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getRightMargin() {
|
||||
return 80;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected EditorHighlighter createHighlighter(EditorColorsScheme scheme) {
|
||||
return EditorHighlighterFactory.getInstance().createEditorHighlighter(new LightVirtualFile("a.json"), scheme, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected FileType getFileType() {
|
||||
return JsonFileType.INSTANCE;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected String getPreviewText() {
|
||||
return ALIGNMENT_SAMPLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(CodeStyleSettings settings) throws ConfigurationException {
|
||||
JsonCodeStyleSettings jsonSettings = getCustomSettings(settings);
|
||||
jsonSettings.PROPERTY_ALIGNMENT = getSelectedAlignmentType().getId();
|
||||
jsonSettings.REMOVE_TRAILING_COMMA = myRemoveTrailingCommaCheckbox.isSelected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified(CodeStyleSettings settings) {
|
||||
JsonCodeStyleSettings jsonSettings = getCustomSettings(settings);
|
||||
return jsonSettings.PROPERTY_ALIGNMENT != getSelectedAlignmentType().getId()
|
||||
|| jsonSettings.REMOVE_TRAILING_COMMA != myRemoveTrailingCommaCheckbox.isSelected();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JComponent getPanel() {
|
||||
return myPanel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImpl(CodeStyleSettings settings) {
|
||||
JsonCodeStyleSettings jsonSettings = getCustomSettings(settings);
|
||||
myRemoveTrailingCommaCheckbox.setSelected(jsonSettings.REMOVE_TRAILING_COMMA);
|
||||
for (int i = 0; i < myPropertiesAlignmentCombo.getItemCount(); i++) {
|
||||
if (((PropertyAlignment)myPropertiesAlignmentCombo.getItemAt(i)).getId() == jsonSettings.PROPERTY_ALIGNMENT) {
|
||||
myPropertiesAlignmentCombo.setSelectedIndex(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PropertyAlignment getSelectedAlignmentType() {
|
||||
return (PropertyAlignment)myPropertiesAlignmentCombo.getSelectedItem();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsonCodeStyleSettings getCustomSettings(@NotNull CodeStyleSettings settings) {
|
||||
return settings.getCustomSettings(JsonCodeStyleSettings.class);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public class JsonCodeStyleSettings extends CustomCodeStyleSettings {
|
||||
|
||||
public boolean SPACE_AFTER_COLON = true;
|
||||
public boolean SPACE_BEFORE_COLON = false;
|
||||
public boolean REMOVE_TRAILING_COMMA = true;
|
||||
public boolean KEEP_TRAILING_COMMA = false;
|
||||
|
||||
// TODO: check whether it's possible to migrate CustomCodeStyleSettings to newer com.intellij.util.xmlb.XmlSerializer
|
||||
/**
|
||||
|
||||
@@ -31,7 +31,6 @@ public class JsonCodeStyleSettingsProvider extends CodeStyleSettingsProvider {
|
||||
addSpacesTab(settings);
|
||||
addBlankLinesTab(settings);
|
||||
addWrappingAndBracesTab(settings);
|
||||
addTab(new JsonCodeStylePanel(settings));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,26 +2,40 @@ package com.intellij.json.formatter;
|
||||
|
||||
import com.intellij.application.options.IndentOptionsEditor;
|
||||
import com.intellij.application.options.SmartIndentOptionsEditor;
|
||||
import com.intellij.json.JsonBundle;
|
||||
import com.intellij.json.JsonLanguage;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.SPACES_OTHER;
|
||||
|
||||
/**
|
||||
* @author Mikhail Golubev
|
||||
*/
|
||||
public class JsonLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {
|
||||
private static String[] ALIGN_OPTIONS = Arrays.stream(JsonCodeStyleSettings.PropertyAlignment.values())
|
||||
.map(alignment -> alignment.getDescription())
|
||||
.toArray(value -> new String[value]);
|
||||
|
||||
private static int[] ALIGN_VALUES =
|
||||
ArrayUtil.toIntArray(Arrays.stream(JsonCodeStyleSettings.PropertyAlignment.values())
|
||||
.map(alignment -> alignment.getId())
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
private static final String SAMPLE = "{\n" +
|
||||
" \"json literals are\": {\n" +
|
||||
" \"strings\": [\"foo\", \"bar\", \"\\u0062\\u0061\\u0072\"],\n" +
|
||||
" \"numbers\": [42, 6.62606975e-34],\n" +
|
||||
" \"boolean values\": [true, false],\n" +
|
||||
" \"and\": {\"null\": null}\n" +
|
||||
" \"boolean values\": [true, false,],\n" +
|
||||
" \"objects\": {\"null\": null,\"another\": null,}\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
@@ -45,6 +59,11 @@ public class JsonLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
|
||||
"WRAP_ON_TYPING",
|
||||
"KEEP_LINE_BREAKS",
|
||||
"WRAP_LONG_LINES");
|
||||
|
||||
consumer.showCustomOption(JsonCodeStyleSettings.class,
|
||||
"KEEP_TRAILING_COMMA",
|
||||
"Trailing comma",
|
||||
CodeStyleSettingsCustomizable.WRAPPING_KEEP);
|
||||
|
||||
consumer.showCustomOption(JsonCodeStyleSettings.class,
|
||||
"ARRAY_WRAPPING",
|
||||
@@ -60,6 +79,13 @@ public class JsonLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
|
||||
CodeStyleSettingsCustomizable.WRAP_OPTIONS,
|
||||
CodeStyleSettingsCustomizable.WRAP_VALUES);
|
||||
|
||||
consumer.showCustomOption(JsonCodeStyleSettings.class,
|
||||
"PROPERTY_ALIGNMENT",
|
||||
JsonBundle.message("formatter.align.properties.caption"),
|
||||
"Objects",
|
||||
ALIGN_OPTIONS,
|
||||
ALIGN_VALUES);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class JsonTrailingCommaRemover implements PreFormatProcessor {
|
||||
JsonCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(rootPsi.getProject())
|
||||
.getCurrentSettings()
|
||||
.getCustomSettings(JsonCodeStyleSettings.class);
|
||||
if (!settings.REMOVE_TRAILING_COMMA) {
|
||||
if (settings.KEEP_TRAILING_COMMA) {
|
||||
return range;
|
||||
}
|
||||
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(rootPsi.getProject());
|
||||
|
||||
@@ -85,7 +85,6 @@ public class JsonFormattingTest extends JsonTestCase {
|
||||
}
|
||||
|
||||
public void testRemoveTrailingCommas() {
|
||||
getCustomCodeStyleSettings().REMOVE_TRAILING_COMMA = true;
|
||||
doTest();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user