diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java b/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java index feaac8aded4c..90a937834fde 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java @@ -18,12 +18,14 @@ package com.intellij.codeInsight.template; import com.intellij.codeInsight.template.impl.TemplateImpl; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateSettings; +import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; +import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.util.LocalTimeCounter; import com.intellij.util.containers.HashMap; import org.jetbrains.annotations.NotNull; @@ -61,8 +63,7 @@ public class CustomTemplateCallback { @Nullable public PsiElement getContext() { - int offset = myStartOffset; - return myFile.findElementAt(offset > 0 ? offset - 1 : offset); + return getContext(myFile, myStartOffset > 0 ? myStartOffset - 1 : myStartOffset); } public void fixInitialState() { @@ -229,4 +230,18 @@ public class CustomTemplateCallback { int caretAt = myEditor.getCaretModel().getOffset(); myEditor.getDocument().deleteString(caretAt - key.length(), caretAt); } + + public static PsiElement getContext(PsiFile file, int offset) { + PsiElement element = null; + if (!InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) { + element = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset); + } + if (element == null) { + element = file.findElementAt(offset > 0 ? offset - 1 : offset); + if (element == null) { + element = file; + } + } + return element; + } } diff --git a/xml/impl/src/com/intellij/codeInsight/template/zencoding/ZenCodingTemplate.java b/xml/impl/src/com/intellij/codeInsight/template/zencoding/ZenCodingTemplate.java index ba46de530a43..d156b1f54f61 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/zencoding/ZenCodingTemplate.java +++ b/xml/impl/src/com/intellij/codeInsight/template/zencoding/ZenCodingTemplate.java @@ -20,7 +20,6 @@ import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.codeInsight.template.CustomLiveTemplate; import com.intellij.codeInsight.template.CustomTemplateCallback; import com.intellij.codeInsight.template.TemplateInvokationListener; -import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.Editor; @@ -30,7 +29,6 @@ import com.intellij.openapi.ui.Messages; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.xml.XmlBundle; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -234,16 +232,7 @@ public abstract class ZenCodingTemplate implements CustomLiveTemplate { return false; } PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments(); - PsiElement element = null; - if (!InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) { - element = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset); - } - if (element == null) { - element = file.findElementAt(offset > 0 ? offset - 1 : offset); - if (element == null) { - element = file; - } - } + PsiElement element = CustomTemplateCallback.getContext(file, offset); return isApplicable(element); }