我正在编写IntelliJ IDEA插件,并且尝试从项目窗格导航到文件而没有触发“ autoscrollfromsource”功能(即使已打开)。

我尝试暂时禁用autoscrollfromsource(然后再重新启用它),但是此尝试未成功。

在下面的代码中,似乎需要一点时间来加载文件,并且到那时autoscrollfromsource已经重新启用。

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);
            }
        });
    }
}


有没有一种更好的方法可以导航到我的节点,而不触发源的autoscroll?

感谢您的专业提示:)

更新1

我将代码跟踪到openapi类OpenFileDescriptor,在这里:

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();
}


基本上,我希望能够执行此navigate方法,而不会触发源代码的自动滚动。

最佳答案

如果navigate(false)还不够。您可以侦听FileEditorManager事件,并阻止打开的编辑器成为所选编辑器。

关于java - IntelliJ插件:在不触发AutoScrollFromSource的情况下从项目 View 导航到文件吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20567413/

10-15 11:27