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 org
.jetbrains
.annotations
.NotNull
;
26 import org
.jetbrains
.annotations
.Nullable
;
28 import java
.util
.List
;
31 * @author Eugene.Kudelevsky
33 public class CustomTemplateCallback
{
34 private final TemplateManager myTemplateManager
;
35 private final Editor myEditor
;
36 private final PsiFile myFile
;
37 private int myStartOffset
;
38 private int myStartLength
;
39 private Project myProject
;
41 public CustomTemplateCallback(Editor editor
, PsiFile file
) {
44 myProject
= file
.getProject();
45 myTemplateManager
= TemplateManagerImpl
.getInstance(myProject
);
48 public void fixInitialEditorState() {
49 myStartOffset
= myEditor
.getCaretModel().getOffset();
50 myStartLength
= myEditor
.getDocument().getCharsSequence().length();
53 public boolean isLiveTemplateApplicable(@NotNull String key
) {
54 List
<TemplateImpl
> templates
= getMatchingTemplates(key
);
55 templates
= TemplateManagerImpl
.filterApplicableCandidates(myFile
, myStartOffset
, templates
);
56 return templates
.size() > 0;
62 * @return returns if template invokation is finished
64 public boolean startTemplate(@NotNull String key
, @Nullable TemplateInvokationListener listener
) {
65 int caretOffset
= myEditor
.getCaretModel().getOffset();
66 List
<TemplateImpl
> templates
= getMatchingTemplates(key
);
67 templates
= TemplateManagerImpl
.filterApplicableCandidates(myFile
, caretOffset
, templates
);
68 if (templates
.size() == 1) {
69 TemplateImpl template
= templates
.get(0);
70 return startTemplate(template
, listener
);
72 else if (listener
!= null) {
73 listener
.finished(false, false);
81 * @return returns if template invokation is finished
83 public boolean startTemplate(@NotNull Template template
, @Nullable final TemplateInvokationListener listener
) {
84 final boolean[] templateEnded
= new boolean[]{false};
85 final boolean[] templateFinished
= new boolean[]{false};
86 myTemplateManager
.startTemplate(myEditor
, template
, new TemplateEditingAdapter() {
89 public void templateExpanded(Template template
) {
90 int lengthAfter
= myEditor
.getDocument().getCharsSequence().length();
91 CodeStyleManager style
= CodeStyleManager
.getInstance(myProject
);
92 style
.reformatText(myFile
, myStartOffset
, myStartOffset
+ lengthAfter
- myStartLength
);
96 public void templateFinished(Template template
) {
97 templateFinished
[0] = true;
98 if (templateEnded
[0] && listener
!= null) {
99 listener
.finished(true, true);
103 templateEnded
[0] = true;
104 if (templateFinished
[0] && listener
!= null) {
105 listener
.finished(false, true);
107 return templateFinished
[0];
110 private static List
<TemplateImpl
> getMatchingTemplates(@NotNull String templateKey
) {
111 TemplateSettings settings
= TemplateSettings
.getInstance();
112 return settings
.collectMatchingCandidates(templateKey
, settings
.getDefaultShortcutChar(), false);
116 public Editor
getEditor() {
120 public PsiFile
getFile() {