【发布时间】:2015-08-17 05:45:55
【问题描述】:
在 Eclipse 插件上工作,并为我的编辑器做一些功能,我有这个方法可以从编辑器中选择突出显示的文本并将其作为字符串返回:
public String getCurrentSelection() {
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
if (part instanceof ITextEditor) {
final ITextEditor editor = (ITextEditor) part;
ISelection sel = editor.getSelectionProvider().getSelection();
if (sel instanceof TextSelection) {
ITextSelection textSel = (ITextSelection) sel;
return textSel.getText();
}
}
return null;
}
但是现在我希望如果我将光标放在一个单词中,它将选择整个单词并将其作为字符串返回。
除了解析整个编辑器、获取光标位置、搜索左右空格等等的复杂算法之外,还有没有更简单的方法可以将文本(光标所在位置)作为字符串获取?
【问题讨论】:
-
如果您有 Eclipse 的源代码,请查找通过 Ctrl+Alt+Left 调用的命令的实现。
标签: java eclipse eclipse-plugin editor