Similar usages: Declaration statement in for statement should consider for statement as context

GitOrigin-RevId: dea5b3a87a8cc1de9888ebf3e155b4cce1690d53
This commit is contained in:
Andrey Sokolov
2022-09-22 20:11:44 +00:00
committed by intellij-monorepo-bot
parent 690c7ce36b
commit da96a1ee33
3 changed files with 33 additions and 15 deletions
@@ -37,21 +37,25 @@ public class JavaUsageSimilarityFeaturesProvider implements UsageSimilarityFeatu
}
public @Nullable PsiElement getContainingStatement(@NotNull PsiElement element) {
return PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class,
PsiExpressionStatement.class,
PsiIfStatement.class,
PsiWhileStatement.class,
PsiTryStatement.class,
PsiThrowStatement.class,
PsiSwitchStatement.class,
PsiReturnStatement.class,
PsiLoopStatement.class,
PsiImportStatement.class,
PsiForStatement.class,
PsiForeachStatement.class,
PsiConditionalLoopStatement.class,
PsiBlockStatement.class,
PsiMethod.class);
PsiElement containingStatement = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class,
PsiExpressionStatement.class,
PsiIfStatement.class,
PsiWhileStatement.class,
PsiTryStatement.class,
PsiThrowStatement.class,
PsiSwitchStatement.class,
PsiReturnStatement.class,
PsiLoopStatement.class,
PsiImportStatement.class,
PsiForStatement.class,
PsiForeachStatement.class,
PsiConditionalLoopStatement.class,
PsiBlockStatement.class,
PsiMethod.class);
if (containingStatement instanceof PsiDeclarationStatement && containingStatement.getParent() instanceof PsiForStatement) {
return containingStatement.getParent();
}
return containingStatement;
}
private static @NotNull Bag getParentStatementFeatures(@NotNull PsiElement context) {
@@ -0,0 +1,7 @@
class A {
public static void main(String[] args) {
List<String> modules = new ArrayList<String>();
for (int i = modules.s<caret>ize(); i < modules.size(); i++)
}
}
@@ -89,6 +89,13 @@ public class JavaUsagesBySimilarityTest extends JavaCodeInsightFixtureTestCase {
}
}
public void testDeclarationInForStatement() {
myFixture.configureByFile("DeclarationInForStatement.java");
PsiElement elementAtCaret = myFixture.getReferenceAtCaretPosition().getElement();
final Bag features = new JavaUsageSimilarityFeaturesProvider().getFeatures(elementAtCaret);
assertEquals(1, features.get("FOR"));
}
public void testBag() {
final Bag bag = new Bag("a", "b");
assertEquals("a : 1\n" +