eclipse / xtext-core

xtext-core
Eclipse Public License 2.0
117 stars 96 forks source link

[ question / xtext / lsp ] xtext accepting multiple clients #2074

Closed genlike closed 1 year ago

genlike commented 1 year ago

Good afternoon,

I want to implement client concurrency within a single xtext LS process.

I saw that someone tried to do a interface before the ls (1239), what I want to do is directly in the xtext server add the code necessary for the server to be able to handle multiple clients, and not spawn a ls process for each client.

Since the last post was a couple years old, I wanted to know if someone already did something like this.

Or give me some hints on where to start, thank you.

kris7t commented 1 year ago

(Not affiliated with the project, but I had similar challenges to solve.)

One way to achive this is to use the Xtext Web interface instead of LSP. Xtext web supports multiple clients out of the box: https://github.com/eclipse/xtext-web However, Xtext Web might lead to worse performance if used from a browser, because it uses separate REST calls for each editing operation instead of a single WebSocket connection.

Since my project was limited in scope, hacked around this issue by rewriting the Xtext Web client to send specific messages through a WebSocket connection instead of separate REST calls, and the corresponding servlet to accept WebSocket connections. But if you absolutely need full LSP functionality, this might be quite painful to rewrite.

genlike commented 1 year ago

Thanks for taking time in answering.

"But if you absolutely need full LSP functionality, this might be quite painful to rewrite." Unfortunately this is my situation.

But I wanted to know, what this work consists of, so that I can analyse if this is worth investigating, or that this is insensible to do and I should be creating a process of language server for each client connecting.

kris7t commented 1 year ago

You can take a look at how I rewrote the Xtext Web servlet for WebSocket in https://github.com/graphs4value/refinery/blob/43d098e61e546d696e03143d284cc820434ccf22/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/servlet/XtextWebSocketServlet.java but it's not full LSP. In particular, I only handle the highlighting, validation, content assist, and occurrences services, so no refactoring support at all (I'm not even sure Xtext Web supports refactoring).

Here's my attempt at a client but it's honestly a mess: https://github.com/graphs4value/refinery/blob/43d098e61e546d696e03143d284cc820434ccf22/subprojects/frontend/src/xtext/XtextWebSocketClient.ts It'd probably be better to just use Monaco on the client side with LSP, but that would mean running a full LSP backend for each user.

If you're building a language from scratch, Langium might be a simpler solution, because it lets you run the whole LSP server in a WebWorker on the client side. But naturally, that only applies if you don't need to do anything in Java on the server.

genlike commented 1 year ago

Ok the langium option seems the best for me, found some articles in converting a xtext grammar to langium, so I will close this question.

Thank you very much for pointing out for me.