diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/manage/CodeStyleConfigurationHandler.kt b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/manage/CodeStyleConfigurationHandler.kt new file mode 100644 index 000000000000..c5c9bb217928 --- /dev/null +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/project/manage/CodeStyleConfigurationHandler.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.externalSystem.service.project.manage + +import com.intellij.openapi.externalSystem.model.project.ConfigurationData +import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider +import com.intellij.openapi.project.Project +import com.intellij.psi.codeStyle.CodeStyleSchemes + +/** + * Created by Nikita.Skvortsov + * date: 18.09.2017. + */ +class CodeStyleConfigurationHandler: ConfigurationHandler { + + override fun apply(project: Project, modelsProvider: IdeModifiableModelsProvider, configuration: ConfigurationData) { + val importedSchemeName = "Gradle Imported" + + val codeStyleSettings: Map = configuration.find("codeStyle") as? Map ?: return + val schemes = CodeStyleSchemes.getInstance() + + val existingScheme = schemes.findPreferredScheme(importedSchemeName) + if (existingScheme.name == importedSchemeName) { + schemes.deleteScheme(existingScheme) + } + + val importedScheme = schemes.createNewScheme(importedSchemeName, schemes.defaultScheme) + + codeStyleSettings["indent"]?.let { indentType -> + val indentOptions = importedScheme.codeStyleSettings.indentOptions ?: return@let + when (indentType) { + "tabs" -> { indentOptions.USE_TAB_CHARACTER = true } + "spaces" -> { indentOptions.USE_TAB_CHARACTER = false } + else -> {} // warn? + } + } + + codeStyleSettings["indentSize"]?.let { + val indentOptions = importedScheme.codeStyleSettings.indentOptions ?: return@let + if (it is Number) { + indentOptions.INDENT_SIZE = it.toInt() + } + } + + schemes.currentScheme = importedScheme + } +} \ No newline at end of file diff --git a/platform/platform-resources/src/META-INF/ExternalSystemExtensions.xml b/platform/platform-resources/src/META-INF/ExternalSystemExtensions.xml index f252a29d7043..20fa7c37f4ab 100644 --- a/platform/platform-resources/src/META-INF/ExternalSystemExtensions.xml +++ b/platform/platform-resources/src/META-INF/ExternalSystemExtensions.xml @@ -36,6 +36,7 @@ + diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleSettingsImportingTest.java b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleSettingsImportingTest.java index 4ee8f86217f2..d9bde324b662 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleSettingsImportingTest.java +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleSettingsImportingTest.java @@ -25,6 +25,9 @@ import com.intellij.openapi.externalSystem.service.project.manage.FacetHandlerEx import com.intellij.openapi.externalSystem.service.project.manage.RunConfigHandlerExtension; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; +import com.intellij.psi.codeStyle.CodeStyleScheme; +import com.intellij.psi.codeStyle.CodeStyleSchemes; +import com.intellij.psi.codeStyle.CodeStyleSettings; import org.jetbrains.annotations.NotNull; import org.junit.Test; import org.junit.runners.Parameterized; @@ -44,14 +47,43 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { return Arrays.asList(new Object[][]{{BASE_GRADLE_VERSION}}); } + + @Test + public void testCodeStyleSettingsImport() throws Exception { + importProject( + "buildscript {\n" + + " dependencies {\n" + + " classpath files('" + getGradlePluginPath() + "')\n" + + " }\n" + + "}\n" + + "apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'\n" + + "idea {\n" + + " project.settings {\n" + + " codeStyle {\n" + + " indent 'tabs'\n" + + " indentSize 3\n" + + " }\n" + + " }\n" + + "}" + ); + + final CodeStyleScheme scheme = CodeStyleSchemes.getInstance().getCurrentScheme(); + final CodeStyleSettings settings = scheme.getCodeStyleSettings(); + + assertEquals("Gradle Imported", scheme.getName()); + assertFalse(scheme.isDefault()); + + assertTrue(settings.getIndentOptions().USE_TAB_CHARACTER); + assertEquals(3, settings.getIndentOptions().INDENT_SIZE); + } + @Test public void testCompilerConfigurationSettingsImport() throws Exception { - final String pathToPlugin = getClass().getResource("/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar").toString(); importProject( "buildscript {\n" + " dependencies {\n" + - " classpath files('" + pathToPlugin + "')\n" + + " classpath files('" + getGradlePluginPath() + "')\n" + " }\n" + "}\n" + "apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'\n" + @@ -86,7 +118,6 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { @Test public void testApplicationRunConfigurationSettingsImport() throws Exception { - final String pathToPlugin = getClass().getResource("/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar").toString(); final String typeName = "testRunConfig"; TestRunConfigHandlerExtension testExtension = new TestRunConfigHandlerExtension(typeName); @@ -95,7 +126,7 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { importProject( "buildscript {\n" + " dependencies {\n" + - " classpath files('" + pathToPlugin + "')\n" + + " classpath files('" + getGradlePluginPath() + "')\n" + " }\n" + "}\n" + "apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'\n" + @@ -130,7 +161,6 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { @Test public void testFacetSettingsImport() throws Exception { - final String pathToPlugin = getClass().getResource("/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar").toString(); TestFacetHandlerExtension testExtension = new TestFacetHandlerExtension("testFacet"); Extensions.getRootArea().getExtensionPoint(FacetHandlerExtension.EP_NAME).registerExtension(testExtension); @@ -139,7 +169,7 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { importProject( "buildscript {\n" + " dependencies {\n" + - " classpath files('" + pathToPlugin + "')\n" + + " classpath files('" + getGradlePluginPath() + "')\n" + " }\n" + "}\n" + "apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'\n" + @@ -168,6 +198,9 @@ public class GradleSettingsImportingTest extends GradleImportingTestCase { assertEquals("Test Value 2", namedSettings.get("testField")); } + private String getGradlePluginPath() { + return getClass().getResource("/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar").toString(); + } } diff --git a/plugins/gradle/tooling-extension-impl/testData/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar b/plugins/gradle/tooling-extension-impl/testData/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar index 32e75c510240..395fec877a98 100755 Binary files a/plugins/gradle/tooling-extension-impl/testData/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar and b/plugins/gradle/tooling-extension-impl/testData/testCompilerConfigurationSettingsImport/gradle-idea-ext.jar differ