Predefined code style bundling feature + PHP Pear style, initial implementation

This commit is contained in:
Rustam.Vishnyakov
2011-04-11 13:46:36 +04:00
parent befacf4c46
commit 9b2df54e5b
9 changed files with 246 additions and 7 deletions
@@ -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<Language> languages = new ArrayList<Language>();
@@ -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;
}
}
@@ -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<String> 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();
}
}
@@ -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<String> processListOptions() {
@@ -13,7 +13,7 @@
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="3fa8b" layout-manager="GridLayoutManager" row-count="2" column-count="9" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="3fa8b" layout-manager="GridLayoutManager" row-count="2" column-count="10" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="4" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -99,7 +99,16 @@
<text value="Copy to Project"/>
</properties>
</component>
<hspacer id="d9677">
<component id="e501" class="javax.swing.JButton" binding="myPredefinedButton">
<constraints>
<grid row="1" column="9" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<text value="Predefined..."/>
</properties>
</component>
<hspacer id="6f6d">
<constraints>
<grid row="1" column="8" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
@@ -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);
}
}
}
@@ -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<String> 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();
}
}
}
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.codeStyle.PredefinedCodeStylesDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="etched"/>
<children>
<component id="8997f" class="com.intellij.ui.components.JBList" binding="myPredefinedCodeStyleList">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="180" height="100"/>
</grid>
</constraints>
<properties>
<selectionMode value="0"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -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<rv> 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];
}
}
}