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

Answered

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()) {
ApplicationManager.getApplication()
.invokeLater(() -> ((Navigatable) navigationElement).navigate(false), ModalityState.current().defaultModalityState(), myProject.getDisposed());
}
} catch (Exception e) {
e.printStackTrace();
}



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?

TIA.

4 comments
Comment actions Permalink

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

0
Comment actions Permalink

@Jakub
Here is the example Github Project: https://github.com/iffatfatima/TestPlugin . 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.

0
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.

1
Comment actions Permalink

Its resolved. Thank you so much.

0

Please sign in to leave a comment.