Get ITextViewer from IEDitorPart (Eclipse)

Eclipse RCP Question

I open the file with

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart editorPart = IDE.openEditor(page, file); 

I also get the document with:

 IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput()); 

I need to get a text viewer for this document (to create a LinkedModeUI), is there a way to do this?

+2
source share
2 answers

The following worked for me:

 IEditorPart editorPart = getSite().getPage().getActiveEditor(); if (editorPart != null) { ITextOperationTarget target = (ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class); if (target instanceof ITextViewer) { ITextViewer textViewer = (ITextViewer)target; // ... } } 
+7
source

1) One document can be opened by several editors. You will have to go through all the editors to search for your file editors. 2) The viewer is encapsulated in the editor. I think the only way is to extend the editor class to add getter. Or redefine it if the viewer is not available to the heirs.

+1
source

Source: https://habr.com/ru/post/1382070/


All Articles