scalameta / metals-feature-requests

Issue tracker for Metals feature requests
37 stars 4 forks source link

Allow editors to start an sbt session #243

Open eed3si9n opened 2 years ago

eed3si9n commented 2 years ago

Is your feature request related to a problem? Please describe.

  1. Open VS Code in a directory where I would like Metals to connect to sbt server.
  2. Suppose I've already configured to use sbt as the build server.
  3. At this point, Metals have already started an sbt session in a background process.
  4. I want to be able to interact with the sbt session.

Describe the solution you'd like

Given that LSP-capable editors like VS Code and Neovim also have a mini-shell environment, I would like Metals to ask the editor plugins to open the build server when it's an interactive build tool like sbt. (For VS Code code could look something like this - https://github.com/sbt/sbt/blob/e323f1f713251968b3d4ecda8a7f0ed52593713a/vscode-sbt-scala/client/src/extension.ts#L34-L45)

This way, we can let Metals drive majority of the interactions as save-compile, test etc, but when we want to perform some sbt commands it would use the built-in shell mechanism.

Describe alternatives you've considered

sbtn is a decent alternative, so I've tried using that. One of the issue is that sbtn was primarily designed to connect to a session that was also originally opened by sbtn, and the first session captures information regarding the terminal capability. On VS Code specifically this means that I would end up seeing error messages in black-and-white, even though the terminal is capable of colors.

So what I end up actually doing is sbtn shutdown && sbt. This shuts down the sbt session opened by Metals, and I can start a new one that Metals hopefully reconnects.

Additional contex

Previously this was filed as https://github.com/scalameta/metals/issues/2152

Search terms

sbt

tgodzik commented 2 years ago

Thanks for reporting! I think we should do something along https://github.com/sbt/sbt/blob/e323f1f713251968b3d4ecda8a7f0ed52593713a/vscode-sbt-scala/client/src/extension.ts#L34-L45 with the difference being that we should use the built-in Metals way of starting sbt in case sbt is not available.

We could possibly use https://code.visualstudio.com/api/references/vscode-api#TerminalProfileProvider so users would be even able to start multiple sbt terminals. We would only need to ask Metals about the way to start an sbt session.

@eed3si9n what is the best way of connecting to sbt session? Something like sbt -client ?

eed3si9n commented 2 years ago

what is the best way of connecting to sbt session? Something like sbt -client?

For BSP client, the best way to connect to an ongoing session is via following .bsp/sbt.json, which contains absolute path to java and sbt launcher.

For human users, sbt --client would connect to a session.

tgodzik commented 2 years ago

For human users, sbt --client would connect to a session.

So that would be something that we run in VS Code then?

terminal = vscode.window.createTerminal(`sbt`);
terminal.show();
terminal.sendText("sbt --client");
eed3si9n commented 2 years ago

So that would be something that we run in VS Code then?

That's effectively what I mentioned in the considered alternatives section. I can already type sbtn to hook into the sbt session created by Metals. The problem is that I'd get a degraded UX because the session seems to be unaware of the terminal capability.

What I would like instead is for VS Code shell to start the initial sbt session. The benefit of this approach is that the session would be aware of the terminal capability of the VS Code shell, so it would display things in color. Other information regarding the terminal capability might include the width of the window, ANSI X3.64 control (for fancy cursor movements) etc.

On the other hand, when using Metals from Neovim, the terminal may not be capable of color, so in that case we don't want to display color.

tgodzik commented 2 years ago

Thanks for the explanation! It might harder to make the plugin start the session, but should be doable.