Search in sources :

Example 46 with GrVariable

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

the class GroovyBlockGenerator method alignVariableDeclarations.

private void alignVariableDeclarations(List<GrStatement> group, boolean classLevel) {
    AlignmentProvider.Aligner typeElement = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner varName = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner eq = myAlignmentProvider.createAligner(true);
    for (GrStatement statement : group) {
        GrVariableDeclaration varDeclaration = (GrVariableDeclaration) statement;
        GrVariable[] variables = varDeclaration.getVariables();
        for (GrVariable variable : variables) {
            varName.append(variable.getNameIdentifierGroovy());
        }
        if (classLevel && myContext.getSettings().ALIGN_GROUP_FIELD_DECLARATIONS) {
            typeElement.append(varDeclaration.getTypeElementGroovy());
            ASTNode current_eq = variables[variables.length - 1].getNode().findChildByType(GroovyTokenTypes.mASSIGN);
            if (current_eq != null) {
                eq.append(current_eq.getPsi());
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) AlignmentProvider(org.jetbrains.plugins.groovy.formatter.AlignmentProvider) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 47 with GrVariable

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

the class GrIntroduceValidatorEngine method validateOccurrencesDown.

/**
   * @param startElement Container to start checking conflicts from
   * @param conflicts    Conflict accumulator
   * @param varName      Variable name
   * @param startOffset
   */
private void validateOccurrencesDown(@NotNull PsiElement startElement, @NotNull MultiMap<PsiElement, String> conflicts, @NotNull String varName, double startOffset) {
    PsiElement child = startElement.getFirstChild();
    while (child != null) {
        // Do not check defined classes, methods, closures and blocks before
        if (child instanceof GrTypeDefinition || child instanceof GrMethod || GroovyRefactoringUtil.isAppropriateContainerForIntroduceVariable(child) && child.getTextRange().getEndOffset() < startOffset) {
            myReporter.check(child, conflicts, varName);
            child = child.getNextSibling();
            continue;
        }
        if (child instanceof GrVariable) {
            myReporter.check(child, conflicts, varName);
            validateOccurrencesDown(child, conflicts, varName, startOffset);
        } else {
            validateOccurrencesDown(child, conflicts, varName, startOffset);
        }
        child = child.getNextSibling();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Example 48 with GrVariable

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

the class GrInplaceConstantIntroducer method getInitialSettingsForInplace.

@Nullable
@Override
protected GrIntroduceConstantSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
    return new GrIntroduceConstantSettings() {

        @Override
        public String getVisibilityModifier() {
            return PsiModifier.PUBLIC;
        }

        @Nullable
        @Override
        public PsiClass getTargetClass() {
            return (PsiClass) context.getScope();
        }

        @Nullable
        @Override
        public String getName() {
            return names[0];
        }

        @Override
        public boolean replaceAllOccurrences() {
            return isReplaceAllOccurrences();
        }

        @Nullable
        @Override
        public PsiType getSelectedType() {
            GrExpression expression = context.getExpression();
            GrVariable var = context.getVar();
            StringPartInfo stringPart = context.getStringPart();
            return var != null ? var.getDeclaredType() : expression != null ? expression.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with GrVariable

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

the class GrIntroduceConstantDialog method createUIComponents.

private void createUIComponents() {
    myJavaVisibilityPanel = new JavaVisibilityPanel(false, true);
    final GrVariable var = myContext.getVar();
    final GrExpression expression = myContext.getExpression();
    final StringPartInfo stringPart = myContext.getStringPart();
    if (expression != null) {
        myTypeCombo = GrTypeComboBox.createTypeComboBoxFromExpression(expression);
    } else if (stringPart != null) {
        myTypeCombo = GrTypeComboBox.createTypeComboBoxFromExpression(stringPart.getLiteral());
    } else {
        assert var != null;
        myTypeCombo = GrTypeComboBox.createTypeComboBoxWithDefType(var.getDeclaredType(), var);
    }
    List<String> names = new ArrayList<>();
    if (var != null) {
        names.add(var.getName());
    }
    if (expression != null) {
        ContainerUtil.addAll(names, suggestNames());
    }
    myNameField = new NameSuggestionsField(ArrayUtil.toStringArray(names), myContext.getProject(), GroovyFileType.GROOVY_FILE_TYPE);
    GrTypeComboBox.registerUpDownHint(myNameField, myTypeCombo);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) JavaVisibilityPanel(com.intellij.refactoring.ui.JavaVisibilityPanel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NameSuggestionsField(com.intellij.refactoring.ui.NameSuggestionsField) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Example 50 with GrVariable

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

the class GrIntroduceConstantHandler method getOccurrenceOptions.

@NotNull
@Override
protected Map<OccurrencesChooser.ReplaceChoice, List<Object>> getOccurrenceOptions(@NotNull GrIntroduceContext context) {
    HashMap<OccurrencesChooser.ReplaceChoice, List<Object>> map = ContainerUtil.newLinkedHashMap();
    GrVariable localVar = resolveLocalVar(context);
    if (localVar != null) {
        map.put(OccurrencesChooser.ReplaceChoice.ALL, Arrays.<Object>asList(context.getOccurrences()));
        return map;
    }
    if (context.getExpression() != null) {
        map.put(OccurrencesChooser.ReplaceChoice.NO, Collections.<Object>singletonList(context.getExpression()));
    } else if (context.getStringPart() != null) {
        map.put(OccurrencesChooser.ReplaceChoice.NO, Collections.<Object>singletonList(context.getStringPart()));
    }
    PsiElement[] occurrences = context.getOccurrences();
    if (occurrences.length > 1) {
        map.put(OccurrencesChooser.ReplaceChoice.ALL, Arrays.<Object>asList(occurrences));
    }
    return map;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) 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