0

I'm currently trying to figure out how to "connect" an Xtext Language Server with an EMFCloud.ModelServer instance so each time a client (VS Code extension in my case) saves a custom DSL text file, the Language Server saves the AST as XMI (or JSON). So then later the model can be included in the Model Server workspace and other editors can react to changes on my model (XMI, JSON)

So far I've seen that the current Xtext LS version does nothing with the "textDocument/didSave" notifications:

@Override
public void didSave(DidSaveTextDocumentParams params) {
    // nothing to do
}

I'd like to enhance my LS instance to provide logic to that method and persist the current AST to XMI/JSON.

So far I was able to see that there is a generator class which the doGenerate method is called when the save is triggered on client side. However, taking a look to the call hierarchy of that method, it seems that is called within a "code generation" process. The documentation found for this is always related to other language generation (i.e. Java or c++) and I'm not sure if this would be the right place also because it seems that the file URI is not accessible (as it is in the didSave method of the LS part)

As summary, is there a way to access the semantic model (AST) from the Language Server "didSave" operation?

  • you can also implement a command and do it there. i also dont understand the `the file URI is not accessible` thing. in IGenerator2 you have a resource as a param. you can ask it for its uri – Christian Dietrich Jan 04 '22 at 15:23
  • Hi @ChristianDietrich, thanks for the comment, could you elaborate more about what do you mean with "implement a command"? – Alejandro González Jan 05 '22 at 07:43
  • there is the concept of commands in lsp https://microsoft.github.io/language-server-protocol/specifications/specification-current/#command – Christian Dietrich Jan 05 '22 at 09:55
  • @ChristianDietrich I see. Currently we're not interested in adding new command "calls" in the client server communication. I've added a summary question because is still not clear how to access the current semantic model from the Language Server (instead of doing that from the generator). I've found a very old post http://koehnlein.blogspot.com/2010/06/semantic-model-access-in-xtext.html but seems that the API changed. – Alejandro González Jan 06 '22 at 10:59
  • you can call a do read as it is done at 10s of places in LanguageServerImpl – Christian Dietrich Jan 06 '22 at 11:05

1 Answers1

0

Following the hints provided by Christian Dietrich, the Language Server uses a ProjectManager which is able to retrieve the XtextResource holding the semantic model for a specific model URI (i.e. the URI passed to the server from the editor). Basically:

XtextResource resource = (XtextResource) getWorkspaceManager().getProjectManager(uri).getResource(uri);

In order to get our model from the resource the following method is provided:

EList<EObject> modelObjects = resource.getContents();

At this point we can persist the semantic model via the EMF Model Server (i.e. by creating or modifying an existing model in the Model Server).