FIXED IDEA-156001 Code Style Import doesn't recognise project-specific .idea/codeStyleSettings.xml + unit tests

This commit is contained in:
Rustam Vishnyakov
2016-10-18 16:05:27 +03:00
parent b266a77d43
commit 9696d096c4
4 changed files with 94 additions and 2 deletions
@@ -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<CodeStyleScheme> schemeFactory) throws SchemeImportException {
Element rootElement = loadSchemeDom(selectedFile);
Element schemeRoot = getSchemeRoot(rootElement);
final Pair<String, CodeStyleScheme> 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 {
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<option name="RIGHT_MARGIN" value="40" />
<option name="WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN" value="true" />
<option name="FORMATTER_TAGS_ENABLED" value="true" />
<option name="FORMATTER_TAGS_ACCEPT_REGEXP" value="true" />
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</component>
</project>
@@ -0,0 +1,4 @@
<code_scheme name="TestScheme">
<option name="AUTODETECT_INDENTS" value="false" />
<option name="RIGHT_MARGIN" value="60" />
</code_scheme>
@@ -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<CodeStyleScheme> schemeFactory = new SchemeFactory<CodeStyleScheme>() {
@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/";
}
}