From 23de2fd41f8d2233ef8165938134c62415bbc45b Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Mon, 7 Jul 2014 17:12:56 +0400 Subject: [PATCH] WEB-12614 Emmet Filters --- .../src/messages/XmlBundle.properties | 2 +- .../options/emmet/EmmetOptions.java | 38 +++++++++++--- .../options/emmet/XmlEmmetConfigurable.form | 26 ++++++---- .../options/emmet/XmlEmmetConfigurable.java | 50 ++++++++++++++----- .../emmet/filters/BemEmmetFilter.java | 12 +++-- .../emmet/filters/CommentZenCodingFilter.java | 8 ++- .../emmet/filters/EscapeZenCodingFilter.java | 8 ++- .../emmet/filters/SingleLineEmmetFilter.java | 8 ++- .../emmet/filters/TrimZenCodingFilter.java | 22 +++++++- .../emmet/filters/XslZenCodingFilter.java | 10 +++- .../emmet/filters/ZenCodingFilter.java | 15 +++--- 11 files changed, 154 insertions(+), 45 deletions(-) diff --git a/platform/platform-resources-en/src/messages/XmlBundle.properties b/platform/platform-resources-en/src/messages/XmlBundle.properties index 1ef6ef385adb..70e26de6ffeb 100644 --- a/platform/platform-resources-en/src/messages/XmlBundle.properties +++ b/platform/platform-resources-en/src/messages/XmlBundle.properties @@ -217,7 +217,7 @@ select.xsd.schema.dialog.title=Select XSD Schema emmet.title=Emmet emmet.configuration.title=Emmet emmet.enable.label=&Enable XML Emmet -emmet.enable.bem.filter=Enable &BEM filter by default +emmet.filters.enabled.by.default=Filters enabled by default emmet.enable.preview=Enable &abbreviation preview emmet.expand.abbreviation.with=Expand &abbreviation with diff --git a/xml/impl/src/com/intellij/application/options/emmet/EmmetOptions.java b/xml/impl/src/com/intellij/application/options/emmet/EmmetOptions.java index 1cf7a7730173..6781db72e93a 100644 --- a/xml/impl/src/com/intellij/application/options/emmet/EmmetOptions.java +++ b/xml/impl/src/com/intellij/application/options/emmet/EmmetOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,6 +15,8 @@ */ package com.intellij.application.options.emmet; +import com.intellij.codeInsight.template.emmet.filters.BemEmmetFilter; +import com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter; import com.intellij.codeInsight.template.impl.TemplateSettings; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.components.*; @@ -26,11 +28,8 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.Map; +import java.util.Set; -/** - * User: zolotov - * Date: 2/20/13 - */ @State( name = "EmmetOptions", storages = { @@ -39,24 +38,46 @@ import java.util.Map; )} ) public class EmmetOptions implements PersistentStateComponent, ExportableComponent { + /** + * @deprecated delete after IDEA 14 + */ private boolean myBemFilterEnabledByDefault = false; private boolean myEmmetEnabled = true; private int myEmmetExpandShortcut = TemplateSettings.TAB_CHAR; private boolean myFuzzySearchEnabled = true; private boolean myAutoInsertCssPrefixedEnabled = true; private boolean myPreviewEnabled = false; + private Set myFiltersEnabledByDefault = ContainerUtil.newHashSet(); @NotNull private Map prefixes = ContainerUtil.newHashMap(); - + /** + * @deprecated delete after IDEA 14 + */ public boolean isBemFilterEnabledByDefault() { return myBemFilterEnabledByDefault; } + /** + * @deprecated delete after IDEA 14 + */ public void setBemFilterEnabledByDefault(boolean enableBemFilterByDefault) { myBemFilterEnabledByDefault = enableBemFilterByDefault; } + @NotNull + public Set getFiltersEnabledByDefault() { + return myFiltersEnabledByDefault; + } + + public void setFiltersEnabledByDefault(@NotNull Set filtersEnabledByDefault) { + myFiltersEnabledByDefault = filtersEnabledByDefault; + } + + public boolean isFilterEnabledByDefault(@NotNull ZenCodingFilter filter) { + return myFiltersEnabledByDefault.contains(filter.getSuffix()); + } + public void setEmmetExpandShortcut(int emmetExpandShortcut) { myEmmetExpandShortcut = emmetExpandShortcut; } @@ -126,6 +147,11 @@ public class EmmetOptions implements PersistentStateComponent, Exp @Override public void loadState(final EmmetOptions state) { XmlSerializerUtil.copyBean(state, this); + + // todo delete after IDEA 14 + if (myFiltersEnabledByDefault.isEmpty() && myBemFilterEnabledByDefault) { + myFiltersEnabledByDefault.add(BemEmmetFilter.SUFFIX); + } } public static EmmetOptions getInstance() { diff --git a/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.form b/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.form index 0c8dec3afcac..ec4cafb26fd0 100644 --- a/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.form +++ b/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.form @@ -19,14 +19,6 @@ - - - - - - - - @@ -37,12 +29,28 @@ - + + + + + + + + + + + + + + + + + diff --git a/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.java b/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.java index babbfc1d3046..764d74f94375 100644 --- a/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.java +++ b/xml/impl/src/com/intellij/application/options/emmet/XmlEmmetConfigurable.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,34 +15,46 @@ */ package com.intellij.application.options.emmet; +import com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter; import com.intellij.openapi.Disposable; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.UnnamedConfigurable; +import com.intellij.ui.CheckBoxList; +import com.intellij.ui.IdeBorderFactory; import com.intellij.ui.components.JBCheckBox; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.xml.XmlBundle; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.Set; -/** - * User: zolotov - * Date: 2/20/13 - */ public class XmlEmmetConfigurable implements UnnamedConfigurable, Disposable, Configurable.NoScroll { - private JBCheckBox myEnableBEMFilterJBCheckBox; private JPanel myPanel; private JBCheckBox myEnableEmmetJBCheckBox; private JBCheckBox myEnablePreviewJBCheckBox; + private CheckBoxList myFiltersCheckBoxList; + private JPanel myFiltersListPanel; public XmlEmmetConfigurable() { myEnableEmmetJBCheckBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean selected = myEnableEmmetJBCheckBox.isSelected(); - myEnableBEMFilterJBCheckBox.setEnabled(selected); myEnablePreviewJBCheckBox.setEnabled(selected); + myFiltersCheckBoxList.setEnabled(selected); + } + }); + myFiltersListPanel.setBorder(IdeBorderFactory.createTitledBorder(XmlBundle.message("emmet.filters.enabled.by.default"), false)); + myFiltersCheckBoxList.setItems(ZenCodingFilter.getInstances(), new Function() { + @Override + public String fun(ZenCodingFilter filter) { + return filter.getDisplayName(); } }); } @@ -62,29 +74,43 @@ public class XmlEmmetConfigurable implements UnnamedConfigurable, Disposable, Co EmmetOptions emmetOptions = EmmetOptions.getInstance(); return emmetOptions.isEmmetEnabled() != myEnableEmmetJBCheckBox.isSelected() || emmetOptions.isPreviewEnabled() != myEnablePreviewJBCheckBox.isSelected() || - emmetOptions.isBemFilterEnabledByDefault() != myEnableBEMFilterJBCheckBox.isSelected(); + !emmetOptions.getFiltersEnabledByDefault().equals(enabledFilters()); } - @Override public void apply() throws ConfigurationException { EmmetOptions emmetOptions = EmmetOptions.getInstance(); emmetOptions.setEmmetEnabled(myEnableEmmetJBCheckBox.isSelected()); - emmetOptions.setBemFilterEnabledByDefault(myEnableBEMFilterJBCheckBox.isSelected()); emmetOptions.setPreviewEnabled(myEnablePreviewJBCheckBox.isSelected()); + emmetOptions.setFiltersEnabledByDefault(enabledFilters()); } + @Override public void reset() { EmmetOptions emmetOptions = EmmetOptions.getInstance(); myEnableEmmetJBCheckBox.setSelected(emmetOptions.isEmmetEnabled()); - myEnableBEMFilterJBCheckBox.setEnabled(emmetOptions.isEmmetEnabled()); - myEnableBEMFilterJBCheckBox.setSelected(emmetOptions.isBemFilterEnabledByDefault()); myEnablePreviewJBCheckBox.setEnabled(emmetOptions.isEmmetEnabled()); myEnablePreviewJBCheckBox.setSelected(emmetOptions.isPreviewEnabled()); + + Set enabledByDefault = emmetOptions.getFiltersEnabledByDefault(); + for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) { + myFiltersCheckBoxList.setItemSelected(filter, enabledByDefault.contains(filter.getSuffix())); + } } @Override public void disposeUIResources() { } + + @NotNull + private Set enabledFilters() { + Set result = ContainerUtil.newHashSet(); + for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) { + if (myFiltersCheckBoxList.isItemSelected(filter)) { + result.add(filter.getSuffix()); + } + } + return result; + } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/BemEmmetFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/BemEmmetFilter.java index 64d097d54777..e9de53d23d2e 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/BemEmmetFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/BemEmmetFilter.java @@ -20,7 +20,6 @@ import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; -import com.intellij.application.options.emmet.EmmetOptions; import com.intellij.codeInsight.template.emmet.nodes.GenerationNode; import com.intellij.lang.xml.XMLLanguage; import com.intellij.openapi.util.Couple; @@ -51,6 +50,8 @@ import static com.google.common.collect.Lists.newLinkedList; * And documentation here: http://docs.emmet.io/filters/bem/ */ public class BemEmmetFilter extends ZenCodingFilter { + public static final String SUFFIX = "bem"; + private static final Key BEM_STATE = Key.create("BEM_STATE"); private static final String ELEMENT_SEPARATOR = "__"; @@ -95,13 +96,14 @@ public class BemEmmetFilter extends ZenCodingFilter { @NotNull @Override - public String getSuffix() { - return "bem"; + public String getDisplayName() { + return "BEM"; } + @NotNull @Override - public boolean isAppliedByDefault(@NotNull PsiElement context) { - return EmmetOptions.getInstance().isBemFilterEnabledByDefault(); + public String getSuffix() { + return SUFFIX; } @Override diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/CommentZenCodingFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/CommentZenCodingFilter.java index 82b6235a53c9..d2a7662dcbcb 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/CommentZenCodingFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/CommentZenCodingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -68,4 +68,10 @@ public class CommentZenCodingFilter extends ZenCodingFilter { public boolean isMyContext(@NotNull PsiElement context) { return context.getLanguage() instanceof XMLLanguage; } + + @NotNull + @Override + public String getDisplayName() { + return "Comment tags"; + } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/EscapeZenCodingFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/EscapeZenCodingFilter.java index 3249b5a156e4..d07654699e34 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/EscapeZenCodingFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/EscapeZenCodingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -43,4 +43,10 @@ public class EscapeZenCodingFilter extends ZenCodingFilter { public boolean isMyContext(@NotNull PsiElement context) { return context.getLanguage() instanceof XMLLanguage; } + + @NotNull + @Override + public String getDisplayName() { + return "Escape"; + } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/SingleLineEmmetFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/SingleLineEmmetFilter.java index acd047a26d1f..443db5edd88a 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/SingleLineEmmetFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/SingleLineEmmetFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -57,4 +57,10 @@ public class SingleLineEmmetFilter extends ZenCodingFilter { public boolean isMyContext(@NotNull PsiElement context) { return context.getLanguage() instanceof XMLLanguage; } + + @NotNull + @Override + public String getDisplayName() { + return "Single line"; + } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/TrimZenCodingFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/TrimZenCodingFilter.java index 1c6369244074..ceb0d1dd61b9 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/TrimZenCodingFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/TrimZenCodingFilter.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2014 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.codeInsight.template.emmet.filters; import com.intellij.codeInsight.template.emmet.nodes.GenerationNode; @@ -29,6 +44,12 @@ public class TrimZenCodingFilter extends ZenCodingFilter { return context.getLanguage() instanceof XMLLanguage; } + @NotNull + @Override + public String getDisplayName() { + return "Trim line markers"; + } + @NotNull @Override public String filterText(@NotNull String text, @NotNull TemplateToken token) { @@ -65,7 +86,6 @@ public class TrimZenCodingFilter extends ZenCodingFilter { final String surroundedText = node.getSurroundedText(); if (surroundedText != null) { node.setSurroundedText(PATTERN.matcher(surroundedText).replaceAll("")); - } else if(node.getTemplateToken() == TemplateToken.EMPTY_TEMPLATE_TOKEN) { } for (GenerationNode child : node.getChildren()) { doFilter(child); diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/XslZenCodingFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/XslZenCodingFilter.java index 685497f7bcd9..e97b72ed662b 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/XslZenCodingFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/XslZenCodingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -90,6 +90,12 @@ public class XslZenCodingFilter extends ZenCodingFilter { @Override public boolean isAppliedByDefault(@NotNull PsiElement context) { - return XslTextContextType.isXslOrXsltFile(context.getContainingFile()); + return XslTextContextType.isXslOrXsltFile(context.getContainingFile()) || super.isAppliedByDefault(context); + } + + @NotNull + @Override + public String getDisplayName() { + return "XSL tuning"; } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/ZenCodingFilter.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/ZenCodingFilter.java index f5057f3e6d9c..1e4ffa713009 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/ZenCodingFilter.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/filters/ZenCodingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,6 +15,7 @@ */ package com.intellij.codeInsight.template.emmet.filters; +import com.intellij.application.options.emmet.EmmetOptions; import com.intellij.codeInsight.template.emmet.nodes.GenerationNode; import com.intellij.codeInsight.template.emmet.tokens.TemplateToken; import com.intellij.openapi.extensions.ExtensionPointName; @@ -29,10 +30,9 @@ import java.util.List; * @author Eugene.Kudelevsky */ public abstract class ZenCodingFilter { - public static final ExtensionPointName EP_NAME = - new ExtensionPointName("com.intellij.xml.zenCodingFilter"); + public static final ExtensionPointName EP_NAME = new ExtensionPointName("com.intellij.xml.zenCodingFilter"); - private static final ZenCodingFilter[] ourStandartFilters = new ZenCodingFilter[]{ + private static final ZenCodingFilter[] OUR_STANDARD_FILTERS = new ZenCodingFilter[]{ new XslZenCodingFilter(), new CommentZenCodingFilter(), new EscapeZenCodingFilter(), @@ -57,12 +57,15 @@ public abstract class ZenCodingFilter { public abstract boolean isMyContext(@NotNull PsiElement context); public boolean isAppliedByDefault(@NotNull PsiElement context) { - return false; + return EmmetOptions.getInstance().isFilterEnabledByDefault(this); } + + @NotNull + public abstract String getDisplayName(); public static List getInstances() { List generators = new ArrayList(); - Collections.addAll(generators, ourStandartFilters); + Collections.addAll(generators, OUR_STANDARD_FILTERS); Collections.addAll(generators, EP_NAME.getExtensions()); return generators; }