Reputation: 33172
I'm currently developing a Eclipse plug-in. When called, my plug-in will read the active (currently opened) source file's data and will do some formatting on the source code and then it will update the results to the same active source file.
Now my questions are:
Upvotes: 1
Views: 1068
Reputation: 6411
You should use Eclipse's infrastructure for editors and documents. I don't think it's a good idea to change files' contents "behind Eclipse's back".
I'm not an expert on this, but I can give you a code sample to get you started:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage activePage = window.getActivePage(); // null check omitted
IEditorPart editorPart = activePage.getActiveEditor(); // null check omitted
ITextEditor textEditor = (ITextEditor) editorPart; // casting check omitted
IDocument currentDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
Check out the org.eclipse.jface.text.IDocument
API. It lets you manipulate the text in the current active source file.
Upvotes: 4