Eclipse插件 - 访问编辑器

3

目前,我正在开发一个eclipse IDE的插件。简而言之,该插件是一款协作实时代码编辑器,其中编辑器为eclipse(类似于Google文档但用于编码且在eclipse上)。这意味着当我安装插件后,我可以使用我的Gmail帐户将eclipse连接到合作伙伴的eclipse上。当我开始在我的机器上编写代码时,我的合作伙伴会看到我所写的内容,反之亦然。

我目前面临的问题是访问eclipse的编辑器。例如,我必须监视活动文档中的所有更改,以便每次发生更改时,其他合作伙伴的IDE都会收到通知。

我找到并阅读了IDcoumentProviderIDocumentIEditorInput类,它们有一定的联系,但我无法理解这种联系或如何使用它。因此,如果有人能够解释这种联系,我将不胜感激。还有其他实现我的目标的方法吗?

1个回答

3
您可以通过 IWorkbenchPage 访问 IEditorPart
IEditorPart editor =  ((IWorkbenchPage) PlatformUI.getWorkbench()
        .getActiveWorkbenchWindow().getActivePage()).getActiveEditor();

从那里,您可以访问各种其他类,包括编辑器的IEditorInput、被该编辑器加载的File或底层GUIControl元素。(请注意,根据编辑器的类型(文本文件、图表等),您可能需要转换为不同的类。)

FileEditorInput input = (FileEditorInput) editor.getEditorInput();
StyledText editorControl = ((StyledText) editor.getAdapter(Control.class));
String path = input.getFile().getRawLocationURI().getRawPath();

现在,您可以向 Control 添加一个监听器,例如一个 KeyAdapter,以监测相应编辑器中发生的所有按键操作。
editorControl.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("Editing in file " + path);
    }
});

或者,如果监测所有按键太过繁琐,你可以在编辑器中注册一个IPropertyListener。该监听器将在编辑器变为'脏状态'或保存时被通知。 propId的含义可以在IWorkbenchPartConstants中找到。

editor.addPropertyListener(new IPropertyListener() {
    @Override
    public void propertyChanged(Object source, int propId) {
        if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
            System.out.println("'Dirty' Property Changed");
        }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接