diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy index b4219ced7135..93b27b312e19 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -547,13 +547,13 @@ class Outer { assert template.templateContext.getOwnValue(stmtContext) assert !template.templateContext.getOwnValue(stmtContext.baseContextType) - template.templateContext.putValue(stmtContext, false) - template.templateContext.putValue(stmtContext.baseContextType, true) + template.templateContext.setEnabled(stmtContext, false) + template.templateContext.setEnabled(stmtContext.baseContextType, true) try { assert !(template in manager.findMatchingTemplates(myFixture.file, editor, Lookup.REPLACE_SELECT_CHAR, TemplateSettings.instance)?.keySet()) } finally { - template.templateContext.putValue(stmtContext, true) - template.templateContext.putValue(stmtContext.baseContextType, false) + template.templateContext.setEnabled(stmtContext, true) + template.templateContext.setEnabled(stmtContext.baseContextType, false) } } @@ -576,7 +576,7 @@ class Outer { copy.writeTemplateContext(write) assert write.children.size() == 2 : JDOMUtil.writeElement(write) - copy.putValue(TemplateContextType.EP_NAME.findExtension(JavaCommentContextType), false) + copy.setEnabled(TemplateContextType.EP_NAME.findExtension(JavaCommentContextType), false) write = new Element("context") copy.writeTemplateContext(write) @@ -589,13 +589,34 @@ class Outer { def defContext = new TemplateContext() def commentContext = TemplateContextType.EP_NAME.findExtension(JavaCommentContextType) - defContext.putValue(commentContext, true) + defContext.setEnabled(commentContext, true) context.setDefaultContext(defContext) assert context.isEnabled(commentContext) assert !context.isEnabled(TemplateContextType.EP_NAME.findExtension(JavaCodeContextType.Generic)) } + public void "test adding new context to Other"() { + def defElement = JDOMUtil.loadDocument('''\ + + ''').rootElement + def context = new TemplateContext() + context.readTemplateContext(defElement) + + def javaContext = TemplateContextType.EP_NAME.findExtension(JavaCodeContextType.Generic) + context.setEnabled(javaContext, true) + + def saved = new Element('context') + context.writeTemplateContext(saved) + + context = new TemplateContext() + context.readTemplateContext(saved) + + assert context.isEnabled(javaContext) + assert context.isEnabled(TemplateContextType.EP_NAME.findExtension(EverywhereContextType)) + } + private boolean isApplicable(String text, TemplateImpl inst) throws IOException { configureFromFileText("a.java", text); return TemplateManagerImpl.isApplicable(myFixture.getFile(), getEditor().getCaretModel().getOffset(), inst); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateSettingsEditor.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateSettingsEditor.java index f25675e54711..a39d2c57337e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateSettingsEditor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateSettingsEditor.java @@ -41,7 +41,6 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.ui.*; import com.intellij.ui.awt.RelativePoint; -import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.MultiMap; import com.intellij.util.ui.GridBag; @@ -150,7 +149,7 @@ public class LiveTemplateSettingsEditor extends JPanel { JPanel panel = new JPanel(new GridBagLayout()); GridBag gb = new GridBag().setDefaultInsets(4, 4, 4, 4).setDefaultWeightY(1).setDefaultFill(GridBagConstraints.BOTH); - + JPanel editorPanel = new JPanel(new BorderLayout(4, 4)); editorPanel.setPreferredSize(JBUI.size(250, 100)); editorPanel.setMinimumSize(editorPanel.getPreferredSize()); @@ -260,11 +259,10 @@ public class LiveTemplateSettingsEditor extends JPanel { else { myTemplate.setShortcutChar(TemplateSettings.SPACE_CHAR); } - } }); expandWithLabel.setLabelFor(myExpandByCombo); - + panel.add(myExpandByCombo, gbConstraints); gbConstraints.weightx = 1; gbConstraints.gridx = 2; @@ -287,7 +285,7 @@ public class LiveTemplateSettingsEditor extends JPanel { gbConstraints.weighty = 1; gbConstraints.gridy++; - panel.add(new JPanel(), gbConstraints); + panel.add(new JPanel(), gbConstraints); return panel; } @@ -295,7 +293,7 @@ public class LiveTemplateSettingsEditor extends JPanel { private List getApplicableContexts() { ArrayList result = new ArrayList(); for (TemplateContextType type : TemplateManagerImpl.getAllContextTypes()) { - if (myContext.isExplicitlyEnabled(type)) { + if (myContext.isEnabled(type)) { result.add(type); } } @@ -449,14 +447,14 @@ public class LiveTemplateSettingsEditor extends JPanel { parent.add(node); if (children.isEmpty()) { - node.setChecked(context.isExplicitlyEnabled(type)); + node.setChecked(context.isEnabled(type)); } else { for (TemplateContextType child : children) { addContextNode(hierarchy, node, child, context); } final CheckedTreeNode other = new CheckedTreeNode(Pair.create(type, "Other")); - other.setChecked(context.isExplicitlyEnabled(type)); + other.setChecked(context.isEnabled(type)); node.add(other); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateContext.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateContext.java index e829585bcebd..4b41a941d787 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateContext.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateContext.java @@ -18,7 +18,6 @@ package com.intellij.codeInsight.template.impl; import com.google.common.annotations.VisibleForTesting; -import com.intellij.codeInsight.template.EverywhereContextType; import com.intellij.codeInsight.template.TemplateContextType; import com.intellij.openapi.util.WriteExternalException; import com.intellij.util.containers.ContainerUtil; @@ -58,25 +57,12 @@ public class TemplateContext { Boolean storedValue = getOwnValue(contextType); if (storedValue == null) { TemplateContextType baseContextType = contextType.getBaseContextType(); - if (baseContextType != null && !(baseContextType instanceof EverywhereContextType)) { - return isEnabled(baseContextType); - } - return false; + return baseContextType != null && isEnabled(baseContextType); } return storedValue.booleanValue(); } } - public void putValue(TemplateContextType context, boolean enabled) { - synchronized (myContextStates) { - myContextStates.put(context.getContextId(), enabled); - } - } - - public boolean isExplicitlyEnabled(TemplateContextType contextType) { - return Boolean.TRUE.equals(getOwnValue(contextType)); - } - @Nullable public Boolean getOwnValue(TemplateContextType contextType) { synchronized (myContextStates) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateEditorUtil.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateEditorUtil.java index 1a0211418253..903e2928b5c1 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateEditorUtil.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateEditorUtil.java @@ -60,7 +60,7 @@ public class TemplateEditorUtil { private static Document createDocument(CharSequence text, @Nullable TemplateContext context, Project project) { if (context != null) { for (TemplateContextType type : TemplateManagerImpl.getAllContextTypes()) { - if (context.isExplicitlyEnabled(type)) { + if (context.isEnabled(type)) { return type.createDocument(text, project); } }