Commit 75a2b0cd authored by Rustam Vishnyakov's avatar Rustam Vishnyakov
Browse files

Support old and new project code style settings in import & command line

See also IDEA-164602
parent 2dad4d6c
Showing with 93 additions and 19 deletions
+93 -19
......@@ -51,7 +51,7 @@ public class CodeStyleSchemeXmlImporter extends CodeStyleSettingsLoader implemen
@NotNull CodeStyleScheme currentScheme,
@NotNull SchemeFactory<CodeStyleScheme> schemeFactory) throws SchemeImportException {
Element rootElement = SchemeImportUtil.loadSchemeDom(selectedFile);
Element schemeRoot = getSchemeRoot(rootElement);
Element schemeRoot = findSchemeRoot(rootElement);
final Pair<String, CodeStyleScheme> importPair =
!ApplicationManager.getApplication().isUnitTestMode() ?
ImportSchemeChooserDialog.selectOrCreateTargetScheme(project, currentScheme, schemeFactory, getSchemeName(schemeRoot)) :
......@@ -75,23 +75,9 @@ public class CodeStyleSchemeXmlImporter extends CodeStyleSettingsLoader implemen
}
return schemeNameAttr.getValue();
}
private static Element getSchemeRoot(@NotNull Element rootElement) throws SchemeImportException {
if ("project".equals(rootElement.getName())) {
Element child = rootElement.getChild("component");
if (child != null && "ProjectCodeStyleSettingsManager".equals(child.getAttributeValue("name"))) {
child = child.getChild("option");
if (child != null && "PER_PROJECT_SETTINGS".equals(child.getAttributeValue("name"))) {
child = child.getChild("value");
if (child != null) return child;
}
}
throw new SchemeImportException("Invalid scheme root: " + rootElement.getName());
}
return rootElement;
}
private CodeStyleScheme readSchemeFromDom(@NotNull Element rootElement, @NotNull CodeStyleScheme scheme)
private static CodeStyleScheme readSchemeFromDom(@NotNull Element rootElement, @NotNull CodeStyleScheme scheme)
throws SchemeImportException {
CodeStyleSettings newSettings = new CodeStyleSettings();
loadSettings(rootElement, newSettings);
......
......@@ -33,13 +33,43 @@ public class CodeStyleSettingsLoader {
return settings;
}
protected void loadSettings(@NotNull Element rootElement, @NotNull CodeStyleSettings settings) throws SchemeImportException {
protected static void loadSettings(@NotNull Element rootElement, @NotNull CodeStyleSettings settings) throws SchemeImportException {
try {
settings.readExternal(rootElement);
settings.readExternal(findSchemeRoot(rootElement));
}
catch (InvalidDataException e) {
throw new SchemeImportException(ApplicationBundle.message("settings.code.style.import.xml.error.can.not.load", e.getMessage()));
}
}
protected static Element findSchemeRoot(@NotNull Element rootElement) throws SchemeImportException {
String rootName = rootElement.getName();
//
// Project code style 172.x and earlier
//
if ("project".equals(rootName)) {
Element child = rootElement.getChild("component");
if (child != null && "ProjectCodeStyleSettingsManager".equals(child.getAttributeValue("name"))) {
child = child.getChild("option");
if (child != null && "PER_PROJECT_SETTINGS".equals(child.getAttributeValue("name"))) {
child = child.getChild("value");
if (child != null) return child;
}
}
throw new SchemeImportException("Invalid scheme root: " + rootName);
}
//
// Project code style 173.x and later
//
else if ("component".equals(rootName)) {
if ("ProjectCodeStyleConfiguration".equals(rootElement.getAttributeValue("name"))) {
Element child = rootElement.getChild("code_scheme");
if (child != null) {
return child;
}
}
throw new SchemeImportException("Invalid scheme root: " + rootName);
}
return rootElement;
}
}
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="LINE_SEPARATOR" value="&#10;" />
<option name="RIGHT_MARGIN" value="140" />
<option name="FORMATTER_TAGS_ENABLED" value="true" />
<option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" />
<option name="BLOCK_COMMENT_AT_FIRST_COLUMN" value="false" />
<codeStyleSettings language="JAVA">
<option name="LINE_COMMENT_AT_FIRST_COLUMN" value="false" />
<option name="BLOCK_COMMENT_AT_FIRST_COLUMN" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" />
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="WHILE_ON_NEW_LINE" value="true" />
<option name="CATCH_ON_NEW_LINE" value="true" />
<option name="FINALLY_ON_NEW_LINE" value="true" />
<option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" />
<option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
<option name="ALIGN_MULTILINE_ASSIGNMENT" value="true" />
<option name="ALIGN_MULTILINE_TERNARY_OPERATION" value="true" />
<option name="ALIGN_MULTILINE_THROWS_LIST" value="true" />
<option name="ALIGN_MULTILINE_EXTENDS_LIST" value="true" />
<option name="ALIGN_MULTILINE_PARENTHESIZED_EXPRESSION" value="true" />
<option name="SPACE_WITHIN_BRACES" value="true" />
<option name="SPACE_AFTER_TYPE_CAST" value="false" />
<option name="CALL_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_WRAP" value="5" />
<option name="EXTENDS_LIST_WRAP" value="1" />
<option name="THROWS_LIST_WRAP" value="5" />
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
<option name="THROWS_KEYWORD_WRAP" value="1" />
<option name="METHOD_CALL_CHAIN_WRAP" value="1" />
<option name="BINARY_OPERATION_WRAP" value="5" />
<option name="TERNARY_OPERATION_WRAP" value="5" />
<option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
<option name="KEEP_SIMPLE_METHODS_IN_ONE_LINE" value="true" />
<option name="FOR_STATEMENT_WRAP" value="5" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="ASSIGNMENT_WRAP" value="1" />
<option name="IF_BRACE_FORCE" value="1" />
<option name="DOWHILE_BRACE_FORCE" value="1" />
<option name="WHILE_BRACE_FORCE" value="1" />
<option name="FOR_BRACE_FORCE" value="1" />
<option name="FIELD_ANNOTATION_WRAP" value="0" />
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="8" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
......@@ -41,6 +41,12 @@ public class CodeStyleXmlImporterTest extends CodeStyleTestCase {
assertEquals(true, settings.FORMATTER_TAGS_ENABLED);
assertEquals(true, settings.FORMATTER_TAGS_ACCEPT_REGEXP);
}
public void testNewProjectSettings() throws SchemeImportException {
CodeStyleSettings settings= importSettings();
assertEquals(140, settings.getDefaultRightMargin());
assertEquals(true, settings.FORMATTER_TAGS_ENABLED);
}
private CodeStyleSettings importSettings() throws SchemeImportException {
final CodeStyleScheme targetScheme = new CodeStyleSchemeImpl("Test", false, null);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment