From 9696d096c44127aacd8f349a388ae0331f6c0f00 Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Tue, 18 Oct 2016 15:29:18 +0300 Subject: [PATCH] FIXED IDEA-156001 Code Style Import doesn't recognise project-specific .idea/codeStyleSettings.xml + unit tests --- .../codeStyle/CodeStyleSchemeXmlImporter.java | 24 ++++++++- .../projectCodeStyleSettings.xml | 14 +++++ .../importSettings/standardCodeStyleXml.xml | 4 ++ .../CodeStyleXmlImporterTest.java | 54 +++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml create mode 100644 platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml create mode 100644 platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java 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 5bcf152991fc..b9a48f6b464c 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 @@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.codeStyle; import com.intellij.application.options.ImportSchemeChooserDialog; import com.intellij.openapi.application.ApplicationBundle; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.options.SchemeFactory; import com.intellij.openapi.options.SchemeImportException; import com.intellij.openapi.options.SchemeImporter; @@ -49,10 +50,13 @@ public class CodeStyleSchemeXmlImporter extends CodeStyleSettingsLoader implemen @NotNull CodeStyleScheme currentScheme, @NotNull SchemeFactory schemeFactory) throws SchemeImportException { Element rootElement = loadSchemeDom(selectedFile); + Element schemeRoot = getSchemeRoot(rootElement); final Pair importPair = - ImportSchemeChooserDialog.selectOrCreateTargetScheme(project, currentScheme, schemeFactory, getSchemeName(rootElement)); + !ApplicationManager.getApplication().isUnitTestMode() ? + ImportSchemeChooserDialog.selectOrCreateTargetScheme(project, currentScheme, schemeFactory, getSchemeName(schemeRoot)) : + Pair.create(currentScheme.getName(), currentScheme); if (importPair != null) { - return readSchemeFromDom(rootElement, importPair.second); + return readSchemeFromDom(schemeRoot, importPair.second); } return null; } @@ -60,6 +64,7 @@ public class CodeStyleSchemeXmlImporter extends CodeStyleSettingsLoader implemen @NotNull private static String getSchemeName(@NotNull Element rootElement) throws SchemeImportException { String rootName = rootElement.getName(); + if ("value".equals(rootElement.getName())) return "Project"; if (!"code_scheme".equals(rootName)) { throw new SchemeImportException(ApplicationBundle.message("settings.code.style.import.xml.error.invalid.file", rootName)); } @@ -69,6 +74,21 @@ 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) throws SchemeImportException { diff --git a/platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml b/platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml new file mode 100644 index 000000000000..72b3f4778a63 --- /dev/null +++ b/platform/lang-impl/testData/codeStyle/importSettings/projectCodeStyleSettings.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml b/platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml new file mode 100644 index 000000000000..fe6afc020da6 --- /dev/null +++ b/platform/lang-impl/testData/codeStyle/importSettings/standardCodeStyleXml.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java b/platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java new file mode 100644 index 000000000000..fba88b0da9ee --- /dev/null +++ b/platform/lang-impl/testSources/com/intellij/application/options/codeStyle/importSettings/CodeStyleXmlImporterTest.java @@ -0,0 +1,54 @@ +package com.intellij.application.options.codeStyle.importSettings; + +import com.intellij.openapi.application.ex.PathManagerEx; +import com.intellij.openapi.options.SchemeFactory; +import com.intellij.openapi.options.SchemeImportException; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.codeStyle.CodeStyleScheme; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl; +import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeXmlImporter; +import com.intellij.testFramework.LightPlatformTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; + +public class CodeStyleXmlImporterTest extends LightPlatformTestCase { + public void testStandardCodeStyleXml() throws SchemeImportException { + CodeStyleSettings settings= importSettings(); + assertEquals(false, settings.AUTODETECT_INDENTS); + assertEquals(60, settings.getDefaultRightMargin()); + } + + public void testProjectCodeStyleSettings() throws SchemeImportException { + CodeStyleSettings settings= importSettings(); + assertEquals(40, settings.getDefaultRightMargin()); + assertEquals(true, settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN); + assertEquals(true, settings.FORMATTER_TAGS_ENABLED); + assertEquals(true, settings.FORMATTER_TAGS_ACCEPT_REGEXP); + } + + private CodeStyleSettings importSettings() throws SchemeImportException { + final CodeStyleScheme targetScheme = new CodeStyleSchemeImpl("Test", false, null); + SchemeFactory schemeFactory = new SchemeFactory() { + @Override + public CodeStyleScheme createNewScheme(@Nullable String name) { + return targetScheme; + } + }; + File ioFile = new File(getTestDataPath() + getTestName(true) + ".xml"); + assertExists(ioFile); + VirtualFile vFile = VfsUtil.findFileByIoFile(ioFile, true); + CodeStyleSchemeXmlImporter importer = new CodeStyleSchemeXmlImporter(); + return importer.importScheme(getProject(), vFile, targetScheme, schemeFactory).getCodeStyleSettings(); + } + + @NotNull + protected String getTestDataPath() { + return PathManagerEx.getTestDataPath() + "/../../../platform/lang-impl/testData/codeStyle/importSettings/"; + } +} + +