IDEA-155623 Loading settings from settings repository fails to import live template
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Tue, 15 Nov 2016 15:07:13 +0000 (16:07 +0100)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Tue, 15 Nov 2016 15:08:14 +0000 (16:08 +0100)
platform/configuration-store-impl/testSrc/TemplateSchemeTest.kt [new file with mode: 0644]
platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateContext.java
platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateSettings.java

diff --git a/platform/configuration-store-impl/testSrc/TemplateSchemeTest.kt b/platform/configuration-store-impl/testSrc/TemplateSchemeTest.kt
new file mode 100644 (file)
index 0000000..6bc0c35
--- /dev/null
@@ -0,0 +1,43 @@
+package com.intellij.configurationStore
+
+import com.intellij.codeInsight.template.impl.TemplateSettings
+import com.intellij.testFramework.InMemoryFsRule
+import com.intellij.testFramework.ProjectRule
+import com.intellij.util.io.readText
+import com.intellij.util.io.write
+import org.assertj.core.api.Assertions.assertThat
+import org.junit.ClassRule
+import org.junit.Rule
+import org.junit.Test
+
+class TemplateSchemeTest {
+  companion object {
+    @JvmField
+    @ClassRule
+    val projectRule = ProjectRule()
+  }
+
+  @JvmField
+  @Rule
+  val fsRule = InMemoryFsRule()
+
+  // https://youtrack.jetbrains.com/issue/IDEA-155623#comment=27-1721029
+  @Test fun `do not remove unknown context`() {
+    val schemeFile = fsRule.fs.getPath("templates/Groovy.xml")
+    val schemeManagerFactory = SchemeManagerFactoryBase.TestSchemeManagerFactory(fsRule.fs.getPath(""))
+    val schemeData = """
+    <templateSet group="Groovy">
+      <template name="serr" value="System.err.println(&quot;$\END$&quot;)dwed" description="Prints a string to System.errwefwe" toReformat="true" toShortenFQNames="true" deactivated="true">
+        <context>
+          <option name="GROOVY_STATEMENT" value="true" />
+        </context>
+      </template>
+    </templateSet>""".trimIndent()
+
+    schemeFile.write(schemeData)
+
+    TemplateSettings(schemeManagerFactory)
+    schemeManagerFactory.save()
+    assertThat(schemeFile.readText()).isEqualTo(schemeData)
+  }
+}
\ No newline at end of file
index 96e49d3bc9268a9f16fb2a5a0aec920926a29336..a6c13fa2fef8591e73d416ccf22e7ab242a39e35 100644 (file)
@@ -17,9 +17,10 @@ package com.intellij.codeInsight.template.impl;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.intellij.codeInsight.template.TemplateContextType;
-import com.intellij.openapi.util.WriteExternalException;
+import com.intellij.util.JdomKt;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.containers.JBIterable;
+import gnu.trove.THashMap;
 import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -122,17 +123,32 @@ public class TemplateContext {
   }
 
   @VisibleForTesting
-  public void writeTemplateContext(Element element) throws WriteExternalException {
+  public void writeTemplateContext(@NotNull Element element) {
+    if (myContextStates.isEmpty()) {
+      return;
+    }
+
+    Map<String, TemplateContextType> idToType = new THashMap<>();
     for (TemplateContextType type : TemplateManagerImpl.getAllContextTypes()) {
-      Boolean ownValue = getOwnValue(type);
-      if (ownValue != null) {
+      idToType.put(type.getContextId(), type);
+    }
+
+    for (Map.Entry<String, Boolean> entry : myContextStates.entrySet()) {
+      Boolean ownValue = entry.getValue();
+      if (ownValue == null) {
+        continue;
+      }
+
+      TemplateContextType type = idToType.get(entry.getKey());
+      if (type == null) {
+        // https://youtrack.jetbrains.com/issue/IDEA-155623#comment=27-1721029
+        JdomKt.addOptionTag(element, entry.getKey(), ownValue.toString());
+      }
+      else {
         TemplateContextType base = type.getBaseContextType();
         boolean baseEnabled = base != null && isEnabled(base);
         if (ownValue != baseEnabled) {
-          Element optionElement = new Element("option");
-          optionElement.setAttribute("name", type.getContextId());
-          optionElement.setAttribute("value", ownValue.toString());
-          element.addContent(optionElement);
+          JdomKt.addOptionTag(element, type.getContextId(), ownValue.toString());
         }
       }
     }
index db19233a50ac85388877c0bcd5b2f2eaf07d32e3..7594043c034ed252abef4ea5c458d52e1fc0ce6a 100644 (file)
@@ -30,7 +30,6 @@ import com.intellij.openapi.options.SchemeState;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.InvalidDataException;
 import com.intellij.openapi.util.JDOMUtil;
-import com.intellij.openapi.util.WriteExternalException;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.util.JdomKt;
 import com.intellij.util.SmartList;
@@ -622,14 +621,9 @@ public class TemplateSettings implements PersistentStateComponent<TemplateSettin
       element.addContent(variableElement);
     }
 
-    try {
-      Element contextElement = new Element(CONTEXT);
-      template.getTemplateContext().writeTemplateContext(contextElement);
-      element.addContent(contextElement);
-    }
-    catch (WriteExternalException e) {
-      LOG.warn(e);
-    }
+    Element contextElement = new Element(CONTEXT);
+    template.getTemplateContext().writeTemplateContext(contextElement);
+    element.addContent(contextElement);
     return element;
   }