Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xtext DSL embedded editor in a dialog

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

like image 241
user2104666 Avatar asked Nov 03 '22 02:11

user2104666


1 Answers

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.

like image 127
thSoft Avatar answered Dec 20 '22 01:12

thSoft