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;
}
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;
}
}
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;
}
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");
}
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;
}
Aggregations