IntelliJ plugin: Navigate from Project View to a file without triggering AutoScrollFromSource?

StackOverflow https://stackoverflow.com/questions/20567413

Domanda

I'm writing an IntelliJ IDEA plugin and I am trying to navigate from the project pane to a file without triggering "autoscrollfromsource" function (even if it is turned on).

I tried temporarily disabling autoscrollfromsource (and re-enabling it afterwards) but this attempt was not successful.

In the code below, it seems it takes a moment for the file to load, and by that time autoscrollfromsource is already re-enabled.

private void navigateWithoutAutoscrollFromSource(final ProjectViewImpl projectView, BasePsiNode<? extends PsiElement> node) {
    boolean wasAutoScrollFromSource = projectView.isAutoscrollFromSource(PROJECT_PANE);
    if (wasAutoScrollFromSource) {
        projectView.setAutoscrollFromSource(false, PROJECT_PANE);
    }

    // this navigation here should NOT trigger autoscrollfromsource!!
    node.navigate(true);

    if (wasAutoScrollFromSource) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                projectView.setAutoscrollFromSource(true, PROJECT_PANE);
            }
        });
    }
}

Is there a better way to navigate to my node, without triggering autoscrollfromsource?

Thanks for any pro tips :)

Update 1

I trace the code to the openapi class OpenFileDescriptor, here:

public void navigate(boolean requestFocus) {
    if (!canNavigate()) {
        throw new IllegalStateException("Navigation is not possible with null project");
    }

    if (!myFile.isDirectory() && navigateInEditor(myProject, requestFocus)) return;

    navigateInProjectView();
}

Basically, I would like to be able to execute this navigate method, without it triggering autoscroll from source.

È stato utile?

Soluzione

If navigate(false) isn't enough. You can listen for FileEditorManager events, and stop the editor you open from becoming the selected editor.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top