Search in sources :

Example 56 with GrVariable

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

the class GrIntroduceFieldDialog method createUIComponents.

private void createUIComponents() {
    final GrExpression expression = myContext.getExpression();
    final GrVariable var = myContext.getVar();
    final StringPartInfo stringPart = myContext.getStringPart();
    List<String> list = new ArrayList<>();
    if (var != null) {
        list.add(var.getName());
    }
    ContainerUtil.addAll(list, suggestNames());
    myNameField = new NameSuggestionsField(ArrayUtil.toStringArray(list), myContext.getProject(), GroovyFileType.GROOVY_FILE_TYPE);
    if (expression != null) {
        myTypeComboBox = GrTypeComboBox.createTypeComboBoxFromExpression(expression);
    } else if (stringPart != null) {
        myTypeComboBox = GrTypeComboBox.createTypeComboBoxFromExpression(stringPart.getLiteral());
    } else {
        myTypeComboBox = GrTypeComboBox.createTypeComboBoxWithDefType(var.getDeclaredType(), var);
    }
    GrTypeComboBox.registerUpDownHint(myNameField, myTypeComboBox);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ArrayList(java.util.ArrayList) 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 57 with GrVariable

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

the class ImportMavenRepositoriesTask method resolveUriFromSimpleExpression.

@Nullable
private static URI resolveUriFromSimpleExpression(@Nullable GrExpression expression) {
    if (expression == null)
        return null;
    try {
        if (expression instanceof PsiLiteral) {
            URI uri = new URI(String.valueOf(PsiLiteral.class.cast(expression).getValue()));
            if (uri.getScheme() != null && StringUtil.startsWith(uri.getScheme(), "http"))
                return uri;
        }
    } catch (URISyntaxException ignored) {
    // ignore it
    }
    try {
        PsiReference reference = expression.getReference();
        if (reference == null)
            return null;
        PsiElement element = reference.resolve();
        if (element instanceof GrVariable) {
            List<GrLiteral> grLiterals = PsiTreeUtil.getChildrenOfTypeAsList(element, GrLiteral.class);
            if (grLiterals.isEmpty())
                return null;
            URI uri = new URI(String.valueOf(grLiterals.get(0).getValue()));
            if (uri.getScheme() != null && StringUtil.startsWith("http", uri.getScheme()))
                return uri;
        }
    } catch (URISyntaxException ignored) {
    // ignore it
    }
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with GrVariable

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

the class GroovyLineMarkerProvider method getGroovyCategory.

private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
    if (element instanceof GrVariableDeclarationImpl) {
        GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
        if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
            return 2;
        }
    }
    if (element instanceof GrField || element instanceof GrTypeParameter)
        return 1;
    if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
        return 2;
    if (element instanceof GrMethod) {
        if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
            return 1;
        }
        TextRange textRange = element.getTextRange();
        int start = textRange.getStartOffset();
        int end = Math.min(documentChars.length(), textRange.getEndOffset());
        int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
        return crlf == 0 ? 1 : 2;
    }
    return 0;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrTypeParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) TextRange(com.intellij.openapi.util.TextRange)

Example 59 with GrVariable

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

the class ExtractUtil method collectUsedLocalVarsOrParamsDeclaredOutside.

private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper) {
    final Collection<GrVariable> result = new HashSet<>();
    final TextRange range = getRangeOfRefactoring(helper);
    final int start = range.getStartOffset();
    final int end = range.getEndOffset();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            final PsiElement resolved = ref.resolve();
            if ((resolved instanceof GrParameter || PsiUtil.isLocalVariable(resolved)) && resolved.isPhysical()) {
                final int offset = resolved.getTextRange().getStartOffset();
                //var is declared outside of selected code
                if (offset < start || end <= offset) {
                    result.add((GrVariable) resolved);
                }
            }
        }
    };
    final GrStatement[] statements = helper.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TextRange(com.intellij.openapi.util.TextRange) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 60 with GrVariable

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

the class ExtractUtil method mustAddVariableDeclaration.

/*
  To declare or not a variable to which method call result will be assigned.
   */
private static List<VariableInfo> mustAddVariableDeclaration(@NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
    Map<String, VariableInfo> names = new HashMap<>();
    for (VariableInfo var : vars) {
        names.put(var.getName(), var);
    }
    List<VariableInfo> result = new ArrayList<>();
    for (GrStatement statement : statements) {
        if (statement instanceof GrVariableDeclaration) {
            GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
            for (GrVariable variable : declaration.getVariables()) {
                final VariableInfo removed = names.remove(variable.getName());
                if (removed != null) {
                    result.add(removed);
                }
            }
        }
    }
    for (String varName : names.keySet()) {
        if (ResolveUtil.resolveProperty(statements[statements.length - 1], varName) == null) {
            result.add(names.get(varName));
        }
    }
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) HashMap(com.intellij.util.containers.HashMap) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

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