Eclipse Plugin Development: How to access code written in the eclipse editor

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 20:15:14

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!