IDEA-179516 "Previous Method" skips methods in anonymous classes

This commit is contained in:
peter
2017-10-11 22:01:07 +02:00
parent 18b13d1479
commit 75ee2ffe59
2 changed files with 36 additions and 18 deletions
@@ -19,34 +19,22 @@ import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
/**
* @author yole
*/
public class JavaMethodNavigationOffsetProvider implements MethodNavigationOffsetProvider {
@Override
@Nullable
public int[] getMethodNavigationOffsets(final PsiFile file, final int caretOffset) {
public int[] getMethodNavigationOffsets(PsiFile file, int caretOffset) {
if (file instanceof PsiJavaFile) {
ArrayList<PsiElement> array = new ArrayList<>();
addNavigationElements(array, file);
return MethodUpDownUtil.offsetsFromElements(array);
return MethodUpDownUtil.offsetsFromElements(SyntaxTraverser.psiTraverser(file).filter(e -> shouldStopAt(e)).toList());
}
return null;
}
private static void addNavigationElements(ArrayList<PsiElement> array, PsiElement element) {
PsiElement[] children = element.getChildren();
boolean stopOnFields = Registry.is("ide.structural.navigation.visit.fields");
for (PsiElement child : children) {
if (child instanceof PsiMethod || child instanceof PsiClass || stopOnFields && child instanceof PsiField) {
array.add(child);
addNavigationElements(array, child);
}
if (element instanceof PsiClass && child instanceof PsiJavaToken && child.getText().equals("}")) {
array.add(child);
}
}
private static boolean shouldStopAt(PsiElement e) {
if (e instanceof PsiMethod || e instanceof PsiClass) return true;
if (e instanceof PsiField) return Registry.is("ide.structural.navigation.visit.fields");
return e instanceof PsiJavaToken && e.getParent() instanceof PsiClass && e.textMatches("}");
}
}
@@ -0,0 +1,30 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.navigation
import com.intellij.codeInsight.navigation.MethodUpDownUtil
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
/**
* @author peter
*/
class JavaMemberNavigationTest extends LightCodeInsightFixtureTestCase {
void "test include anonymous and local classes"() {
def file = myFixture.configureByText('a.java', '''
class Foo {
void bar() {
new Runnable() {
void run() {}
};
class Local {
void localMethod() {}
}
}
}
''')
def offsets = MethodUpDownUtil.getNavigationOffsets(file, 0)
assert file.text.indexOf('run') in offsets
assert file.text.indexOf('Local') in offsets
assert file.text.indexOf('localMethod') in offsets
}
}