From 9b2df54e5b1e4595236b3c35c9b6eda456fb67ec Mon Sep 17 00:00:00 2001 From: "Rustam.Vishnyakov" Date: Thu, 7 Apr 2011 18:36:10 +0400 Subject: [PATCH] Predefined code style bundling feature + PHP Pear style, initial implementation --- .../LanguageCodeStyleSettingsProvider.java | 8 ++ .../psi/codeStyle/PredefinedCodeStyle.java | 66 +++++++++++++++++ .../options/CodeStyleAbstractPanel.java | 15 +++- .../options/codeStyle/CodeStyleMainPanel.java | 2 + .../codeStyle/CodeStyleSchemesPanel.form | 13 +++- .../codeStyle/CodeStyleSchemesPanel.java | 27 ++++++- .../codeStyle/NewCodeStyleSettingsPanel.java | 26 +++++++ .../codeStyle/PredefinedCodeStylesDialog.form | 23 ++++++ .../codeStyle/PredefinedCodeStylesDialog.java | 73 +++++++++++++++++++ 9 files changed, 246 insertions(+), 7 deletions(-) create mode 100644 platform/lang-api/src/com/intellij/psi/codeStyle/PredefinedCodeStyle.java create mode 100644 platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.form create mode 100644 platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.java diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java index 9d17d9faa69b..1c80340cd240 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/LanguageCodeStyleSettingsProvider.java @@ -92,6 +92,14 @@ public abstract class LanguageCodeStyleSettingsProvider { return null; } + /** + * @return Array of predefined code styles (empty array by default). + */ + @NotNull + public PredefinedCodeStyle[] getPredefinedCodeStyles() { + return PredefinedCodeStyle.EMPTY_ARRAY; + } + @NotNull public static Language[] getLanguagesWithCodeStyleSettings() { final ArrayList languages = new ArrayList(); diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/PredefinedCodeStyle.java b/platform/lang-api/src/com/intellij/psi/codeStyle/PredefinedCodeStyle.java new file mode 100644 index 000000000000..24c2a0e8137c --- /dev/null +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/PredefinedCodeStyle.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2011 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 com.intellij.psi.codeStyle; + + +import com.intellij.lang.Language; +import org.jetbrains.annotations.NotNull; + +/** + * @author Rustam Vishnyakov + */ +public abstract class PredefinedCodeStyle { + + public final static PredefinedCodeStyle[] EMPTY_ARRAY = new PredefinedCodeStyle[]{}; + private final String myName; + private final Language myLanguage; + + public PredefinedCodeStyle(@NotNull String name, @NotNull Language language) { + myName = name; + myLanguage = language; + } + + /** + * Applies the predefined code style to given settings. Code style settings which are not specified by + * the code style may be left unchanged (as defined by end-user). If the name doesn't match any predefined styles, + * the method does nothing. + * + * @param settings The common settings to change. + * @param indentOptions The indent options to change. + */ + public abstract void apply(CommonCodeStyleSettings settings, + CodeStyleSettings.IndentOptions indentOptions); + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof PredefinedCodeStyle)) return false; + PredefinedCodeStyle otherStyle = (PredefinedCodeStyle)obj; + return myName.equals(otherStyle.getName()); + } + + public String getName() { + return myName; + } + + @Override + public String toString() { + return myName; + } + + public Language getLanguage() { + return myLanguage; + } +} diff --git a/platform/lang-impl/src/com/intellij/application/options/CodeStyleAbstractPanel.java b/platform/lang-impl/src/com/intellij/application/options/CodeStyleAbstractPanel.java index 47b014ae8f78..baf07deb2037 100644 --- a/platform/lang-impl/src/com/intellij/application/options/CodeStyleAbstractPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/CodeStyleAbstractPanel.java @@ -32,6 +32,7 @@ import com.intellij.openapi.editor.ex.util.EditorUtil; import com.intellij.openapi.editor.highlighter.EditorHighlighter; import com.intellij.openapi.editor.markup.*; import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.fileTypes.FileTypesBundle; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.util.TextRange; @@ -39,9 +40,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; -import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.CodeStyleSettings; -import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.*; import com.intellij.ui.UserActivityListener; import com.intellij.ui.UserActivityWatcher; import com.intellij.util.Alarm; @@ -564,4 +563,14 @@ public abstract class CodeStyleAbstractPanel implements Disposable { public Set processListOptions() { return Collections.emptySet(); } + + public final void applyPredefinedSettings(@NotNull PredefinedCodeStyle codeStyle) { + CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getCurrentProject()).clone(); + CommonCodeStyleSettings commonSettings = settings.getCommonSettings(codeStyle.getLanguage()); + FileType fileType = codeStyle.getLanguage().getAssociatedFileType(); + CodeStyleSettings.IndentOptions indentOptions = settings.getIndentOptions(fileType); + codeStyle.apply(commonSettings, indentOptions); + reset(settings); + onSomethingChanged(); + } } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java index 49a87b4d122b..2ed3b19d7d5c 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleMainPanel.java @@ -141,6 +141,7 @@ public class CodeStyleMainPanel extends JPanel implements LanguageSelectorListen public boolean isModified() { final NewCodeStyleSettingsPanel[] panels = getPanels(); for (NewCodeStyleSettingsPanel panel : panels) { + if (!panel.isMultiLanguage()) mySchemesPanel.setPredefinedEnabled(false); if (panel.isModified()) return true; } return false; @@ -224,6 +225,7 @@ public class CodeStyleMainPanel extends JPanel implements LanguageSelectorListen for (NewCodeStyleSettingsPanel panel : mySettingsPanels.values()) { panel.setLanguage(lang); } + mySchemesPanel.setPredefinedEnabled(lang.getDisplayName().contains("PHP")); } public Set processListOptions() { diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form index d551294925fb..b2bc36b16419 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form @@ -13,7 +13,7 @@ - + @@ -99,7 +99,16 @@ - + + + + + + + + + + diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java index fafda0977b9b..22440fc86071 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java @@ -24,8 +24,7 @@ import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.options.SchemesManager; import com.intellij.openapi.ui.Messages; -import com.intellij.psi.codeStyle.CodeStyleScheme; -import com.intellij.psi.codeStyle.CodeStyleSchemes; +import com.intellij.psi.codeStyle.*; import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl; import javax.swing.*; @@ -50,6 +49,7 @@ public class CodeStyleSchemesPanel{ private JPanel myPanel; private JButton myExportAsGlobalButton; private JButton myCopyToProjectButton; + public JButton myPredefinedButton; private boolean myIsReset = false; private NewCodeStyleSettingsPanel mySettingsPanel; @@ -150,6 +150,14 @@ public class CodeStyleSchemesPanel{ onExportProjectScheme(); } }); + + myPredefinedButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + selectPredefinedStyle(); + } + }); } private void onExportProjectScheme() { @@ -308,4 +316,19 @@ public class CodeStyleSchemesPanel{ public void setCodeStyleSettingsPanel(NewCodeStyleSettingsPanel settingsPanel) { mySettingsPanel = settingsPanel; } + + public void setPredefinedEnabled(boolean isEnabled) { + myPredefinedButton.setEnabled(isEnabled); + } + + private void selectPredefinedStyle() { + Language selectedLanguage = mySettingsPanel.getSelectedLanguage(); + if (selectedLanguage == null) return; + PredefinedCodeStylesDialog dialog = new PredefinedCodeStylesDialog(myPredefinedButton, selectedLanguage); + dialog.show(); + if (dialog.isOK()) { + PredefinedCodeStyle codeStyle = dialog.getSelectedStyle(); + if (codeStyle != null) mySettingsPanel.applyPredefinedSettings(codeStyle); + } + } } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/NewCodeStyleSettingsPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/NewCodeStyleSettingsPanel.java index 96e8f1fbd775..4bd331e23ee8 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/NewCodeStyleSettingsPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/NewCodeStyleSettingsPanel.java @@ -22,6 +22,8 @@ import com.intellij.lang.Language; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; +import com.intellij.psi.codeStyle.PredefinedCodeStyle; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -36,6 +38,7 @@ public class NewCodeStyleSettingsPanel extends JPanel { private static final Logger LOG = Logger.getInstance("#com.intellij.application.options.codeStyle.NewCodeStyleSettingsPanel"); private final Configurable myTab; + private Language mySelectedLanguage; public NewCodeStyleSettingsPanel(Configurable tab) { super(new BorderLayout()); @@ -118,9 +121,25 @@ public class NewCodeStyleSettingsPanel extends JPanel { if (myTab instanceof CodeStyleAbstractConfigurable) { CodeStyleAbstractConfigurable configurable = (CodeStyleAbstractConfigurable)myTab; if (configurable.getPanel() instanceof MultilanguageCodeStyleAbstractPanel) { + mySelectedLanguage = language; ((MultilanguageCodeStyleAbstractPanel)configurable.getPanel()).setPanelLanguage(language); + return; } } + mySelectedLanguage = null; + } + + @Nullable + public Language getSelectedLanguage() { + return mySelectedLanguage; + } + + public boolean isMultiLanguage() { + if (myTab instanceof CodeStyleAbstractConfigurable) { + CodeStyleAbstractConfigurable configurable = (CodeStyleAbstractConfigurable)myTab; + return (configurable.getPanel() instanceof MultilanguageCodeStyleAbstractPanel); + } + return false; } public Set processListOptions() { @@ -129,4 +148,11 @@ public class NewCodeStyleSettingsPanel extends JPanel { } return Collections.emptySet(); } + + public void applyPredefinedSettings(@NotNull PredefinedCodeStyle codeStyle) { + if (myTab instanceof CodeStyleAbstractConfigurable) { + ((CodeStyleAbstractConfigurable)myTab).getPanel().applyPredefinedSettings(codeStyle); + ((CodeStyleAbstractConfigurable)myTab).getPanel().getPanel().repaint(); + } + } } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.form new file mode 100644 index 000000000000..a19bca4fdf4b --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.form @@ -0,0 +1,23 @@ + +
+ + + + + + + + + + + + + + + + + + + + +
diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.java new file mode 100644 index 000000000000..d834584a6072 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/PredefinedCodeStylesDialog.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2011 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 com.intellij.application.options.codeStyle; + +import com.intellij.lang.Language; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider; +import com.intellij.psi.codeStyle.PredefinedCodeStyle; +import com.intellij.ui.components.JBList; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; + +public class PredefinedCodeStylesDialog extends DialogWrapper { + + private JPanel contentPane; + private JBList myPredefinedCodeStyleList; + private LanguageCodeStyleSettingsProvider myProvider; + + protected PredefinedCodeStylesDialog(Component parent, Language language) { + super(parent, false); + myProvider = LanguageCodeStyleSettingsProvider.forLanguage(language); + MyListModel model = new MyListModel(); + myPredefinedCodeStyleList.setModel(model); + setTitle("Select Predefined Style"); //TODO Move to resource bundle + init(); + } + + @Nullable + public PredefinedCodeStyle getSelectedStyle() { + Object selection = myPredefinedCodeStyleList.getSelectedValue(); + return selection instanceof PredefinedCodeStyle ? (PredefinedCodeStyle)selection : null; + } + + + @Override + protected JComponent createCenterPanel() { + return contentPane; + } + + private class MyListModel extends AbstractListModel { + private PredefinedCodeStyle[] myCodeStyles; + + public MyListModel() { + myCodeStyles = myProvider.getPredefinedCodeStyles(); + } + + + @Override + public int getSize() { + return myCodeStyles.length; + } + + @Override + public Object getElementAt(int index) { + return myCodeStyles[index]; + } + } +}