From 3028d3e54a7298f40bf85d7fd97bd22d217858fe Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 29 Jun 2015 14:27:23 +0300 Subject: [PATCH] provide context for generated variable declaration: otherwise e.g. language level for generated element would be defined by project language level instead of module one --- .../introduceVariable/IntroduceVariableBase.java | 4 ++-- .../src/com/intellij/psi/PsiElementFactory.java | 10 +++++++++- .../intellij/psi/impl/PsiElementFactoryImpl.java | 13 +++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 385fe8b578ee..9a7ab1f1f15d 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -872,7 +872,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { } PsiDeclarationStatement declaration = JavaPsiFacade.getInstance(project).getElementFactory() - .createVariableDeclarationStatement(settings.getEnteredName(), selectedType.getType(), initializer); + .createVariableDeclarationStatement(settings.getEnteredName(), selectedType.getType(), initializer, container); if (!isInsideLoop) { declaration = addDeclaration(declaration, initializer); LOG.assertTrue(expr1.isValid()); @@ -978,7 +978,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { if (initializer instanceof PsiNewExpression) { final PsiNewExpression newExpression = (PsiNewExpression)initializer; final PsiExpression tryToDetectDiamondNewExpr = ((PsiVariable)JavaPsiFacade.getElementFactory(initializer.getProject()) - .createVariableDeclarationStatement("x", expectedType, initializer).getDeclaredElements()[0]) + .createVariableDeclarationStatement("x", expectedType, initializer, initializer).getDeclaredElements()[0]) .getInitializer(); if (tryToDetectDiamondNewExpr instanceof PsiNewExpression && PsiDiamondTypeUtil.canCollapseToDiamond((PsiNewExpression)tryToDetectDiamondNewExpr, diff --git a/java/java-psi-api/src/com/intellij/psi/PsiElementFactory.java b/java/java-psi-api/src/com/intellij/psi/PsiElementFactory.java index 99960483bfe7..235872ec67cb 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiElementFactory.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiElementFactory.java @@ -367,6 +367,12 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor */ @NotNull PsiImportStatement createImportStatementOnDemand(@NotNull @NonNls String packageName) throws IncorrectOperationException; + /** + * @see #createVariableDeclarationStatement(String, PsiType, PsiExpression, PsiElement) + */ + @NotNull PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer) + throws IncorrectOperationException; + /** * Creates a local variable declaration statement with the specified name, type and initializer, * optionally without reformatting the declaration. @@ -374,11 +380,13 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor * @param name the name of the variable to create. * @param type the type of the variable to create. * @param initializer the initializer for the variable. + * @param context the context for dummy holder * @return the variable instance. * @throws IncorrectOperationException if name is not a valid identifier or * type is not a valid type. */ - @NotNull PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer) + @NotNull PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, @NotNull PsiType type, + @Nullable PsiExpression initializer, @Nullable PsiElement context) throws IncorrectOperationException; /** diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java index ede86356fdfe..58169e20f193 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java @@ -609,9 +609,18 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps @NotNull @Override - public PsiDeclarationStatement createVariableDeclarationStatement(@NotNull String name, + public PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer) throws IncorrectOperationException { + return createVariableDeclarationStatement(name, type, initializer, null); + } + + @NotNull + @Override + public PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, + @NotNull PsiType type, + @Nullable PsiExpression initializer, + @Nullable PsiElement context) throws IncorrectOperationException { if (!isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } @@ -620,7 +629,7 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps } String text = "X " + name + (initializer != null ? " = x" : "") + ";"; - PsiDeclarationStatement statement = (PsiDeclarationStatement)createStatementFromText(text, null); + PsiDeclarationStatement statement = (PsiDeclarationStatement)createStatementFromText(text, context); PsiVariable variable = (PsiVariable)statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text);