How can I get the selected code in Eclipse?(如何在 Eclipse 中获取选定的代码?)
问题描述
对于我的插件,我正在尝试访问 CompilationUnitEditor 中的选定代码.因此,我向上下文菜单添加了一个贡献并使用以下代码:
For my plugin I'm trying to access the selected code in a CompilationUnitEditor. Therefore I added a contribution to the context menu and use following code:
public class ContextMenuHandler implements IEditorActionDelegate {
private IEditorPart editorPart;
@Override
public void setActiveEditor(IAction action, IEditorPart editorPart) {
this.editorPart = editorPart;
}
@Override
public void run(IAction action) {
JavaUI.getEditorInputJavaElement(editorPart.getEditorInput());
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof TextSelection) {
TextSelection text = (TextSelection) selection;
System.out.println("Text: " + text.getText());
} else {
System.out.println(selection);
}
}
}
现在的问题是方法 selectionChanged(...) 仅在我真正选择某些内容时才被调用,以便我可以复制/粘贴它.但我想访问像这样突出显示的代码元素(这里我想获取IEditorPart")
Now the problem is that the method selectionChanged(...) is only called when i really select something so that I could copy/paste it. But I want to access the Code elements that are highlighted like this (here I would like to get the "IEditorPart")
不幸的是,我不知道我应该寻找什么.
Unfortunately, I have no idea what I should look for.
推荐答案
使用其他答案的输入,我最终得到了以下解决方案:
Using the inputs from the other answers, I ended up with the following solution:
@Override
public void setActiveEditor(IAction action, IEditorPart editorPart) {
((CompilationUnitEditor) editorPart).getViewer().addTextListener(new ITextListener() {
@Override
public void textChanged(TextEvent event) {
selectedText = event.getText();
}
});
}
这篇关于如何在 Eclipse 中获取选定的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Eclipse 中获取选定的代码?
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
