3 * Copyright 2000-2009 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package com
.intellij
.psi
.impl
;
19 import com
.intellij
.lexer
.JavaLexer
;
20 import com
.intellij
.lexer
.Lexer
;
21 import com
.intellij
.openapi
.application
.ApplicationManager
;
22 import com
.intellij
.openapi
.roots
.LanguageLevelProjectExtension
;
23 import com
.intellij
.pom
.java
.LanguageLevel
;
24 import com
.intellij
.psi
.JavaPsiFacade
;
25 import com
.intellij
.psi
.JavaTokenType
;
26 import com
.intellij
.psi
.PsiNameHelper
;
27 import org
.jetbrains
.annotations
.NotNull
;
28 import org
.jetbrains
.annotations
.Nullable
;
30 public class PsiNameHelperImpl
extends PsiNameHelper
{
31 private final JavaPsiFacade myManager
;
32 private Lexer myLexer
;
33 private LanguageLevel myLastLanguageLevel
;
34 private final Object LOCK
= new Object();
36 public PsiNameHelperImpl(JavaPsiFacade manager
) {
38 myLastLanguageLevel
= LanguageLevelProjectExtension
.getInstance(manager
.getProject()).getLanguageLevel();
39 myLexer
= new JavaLexer(myLastLanguageLevel
);
42 private void updateLexer(LanguageLevel languageLevel
){
43 if (!myLastLanguageLevel
.equals(languageLevel
)){
44 myLastLanguageLevel
= languageLevel
;
45 myLexer
= new JavaLexer(myLastLanguageLevel
);
49 public boolean isIdentifier(@Nullable String text
) {
50 ApplicationManager
.getApplication().assertReadAccessAllowed();
51 if (text
== null) return false;
54 updateLexer(LanguageLevelProjectExtension
.getInstance(myManager
.getProject()).getLanguageLevel());
56 if (myLexer
.getTokenType() != JavaTokenType
.IDENTIFIER
) return false;
58 return myLexer
.getTokenType() == null;
62 public boolean isIdentifier(@Nullable String text
, @NotNull LanguageLevel languageLevel
) {
63 ApplicationManager
.getApplication().assertReadAccessAllowed();
64 if (text
== null) return false;
67 updateLexer(languageLevel
);
69 if (myLexer
.getTokenType() != JavaTokenType
.IDENTIFIER
) return false;
71 return myLexer
.getTokenType() == null;
75 public boolean isKeyword(@Nullable String text
) {
76 ApplicationManager
.getApplication().assertReadAccessAllowed();
77 if (text
== null) return false;
80 updateLexer(LanguageLevelProjectExtension
.getInstance(myManager
.getProject()).getLanguageLevel());
82 if (myLexer
.getTokenType() == null || !JavaTokenType
.KEYWORD_BIT_SET
.contains(myLexer
.getTokenType())) return false;
84 return myLexer
.getTokenType() == null;
88 public boolean isQualifiedName(@Nullable String text
){
89 if (text
== null) return false;
92 int index1
= text
.indexOf('.', index
);
93 if (index1
< 0) index1
= text
.length();
94 if (!isIdentifier(text
.substring(index
, index1
))) return false;
95 if (index1
== text
.length()) return true;