如何立即获取插入符号位置周围的方法?
how do I get the method immediately surrounding the caret position?
我对 Eclipse 插件开发还很陌生,因此需要一些帮助。
我有一个项目,我必须在活动编辑器中获取光标的当前位置,然后单击按钮,我将在对话框中显示直接围绕它的方法。我已经尝试了以下方法,到目前为止我只能得到方法的名称,但不能得到我想要的整个源代码。如果我只尝试使用 compilationUnit,我还可以获得活动编辑器的完整源代码。其他问题似乎只需要方法的名称,但我有兴趣获得该方法的完整源代码。有什么办法可以只获取直接围绕光标的方法的源代码吗?
IWorkbenchPage page = window.getActivePage();
IEditorPart editor = page.getActiveEditor();
ITextEditor textEditor = (ITextEditor) page.getActiveEditor();
IJavaElement element = JavaUI.getEditorInputJavaElement(textEditor.getEditorInput());
if (element instanceof ICompilationUnit) {
ITextSelection selection = (ITextSelection) ((JavaEditor) textEditor).getSelectionProvider().getSelection();
IJavaElement selected;
try {
selected = ((ICompilationUnit) element).getElementAt(selection.getOffset());
if (selected != null && selected.getElementType() == IJavaElement.METHOD) {
return (IMethod) selected;
}
MessageDialog.openInformation(
window.getShell(),
editor.getTitle(),
selected +"\n"); //(IMethod)
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
是的,所以我找到了答案。花了一段时间,但我终于想通了。
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
try {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = workbenchWindow.getSelectionService().getSelection();
ITextSelection textSelection;
if (selection instanceof ITextSelection)
textSelection = (ITextSelection) selection;
else
throw new RuntimeException("No text selection");
IEditorInput editorInput = workbenchWindow.getActivePage().getActiveEditor().getEditorInput();
ICompilationUnit compilationUnit = JavaUI.getWorkingCopyManager().getWorkingCopy(editorInput);
IJavaElement elementWithCursorInside = compilationUnit.getElementAt(textSelection.getOffset());
ISourceReference sourceReference;
if (elementWithCursorInside instanceof ISourceReference)
sourceReference = (ISourceReference) elementWithCursorInside;
else
throw new RuntimeException("Not an ISourceReference");
MessageDialog.openInformation(window.getShell(), "Source Code of the Method", sourceReference.getSource());
} catch (Exception e) {
e.printStackTrace();
}
return null;
现在这对我来说似乎很管用。我希望也许我的解决方案可以帮助遇到同样问题的其他人
我对 Eclipse 插件开发还很陌生,因此需要一些帮助。 我有一个项目,我必须在活动编辑器中获取光标的当前位置,然后单击按钮,我将在对话框中显示直接围绕它的方法。我已经尝试了以下方法,到目前为止我只能得到方法的名称,但不能得到我想要的整个源代码。如果我只尝试使用 compilationUnit,我还可以获得活动编辑器的完整源代码。其他问题似乎只需要方法的名称,但我有兴趣获得该方法的完整源代码。有什么办法可以只获取直接围绕光标的方法的源代码吗?
IWorkbenchPage page = window.getActivePage();
IEditorPart editor = page.getActiveEditor();
ITextEditor textEditor = (ITextEditor) page.getActiveEditor();
IJavaElement element = JavaUI.getEditorInputJavaElement(textEditor.getEditorInput());
if (element instanceof ICompilationUnit) {
ITextSelection selection = (ITextSelection) ((JavaEditor) textEditor).getSelectionProvider().getSelection();
IJavaElement selected;
try {
selected = ((ICompilationUnit) element).getElementAt(selection.getOffset());
if (selected != null && selected.getElementType() == IJavaElement.METHOD) {
return (IMethod) selected;
}
MessageDialog.openInformation(
window.getShell(),
editor.getTitle(),
selected +"\n"); //(IMethod)
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
是的,所以我找到了答案。花了一段时间,但我终于想通了。
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
try {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = workbenchWindow.getSelectionService().getSelection();
ITextSelection textSelection;
if (selection instanceof ITextSelection)
textSelection = (ITextSelection) selection;
else
throw new RuntimeException("No text selection");
IEditorInput editorInput = workbenchWindow.getActivePage().getActiveEditor().getEditorInput();
ICompilationUnit compilationUnit = JavaUI.getWorkingCopyManager().getWorkingCopy(editorInput);
IJavaElement elementWithCursorInside = compilationUnit.getElementAt(textSelection.getOffset());
ISourceReference sourceReference;
if (elementWithCursorInside instanceof ISourceReference)
sourceReference = (ISourceReference) elementWithCursorInside;
else
throw new RuntimeException("Not an ISourceReference");
MessageDialog.openInformation(window.getShell(), "Source Code of the Method", sourceReference.getSource());
} catch (Exception e) {
e.printStackTrace();
}
return null;
现在这对我来说似乎很管用。我希望也许我的解决方案可以帮助遇到同样问题的其他人