zen-coding migrate to ctrl+shift+enter (SmartEnter)
authorEugene Kudelevsky <Eugene.Kudelevsky@jetbrains.com>
Wed, 17 Mar 2010 17:06:00 +0000 (20:06 +0300)
committerEugene Kudelevsky <Eugene.Kudelevsky@jetbrains.com>
Wed, 17 Mar 2010 17:06:00 +0000 (20:06 +0300)
platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java
platform/platform-resources/src/META-INF/XmlPlugin.xml
xml/impl/src/com/intellij/codeInsight/completion/XmlSmartEnterProcessor.java
xml/impl/src/com/intellij/codeInsight/template/zencoding/XmlZenCodingTemplate.java

index 0a331403e0df923645062d2e4a1eb1d2ad916891..ba3d51d31d3bdb4dbcf7c1fe8b0ec7b3fbcc4a64 100644 (file)
@@ -238,31 +238,7 @@ public class TemplateManagerImpl extends TemplateManager implements ProjectCompo
     PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, myProject);
     if (file == null) return false;
     TemplateSettings templateSettings = TemplateSettings.getInstance();
-
     Map<TemplateImpl, String> template2argument = findMatchingTemplates(file, editor, shortcutChar, templateSettings);
-
-    if (shortcutChar == templateSettings.getDefaultShortcutChar()) {
-      for (final CustomLiveTemplate customLiveTemplate : CustomLiveTemplate.EP_NAME.getExtensions()) {
-        int caretOffset = editor.getCaretModel().getOffset();
-        if (customLiveTemplate.isApplicable(file, caretOffset, false)) {
-          final CustomTemplateCallback callback = new CustomTemplateCallback(editor, file);
-          String key = customLiveTemplate.computeTemplateKey(callback);
-          if (key != null) {
-            int offsetBeforeKey = caretOffset - key.length();
-            CharSequence text = editor.getDocument().getCharsSequence();
-            if (template2argument == null || !containsTemplateStartingBefore(template2argument, offsetBeforeKey, caretOffset, text)) {
-              callback.getEditor().getDocument().deleteString(offsetBeforeKey, caretOffset);
-              customLiveTemplate.expand(key, callback, new TemplateInvokationListener() {
-                public void finished(boolean inSeparateEvent) {
-                  callback.finish();
-                }
-              });
-              return true;
-            }
-          }
-        }
-      }
-    }
     return startNonCustomTemplates(template2argument, editor, processor);
   }
 
index 0b04b7fe2ad722f8a199d166789e5ed471c991c1..7e8665c34c8a407f35cbb9b9d053dd4212ef50e3 100644 (file)
                                         implementationClass="com.intellij.codeInsight.hint.XmlImplementationTextSelectioner"/>
     <basicWordSelectionFilter implementation="com.intellij.codeInsight.editorActions.XmlBasicWordSelectionFilter"/>
     <defaultLiveTemplatesProvider implementation="com.intellij.codeInsight.template.XmlDefaultLiveTemplatesProvider"/>
-    <customLiveTemplate implementation="com.intellij.codeInsight.template.zencoding.XmlZenCodingTemplate"/>
     <productivityFeaturesProvider implementation="com.intellij.featureStatistics.XmlProductivityFeatureProvider"/>
 
     <idIndexer filetype="XML" implementationClass="com.intellij.psi.impl.cache.impl.idCache.XmlIdIndexer"/>
index 584a1eae2dd730e4bcd0e923723978f042404320..745dd2353a4ba87ac28bbfcaa73f892fe2517ed9 100644 (file)
@@ -16,6 +16,7 @@
 package com.intellij.codeInsight.completion;
 
 import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor;
+import com.intellij.codeInsight.template.zencoding.XmlZenCodingTemplate;
 import com.intellij.lang.ASTNode;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
@@ -48,7 +49,7 @@ public class XmlSmartEnterProcessor extends SmartEnterProcessor {
         final ASTNode emptyTagEnd = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(tagAtCaret.getNode());
         final ASTNode endTagEnd = XmlChildRole.START_TAG_END_FINDER.findChild(tagAtCaret.getNode());
         if (emptyTagEnd != null || endTagEnd != null) {
-          return false;
+          return XmlZenCodingTemplate.startZenCoding(editor, psiFile);
         }
 
         int insertionOffset = tagAtCaret.getTextRange().getEndOffset();
@@ -91,7 +92,7 @@ public class XmlSmartEnterProcessor extends SmartEnterProcessor {
             if (shouldInsertClosingTag(xmlAttribute, tagAtCaret)) {
               doc.insertString(tag.getTextRange().getEndOffset() + text2insert.length(), "</" + tagAtCaret.getName() + ">");
             }
-            
+
             caretTo = tag.getTextRange().getEndOffset() + text2insert.length();
           }
           else {
index 29fdbb9087435434cd9f5b7094a8f5ac1b22133e..78060e505d02d3035b2ff546a625cb7868226ce9 100644 (file)
 package com.intellij.codeInsight.template.zencoding;
 
 import com.intellij.application.options.editor.WebEditorOptions;
-import com.intellij.codeInsight.template.CustomLiveTemplate;
 import com.intellij.codeInsight.template.CustomTemplateCallback;
 import com.intellij.codeInsight.template.TemplateInvokationListener;
+import com.intellij.codeInsight.template.TemplateManager;
 import com.intellij.codeInsight.template.impl.TemplateImpl;
+import com.intellij.codeInsight.template.impl.TemplateSettings;
 import com.intellij.lang.xml.XMLLanguage;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.Project;
@@ -27,18 +28,23 @@ import com.intellij.openapi.util.Pair;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiFileFactory;
-import com.intellij.psi.xml.*;
+import com.intellij.psi.xml.XmlDocument;
+import com.intellij.psi.xml.XmlFile;
+import com.intellij.psi.xml.XmlTag;
 import com.intellij.util.containers.HashSet;
 import org.apache.xerces.util.XML11Char;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
 
 /**
  * @author Eugene.Kudelevsky
  */
-public class XmlZenCodingTemplate implements CustomLiveTemplate {
+public class XmlZenCodingTemplate {
   static final char MARKER = '$';
   private static final String OPERATIONS = ">+*";
   private static final String SELECTORS = ".#[";
@@ -346,4 +352,26 @@ public class XmlZenCodingTemplate implements CustomLiveTemplate {
     XmlDocument document = xmlFile.getDocument();
     return document == null ? null : document.getRootTag();
   }
+
+  public static boolean startZenCoding(Editor editor, PsiFile file) {
+    int caretAt = editor.getCaretModel().getOffset();
+    XmlZenCodingTemplate template = new XmlZenCodingTemplate();
+    if (template.isApplicable(file, caretAt, false)) {
+      final CustomTemplateCallback callback = new CustomTemplateCallback(editor, file);
+      String key = template.computeTemplateKey(callback);
+      if (key != null) {
+        int offsetBeforeKey = caretAt - key.length();
+        callback.getEditor().getDocument().deleteString(offsetBeforeKey, caretAt);
+        template.expand(key, callback, new TemplateInvokationListener() {
+          public void finished(boolean inSeparateEvent) {
+            callback.finish();
+          }
+        });
+        return true;
+      }
+    }
+    // if it is simple live template invokation, we should start it using TemplateManager because template may be ambiguous
+    TemplateManager manager = TemplateManager.getInstance(file.getProject());
+    return manager.startTemplate(editor, TemplateSettings.getInstance().getDefaultShortcutChar());
+  }
 }
\ No newline at end of file