Extract setting configurator to utility class
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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 org.jetbrains.jet.formatter;
|
||||
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class FormattingSettingsConfigurator {
|
||||
private final String[] settingsToTrue;
|
||||
private final String[] settingsToFalse;
|
||||
|
||||
public FormattingSettingsConfigurator(String fileText) {
|
||||
settingsToTrue = InTextDirectivesUtils.findArrayWithPrefixes(fileText, "// SET_TRUE:");
|
||||
settingsToFalse = InTextDirectivesUtils.findArrayWithPrefixes(fileText, "// SET_FALSE:");
|
||||
}
|
||||
|
||||
public void configureSettings(CodeStyleSettings settings) {
|
||||
JetCodeStyleSettings jetSettings = settings.getCustomSettings(JetCodeStyleSettings.class);
|
||||
CommonCodeStyleSettings jetCommonSettings = settings.getCommonSettings(JetLanguage.INSTANCE);
|
||||
|
||||
for (String trueSetting : settingsToTrue) {
|
||||
configureSetting(jetSettings, jetCommonSettings, trueSetting, true);
|
||||
}
|
||||
|
||||
for (String falseSetting : settingsToFalse) {
|
||||
configureSetting(jetSettings, jetCommonSettings, falseSetting, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void configureInvertedSettings(CodeStyleSettings settings) {
|
||||
JetCodeStyleSettings jetSettings = settings.getCustomSettings(JetCodeStyleSettings.class);
|
||||
CommonCodeStyleSettings jetCommonSettings = settings.getCommonSettings(JetLanguage.INSTANCE);
|
||||
|
||||
for (String trueSetting : settingsToTrue) {
|
||||
configureSetting(jetSettings, jetCommonSettings, trueSetting, false);
|
||||
}
|
||||
|
||||
for (String falseSetting : settingsToFalse) {
|
||||
configureSetting(jetSettings, jetCommonSettings, falseSetting, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void configureSetting(JetCodeStyleSettings jetSettings,
|
||||
CommonCodeStyleSettings jetCommonSettings,
|
||||
String settingName,
|
||||
boolean value
|
||||
) {
|
||||
try {
|
||||
Field field = jetCommonSettings.getClass().getDeclaredField(settingName);
|
||||
setSetting(field, jetCommonSettings, value);
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
try {
|
||||
Field field = jetSettings.getClass().getDeclaredField(settingName);
|
||||
setSetting(field, jetSettings, value);
|
||||
}
|
||||
catch (NoSuchFieldException e1) {
|
||||
Assert.assertTrue(String.format("There's no property with name '%s' for kotlin language", settingName), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void setSetting(Field settingField, Object settings, boolean value) {
|
||||
try {
|
||||
settingField.setBoolean(settings, value);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
Assert.assertTrue(String.format("Can't set property with the name %s", settingField.getName()), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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 org.jetbrains.jet.formatter;
|
||||
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.testFramework.LightPlatformTestCase;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
|
||||
import org.jetbrains.jet.testing.SettingsConfigurator;
|
||||
|
||||
public class JetFormatSettingsUtil {
|
||||
private JetFormatSettingsUtil() {
|
||||
}
|
||||
|
||||
public static CodeStyleSettings getSettings() {
|
||||
return CodeStyleSettingsManager.getSettings(LightPlatformTestCase.getProject());
|
||||
}
|
||||
|
||||
public static SettingsConfigurator createConfigurator(String fileText, CodeStyleSettings settings) {
|
||||
return new SettingsConfigurator(fileText,
|
||||
settings.getCustomSettings(JetCodeStyleSettings.class),
|
||||
settings.getCommonSettings(JetLanguage.INSTANCE));
|
||||
}
|
||||
|
||||
public static SettingsConfigurator createConfigurator(String fileText) {
|
||||
return createConfigurator(fileText, getSettings());
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.formatter;
|
||||
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import org.jetbrains.jet.testing.SettingsConfigurator;
|
||||
|
||||
/**
|
||||
* Based on com.intellij.psi.formatter.java.JavaFormatterTest
|
||||
@@ -132,10 +131,6 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public static CodeStyleSettings getSettings() {
|
||||
return CodeStyleSettingsManager.getSettings(getProject());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doTest() throws Exception {
|
||||
String originalFileText = AbstractJetFormatterTest.loadFile(getTestName(false) + ".kt");
|
||||
@@ -143,29 +138,28 @@ public class JetFormatterTest extends AbstractJetFormatterTest {
|
||||
String afterFileName = getTestName(false) + "_after.kt";
|
||||
String afterText = AbstractJetFormatterTest.loadFile(afterFileName);
|
||||
|
||||
FormattingSettingsConfigurator configurator = new FormattingSettingsConfigurator(AbstractJetFormatterTest.loadFile(
|
||||
getTestName(false) + ".kt"));
|
||||
configurator.configureSettings(getSettings());
|
||||
SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText);
|
||||
configurator.configureSettings();
|
||||
|
||||
doTextTest(originalFileText, afterText, String.format("Failure in NORMAL file: %s", afterFileName));
|
||||
|
||||
getSettings().clearCodeStyleSettings();
|
||||
JetFormatSettingsUtil.getSettings().clearCodeStyleSettings();
|
||||
}
|
||||
|
||||
public void doTestWithInvert() throws Exception {
|
||||
String originalFileText = AbstractJetFormatterTest.loadFile(getTestName(false) + ".kt");
|
||||
FormattingSettingsConfigurator configurator = new FormattingSettingsConfigurator(originalFileText);
|
||||
SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText, JetFormatSettingsUtil.getSettings());
|
||||
|
||||
String afterFileName = getTestName(false) + "_after.kt";
|
||||
String afterText = AbstractJetFormatterTest.loadFile(afterFileName);
|
||||
configurator.configureSettings(getSettings());
|
||||
configurator.configureSettings();
|
||||
doTextTest(originalFileText, afterText, String.format("Failure in NORMAL file: %s", afterFileName));
|
||||
|
||||
String afterInvertedFileName = getTestName(false) + "_after_inv.kt";
|
||||
String afterInvertedText = AbstractJetFormatterTest.loadFile(afterInvertedFileName);
|
||||
configurator.configureInvertedSettings(getSettings());
|
||||
configurator.configureInvertedSettings();
|
||||
doTextTest(originalFileText, afterInvertedText, String.format("Failure in INVERTED file: %s", afterInvertedFileName));
|
||||
|
||||
getSettings().clearCodeStyleSettings();
|
||||
JetFormatSettingsUtil.getSettings().clearCodeStyleSettings();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
import org.jetbrains.jet.testing.SettingsConfigurator;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -104,12 +106,13 @@ public class JetTypingIndentationTest extends LightCodeInsightTestCase {
|
||||
|
||||
public void doFileSettingNewLineTest() throws Exception {
|
||||
String originalFileText = FileUtil.loadFile(new File(getTestDataPath(), getBeforeFileName()));
|
||||
FormattingSettingsConfigurator configurator = new FormattingSettingsConfigurator(originalFileText);
|
||||
|
||||
configurator.configureSettings(getSettings());
|
||||
SettingsConfigurator configurator = JetFormatSettingsUtil.createConfigurator(originalFileText);
|
||||
|
||||
configurator.configureSettings();
|
||||
doNewlineTest(getBeforeFileName(), getAfterFileName());
|
||||
|
||||
configurator.configureInvertedSettings(getSettings());
|
||||
configurator.configureInvertedSettings();
|
||||
doNewlineTest(getBeforeFileName(), getInvertedAfterFileName());
|
||||
|
||||
getSettings().clearCodeStyleSettings();
|
||||
@@ -125,9 +128,9 @@ public class JetTypingIndentationTest extends LightCodeInsightTestCase {
|
||||
return CodeStyleSettingsManager.getSettings(getProject());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
|
||||
String testRelativeDir = "formatter/IndentationOnNewline";
|
||||
return new File(PluginTestCaseBase.getTestDataPathBase(), testRelativeDir).getPath() +
|
||||
File.separator;
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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 org.jetbrains.jet.testing;
|
||||
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SettingsConfigurator {
|
||||
private final String[] settingsToTrue;
|
||||
private final String[] settingsToFalse;
|
||||
private final Object[] objects;
|
||||
|
||||
public SettingsConfigurator(String fileText, Object... objects) {
|
||||
settingsToTrue = InTextDirectivesUtils.findArrayWithPrefixes(fileText, "// SET_TRUE:");
|
||||
settingsToFalse = InTextDirectivesUtils.findArrayWithPrefixes(fileText, "// SET_FALSE:");
|
||||
this.objects = objects;
|
||||
}
|
||||
|
||||
public static void setBooleanSetting(String settingName, boolean value, Object... objects) {
|
||||
for (Object object : objects) {
|
||||
if (setSettingWithField(settingName, object, value) || setSettingWithMethod(settingName, object, value)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"There's no property or method with name '%s' in given objects: %s", settingName, Arrays.toString(objects)));
|
||||
}
|
||||
|
||||
public void configureSettings() {
|
||||
for (String trueSetting : settingsToTrue) {
|
||||
setBooleanSetting(trueSetting, true, objects);
|
||||
}
|
||||
|
||||
for (String falseSetting : settingsToFalse) {
|
||||
setBooleanSetting(falseSetting, false, objects);
|
||||
}
|
||||
}
|
||||
|
||||
public void configureInvertedSettings() {
|
||||
for (String trueSetting : settingsToTrue) {
|
||||
setBooleanSetting(trueSetting, false, objects);
|
||||
}
|
||||
|
||||
for (String falseSetting : settingsToFalse) {
|
||||
setBooleanSetting(falseSetting, true, objects);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean setSettingWithField(String settingName, Object object, boolean value) {
|
||||
try {
|
||||
Field field = object.getClass().getDeclaredField(settingName);
|
||||
field.setBoolean(object, value);
|
||||
return true;
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(String.format("Can't set property with the name %s in object %s", settingName, object));
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
// Do nothing - will try other variants
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean setSettingWithMethod(String setterName, Object object, boolean value) {
|
||||
try {
|
||||
Method method = object.getClass().getMethod(setterName, boolean.class);
|
||||
method.invoke(object, value);
|
||||
return true;
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
throw new IllegalArgumentException(String.format("Can't call method with name %s for object %s", setterName, object));
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(String.format("Can't access to method with name %s for object %s", setterName, object));
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
// Do nothing - will try other variants
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user