Search in sources :

Example 6 with PsiVariable

use of com.intellij.psi.PsiVariable in project intellij-community by JetBrains.

the class TypeOfVariableMacro method calculateResult.

@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    if (params.length == 0)
        return null;
    final Project project = context.getProject();
    Result result = params[0].calculateQuickResult(context);
    if (result instanceof PsiElementResult) {
        final PsiElement element = ((PsiElementResult) result).getElement();
        if (element instanceof PsiVariable) {
            return new PsiTypeResult(((PsiVariable) element).getType(), project);
        }
    } else if (result instanceof TextResult) {
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
        PsiElement place = file.findElementAt(context.getStartOffset());
        final PsiVariable[] vars = MacroUtil.getVariablesVisibleAt(place, "");
        final String name = result.toString();
        for (final PsiVariable var : vars) {
            if (name.equals(var.getName()))
                return new PsiTypeResult(var.getType(), project);
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) PsiVariable(com.intellij.psi.PsiVariable) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiVariable

use of com.intellij.psi.PsiVariable in project intellij-community by JetBrains.

the class InputValidator method isOK.

public boolean isOK(IntroduceVariableSettings settings) {
    String name = settings.getEnteredName();
    final PsiElement anchor;
    final boolean replaceAllOccurrences = settings.isReplaceAllOccurrences();
    if (replaceAllOccurrences) {
        anchor = myAnchorStatementIfAll;
    } else {
        anchor = myAnchorStatement;
    }
    final PsiElement scope = anchor.getParent();
    if (scope == null)
        return true;
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final HashSet<PsiVariable> reportedVariables = new HashSet<>();
    JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor visitor = new JavaUnresolvableLocalCollisionDetector.CollidingVariableVisitor() {

        public void visitCollidingElement(PsiVariable collidingVariable) {
            if (!reportedVariables.contains(collidingVariable)) {
                reportedVariables.add(collidingVariable);
                String message = RefactoringBundle.message("introduced.variable.will.conflict.with.0", RefactoringUIUtil.getDescription(collidingVariable, true));
                conflicts.putValue(collidingVariable, message);
            }
        }
    };
    JavaUnresolvableLocalCollisionDetector.visitLocalsCollisions(anchor, name, scope, anchor, visitor);
    if (replaceAllOccurrences) {
        final PsiExpression[] occurences = myOccurenceManager.getOccurrences();
        for (PsiExpression occurence : occurences) {
            IntroduceVariableBase.checkInLoopCondition(occurence, conflicts);
        }
    } else {
        IntroduceVariableBase.checkInLoopCondition(myOccurenceManager.getMainOccurence(), conflicts);
    }
    if (conflicts.size() > 0) {
        return myIntroduceVariableBase.reportConflicts(conflicts, myProject, settings);
    } else {
        return true;
    }
}
Also used : JavaUnresolvableLocalCollisionDetector(com.intellij.refactoring.rename.JavaUnresolvableLocalCollisionDetector) PsiVariable(com.intellij.psi.PsiVariable) PsiExpression(com.intellij.psi.PsiExpression) MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 8 with PsiVariable

use of com.intellij.psi.PsiVariable in project intellij-community by JetBrains.

the class ResolveVariableUtil method resolveVariable.

public static PsiVariable resolveVariable(@NotNull PsiJavaCodeReferenceElement ref, boolean[] problemWithAccess, boolean[] problemWithStatic) {
    /*
    long time1 = System.currentTimeMillis();
    */
    final VariableResolverProcessor processor = new VariableResolverProcessor(ref, ref.getContainingFile());
    PsiScopesUtil.resolveAndWalk(processor, ref, null);
    /*
    long time2 = System.currentTimeMillis();
    Statistics.resolveVariableTime += (time2 - time1);
    Statistics.resolveVariableCount++;
    */
    final JavaResolveResult[] result = processor.getResult();
    if (result.length != 1)
        return null;
    final PsiVariable refVar = (PsiVariable) result[0].getElement();
    if (problemWithAccess != null) {
        problemWithAccess[0] = !result[0].isAccessible();
    }
    if (problemWithStatic != null) {
        problemWithStatic[0] = !result[0].isStaticsScopeCorrect();
    }
    return refVar;
}
Also used : PsiVariable(com.intellij.psi.PsiVariable) JavaResolveResult(com.intellij.psi.JavaResolveResult)

Example 9 with PsiVariable

use of com.intellij.psi.PsiVariable in project intellij-community by JetBrains.

the class MoveInstanceMethodTest method doTest.

private void doTest(boolean isTargetParameter, final int targetIndex, final String newVisibility) throws Exception {
    final String filePath = "/refactoring/moveInstanceMethod/" + getTestName(false) + ".java";
    configureByFile(filePath);
    final PsiElement targetElement = TargetElementUtil.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED);
    assertTrue("<caret> is not on method name", targetElement instanceof PsiMethod);
    PsiMethod method = (PsiMethod) targetElement;
    final PsiVariable targetVariable = isTargetParameter ? method.getParameterList().getParameters()[targetIndex] : method.getContainingClass().getFields()[targetIndex];
    new MoveInstanceMethodProcessor(getProject(), method, targetVariable, newVisibility, MoveInstanceMethodHandler.suggestParameterNames(method, targetVariable)).run();
    checkResultByFile(filePath + ".after");
}
Also used : PsiVariable(com.intellij.psi.PsiVariable) PsiMethod(com.intellij.psi.PsiMethod) PsiElement(com.intellij.psi.PsiElement) MoveInstanceMethodProcessor(com.intellij.refactoring.move.moveInstanceMethod.MoveInstanceMethodProcessor)

Example 10 with PsiVariable

use of com.intellij.psi.PsiVariable in project intellij-community by JetBrains.

the class VariablesProcessor method execute.

/** Always return true since we wanna get all vars in scope */
@Override
public boolean execute(@NotNull PsiElement pe, @NotNull ResolveState state) {
    if (pe instanceof PsiVariable) {
        final PsiVariable pvar = (PsiVariable) pe;
        final String pvar_name = pvar.getName();
        if (pvar_name.startsWith(myPrefix)) {
            if (!myStaticSensitiveFlag || (!myStaticScopeFlag || pvar.hasModifierProperty(PsiModifier.STATIC))) {
                myResultList.add(pvar);
            }
        }
    }
    return true;
}
Also used : PsiVariable(com.intellij.psi.PsiVariable)

Aggregations

PsiVariable (com.intellij.psi.PsiVariable)14 PsiElement (com.intellij.psi.PsiElement)9 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)5 Nullable (com.android.annotations.Nullable)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)3 PsiMethod (com.intellij.psi.PsiMethod)3 UCallExpression (org.jetbrains.uast.UCallExpression)3 UExpression (org.jetbrains.uast.UExpression)3 PsiClass (com.intellij.psi.PsiClass)2 PsiFile (com.intellij.psi.PsiFile)2 PsiParameter (com.intellij.psi.PsiParameter)2 PsiType (com.intellij.psi.PsiType)2 ArrayList (java.util.ArrayList)2 UElement (org.jetbrains.uast.UElement)2 UIfExpression (org.jetbrains.uast.UIfExpression)2 UParenthesizedExpression (org.jetbrains.uast.UParenthesizedExpression)2 UQualifiedReferenceExpression (org.jetbrains.uast.UQualifiedReferenceExpression)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1