Intellij idea 如何从基于ToolWindowFactory的类访问当前源代码

Intellij idea 如何从基于ToolWindowFactory的类访问当前源代码,intellij-idea,intellij-plugin,Intellij Idea,Intellij Plugin,Ian正在为IntelliJ编写一个检查插件。对于这个插件,我需要从基于ToolWindowFactory的类访问当前的源代码(光标位置等)。在PSIManager中有一种方法,但只适用于Action派生类,而不适用于ToolWindowFactory派生类。有什么想法吗?也许一个com.intellij.openapi.editor.event.CaretListener可以工作?您可以按如下方式注册它以接收所有打开编辑器的事件 com.intellij.openapi.editor.Edit

Ian正在为IntelliJ编写一个检查插件。对于这个插件,我需要从基于ToolWindowFactory的类访问当前的源代码(光标位置等)。在PSIManager中有一种方法,但只适用于Action派生类,而不适用于ToolWindowFactory派生类。有什么想法吗?

也许一个com.intellij.openapi.editor.event.CaretListener可以工作?您可以按如下方式注册它以接收所有打开编辑器的事件

com.intellij.openapi.editor.EditorFactory.getInstance().getEventMulticaster().addCaretListener(myCaretListener);

什么是“当前源代码”?可以打开多个拆分窗格,每个窗格都有自己的编辑器。hmmm,ok。然后最后一次使用。
        CaretListener listener = new CaretAdapter() {
        @Override
        public void caretPositionChanged(CaretEvent e) {
            System.out.println(e.getNewPosition());
        }
    };
    com.intellij.openapi.editor.EditorFactory.getInstance().getEventMulticaster().addCaretListener(listener);