I'm making an Eclipse plug-in that requires access to code written in the Eclipse editor. I've followed the process mentioned in the link. Accessing Eclipse editor code But it's showing filepath instead of code in the message box. The getEditorInput() of IEditorEditor class isn't doing what it should do according to the link. Here's my code. Please help me find what i'm doing wrong.
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
IEditorInput input = (IEditorInput) editor.getEditorInput(); // accessing code from eclipse editor
String code = input.toString();
MessageDialog.openInformation(
window.getShell(),
"Project",
code);
return null;
}
You can do this two different ways. This way works regardless of whether the contents are backed by a file on disk or not.
This method gets the text IDocument
from the editor, which is how the contents are stored and accessed for most uses. StyledText
is a widget, and unless you are doing something with Widgets and Controls, it's not the right way in. For that you're going to go from the editor part, through the ITextEditor
interface, and then use the IDocumentProvider
with the current editor input. This is skipping the instanceof
check you'd want to do beforehand, as well as anything you might have to do if this is a page in a MultiPageEditorPart
(there's no standard way for handling those).
org.eclipse.jface.text.IDocument document =
((org.eclipse.ui.texteditor.ITextEditor)editor).
getDocumentProvider().
getDocument(input);
You can get, and modify, the contents through the IDocument
.
来源:https://stackoverflow.com/questions/50719777/eclipse-plugin-development-how-to-access-code-written-in-the-eclipse-editor