mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[code style] CODE_STYLE_DEFAULTS should write to settings
^KTIJ-7687 Fixed GitOrigin-RevId: 7c485157d0977ff6635832d8d489769ab7d65132
This commit is contained in:
committed by
intellij-monorepo-bot
parent
3dcf5403b8
commit
22fc15480c
+7
-15
@@ -12,6 +12,7 @@ import com.intellij.openapi.util.InvalidDataException;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.LanguageCodeStyleProvider;
|
||||
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
|
||||
import com.intellij.psi.codeStyle.arrangement.ArrangementSettings;
|
||||
import com.intellij.psi.codeStyle.arrangement.ArrangementUtil;
|
||||
@@ -66,11 +67,9 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(Element element) throws WriteExternalException {
|
||||
CommonCodeStyleSettings defaultSettings = getDefaultSettings();
|
||||
if (defaultSettings != null) {
|
||||
FormatterUtilKt.applyKotlinCodeStyle(CODE_STYLE_DEFAULTS, defaultSettings, false);
|
||||
}
|
||||
public void writeExternal(@NotNull Element element, @NotNull LanguageCodeStyleProvider provider) {
|
||||
CommonCodeStyleSettings defaultSettings = provider.getDefaultCommonSettings();
|
||||
FormatterUtilKt.applyKotlinCodeStyle(CODE_STYLE_DEFAULTS, defaultSettings, false);
|
||||
|
||||
writeExternalBase(element, defaultSettings);
|
||||
}
|
||||
@@ -121,8 +120,7 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
|
||||
Method setRootSettingsMethod = CommonCodeStyleSettings.class.getDeclaredMethod("setRootSettings", CodeStyleSettings.class);
|
||||
setRootSettingsMethod.setAccessible(true);
|
||||
setRootSettingsMethod.invoke(commonSettings, rootSettings);
|
||||
}
|
||||
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
@@ -149,8 +147,7 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
|
||||
setRootSettingsMethod.setAccessible(true);
|
||||
setRootSettingsMethod.invoke(commonSettings, getSoftMargins());
|
||||
}
|
||||
}
|
||||
catch (IllegalAccessException | InvocationTargetException e) {
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
@@ -195,11 +192,6 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
|
||||
|
||||
private final Language myLanguage = KotlinLanguage.INSTANCE;
|
||||
|
||||
@Nullable
|
||||
private CommonCodeStyleSettings getDefaultSettings() {
|
||||
return LanguageCodeStyleSettingsProvider.getDefaultCommonSettings(myLanguage);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Set<String> getSupportedFields() {
|
||||
final LanguageCodeStyleSettingsProvider provider = LanguageCodeStyleSettingsProvider.forLanguage(myLanguage);
|
||||
@@ -209,7 +201,7 @@ public class KotlinCommonCodeStyleSettings extends CommonCodeStyleSettings {
|
||||
private static class SupportedFieldsDiffFilter extends DifferenceFilter<CommonCodeStyleSettings> {
|
||||
private final Set<String> mySupportedFieldNames;
|
||||
|
||||
public SupportedFieldsDiffFilter(
|
||||
private SupportedFieldsDiffFilter(
|
||||
final CommonCodeStyleSettings object,
|
||||
Set<String> supportedFiledNames,
|
||||
final CommonCodeStyleSettings parentObject
|
||||
|
||||
+34
-2
@@ -6,11 +6,13 @@
|
||||
package org.jetbrains.kotlin.formatter
|
||||
|
||||
import com.intellij.application.options.CodeStyle
|
||||
import com.intellij.openapi.util.JDOMUtil
|
||||
import com.intellij.psi.codeStyle.CodeStyleScheme
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
|
||||
import com.intellij.psi.impl.source.codeStyle.json.CodeStyleSchemeJsonExporter
|
||||
import com.intellij.testFramework.LightPlatformTestCase
|
||||
import org.jdom.Element
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinPackageEntry
|
||||
import org.jetbrains.kotlin.idea.formatter.*
|
||||
import org.jetbrains.kotlin.idea.test.IDEA_TEST_DATA_DIR
|
||||
@@ -33,6 +35,35 @@ class KotlinCodeStyleSettingsTest : LightPlatformTestCase() {
|
||||
it.kotlinCommonSettings.BRACE_STYLE = 10
|
||||
}
|
||||
|
||||
fun `test official code style scheme`() = doTestWithScheme(KotlinStyleGuideCodeStyle.INSTANCE, "officialCodeStyleScheme.xml")
|
||||
fun `test obsolete code style scheme`() = doTestWithScheme(KotlinObsoleteCodeStyle.INSTANCE, "obsoleteCodeStyleScheme.xml")
|
||||
|
||||
private fun doTestWithScheme(codeStyle: KotlinPredefinedCodeStyle, fileName: String) {
|
||||
doWithTemporarySettings {
|
||||
ProjectCodeStyleImporter.apply(project, codeStyle)
|
||||
val optionElement = Element("code_scheme")
|
||||
optionElement.setAttribute("name", "Project")
|
||||
it.writeExternal(optionElement)
|
||||
KotlinTestUtils.assertEqualsToFile(getTestFile(fileName), JDOMUtil.writeElement(optionElement))
|
||||
}
|
||||
}
|
||||
|
||||
private fun doWithTemporarySettings(action: (CodeStyleSettings) -> Unit) {
|
||||
val settingsManager = CodeStyleSettingsManager.getInstance()
|
||||
val tempSettingsBefore = settingsManager.temporarySettings
|
||||
try {
|
||||
val tempSettings = settingsManager.createTemporarySettings()
|
||||
tempSettings.copyFrom(CodeStyle.getSettings(project))
|
||||
action(tempSettings)
|
||||
} finally {
|
||||
if (tempSettingsBefore != null) {
|
||||
settingsManager.setTemporarySettings(tempSettingsBefore)
|
||||
} else {
|
||||
settingsManager.dropTemporarySettings()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun compareCodeStyle(transformer: (CodeStyleSettings) -> Unit) {
|
||||
val settings = CodeStyle.getSettings(project)
|
||||
val copyOfSettings = CodeStyleSettingsManager.getInstance().cloneSettings(settings)
|
||||
@@ -55,8 +86,7 @@ class KotlinCodeStyleSettingsTest : LightPlatformTestCase() {
|
||||
}
|
||||
|
||||
private fun doTestWithJson(codeStyle: KotlinPredefinedCodeStyle, fileName: String) {
|
||||
val jsonScheme = File(IDEA_TEST_DATA_DIR, "codeStyle/$fileName.json")
|
||||
assert(jsonScheme.exists())
|
||||
val jsonScheme = getTestFile("$fileName.json")
|
||||
|
||||
val testScheme = createTestScheme()
|
||||
val settings = testScheme.codeStyleSettings
|
||||
@@ -73,6 +103,8 @@ private fun doTestWithJson(codeStyle: KotlinPredefinedCodeStyle, fileName: Strin
|
||||
KotlinTestUtils.assertEqualsToFile(jsonScheme, outputStream.toString())
|
||||
}
|
||||
|
||||
private fun getTestFile(fileName: String): File = File(IDEA_TEST_DATA_DIR, "codeStyle/$fileName").also { assert(it.exists()) }
|
||||
|
||||
private fun createTestScheme() = object : CodeStyleScheme {
|
||||
private val mySettings = CodeStyle.createTestSettings()
|
||||
override fun getName(): String = "Test"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OLD_DEFAULTS" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OLD_DEFAULTS" />
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
@@ -0,0 +1,8 @@
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
Reference in New Issue
Block a user