CR-IC-1738 IDEA-88210 "Remember" the global code style selection for each project

This commit is contained in:
Rustam Vishnyakov
2013-08-05 17:07:46 +04:00
parent 6641047e06
commit 5e69d3f186
3 changed files with 38 additions and 7 deletions
@@ -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);
@@ -32,6 +32,7 @@ public class CodeStyleSettingsManager implements PersistentStateComponent<Elemen
public volatile CodeStyleSettings PER_PROJECT_SETTINGS = null;
public volatile boolean USE_PER_PROJECT_SETTINGS = false;
public volatile String PREFERRED_PROJECT_CODE_STYLE = null;
private volatile CodeStyleSettings myTemporarySettings;
private volatile boolean myIsLoaded = false;
@@ -72,7 +73,7 @@ public class CodeStyleSettingsManager implements PersistentStateComponent<Elemen
if (temporarySettings != null) return temporarySettings;
CodeStyleSettings projectSettings = PER_PROJECT_SETTINGS;
if (USE_PER_PROJECT_SETTINGS && projectSettings != null) return projectSettings;
return CodeStyleSchemes.getInstance().getCurrentScheme().getCodeStyleSettings();
return CodeStyleSchemes.getInstance().findPreferredScheme(PREFERRED_PROJECT_CODE_STYLE).getCodeStyleSettings();
}
private void readExternal(Element element) throws InvalidDataException {
@@ -104,7 +104,7 @@ public class CodeStyleSchemesModel {
mySettingsToClone.clear();
mySchemes.clear();
ContainerUtil.addAll(mySchemes, allSchemes);
myGlobalSelected = CodeStyleSchemes.getInstance().getCurrentScheme();
myGlobalSelected = CodeStyleSchemes.getInstance().findPreferredScheme(getProjectSettings().PREFERRED_PROJECT_CODE_STYLE);
CodeStyleSettings perProjectSettings = getProjectSettings().PER_PROJECT_SETTINGS;
if (perProjectSettings != null) {
@@ -150,17 +150,24 @@ public class CodeStyleSchemesModel {
}
public boolean isSchemeListModified() {
CodeStyleSchemes schemes = CodeStyleSchemes.getInstance();
if (getProjectSettings().USE_PER_PROJECT_SETTINGS != myUsePerProjectSettings) return true;
if (!myUsePerProjectSettings && (getSelectedScheme() != CodeStyleSchemes.getInstance().getCurrentScheme())) return true;
if (!myUsePerProjectSettings &&
(getSelectedScheme() != schemes.findPreferredScheme(getProjectSettings().PREFERRED_PROJECT_CODE_STYLE))) {
return true;
}
Set<CodeStyleScheme> configuredSchemesSet = new HashSet<CodeStyleScheme>(getSchemes());
Set<CodeStyleScheme> savedSchemesSet = new HashSet<CodeStyleScheme>(Arrays.asList(CodeStyleSchemes.getInstance().getSchemes()));
Set<CodeStyleScheme> savedSchemesSet = new HashSet<CodeStyleScheme>(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<CodeStyleScheme> savedSchemesSet = new HashSet<CodeStyleScheme>(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;