2 * Copyright 2000-2009 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
.CodeInsightBundle
;
19 import com
.intellij
.ide
.highlighter
.JavaFileHighlighter
;
20 import com
.intellij
.lang
.Language
;
21 import com
.intellij
.lang
.StdLanguages
;
22 import com
.intellij
.openapi
.fileTypes
.FileType
;
23 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
24 import com
.intellij
.openapi
.fileTypes
.SyntaxHighlighter
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.util
.PsiTreeUtil
;
27 import com
.intellij
.psi
.util
.PsiUtilBase
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 public class JavaCodeContextType
extends TemplateContextType
{
31 public JavaCodeContextType() {
32 super("JAVA_CODE", CodeInsightBundle
.message("dialog.edit.template.checkbox.java.code"));
36 public boolean isInContext(@NotNull final PsiFile file
, final int offset
) {
37 FileType fileType
= file
.getFileType();
38 if (fileType
== StdFileTypes
.JAVA
) {
39 PsiElement element
= file
.findElementAt(offset
);
40 if (element
instanceof PsiWhiteSpace
&& offset
> 0) {
41 element
= file
.findElementAt(offset
-1);
43 return element
!= null && PsiTreeUtil
.getParentOfType(element
, PsiComment
.class, false) == null &&
44 !(element
instanceof PsiJavaToken
&& ((PsiJavaToken
) element
).getTokenType() == JavaTokenType
.STRING_LITERAL
);
46 if (fileType
== StdFileTypes
.JSP
|| fileType
== StdFileTypes
.JSPX
) {
47 final Language language
= PsiUtilBase
.getLanguageAtOffset(file
, offset
);
48 return language
.equals(StdLanguages
.JAVA
);
54 public boolean isInContext(@NotNull final FileType fileType
) {
55 return fileType
== StdFileTypes
.JAVA
|| fileType
== StdFileTypes
.JSP
|| fileType
== StdFileTypes
.JSPX
;
60 public SyntaxHighlighter
createHighlighter() {
61 return new JavaFileHighlighter();