refactoring, CustomTemplateCallback#getContext() should work through injection

This commit is contained in:
Eugene Kudelevsky
2010-04-22 21:07:17 +04:00
parent b202703bb5
commit e382af62d6
2 changed files with 18 additions and 14 deletions
@@ -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;
}
}
@@ -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);
}