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
.openapi
.components
.ServiceManager
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.application
.ApplicationManager
;
22 import com
.intellij
.psi
.*;
23 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
24 import com
.intellij
.psi
.codeStyle
.SuggestedNameInfo
;
25 import com
.intellij
.psi
.codeStyle
.VariableKind
;
26 import com
.intellij
.psi
.text
.BlockSupport
;
27 import com
.intellij
.util
.IncorrectOperationException
;
28 import org
.jetbrains
.annotations
.Nullable
;
33 public class ExpressionUtil
{
34 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.template.ExpressionUtil");
36 private ExpressionUtil() {
40 public static String
[] getNames(final ExpressionContext context
) {
41 final Project project
= context
.getProject();
42 final int offset
= context
.getStartOffset();
44 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
46 String
[] names
= null;
47 PsiFile file
= PsiDocumentManager
.getInstance(project
).getPsiFile(context
.getEditor().getDocument());
48 PsiElement element
= file
.findElementAt(offset
);
49 if (element
instanceof PsiIdentifier
){
50 names
= getNamesForIdentifier(project
, (PsiIdentifier
)element
);
53 final PsiFile fileCopy
= (PsiFile
)file
.copy();
54 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
56 BlockSupport blockSupport
= ServiceManager
.getService(project
, BlockSupport
.class);
58 blockSupport
.reparseRange(fileCopy
, offset
, offset
, "xxx");
60 catch(IncorrectOperationException e
){
65 PsiElement identifierCopy
= fileCopy
.findElementAt(offset
);
66 if (identifierCopy
instanceof PsiIdentifier
) {
67 names
= getNamesForIdentifier(project
, (PsiIdentifier
)identifierCopy
);
74 private static String
[] getNamesForIdentifier(Project project
, PsiIdentifier identifier
){
75 if (identifier
.getParent() instanceof PsiVariable
){
76 PsiVariable var
= (PsiVariable
)identifier
.getParent();
77 if (identifier
.equals(var
.getNameIdentifier())){
78 JavaCodeStyleManager codeStyleManager
= JavaCodeStyleManager
.getInstance(project
);
79 VariableKind variableKind
= codeStyleManager
.getVariableKind(var
);
80 PsiExpression initializer
= var
.getInitializer();
81 if (var
instanceof PsiParameter
&& ((PsiParameter
)var
).getDeclarationScope() instanceof PsiForeachStatement
) {
82 //synthesize initializer
83 PsiForeachStatement foreachStatement
= (PsiForeachStatement
)((PsiParameter
)var
).getDeclarationScope();
84 final PsiExpression iteratedValue
= foreachStatement
.getIteratedValue();
85 if (iteratedValue
!= null) {
87 final PsiArrayAccessExpression expr
= (PsiArrayAccessExpression
)JavaPsiFacade
.getInstance(iteratedValue
.getProject())
88 .getElementFactory().createExpressionFromText("a[0]", var
);
89 expr
.getArrayExpression().replace(iteratedValue
);
90 initializer
= expr
; //note: non physical with no parent
92 catch (IncorrectOperationException e
) {
97 SuggestedNameInfo suggestedInfo
= codeStyleManager
.suggestVariableName(variableKind
, null, initializer
, var
.getType());
98 return suggestedInfo
.names
;