From a7f14e657d2cc80f2c1dc758b027871368fbb4ab Mon Sep 17 00:00:00 2001 From: "Rustam.Vishnyakov" Date: Mon, 30 Aug 2010 15:58:50 +0400 Subject: [PATCH] Per-language code style settings, more refactoring and test fixes --- .../psi/codeStyle/CodeStyleSettings.java | 27 +++--- .../codeStyle/CommonCodeStyleSettings.java | 94 +++++++++++++------ .../CommonCodeStyleSettingsManager.java | 6 +- .../LanguageCodeStyleSettingsProvider.java | 3 +- 4 files changed, 79 insertions(+), 51 deletions(-) diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java index 6c5a851c94e1..e8d0589e71ac 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettings.java @@ -39,6 +39,12 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea private final ClassMap myCustomSettings = new ClassMap(); @NonNls private static final String ADDITIONAL_INDENT_OPTIONS = "ADDITIONAL_INDENT_OPTIONS"; + + @Override + public CodeStyleSettings getMainSettings() { + return this; + } + @NonNls private static final String FILETYPE = "fileType"; private final CommonCodeStyleSettingsManager myCommonSettingsManager = new CommonCodeStyleSettingsManager(this); @@ -47,6 +53,7 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea } public CodeStyleSettings(boolean loadExtensions) { + super(null); initTypeToName(); initImportsByDefault(); @@ -616,22 +623,6 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea //---------------------------------------------------------------------------------------- - //-------------- Annotation formatting settings------------------------------------------- - - public int METHOD_ANNOTATION_WRAP = WRAP_ALWAYS; - public int CLASS_ANNOTATION_WRAP = WRAP_ALWAYS; - public int FIELD_ANNOTATION_WRAP = WRAP_ALWAYS; - public int PARAMETER_ANNOTATION_WRAP = DO_NOT_WRAP; - public int VARIABLE_ANNOTATION_WRAP = DO_NOT_WRAP; - - public boolean SPACE_BEFORE_ANOTATION_PARAMETER_LIST = false; - public boolean SPACE_WITHIN_ANNOTATION_PARENTHESES = false; - - //---------------------------------------------------------------------------------------- - - - //-------------------------Enums---------------------------------------------------------- - public int ENUM_CONSTANTS_WRAP = DO_NOT_WRAP; //---------------------------------------------------------------------------------------- private CodeStyleSettings myParentSettings; @@ -997,4 +988,8 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea public CommonCodeStyleSettings getCommonSettings(Language lang) { return myCommonSettingsManager.getCommonSettings(lang); } + + public CommonCodeStyleSettings createCommonSettings(Language lang) { + return new CommonCodeStyleSettings(this); + } } diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java index 49c05902ccd6..65aed3468dc3 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java @@ -15,6 +15,8 @@ */ package com.intellij.psi.codeStyle; +import org.jetbrains.annotations.Nullable; + /** * Common code style settings can be used by several programming languages. Each language may have its own * instance of CommonCodeStyleSettings. @@ -23,6 +25,16 @@ package com.intellij.psi.codeStyle; */ public class CommonCodeStyleSettings { + private final CodeStyleSettings myMainSettings; + + CommonCodeStyleSettings(@Nullable CodeStyleSettings mainSettings) { + myMainSettings = mainSettings; + } + + public CodeStyleSettings getMainSettings() { + return myMainSettings; + } + //----------------- GENERAL -------------------- public boolean LINE_COMMENT_AT_FIRST_COLUMN = true; @@ -42,6 +54,39 @@ public class CommonCodeStyleSettings { */ public boolean KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true; +//----------------- BLANK LINES -------------------- + + /** + * Keep up to this amount of blank lines between declarations + */ + public int KEEP_BLANK_LINES_IN_DECLARATIONS = 2; + + /** + * Keep up to this amount of blank lines in code + */ + public int KEEP_BLANK_LINES_IN_CODE = 2; + + public int KEEP_BLANK_LINES_BEFORE_RBRACE = 2; + + public int BLANK_LINES_BEFORE_PACKAGE = 0; + public int BLANK_LINES_AFTER_PACKAGE = 1; + public int BLANK_LINES_BEFORE_IMPORTS = 1; + public int BLANK_LINES_AFTER_IMPORTS = 1; + + public int BLANK_LINES_AROUND_CLASS = 1; + public int BLANK_LINES_AROUND_FIELD = 0; + public int BLANK_LINES_AROUND_METHOD = 1; + public int BLANK_LINES_BEFORE_METHOD_BODY = 0; + + public int BLANK_LINES_AROUND_FIELD_IN_INTERFACE = 0; + public int BLANK_LINES_AROUND_METHOD_IN_INTERFACE = 1; + + + public int BLANK_LINES_AFTER_CLASS_HEADER = 0; + public int BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER = 0; + //public int BLANK_LINES_BETWEEN_CASE_BLOCKS; + + //----------------- BRACES & INDENTS -------------------- /** @@ -196,37 +241,6 @@ public class CommonCodeStyleSettings { public boolean ALIGN_GROUP_FIELD_DECLARATIONS = false; -//----------------- BLANK LINES -------------------- - - /** - * Keep up to this amount of blank lines between declarations - */ - public int KEEP_BLANK_LINES_IN_DECLARATIONS = 2; - - /** - * Keep up to this amount of blank lines in code - */ - public int KEEP_BLANK_LINES_IN_CODE = 2; - - public int KEEP_BLANK_LINES_BEFORE_RBRACE = 2; - - public int BLANK_LINES_BEFORE_PACKAGE = 0; - public int BLANK_LINES_AFTER_PACKAGE = 1; - public int BLANK_LINES_BEFORE_IMPORTS = 1; - public int BLANK_LINES_AFTER_IMPORTS = 1; - - public int BLANK_LINES_AROUND_CLASS = 1; - public int BLANK_LINES_AROUND_FIELD = 0; - public int BLANK_LINES_AROUND_METHOD = 1; - public int BLANK_LINES_BEFORE_METHOD_BODY = 0; - - public int BLANK_LINES_AROUND_FIELD_IN_INTERFACE = 0; - public int BLANK_LINES_AROUND_METHOD_IN_INTERFACE = 0; - - - public int BLANK_LINES_AFTER_CLASS_HEADER = 0; - public int BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER = 0; - //public int BLANK_LINES_BETWEEN_CASE_BLOCKS; //----------------- SPACES -------------------- @@ -591,4 +605,22 @@ public class CommonCodeStyleSettings { public int WHILE_BRACE_FORCE = DO_NOT_FORCE; public int FOR_BRACE_FORCE = DO_NOT_FORCE; + //-------------- Annotation formatting settings------------------------------------------- + + public int METHOD_ANNOTATION_WRAP = WRAP_ALWAYS; + public int CLASS_ANNOTATION_WRAP = WRAP_ALWAYS; + public int FIELD_ANNOTATION_WRAP = WRAP_ALWAYS; + public int PARAMETER_ANNOTATION_WRAP = DO_NOT_WRAP; + public int VARIABLE_ANNOTATION_WRAP = DO_NOT_WRAP; + + public boolean SPACE_BEFORE_ANOTATION_PARAMETER_LIST = false; + public boolean SPACE_WITHIN_ANNOTATION_PARENTHESES = false; + + //---------------------------------------------------------------------------------------- + + + //-------------------------Enums---------------------------------------------------------- + public int ENUM_CONSTANTS_WRAP = DO_NOT_WRAP; + + } diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettingsManager.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettingsManager.java index c72d392444c7..639487415130 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettingsManager.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettingsManager.java @@ -31,9 +31,9 @@ import java.util.Map; public class CommonCodeStyleSettingsManager { private Map myCommonSettingsMap; - private final CommonCodeStyleSettings myParentSettings; + private final CodeStyleSettings myParentSettings; - public CommonCodeStyleSettingsManager(CommonCodeStyleSettings parentSettings) { + public CommonCodeStyleSettingsManager(CodeStyleSettings parentSettings) { myParentSettings = parentSettings; } @@ -60,7 +60,7 @@ public class CommonCodeStyleSettingsManager { final LanguageCodeStyleSettingsProvider[] providers = Extensions.getExtensions(LanguageCodeStyleSettingsProvider.EP_NAME); for (final LanguageCodeStyleSettingsProvider provider : providers) { if (!myCommonSettingsMap.containsKey(provider.getLanguage())) { - CommonCodeStyleSettings initialSettings = provider.getDefaultCommonSettings(); + CommonCodeStyleSettings initialSettings = provider.getDefaultCommonSettings(myParentSettings); if (initialSettings != null) { registerCommonSettings(provider.getLanguage(), initialSettings); } diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java index 0c8fcfae5567..0d5593f07091 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java @@ -46,11 +46,12 @@ public abstract class LanguageCodeStyleSettingsProvider { * Creates an instance of CommonCodeStyleSettings and sets initial default values for those * settings which differ from the original. * + * @param settings Main settings containing the common code style settings. * @return Created instance of CommonCodeStyleSettings or null if associated language doesn't * use its own language-specific common settings (the settings are shared with other languages). */ @Nullable - public CommonCodeStyleSettings getDefaultCommonSettings() { + public CommonCodeStyleSettings getDefaultCommonSettings(CodeStyleSettings settings) { return null; }