-->

Xtext DSL embedded editor in a dialog

2020-07-16 08:56发布

问题:

I am new to xtext, and i have created a DSL using xtext and i have generated the artifacts, which has generated the editor which has many features like content assist and syntax coloring now the problem is i want to embed the editor inside a dialog.

For achieving this im using EmbeddedEditor, i am able to get the embedded editor and place it in the dialog, but the embedded editor is not displaying the contents of the file.

The file C:/Eclipse_Work/workspace/runtime_workspace/apa/ex.mydsl contains:

import com.ex.test;
entity{
 element a;
}

The code in the createcontrol() of dialog is :

    IEditedResourceProvider resourceProvider=new IEditedResourceProvider() {
        
        @Override
        public XtextResource createResource() {
            try {

                Resource resource = resourceSet.createResource(URI.createURI("C:/Eclipse_Work/workspace/runtime_workspace/apa/ex.mydsl"));
                XtextResource resource2=(XtextResource)resource;
                
                return (XtextResource) resource;
            } catch (Exception e) {
                return null;
            }
        }
    };
    
    MyDslActivator activator = MyDslActivator.getInstance();
    Injector injector = activator
            .getInjector(MyDslActivator.COM_APAMA_STUDIO_QUERY_EXT_MYDSL);
    
    @SuppressWarnings("restriction")
    EmbeddedEditorFactory factory = injector.getInstance(EmbeddedEditorFactory.class);
   EmbeddedEditor handle= factory.newEditor(resourceProvider).withParent(
            composite);
   
   EmbeddedEditorModelAccess partialEditor= handle.createPartialEditor();

   
   handle.getViewer().getControl().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 0));  

When i run the project the dialog opens with a editor area but it is not displaying the code present in ex.mydsl, the editor is empty.

Please tell me how to show the code in the embedded editor

回答1:

You have to specify the editor's initial contents as the editablePart parameter of createPartialEditor(String prefix, String editablePart, String suffix, boolean insertLineBreaks). To obtain your XtextResource's contents as text, save it to a ByteArrayOutputStream, then convert it to a string using toString.