add simplistic code style settings import

This commit is contained in:
Nikita Skvortsov
2017-10-02 11:34:02 +03:00
parent 6203b3f440
commit a2388dbff1
4 changed files with 100 additions and 6 deletions
@@ -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<String, *> = configuration.find("codeStyle") as? Map<String,*> ?: 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
}
}
@@ -36,6 +36,7 @@
<externalProjectDataService implementation="com.intellij.openapi.externalSystem.service.project.manage.ConfigurationDataService"/>
<externalSystemConfigurationHandler implementation="com.intellij.openapi.externalSystem.service.project.manage.CompilerConfigurationHandler"/>
<externalSystemConfigurationHandler implementation="com.intellij.openapi.externalSystem.service.project.manage.CodeStyleConfigurationHandler"/>
<externalSystemConfigurationHandler implementation="com.intellij.openapi.externalSystem.service.project.manage.RunConfigurationHandler"/>
<externalSystemConfigurationHandler implementation="com.intellij.openapi.externalSystem.service.project.manage.FacetHandler"/>
<runConfigurationHandlerExtension implementation="com.intellij.openapi.externalSystem.service.project.manage.ApplicationRunConfigHandler"/>
@@ -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();
}
}