code style options customizers

This commit is contained in:
Dmitry Jemerov
2010-05-20 16:56:08 +04:00
parent 6966639dc2
commit dfaa3a1790
7 changed files with 193 additions and 12 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2010 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;
import com.intellij.psi.codeStyle.SpacingOptionsConsumer;
import com.intellij.psi.codeStyle.CodeStyleOptionsCustomizer;
/**
* @author yole
*/
public class JavaCodeStyleOptionsCustomizer implements CodeStyleOptionsCustomizer {
public void customizeSpacingOptions(SpacingOptionsConsumer consumer) {
consumer.showAllStandardOptions();
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2010 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.openapi.extensions.ExtensionPointName;
/**
* @author yole
*/
public interface CodeStyleOptionsCustomizer {
ExtensionPointName<CodeStyleOptionsCustomizer> EP_NAME = ExtensionPointName.create("com.intellij.codeStyleOptionsCustomizer");
void customizeSpacingOptions(SpacingOptionsConsumer consumer);
}
@@ -0,0 +1,35 @@
/*
* Copyright 2000-2010 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.openapi.application.ApplicationBundle;
/**
* @author yole
*/
public interface SpacingOptionsConsumer {
void showAllStandardOptions();
void showStandardOptions(String... optionNames);
void showCustomOption(Class<? extends CustomCodeStyleSettings> settingsClass, String fieldName, String optionName, String groupName);
String AROUND_OPERATORS = ApplicationBundle.message("group.spaces.around.operators");
String BEFORE_PARENTHESES = ApplicationBundle.message("group.spaces.before.parentheses");
String BEFORE_LEFT_BRACE = ApplicationBundle.message("group.spaces.before.left.brace");
String WITHIN_PARENTHESES = ApplicationBundle.message("group.spaces.within.parentheses");
String TERNARY_OPERATOR = ApplicationBundle.message("group.spaces.in.ternary.operator");
String TYPE_ARGUMENTS = ApplicationBundle.message("group.spaces.in.type.arguments");
String OTHER = ApplicationBundle.message("group.spaces.other");
}
@@ -16,13 +16,25 @@
package com.intellij.application.options.codeStyle;
import com.intellij.openapi.application.ApplicationBundle;
//import com.intellij.openapi.editor.Editor;
//import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.Trinity;
import com.intellij.psi.codeStyle.CodeStyleOptionsCustomizer;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
import com.intellij.psi.codeStyle.SpacingOptionsConsumer;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NonNls;
import javax.swing.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel implements SpacingOptionsConsumer {
private boolean myShowAllStandardOptions = false;
private Set<String> myAllowedOptions;
private MultiMap<String, Trinity<Class<? extends CustomCodeStyleSettings>, String, String>> myCustomOptions;
public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
public CodeStyleSpacesPanel(CodeStyleSettings settings) {
super(settings);
}
@@ -32,15 +44,13 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
return LanguageCodeStyleSettingsProvider.SettingsType.SPACING_SETTINGS;
}
private static final String AROUND_OPERATORS = ApplicationBundle.message("group.spaces.around.operators");
private static final String BEFORE_PARENTHESES = ApplicationBundle.message("group.spaces.before.parentheses");
private static final String BEFORE_LEFT_BRACE = ApplicationBundle.message("group.spaces.before.left.brace");
private static final String WITHIN_PARENTHESES = ApplicationBundle.message("group.spaces.within.parentheses");
private static final String TERNARY_OPERATOR = ApplicationBundle.message("group.spaces.in.ternary.operator");
private static final String TYPE_ARGUMENTS = ApplicationBundle.message("group.spaces.in.type.arguments");
private static final String OTHER = ApplicationBundle.message("group.spaces.other");
protected void initTables() {
myAllowedOptions = new HashSet<String>();
myCustomOptions = new MultiMap<String, Trinity<Class<? extends CustomCodeStyleSettings>, String, String>>();
for(CodeStyleOptionsCustomizer contributor: Extensions.getExtensions(CodeStyleOptionsCustomizer.EP_NAME)) {
contributor.customizeSpacingOptions(this);
}
initBooleanField("SPACE_BEFORE_METHOD_CALL_PARENTHESES", ApplicationBundle.message("checkbox.spaces.method.call.parentheses"), BEFORE_PARENTHESES);
initBooleanField("SPACE_BEFORE_METHOD_PARENTHESES", ApplicationBundle.message("checkbox.spaces.method.declaration.parentheses"), BEFORE_PARENTHESES);
initBooleanField("SPACE_BEFORE_IF_PARENTHESES", ApplicationBundle.message("checkbox.spaces.if.parentheses"), BEFORE_PARENTHESES);
@@ -50,6 +60,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_BEFORE_SWITCH_PARENTHESES", ApplicationBundle.message("checkbox.spaces.switch.parentheses"), BEFORE_PARENTHESES);
initBooleanField("SPACE_BEFORE_SYNCHRONIZED_PARENTHESES", ApplicationBundle.message("checkbox.spaces.synchronized.parentheses"), BEFORE_PARENTHESES);
initBooleanField("SPACE_BEFORE_ANOTATION_PARAMETER_LIST", ApplicationBundle.message("checkbox.spaces.annotation.parameters"), BEFORE_PARENTHESES);
initCustomOptions(BEFORE_PARENTHESES);
initBooleanField("SPACE_AROUND_ASSIGNMENT_OPERATORS", ApplicationBundle.message("checkbox.spaces.assignment.operators"), AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_LOGICAL_OPERATORS", ApplicationBundle.message("checkbox.spaces.logical.operators"), AROUND_OPERATORS);
@@ -59,6 +70,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_AROUND_ADDITIVE_OPERATORS", ApplicationBundle.message("checkbox.spaces.additive.operators"), AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_MULTIPLICATIVE_OPERATORS", ApplicationBundle.message("checkbox.spaces.multiplicative.operators"), AROUND_OPERATORS);
initBooleanField("SPACE_AROUND_SHIFT_OPERATORS", ApplicationBundle.message("checkbox.spaces.shift.operators"), AROUND_OPERATORS);
initCustomOptions(AROUND_OPERATORS);
initBooleanField("SPACE_BEFORE_CLASS_LBRACE", ApplicationBundle.message("checkbox.spaces.class.left.brace"), BEFORE_LEFT_BRACE);
initBooleanField("SPACE_BEFORE_METHOD_LBRACE", ApplicationBundle.message("checkbox.spaces.method.left.brace"), BEFORE_LEFT_BRACE);
@@ -73,6 +85,7 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_BEFORE_FINALLY_LBRACE", ApplicationBundle.message("checkbox.spaces.finally.left.brace"), BEFORE_LEFT_BRACE);
initBooleanField("SPACE_BEFORE_SYNCHRONIZED_LBRACE", ApplicationBundle.message("checkbox.spaces.synchronized.left.brace"), BEFORE_LEFT_BRACE);
initBooleanField("SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE", ApplicationBundle.message("checkbox.spaces.array.initializer.left.brace"), BEFORE_LEFT_BRACE);
initCustomOptions(BEFORE_LEFT_BRACE);
initBooleanField("SPACE_WITHIN_PARENTHESES", ApplicationBundle.message("checkbox.spaces.parentheses"), WITHIN_PARENTHESES);
initBooleanField("SPACE_WITHIN_METHOD_CALL_PARENTHESES", ApplicationBundle.message("checkbox.spaces.checkbox.spaces.method.call.parentheses"), WITHIN_PARENTHESES);
@@ -85,13 +98,16 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_WITHIN_SYNCHRONIZED_PARENTHESES", ApplicationBundle.message("checkbox.spaces.synchronized.parentheses"), WITHIN_PARENTHESES);
initBooleanField("SPACE_WITHIN_CAST_PARENTHESES", ApplicationBundle.message("checkbox.spaces.type.cast.parentheses"), WITHIN_PARENTHESES);
initBooleanField("SPACE_WITHIN_ANNOTATION_PARENTHESES", ApplicationBundle.message("checkbox.spaces.annotation.parentheses"), WITHIN_PARENTHESES);
initCustomOptions(WITHIN_PARENTHESES);
initBooleanField("SPACE_BEFORE_QUEST", ApplicationBundle.message("checkbox.spaces.before.question"), TERNARY_OPERATOR);
initBooleanField("SPACE_AFTER_QUEST", ApplicationBundle.message("checkbox.spaces.after.question"), TERNARY_OPERATOR);
initBooleanField("SPACE_BEFORE_COLON", ApplicationBundle.message("checkbox.spaces.before.colon"), TERNARY_OPERATOR);
initBooleanField("SPACE_AFTER_COLON", ApplicationBundle.message("checkbox.spaces.after.colon"), TERNARY_OPERATOR);
initCustomOptions(TERNARY_OPERATOR);
initBooleanField("SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS", ApplicationBundle.message("checkbox.spaces.after.comma"), TYPE_ARGUMENTS);
initCustomOptions(TYPE_ARGUMENTS);
//TODO looks like this option is never implemented: initBooleanField("SPACE_AFTER_LABEL", ApplicationBundle.message("checkbox.spaces.after.colon.in.label.declaration"), OTHER);
initBooleanField("SPACE_WITHIN_BRACKETS", ApplicationBundle.message("checkbox.spaces.within.brackets"), OTHER);
@@ -102,6 +118,13 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
initBooleanField("SPACE_BEFORE_SEMICOLON", ApplicationBundle.message("checkbox.spaces.before.semicolon"), OTHER);
initBooleanField("SPACE_AFTER_TYPE_CAST", ApplicationBundle.message("checkbox.spaces.after.type.cast"), OTHER);
initBooleanField("SPACE_AFTER_UNARY_OPERATOR", ApplicationBundle.message("checkbox.spaces.after.unary.operator"), OTHER);
initCustomOptions(OTHER);
}
private void initCustomOptions(String groupName) {
for(Trinity<Class<? extends CustomCodeStyleSettings>, String, String> option: myCustomOptions.get(groupName)) {
initCustomBooleanField(option.first, option.second, option.third, groupName);
}
}
/*
@@ -117,7 +140,30 @@ public class CodeStyleSpacesPanel extends OptionTreeWithPreviewPanel {
}
*/
@Override
protected void initBooleanField(@NonNls String fieldName, String cbName, String groupName) {
if (myShowAllStandardOptions || myAllowedOptions.contains(fieldName)) {
super.initBooleanField(fieldName, cbName, groupName);
}
}
public JComponent getPanel() {
return getInternalPanel();
}
public void showAllStandardOptions() {
myShowAllStandardOptions = true;
}
public void showStandardOptions(String... optionNames) {
Collections.addAll(myAllowedOptions, optionNames);
}
public void showCustomOption(Class<? extends CustomCodeStyleSettings> settingsClass,
String fieldName,
String optionName,
String groupName) {
myCustomOptions.putValue(groupName,
Trinity.<Class<? extends CustomCodeStyleSettings>, String, String>create(settingsClass, fieldName, optionName));
}
}
@@ -18,10 +18,12 @@ package com.intellij.application.options.codeStyle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.tree.*;
@@ -198,7 +200,7 @@ public abstract class OptionTreeWithPreviewPanel extends MultilanguageCodeStyleA
}
private boolean isModified(TreeNode node, final CodeStyleSettings settings) {
private static boolean isModified(TreeNode node, final CodeStyleSettings settings) {
if (node instanceof MyToggleTreeNode) {
if (isToggleNodeModified((MyToggleTreeNode)node, settings)) {
return true;
@@ -242,6 +244,19 @@ public abstract class OptionTreeWithPreviewPanel extends MultilanguageCodeStyleA
}
}
protected <T extends CustomCodeStyleSettings> void initCustomBooleanField(@NotNull Class<T> customClass, String fieldName, String cbName, String groupName) {
try {
Field field = customClass.getField(fieldName);
myKeys.add(new CustomBooleanOptionKey(groupName, cbName, customClass, field));
}
catch (NoSuchFieldException e) {
LOG.error(e);
}
catch (SecurityException e) {
LOG.error(e);
}
}
protected void prepareForReformat(final PsiFile psiFile) {
//psiFile.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, LanguageLevel.HIGHEST);
}
@@ -379,6 +394,32 @@ public abstract class OptionTreeWithPreviewPanel extends MultilanguageCodeStyleA
}
}
private static class CustomBooleanOptionKey<T extends CustomCodeStyleSettings> extends BooleanOptionKey {
private final Class<T> mySettingsClass;
public CustomBooleanOptionKey(String groupName, String cbName, Class<T> settingsClass, Field field) {
super(groupName, cbName, field);
mySettingsClass = settingsClass;
}
@Override
public void setValue(CodeStyleSettings settings, Boolean aBoolean) {
final CustomCodeStyleSettings customSettings = settings.getCustomSettings(mySettingsClass);
try {
field.set(customSettings, aBoolean);
}
catch (IllegalAccessException e) {
LOG.error(e);
}
}
@Override
public boolean getValue(CodeStyleSettings settings) throws IllegalAccessException {
final CustomCodeStyleSettings customSettings = settings.getCustomSettings(mySettingsClass);
return field.getBoolean(customSettings);
}
}
private static class MyToggleTreeNode extends DefaultMutableTreeNode {
private final Object myKey;
private final String myText;
@@ -59,6 +59,8 @@
<extensionPoint name="codeStyleSettingsProvider"
interface="com.intellij.psi.codeStyle.CodeStyleSettingsProvider"/>
<extensionPoint name="codeStyleOptionsCustomizer"
interface="com.intellij.psi.codeStyle.CodeStyleOptionsCustomizer"/>
<extensionPoint name="daemon.highlightInfoFilter"
interface="com.intellij.codeInsight.daemon.impl.HighlightInfoFilter"/>
+2
View File
@@ -720,6 +720,8 @@
<langCodeStyleSettingsProvider implementation="com.intellij.ide.JavaLanguageCodeStyleSettingsProvider"/>
<codeStyleOptionsCustomizer implementation="com.intellij.application.options.JavaCodeStyleOptionsCustomizer"/>
<!-- new project wizard -->
<wizardMode implementation="com.intellij.ide.util.newProjectWizard.modes.CreateFromScratchMode"/>
<wizardMode implementation="com.intellij.ide.util.newProjectWizard.modes.CreateFromSourcesMode"/>