From 75a2b0cd034f3370be6af6f9a5172d6e428ac63c Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Wed, 25 Oct 2017 16:02:25 +0300 Subject: [PATCH] Support old and new project code style settings in import & command line See also IDEA-164602 --- .../codeStyle/CodeStyleSchemeXmlImporter.java | 20 ++----- .../codeStyle/CodeStyleSettingsLoader.java | 34 +++++++++++- .../importSettings/newProjectSettings.xml | 52 +++++++++++++++++++ .../CodeStyleXmlImporterTest.java | 6 +++ 4 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 platform/platform-tests/testData/codeStyle/importSettings/newProjectSettings.xml diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeXmlImporter.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeXmlImporter.java index 60e1a23a2ba4..a275686505ab 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeXmlImporter.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeXmlImporter.java @@ -51,7 +51,7 @@ public class CodeStyleSchemeXmlImporter extends CodeStyleSettingsLoader implemen @NotNull CodeStyleScheme currentScheme, @NotNull SchemeFactory schemeFactory) throws SchemeImportException { Element rootElement = SchemeImportUtil.loadSchemeDom(selectedFile); - Element schemeRoot = getSchemeRoot(rootElement); + Element schemeRoot = findSchemeRoot(rootElement); final Pair 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); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSettingsLoader.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSettingsLoader.java index 7e15d41fd691..34060e714774 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSettingsLoader.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSettingsLoader.java @@ -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; + } } diff --git a/platform/platform-tests/testData/codeStyle/importSettings/newProjectSettings.xml b/platform/platform-tests/testData/codeStyle/importSettings/newProjectSettings.xml new file mode 100644 index 000000000000..8f2f8deed955 --- /dev/null +++ b/platform/platform-tests/testData/codeStyle/importSettings/newProjectSettings.xml @@ -0,0 +1,52 @@ + + + + \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java index f7bb4eed4400..4e57e0d8352e 100644 --- a/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java +++ b/platform/platform-tests/testSrc/com/intellij/ide/codeStyleSettings/CodeStyleXmlImporterTest.java @@ -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);