Search in sources :

Example 56 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression 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)

Example 57 with GrExpression

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

the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.

private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
    final StringPartInfo part = context.getStringPart();
    GrExpression expression = context.getExpression();
    if (expression != null) {
        expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
        if (expression == null)
            return false;
        if (expression instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
            if (PsiUtil.isLocalVariable(resolved)) {
                expression = ((GrVariable) resolved).getInitializerGroovy();
                if (expression == null)
                    return false;
            }
        }
        ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
        expression.accept(visitor);
        return visitor.isResult();
    }
    if (part != null) {
        for (GrStringInjection injection : part.getInjections()) {
            GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
            assert scope != null;
            ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
            scope.accept(visitor);
            if (!visitor.isResult()) {
                return visitor.isResult();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 58 with GrExpression

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

the class SwitchStatementGenerator method generateIfs.

private static void generateIfs(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull GrExpression condition, @NotNull GrCaseSection[] caseSections) {
    final GrExpression ref;
    if (condition instanceof GrReferenceExpression) {
        ref = condition;
    } else {
        final String varName = generateConditionVar(builder, context, condition);
        ref = GroovyPsiElementFactory.getInstance(context.project).createExpressionFromText(varName);
    }
    final GrExpression[] args = { ref };
    generateIfFromCaseSection(builder, context, caseSections, 0, args);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 59 with GrExpression

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

the class GrIntroduceConstantHandler method checkVariable.

@Override
protected void checkVariable(@NotNull GrVariable variable) throws GrRefactoringError {
    final GrExpression initializer = variable.getInitializerGroovy();
    if (initializer == null) {
        throw new GrRefactoringError(RefactoringBundle.message("variable.does.not.have.an.initializer", variable.getName()));
    }
    checkExpression(initializer);
}
Also used : GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 60 with GrExpression

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

the class GroovyExtractChooser method invoke.

public static InitialInfo invoke(Project project, Editor editor, PsiFile file, int start, int end, boolean forceStatements) throws GrRefactoringError {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (!(file instanceof GroovyFileBase)) {
        throw new GrRefactoringError(GroovyRefactoringBundle.message("only.in.groovy.files"));
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) {
        throw new GrRefactoringError(RefactoringBundle.message("readonly.occurences.found"));
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final StringPartInfo stringPart = StringPartInfo.findStringPart(file, start, end);
    if (stringPart != null) {
        return new InitialInfo(new VariableInfo[0], new VariableInfo[0], PsiElement.EMPTY_ARRAY, GrStatement.EMPTY_ARRAY, new ArrayList<>(), stringPart, project, null);
    }
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!forceStatements) {
        GrVariable variable = GrIntroduceHandlerBase.findVariable(file, start, end);
        if (variable != null) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                TextRange range = initializer.getTextRange();
                return buildInfo(project, file, range.getStartOffset(), range.getEndOffset(), forceStatements, selectionModel, variable);
            }
        }
    }
    return buildInfo(project, file, start, end, forceStatements, selectionModel, null);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16