mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-26 19:06:24 +07:00
IteratorDeclaration: support parenthesized hasNext, etc.
This commit is contained in:
@@ -21,6 +21,7 @@ import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import one.util.streamex.MoreCollectors;
|
||||
import one.util.streamex.StreamEx;
|
||||
@@ -76,6 +77,9 @@ public class IteratorDeclaration {
|
||||
}
|
||||
|
||||
public boolean isIteratorMethodCall(PsiElement candidate, String method) {
|
||||
while (candidate instanceof PsiParenthesizedExpression) {
|
||||
candidate = ((PsiParenthesizedExpression)candidate).getExpression();
|
||||
}
|
||||
if (!(candidate instanceof PsiMethodCallExpression)) return false;
|
||||
PsiMethodCallExpression call = (PsiMethodCallExpression)candidate;
|
||||
if (!call.getArgumentList().isEmpty()) return false;
|
||||
@@ -84,24 +88,24 @@ public class IteratorDeclaration {
|
||||
}
|
||||
|
||||
public PsiVariable getNextElementVariable(PsiStatement statement) {
|
||||
PsiLocalVariable var = getDeclaredVariable(statement);
|
||||
if (var == null || !isIteratorMethodCall(var.getInitializer(), "next")) return null;
|
||||
return var;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiLocalVariable getDeclaredVariable(PsiStatement statement) {
|
||||
if (!(statement instanceof PsiDeclarationStatement)) return null;
|
||||
PsiDeclarationStatement declaration = (PsiDeclarationStatement)statement;
|
||||
if (declaration.getDeclaredElements().length != 1) return null;
|
||||
PsiElement element = declaration.getDeclaredElements()[0];
|
||||
if (!(element instanceof PsiLocalVariable)) return null;
|
||||
PsiLocalVariable var = (PsiLocalVariable)element;
|
||||
if (!isIteratorMethodCall(var.getInitializer(), "next")) return null;
|
||||
return var;
|
||||
PsiElement[] elements = declaration.getDeclaredElements();
|
||||
if (elements.length != 1) return null;
|
||||
return ObjectUtils.tryCast(elements[0], PsiLocalVariable.class);
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
private static IteratorDeclaration extract(PsiStatement statement) {
|
||||
if (!(statement instanceof PsiDeclarationStatement)) return null;
|
||||
PsiDeclarationStatement declaration = (PsiDeclarationStatement)statement;
|
||||
if (declaration.getDeclaredElements().length != 1) return null;
|
||||
PsiElement element = declaration.getDeclaredElements()[0];
|
||||
if (!(element instanceof PsiLocalVariable)) return null;
|
||||
PsiLocalVariable variable = (PsiLocalVariable)element;
|
||||
PsiLocalVariable variable = getDeclaredVariable(statement);
|
||||
if (variable == null) return null;
|
||||
PsiExpression initializer = PsiUtil.skipParenthesizedExprDown(variable.getInitializer());
|
||||
if (!(initializer instanceof PsiMethodCallExpression)) return null;
|
||||
PsiMethodCallExpression call = (PsiMethodCallExpression)initializer;
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import java.util.*;
|
||||
|
||||
public class Main {
|
||||
public void removeEmpty(List<String> list) throws Exception {
|
||||
f<caret>or(Iterator<String> it = list/*here's list*/.iterator(); it.hasNext();) {
|
||||
f<caret>or(Iterator<String> it = list/*here's list*/.iterator(); (it.hasNext());) {
|
||||
// iterate over list
|
||||
String str = it.next();
|
||||
// if it's empty
|
||||
|
||||
Reference in New Issue
Block a user