After calling psielement.navigate(true) , I am unable to switch to another file in the Intellij Editor


I use this code to navigate to a PsiElement in the code.

PsiElement navigationElement = (detectedEntityUI.getPsiElement()).getNavigationElement();
try {
if (navigationElement != null && navigationElement instanceof Navigatable && ((Navigatable) navigationElement).canNavigate()) {
.invokeLater(() -> ((Navigatable) navigationElement).navigate(false), ModalityState.current().defaultModalityState(), myProject.getDisposed());
} catch (Exception e) {

Once I navigate, I cannot manually open another file in the editor, it takes me back to the file that I navigated to programatically earlier. Is there a way around this?


Comment actions Permalink

Can you please provide a minimal reproducible example, i.e. as a GitHub project?

Comment actions Permalink

Here is the example Github Project: . It runs when clicked on Tools > TestPlugin
It reads all PsiMethods from the project and displays their names in a list. If a method name is selected in the list, it navigates to the file in the editor where the method is declared. But if i switch to another file in the same editor, it takes me back to the file i navigated to by click.
I tested it on a Java project.

Comment actions Permalink

You're invoking the navigate method in the renderer instead of some on-cell click event. The getListCellRendererComponent is rendered quite often, i.e., when it loses focus when you change a file in the editor.

Instead, you should bind the selection change event to the cells with JList.addListSelectionListener(ListSelectionListener) and move there the part responsible for navigation.

Comment actions Permalink

Its resolved. Thank you so much.


Please sign in to leave a comment.