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

This commit is contained in:
Anna Kozlova
2015-06-29 15:59:09 +03:00
parent 3daa8399fe
commit 3028d3e54a
3 changed files with 22 additions and 5 deletions
@@ -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,
@@ -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 <code>name</code> is not a valid identifier or
* <code>type</code> 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;
/**
@@ -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);