Eclipse 插件 - 访问编辑器

2023-12-28

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

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

我发现并阅读了有关ID凭证提供者, 文档 and IEditor输入类,它们以某种方式连接,但我无法理解这种连接或如何使用它。因此,如果有人可以解释这种联系,我将非常感激。另外是否还有其他方法可以实现我的目标?


您可以访问IEditorPart通过IWorkbenchPage.

IEditorPart editor =  ((IWorkbenchPage) PlatformUI.getWorkbench()
        .getActiveWorkbenchWindow().getActivePage()).getActiveEditor();

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

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

现在,您可以向Control,例如AKeyAdapter用于监视相应编辑器中发生的所有击键。

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");
        }
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Eclipse 插件 - 访问编辑器 的相关文章

随机推荐