mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Per-language code style settings: saving/restoring
This commit is contained in:
@@ -48,6 +48,7 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
}
|
||||
|
||||
public CodeStyleSettings(boolean loadExtensions) {
|
||||
super(null);
|
||||
initTypeToName();
|
||||
initImportsByDefault();
|
||||
|
||||
@@ -627,6 +628,8 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
copyOldIndentOptions("java", JAVA_INDENT_OPTIONS);
|
||||
copyOldIndentOptions("jsp", JSP_INDENT_OPTIONS);
|
||||
copyOldIndentOptions("xml", XML_INDENT_OPTIONS);
|
||||
|
||||
myCommonSettingsManager.readExternal(element);
|
||||
}
|
||||
|
||||
private void copyOldIndentOptions(@NonNls final String extension, final IndentOptions options) {
|
||||
@@ -713,6 +716,8 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
additionalIndentOptions.setAttribute(FILETYPE,fileType.getDefaultExtension());
|
||||
element.addContent(additionalIndentOptions);
|
||||
}
|
||||
|
||||
myCommonSettingsManager.writeExternal(element);
|
||||
}
|
||||
|
||||
public IndentOptions getIndentOptions(FileType fileType) {
|
||||
|
||||
+54
-5
@@ -17,22 +17,27 @@ package com.intellij.psi.codeStyle;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.util.InvalidDataException;
|
||||
import com.intellij.openapi.util.JDOMExternalizable;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Manages common code style settings for every language using them.
|
||||
*
|
||||
* @author Rustam Vishnyakov
|
||||
*/
|
||||
public class CommonCodeStyleSettingsManager {
|
||||
public class CommonCodeStyleSettingsManager implements JDOMExternalizable {
|
||||
|
||||
private Map<Language, CommonCodeStyleSettings> myCommonSettingsMap;
|
||||
private final CodeStyleSettings myParentSettings;
|
||||
|
||||
private static final String COMMON_SETTINGS_TAG = "commonCodeStyleSettings";
|
||||
private static final String LANGUAGE_ATTR = "language";
|
||||
|
||||
public CommonCodeStyleSettingsManager(CodeStyleSettings parentSettings) {
|
||||
myParentSettings = parentSettings;
|
||||
}
|
||||
@@ -83,9 +88,53 @@ public class CommonCodeStyleSettingsManager {
|
||||
if (myCommonSettingsMap != null) {
|
||||
settingsManager.initCommonSettingsMap();
|
||||
for (Map.Entry<Language, CommonCodeStyleSettings> entry : myCommonSettingsMap.entrySet()) {
|
||||
settingsManager.registerCommonSettings(entry.getKey(), entry.getValue().clone(parentSettings));
|
||||
settingsManager.registerCommonSettings(entry.getKey(), entry.getValue().clone());
|
||||
}
|
||||
}
|
||||
return settingsManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(Element element) throws InvalidDataException {
|
||||
initCommonSettingsMap();
|
||||
final List list = element.getChildren(COMMON_SETTINGS_TAG);
|
||||
if (list != null) {
|
||||
for(Object o:list) {
|
||||
if (o instanceof Element) {
|
||||
final Element commonSettingsElement = (Element)o;
|
||||
final String languageId = commonSettingsElement.getAttributeValue(LANGUAGE_ATTR);
|
||||
if (languageId != null && languageId.length() > 0) {
|
||||
Language target = Language.findLanguageByID(languageId);
|
||||
if (target == null) {
|
||||
target = new Language(languageId) {};
|
||||
}
|
||||
final CommonCodeStyleSettings settings = new CommonCodeStyleSettings(target);
|
||||
settings.readExternal(commonSettingsElement);
|
||||
registerCommonSettings(target, settings);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(Element element) throws WriteExternalException {
|
||||
if (myCommonSettingsMap == null) return;
|
||||
final Language[] languages = myCommonSettingsMap.keySet().toArray(new Language[myCommonSettingsMap.keySet().size()]);
|
||||
Arrays.sort(languages, new Comparator<Language>() {
|
||||
public int compare(final Language o1, final Language o2) {
|
||||
return o1.getDisplayName().compareTo(o2.getDisplayName());
|
||||
}
|
||||
});
|
||||
|
||||
for (Language language : languages) {
|
||||
final CommonCodeStyleSettings commonSettings = myCommonSettingsMap.get(language);
|
||||
Element commonSettingsElement = new Element(COMMON_SETTINGS_TAG);
|
||||
commonSettings.writeExternal(commonSettingsElement);
|
||||
commonSettingsElement.setAttribute(LANGUAGE_ATTR, language.getID());
|
||||
if (!element.getContent().isEmpty()) {
|
||||
element.addContent(commonSettingsElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -83,4 +83,15 @@ public abstract class LanguageCodeStyleSettingsProvider {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CommonCodeStyleSettings getDefaultCommonSettings(Language lang) {
|
||||
for (LanguageCodeStyleSettingsProvider provider : Extensions.getExtensions(EP_NAME)) {
|
||||
if (provider.getLanguage().equals(lang)) {
|
||||
return provider.getDefaultCommonSettings();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user