support of predefined variable values
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / template / CustomTemplateCallback.java
blob4b46fd9625f219911fa73ad9a483cfa606ab6572
1 /*
2 * Copyright 2000-2010 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.template;
18 import com.intellij.codeInsight.template.impl.TemplateImpl;
19 import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
20 import com.intellij.codeInsight.template.impl.TemplateSettings;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.PsiFile;
24 import com.intellij.psi.codeStyle.CodeStyleManager;
25 import com.intellij.util.containers.HashSet;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
33 /**
34 * @author Eugene.Kudelevsky
36 public class CustomTemplateCallback {
37 private final TemplateManager myTemplateManager;
38 private final Editor myEditor;
39 private final PsiFile myFile;
40 private int myStartOffset;
41 private int myStartLength;
42 private Project myProject;
44 public CustomTemplateCallback(Editor editor, PsiFile file) {
45 myEditor = editor;
46 myFile = file;
47 myProject = file.getProject();
48 myTemplateManager = TemplateManagerImpl.getInstance(myProject);
51 public void fixInitialEditorState() {
52 myStartOffset = myEditor.getCaretModel().getOffset();
53 myStartLength = myEditor.getDocument().getCharsSequence().length();
56 public boolean isLiveTemplateApplicable(@NotNull String key) {
57 List<TemplateImpl> templates = getMatchingTemplates(key);
58 templates = TemplateManagerImpl.filterApplicableCandidates(myFile, myStartOffset, templates);
59 return templates.size() > 0;
62 public boolean isTemplateContainsVars(@NotNull String key, String... varNames) {
63 List<TemplateImpl> templates = getMatchingTemplates(key);
64 templates = TemplateManagerImpl.filterApplicableCandidates(myFile, myStartOffset, templates);
65 if (templates.size() == 0) {
66 return false;
68 TemplateImpl template = templates.get(0);
69 Set<String> varSet = new HashSet<String>();
70 for (int i = 0; i < template.getVariableCount(); i++) {
71 varSet.add(template.getVariableNameAt(i));
73 for (String varName : varNames) {
74 if (!varSet.contains(varName)) {
75 return false;
78 return true;
81 /**
82 * @param key
83 * @param predefinedVarValues
84 * @param listener @return returns if template invokation is finished
86 public boolean startTemplate(@NotNull String key,
87 Map<String, String> predefinedVarValues,
88 @Nullable TemplateInvokationListener listener) {
89 int caretOffset = myEditor.getCaretModel().getOffset();
90 List<TemplateImpl> templates = getMatchingTemplates(key);
91 templates = TemplateManagerImpl.filterApplicableCandidates(myFile, caretOffset, templates);
92 if (templates.size() > 0) {
93 TemplateImpl template = templates.get(0);
94 return startTemplate(template, predefinedVarValues, listener);
96 else if (listener != null) {
97 listener.finished(false, false);
99 return true;
103 * @param template
104 * @param predefinedVarValues
105 * @param listener
106 * @return returns if template invokation is finished
108 public boolean startTemplate(@NotNull Template template,
109 Map<String, String> predefinedVarValues,
110 @Nullable final TemplateInvokationListener listener) {
111 final boolean[] templateEnded = new boolean[]{false};
112 final boolean[] templateFinished = new boolean[]{false};
113 myTemplateManager.startTemplate(myEditor, template, false, predefinedVarValues, new TemplateEditingAdapter() {
114 @Override
115 public void templateFinished(Template template, boolean brokenOff) {
116 int lengthAfter = myEditor.getDocument().getCharsSequence().length();
117 CodeStyleManager style = CodeStyleManager.getInstance(myProject);
118 style.reformatText(myFile, myStartOffset, myStartOffset + lengthAfter - myStartLength);
119 if (brokenOff) return;
120 templateFinished[0] = true;
121 if (templateEnded[0] && listener != null) {
122 listener.finished(true, true);
126 templateEnded[0] = true;
127 if (templateFinished[0] && listener != null) {
128 listener.finished(false, true);
130 return templateFinished[0];
133 private static List<TemplateImpl> getMatchingTemplates(@NotNull String templateKey) {
134 TemplateSettings settings = TemplateSettings.getInstance();
135 return settings.collectMatchingCandidates(templateKey, settings.getDefaultShortcutChar(), false);
138 @NotNull
139 public Editor getEditor() {
140 return myEditor;
143 public PsiFile getFile() {
144 return myFile;