Search in sources :

Example 21 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.

the class GrAbstractInplaceIntroducer method createFieldToStartTemplateOn.

@Nullable
@Override
protected GrVariable createFieldToStartTemplateOn(boolean replaceAll, @NotNull String[] names) {
    final Settings settings = getInitialSettingsForInplace(myContext, myReplaceChoice, names);
    if (settings == null)
        return null;
    GrVariable var = runRefactoring(myContext, settings, false);
    if (var != null) {
        myVarMarker = myContext.getEditor().getDocument().createRangeMarker(var.getTextRange());
    }
    return var;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.

the class GrInplaceParameterIntroducer method createExpressionWrapper.

@NotNull
private static GrExpressionWrapper createExpressionWrapper(@NotNull GrIntroduceContext context) {
    GrExpression expression = context.getExpression();
    GrVariable var = context.getVar();
    assert expression != null || var != null;
    GrExpression initializer = expression != null ? expression : var.getInitializerGroovy();
    return new GrExpressionWrapper(initializer);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.

the class GrIntroduceConstantProcessor method run.

@Nullable
public GrField run() {
    final PsiClass targetClass = settings.getTargetClass();
    if (targetClass == null)
        return null;
    if (checkErrors(targetClass)) {
        return null;
    }
    final GrVariableDeclaration declaration = addDeclaration(targetClass);
    final GrField field = (GrField) declaration.getVariables()[0];
    GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context);
    if (localVar != null) {
        assert localVar.getInitializerGroovy() != null : "initializer should exist: " + localVar.getText();
        GrIntroduceHandlerBase.deleteLocalVar(localVar);
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, localVar.getInitializerGroovy(), isEscalateVisibility());
        }
    } else if (context.getStringPart() != null) {
        final GrExpression ref = context.getStringPart().replaceLiteralWithConcatenation(field.getName());
        final PsiElement element = replaceOccurrence(field, ref, isEscalateVisibility());
        updateCaretPosition(element);
    } else if (context.getExpression() != null) {
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, context.getExpression(), isEscalateVisibility());
        }
    }
    return field;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.

the class GrIntroduceConstantProcessor method getInitializer.

@NotNull
protected GrExpression getInitializer() {
    GrVariable var = GrIntroduceHandlerBase.resolveLocalVar(context);
    GrExpression expression = context.getExpression();
    if (var != null) {
        return var.getInitializerGroovy();
    } else if (expression != null) {
        return expression;
    } else {
        return context.getStringPart().createLiteralFromSelected();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.

the class GrFieldNameSuggester method suggestNames.

@NotNull
public LinkedHashSet<String> suggestNames() {
    final GrExpression expression = myContext.getExpression();
    final GrVariable var = myContext.getVar();
    final StringPartInfo stringPart = myContext.getStringPart();
    if (expression != null) {
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNames(expression, myValidator, myForStatic)));
    } else if (stringPart != null) {
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNames(stringPart.getLiteral(), myValidator, myForStatic)));
    } else {
        assert var != null;
        return new LinkedHashSet<>(Arrays.asList(GroovyNameSuggestionUtil.suggestVariableNameByType(var.getType(), myValidator)));
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LinkedHashSet(java.util.LinkedHashSet) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5