How to replace XtextDocument in the editor with a projection of it [message #1007535] |
Wed, 06 February 2013 10:55 |
Marco Naddeo Messages: 62 Registered: November 2012 |
Member |
|
|
Hi
Given an editor opened for my DSL language, I can generate a ProjectionDocument which is a projection of the master document (that opened in the editor), and where in particular I remove some portions of text.
Now, I'd like to "substitute" the master document in the editor with its projection, making it editable for the user; in other words, I want to give the user an action which limits his possible editing of the document to the projected text.
I tried implementing a UI action with this code:
public class MyHandler extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
XtextEditor xtextEditor = (XtextEditor) HandlerUtil.getActiveEditor(event);
IXtextDocument masterDocument = xtextEditor.getDocument();
ProjectionDocumentManager manager = new ProjectionDocumentManager();
ProjectionDocument projectedDocument = (ProjectionDocument) manager.createSlaveDocument(masterDocument);
try {
projectedDocument.addMasterDocumentRange(0, masterDocument.getLength());
} catch (BadLocationException e1) {
e1.printStackTrace();
}
//...remove some portions of text from projected document using removeMasterDocumentRange...
ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
sourceViewer.setDocument(projectedDocument);
return null;
}
}
This code generates a ClassCastException:
org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.jface.text.projection.ProjectionDocument cannot be cast to org.eclipse.xtext.ui.editor.model.XtextDocument
Taking a look to the API, I saw that in fact XtextDocument and ProjectionDocument are independent subclasses of AbstractDocument. So, a XtextDocument is not a ProjectionDocument and vice versa, but surely a XtextEditor supports folding and projection in general: I see that XtextSourceViewer extends ProjectionViewer.
I also tried to use the static method "get" in XtextDocumentUtil to obtain a XtextDocument given a ProjectionDocument, but in this way I still see the complete document in the editor!
Please help me! Have you any code example or suggestions?
Bye,
Marco
|
|
|
Re: How to replace XtextDocument in the editor with a projection of it [message #1007976 is a reply to message #1007535] |
Fri, 08 February 2013 07:22 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Marco,
you may want to hook into
org.eclipse.xtext.ui.editor.model.XtextDocumentProvider.createDocument(Object)
Your projection document will have to implement IXtextDocument. Your
handler could then use the projection-enabled document to trigger
whatever you try to do there.
Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Am 06.02.13 11:55, schrieb Marco Naddeo:
> Hi :)
>
> Given an editor opened for my DSL language, I can generate a
> ProjectionDocument which is a projection of the master document (that
> opened in the editor), and where in particular I remove some portions of
> text.
>
> Now, I'd like to "substitute" the master document in the editor with its
> projection, making it editable for the user; in other words, I want to
> give the user an action which limits his possible editing of the
> document to the projected text.
>
> I tried implementing a UI action with this code:
>
>
> public class MyHandler extends AbstractHandler implements IHandler {
>
> @Override
> public Object execute(ExecutionEvent event) throws ExecutionException {
> XtextEditor xtextEditor = (XtextEditor)
> HandlerUtil.getActiveEditor(event);
> IXtextDocument masterDocument = xtextEditor.getDocument();
> ProjectionDocumentManager manager = new ProjectionDocumentManager();
> ProjectionDocument projectedDocument = (ProjectionDocument)
> manager.createSlaveDocument(masterDocument);
>
> try {
> projectedDocument.addMasterDocumentRange(0,
> masterDocument.getLength());
> } catch (BadLocationException e1) {
> e1.printStackTrace();
> }
>
> //...remove some portions of text from projected document using
> removeMasterDocumentRange...
>
> ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
> sourceViewer.setDocument(projectedDocument);
>
> return null;
> }
>
> }
>
> This code generates a ClassCastException:
>
> org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException:
> org.eclipse.jface.text.projection.ProjectionDocument cannot be cast to
> org.eclipse.xtext.ui.editor.model.XtextDocument
>
> Taking a look to the API, I saw that in fact XtextDocument and
> ProjectionDocument are independent subclasses of AbstractDocument. So, a
> XtextDocument is not a ProjectionDocument and vice versa, but surely a
> XtextEditor supports folding and projection in general: I see that
> XtextSourceViewer extends ProjectionViewer.
>
> I also tried to use the static method "get" in XtextDocumentUtil to
> obtain a XtextDocument given a ProjectionDocument, but in this way I
> still see the complete document in the editor!
>
> Please help me! Have you any code example or suggestions? :roll:
> Bye,
> Marco
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03730 seconds