From 5e69d3f186500df2c6db32b6fcf8a400aee4323f Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Mon, 5 Aug 2013 17:02:22 +0400 Subject: [PATCH] CR-IC-1738 IDEA-88210 "Remember" the global code style selection for each project --- .../psi/codeStyle/CodeStyleSchemes.java | 22 +++++++++++++++++++ .../codeStyle/CodeStyleSettingsManager.java | 3 ++- .../codeStyle/CodeStyleSchemesModel.java | 20 ++++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java index a5fb8c08102b..9b0c60f485df 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java @@ -16,6 +16,7 @@ package com.intellij.psi.codeStyle; import com.intellij.openapi.components.ServiceManager; +import org.jetbrains.annotations.Nullable; /** * @author MYakovlev @@ -43,6 +44,27 @@ public abstract class CodeStyleSchemes { public abstract CodeStyleScheme findSchemeByName(String name); + /** + * Attempts to find a scheme with a given name or an alternative suitable scheme. + * + * @param preferredSchemeName The scheme name to find or null for the currently selected scheme. + * @return A found scheme or a default scheme if the scheme name was not found or, if neither exists or the scheme name is null, the + * currently selected scheme. + */ + public CodeStyleScheme findPreferredScheme(@Nullable String preferredSchemeName) { + CodeStyleScheme scheme = null; + if (preferredSchemeName != null) { + scheme = findSchemeByName(preferredSchemeName); + if (scheme == null) { + scheme = getDefaultScheme(); + } + } + if (scheme == null) { + scheme = getCurrentScheme(); + } + return scheme; + } + public abstract CodeStyleScheme getDefaultScheme(); public abstract void addScheme(CodeStyleScheme currentScheme); diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsManager.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsManager.java index de7d75358753..79466ce564a4 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsManager.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSettingsManager.java @@ -32,6 +32,7 @@ public class CodeStyleSettingsManager implements PersistentStateComponent configuredSchemesSet = new HashSet(getSchemes()); - Set savedSchemesSet = new HashSet(Arrays.asList(CodeStyleSchemes.getInstance().getSchemes())); + Set savedSchemesSet = new HashSet(Arrays.asList(schemes.getSchemes())); if (!configuredSchemesSet.equals(savedSchemesSet)) return true; return false; } - + public void apply() { - getProjectSettings().USE_PER_PROJECT_SETTINGS = myUsePerProjectSettings; - getProjectSettings().PER_PROJECT_SETTINGS = myProjectScheme.getCodeStyleSettings(); + CodeStyleSettingsManager projectSettingsManager = getProjectSettings(); + projectSettingsManager.USE_PER_PROJECT_SETTINGS = myUsePerProjectSettings; + projectSettingsManager.PREFERRED_PROJECT_CODE_STYLE = + myUsePerProjectSettings || myGlobalSelected == null ? null : myGlobalSelected.getName(); + projectSettingsManager.PER_PROJECT_SETTINGS = myProjectScheme.getCodeStyleSettings(); final CodeStyleScheme[] savedSchemes = CodeStyleSchemes.getInstance().getSchemes(); final Set savedSchemesSet = new HashSet(Arrays.asList(savedSchemes)); @@ -261,6 +268,7 @@ public class CodeStyleSchemesModel { return new CodeStyleSchemeImpl(name, false, parentScheme); } + @Nullable private CodeStyleScheme findSchemeByName(final String name) { for (CodeStyleScheme scheme : mySchemes) { if (name.equals(scheme.getName())) return scheme;