0
votes

I am developing a new plugin for my dsl language (that I used xtext for developing it). In my plugin, I want that for every *.mydsl file, it's should view graph of vars that define in the file that open.

For that, I need to get some EObject that define in the open file. If I would have some EObject, I can travel the AST to get all the vars I need for the graph.

So, I extends ViewPart class, and I tried to find the current file that open in this code:

  IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

But, I don't know how to get from IWorkbenchPage some EObject type, can someone tell me how I can do it? (or another way that will work)

--------UPDATE:---------

I found it hard to go over the AST of the file by getting EObject (because I wanted to use functions like: EcoreUtil2.getContainerOfType(context, Model) and it's work only in the xtext project.) Instead of finding EObject, is there a way to get the AST of the file? I want all vars of specific types that define in the active file. (The file in the current tab) Thanks!

1
The IWorkbenchPage contains all the views and editors currently open on the page, the page doesn't have an input file. You will need to find the active editor and find a way to get what you want from that. - greg-449
Thanks!! This is what I did IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart activeEditor = page.getActiveEditor(); How can I get the AST of the file? or all the vars of specific types? - RoG

1 Answers

1
votes
IEditorPart activeEditor = page.getActiveEditor();
if (activeEditor instanceof XtextEditor) {
    XtextEditor xtextEditor = (XtextEditor) activeEditor;
    xtextEditor.getDocument().readOnly((XtextResource resource) -> {
        EObject content = resource.getContents().get(0);
        // Do your AST related stuff here
        ...

        // Optionally return a result
        return null;
    });
}