use of com.intellij.psi.scope.processor.VariablesNotProcessor in project intellij-community by JetBrains.
the class HighlightUtil method checkVariableAlreadyDefined.
@Nullable
static HighlightInfo checkVariableAlreadyDefined(@NotNull PsiVariable variable) {
if (variable instanceof ExternallyDefinedPsiElement)
return null;
boolean isIncorrect = false;
PsiElement declarationScope = null;
if (variable instanceof PsiLocalVariable || variable instanceof PsiParameter && ((declarationScope = ((PsiParameter) variable).getDeclarationScope()) instanceof PsiCatchSection || declarationScope instanceof PsiForeachStatement || declarationScope instanceof PsiLambdaExpression)) {
@SuppressWarnings("unchecked") PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {
@Override
protected boolean check(final PsiVariable var, final ResolveState state) {
return (var instanceof PsiLocalVariable || var instanceof PsiParameter) && super.check(var, state);
}
};
PsiIdentifier identifier = variable.getNameIdentifier();
assert identifier != null : variable;
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
if (scope instanceof PsiResourceList && proc.size() == 0) {
scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
}
if (proc.size() > 0) {
isIncorrect = true;
} else if (declarationScope instanceof PsiLambdaExpression) {
isIncorrect = checkSameNames(variable);
}
} else if (variable instanceof PsiField) {
PsiField field = (PsiField) variable;
PsiClass aClass = field.getContainingClass();
if (aClass == null)
return null;
PsiField fieldByName = aClass.findFieldByName(variable.getName(), false);
if (fieldByName != null && fieldByName != field) {
isIncorrect = true;
}
} else {
isIncorrect = checkSameNames(variable);
}
if (isIncorrect) {
String description = JavaErrorMessages.message("variable.already.defined", variable.getName());
PsiIdentifier identifier = variable.getNameIdentifier();
assert identifier != null : variable;
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
if (variable instanceof PsiLocalVariable) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createReuseVariableDeclarationFix((PsiLocalVariable) variable));
}
return highlightInfo;
}
return null;
}
use of com.intellij.psi.scope.processor.VariablesNotProcessor in project intellij-community by JetBrains.
the class ReuseVariableDeclarationFix method findPreviousVariable.
@Nullable
private PsiVariable findPreviousVariable() {
PsiElement scope = myVariable.getParent();
while (scope != null) {
if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiClassInitializer)
break;
scope = scope.getParent();
}
if (scope == null)
return null;
PsiIdentifier nameIdentifier = myVariable.getNameIdentifier();
if (nameIdentifier == null) {
return null;
}
final VariablesNotProcessor processor = new VariablesNotProcessor(myVariable, false);
PsiScopesUtil.treeWalkUp(processor, nameIdentifier, scope);
return processor.size() > 0 ? processor.getResult(0) : null;
}
Aggregations