/json/src/com/intellij/json/formatter/JsonLanguageCodeStyleSettingsProvider.java

http://github.com/JetBrains/intellij-community · Java · 115 lines · 96 code · 15 blank · 4 comment · 8 complexity · d46578caed2835db4d6437090b83d64d MD5 · raw file

  1. package com.intellij.json.formatter;
  2. import com.intellij.application.options.IndentOptionsEditor;
  3. import com.intellij.application.options.SmartIndentOptionsEditor;
  4. import com.intellij.json.JsonBundle;
  5. import com.intellij.json.JsonLanguage;
  6. import com.intellij.lang.Language;
  7. import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable;
  8. import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
  9. import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
  10. import com.intellij.util.ArrayUtil;
  11. import com.intellij.util.containers.ContainerUtil;
  12. import org.jetbrains.annotations.NotNull;
  13. import org.jetbrains.annotations.Nullable;
  14. import java.util.Arrays;
  15. import static com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.SPACES_OTHER;
  16. /**
  17. * @author Mikhail Golubev
  18. */
  19. public class JsonLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {
  20. private static class Holder {
  21. private static final String[] ALIGN_OPTIONS = Arrays.stream(JsonCodeStyleSettings.PropertyAlignment.values())
  22. .map(alignment -> alignment.getDescription())
  23. .toArray(value -> new String[value]);
  24. private static final int[] ALIGN_VALUES =
  25. ArrayUtil.toIntArray(
  26. ContainerUtil.map(JsonCodeStyleSettings.PropertyAlignment.values(), alignment -> alignment.getId()));
  27. private static final String SAMPLE = "{\n" +
  28. " \"json literals are\": {\n" +
  29. " \"strings\": [\"foo\", \"bar\", \"\\u0062\\u0061\\u0072\"],\n" +
  30. " \"numbers\": [42, 6.62606975e-34],\n" +
  31. " \"boolean values\": [true, false,],\n" +
  32. " \"objects\": {\"null\": null,\"another\": null,}\n" +
  33. " }\n" +
  34. "}";
  35. }
  36. @Override
  37. public void customizeSettings(@NotNull CodeStyleSettingsCustomizable consumer, @NotNull SettingsType settingsType) {
  38. if (settingsType == SettingsType.SPACING_SETTINGS) {
  39. consumer.showStandardOptions("SPACE_WITHIN_BRACKETS",
  40. "SPACE_WITHIN_BRACES",
  41. "SPACE_AFTER_COMMA",
  42. "SPACE_BEFORE_COMMA");
  43. consumer.renameStandardOption("SPACE_WITHIN_BRACES", "Braces");
  44. consumer.showCustomOption(JsonCodeStyleSettings.class, "SPACE_BEFORE_COLON", "Before ':'", SPACES_OTHER);
  45. consumer.showCustomOption(JsonCodeStyleSettings.class, "SPACE_AFTER_COLON", "After ':'", SPACES_OTHER);
  46. }
  47. else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
  48. consumer.showStandardOptions("KEEP_BLANK_LINES_IN_CODE");
  49. }
  50. else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) {
  51. consumer.showStandardOptions("RIGHT_MARGIN",
  52. "WRAP_ON_TYPING",
  53. "KEEP_LINE_BREAKS",
  54. "WRAP_LONG_LINES");
  55. consumer.showCustomOption(JsonCodeStyleSettings.class,
  56. "KEEP_TRAILING_COMMA",
  57. "Trailing comma",
  58. CodeStyleSettingsCustomizable.WRAPPING_KEEP);
  59. consumer.showCustomOption(JsonCodeStyleSettings.class,
  60. "ARRAY_WRAPPING",
  61. "Arrays",
  62. null,
  63. CodeStyleSettingsCustomizable.WRAP_OPTIONS,
  64. CodeStyleSettingsCustomizable.WRAP_VALUES);
  65. consumer.showCustomOption(JsonCodeStyleSettings.class,
  66. "OBJECT_WRAPPING",
  67. "Objects",
  68. null,
  69. CodeStyleSettingsCustomizable.WRAP_OPTIONS,
  70. CodeStyleSettingsCustomizable.WRAP_VALUES);
  71. consumer.showCustomOption(JsonCodeStyleSettings.class,
  72. "PROPERTY_ALIGNMENT",
  73. JsonBundle.message("formatter.align.properties.caption"),
  74. "Objects",
  75. Holder.ALIGN_OPTIONS,
  76. Holder.ALIGN_VALUES);
  77. }
  78. }
  79. @NotNull
  80. @Override
  81. public Language getLanguage() {
  82. return JsonLanguage.INSTANCE;
  83. }
  84. @Nullable
  85. @Override
  86. public IndentOptionsEditor getIndentOptionsEditor() {
  87. return new SmartIndentOptionsEditor();
  88. }
  89. @Override
  90. public String getCodeSample(@NotNull SettingsType settingsType) {
  91. return Holder.SAMPLE;
  92. }
  93. @Override
  94. protected void customizeDefaults(@NotNull CommonCodeStyleSettings commonSettings,
  95. @NotNull CommonCodeStyleSettings.IndentOptions indentOptions) {
  96. indentOptions.INDENT_SIZE = 2;
  97. // strip all blank lines by default
  98. commonSettings.KEEP_BLANK_LINES_IN_CODE = 0;
  99. }
  100. }