From dc33e2c903ca10725d209219d181628e05df8ea8 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:37:36 +0100 Subject: [PATCH 1/3] fix: select Socket, with a hint, and warn of stdio, for a language server whose template lists only socket modes A template can say that its language server talks LSP only over a socket, by listing only socket modes in `transports`, but nothing used that: no hint, no warning for stdio, and without a `socket` the New Language Server dialog selected stdio, over which such a language server can't talk. - A grey hint below the transport, "This language server talks LSP only over a socket.", in the dialog and on the settings page of a language server created from a bundled template. - Stdio gives the warning "The template says this language server talks LSP only over a socket" on the stdio button. It is asWarning().withOKEnabled(), so OK and Apply still save. - Without a `socket`, the dialog selects Socket for it, in the first socket mode of `transports`, with an Auto port (attach: no port yet). - Unchanged: a template without `transports`, or with stdio and a socket mode; a template's own `socket`; and saved settings, which keep their transport (a saved stdio shows with the warning, and Apply saves it). Part of #1947. --- docs/UserDefinedLanguageServerTemplate.md | 24 ++++-- .../launching/ui/NewLanguageServerDialog.java | 19 ++++- .../launching/TransportSupport.java | 14 ++++ .../settings/ui/LanguageServerPanel.java | 5 +- .../lsp4ij/settings/ui/TransportFields.java | 34 +++++--- .../messages/LanguageServerBundle.properties | 2 + .../NewLanguageServerDialogTransportTest.java | 77 +++++++++++++++++ .../LanguageServerViewTransportTest.java | 83 +++++++++++++++++-- .../settings/ui/TransportFieldsTest.java | 51 ++++++++++++ 9 files changed, 280 insertions(+), 29 deletions(-) diff --git a/docs/UserDefinedLanguageServerTemplate.md b/docs/UserDefinedLanguageServerTemplate.md index c9eb41c5c..a6bfd1c3e 100644 --- a/docs/UserDefinedLanguageServerTemplate.md +++ b/docs/UserDefinedLanguageServerTemplate.md @@ -133,7 +133,7 @@ This language server talks LSP over its standard input and output only, so the t | `env` | the environment variables of the `Server` tab: `variables`, an object of names and values, and `includeSystemEnvironmentVariables`, `true` to pass the IDE's environment variables too | without `env`, no variables, and the IDE's are passed; `includeSystemEnvironmentVariables` is `false` when `env` doesn't set it | | `fileTypeMappings` | the files of the language server: each entry has a `fileType`, with the `name` of an IntelliJ file type, file name `patterns`, or both, and the `languageId` of the files | none | | `languageMappings` | the files of the language server by IntelliJ language: each entry has the `language`, an IntelliJ language id, and the `languageId` of the files | none | -| `socket` | how LSP4IJ talks to a language server over a TCP socket, see [Socket](#socket) | the standard input and output | +| `socket` | how LSP4IJ talks to a language server over a TCP socket, see [Socket](#socket) | the standard input and output, or the first mode of `transports` when it lists only socket modes | | `transports` | the transports which the language server supports, see [Transports](#transports) | none | | `expandConfiguration` | `true` checks `Expand JSON when configuration is sent to the server`: a name with dots in the `Configuration`, as `"css.validate": true`, is sent as nested objects, `{"css": {"validate": true}}` | `false` | | `disablePromotionFor` | plugin ids: while one of these plugins is installed, the banner which suggests the template for a file it supports doesn't show, as the Clojure LSP template does for `com.github.clojure-lsp` | none | @@ -166,9 +166,9 @@ placeholders `${instanceRoot}` and `${port}`, see [Macro syntax](./UserDefinedLa #### Socket -`socket` is optional: without it, LSP4IJ talks to the language server over its standard input and output. A language -server which talks LSP over a TCP socket instead has a `socket`, whose `mode` is one of the -[transports](./UserDefinedLanguageServer.md#transport): +`socket` is optional: without it, LSP4IJ talks to the language server over its standard input and output, unless its +[`transports`](#transports) lists only socket modes. A language server which talks LSP over a TCP socket instead has a +`socket`, whose `mode` is one of the [transports](./UserDefinedLanguageServer.md#transport): * `attach`: LSP4IJ attaches to a language server which runs already and listens on `host` (the loopback address by default) and `port`. @@ -206,13 +206,19 @@ does instead: `transports` is optional. A template can list the transports which its language server supports, in `transports`, each with its `mode` (`stdio` or one of the socket modes above) and the `argument` of the command for it. The New Language Server dialog then helps with the transport: for a language server which lists only -`stdio`, a hint says that it talks LSP only over its standard input and output, and for one which lists a socket mode, -a hint below the socket modes names the mode and the argument. Every transport can still be chosen, as the template may -be the starting point of a language server of your own which does talk over a socket: Socket, for a language server -which lists only `stdio`, gives the warning "The template says this language server talks LSP only over its standard -input and output", which blocks neither OK nor Apply in the settings of a language server created from the template. +`stdio`, a hint says that it talks LSP only over its standard input and output, for one which lists only socket modes, a +hint says that it talks LSP only over a socket, and for one which lists a socket mode, a hint below the socket modes +names the mode and the argument. Every transport can still be chosen, as the template may be the starting point of a +language server of your own which does talk over a socket: Socket, for a language server which lists only `stdio`, +gives the warning "The template says this language server talks LSP only over its standard input and output", which +blocks neither OK nor Apply in the settings of a language server created from the template. Without `transports`, no hint shows. +A language server which lists only socket modes is the other way round: `Standard input and output (stdio)` gives the +warning "The template says this language server talks LSP only over a socket", which blocks neither OK nor Apply +either. Without a `socket`, the New Language Server dialog selects `Socket` for it, in the first mode of the list, with +an `Auto` port, or, for `attach`, no port yet. + | Field | Value | Required | |------------|-----------------------------------------------------------------------|----------| | `mode` | `stdio`, `attach`, `startAndConnect` or `startAndAccept` | yes | diff --git a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java index 44aa82af5..c12ca31d8 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java @@ -33,7 +33,9 @@ import com.redhat.devtools.lsp4ij.templates.ServerMappingSettings; import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate; import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; +import com.redhat.devtools.lsp4ij.server.definition.launching.TransportSupport; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; +import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedTransport; import com.redhat.devtools.lsp4ij.settings.ui.LanguageServerPanel; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -268,7 +270,7 @@ public void loadFromTemplate(@NotNull LanguageServerTemplate template) { String command = getCommandLine(template); languageServerPanel.setCommandLine(command); languageServerPanel.setEnvData(template.getEnvData()); - languageServerPanel.setTransport(template.getTransport()); + languageServerPanel.setTransport(getTransport(template)); languageServerPanel.setSupportedTransports(template.getSupportedTransports()); templateProblems = template.getProblems(); @@ -301,6 +303,21 @@ public void loadFromTemplate(@NotNull LanguageServerTemplate template) { updateHelpButton(); } + /** + * Returns the transport which a language server created from the template starts with: the template's socket, and + * without one, for a language server which the template says talks LSP only over a socket, the first socket mode + * which it lists, on a port which LSP4IJ chooses, or which the user gives for a language server which runs already. + */ + private static @NotNull UserDefinedTransport getTransport(@NotNull LanguageServerTemplate template) { + var transport = template.getTransport(); + var supportedTransports = template.getSupportedTransports(); + if (transport.mode() != UserDefinedTransport.Mode.STDIO + || !TransportSupport.talksOnlyOverASocket(supportedTransports)) { + return transport; + } + return new UserDefinedTransport(supportedTransports.get(0).mode(), null, 0); + } + @Override public JComponent getPreferredFocusedComponent() { return this.languageServerPanel.getServerName(); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/TransportSupport.java b/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/TransportSupport.java index 81cf3d43e..50ba8b5f9 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/TransportSupport.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/TransportSupport.java @@ -13,6 +13,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + /** * A transport which a language server supports, as its template lists it: the mode, and the argument of the command * for it, such as {@code --stdio}, or {@code --socket=${port}} for a socket. @@ -21,4 +23,16 @@ * @param argument the argument of the command for this mode, or null if the template names none. */ public record TransportSupport(@NotNull UserDefinedTransport.Mode mode, @Nullable String argument) { + + /** + * Returns true if the given transports, as a template lists them, are all sockets: the template says that its + * language server talks LSP only over a socket. A template which lists no transports doesn't say. + * + * @param supportedTransports the transports which a template lists, empty if it lists none. + * @return true if the template lists transports, and none of them is the standard input and output. + */ + public static boolean talksOnlyOverASocket(@NotNull List supportedTransports) { + return !supportedTransports.isEmpty() && supportedTransports.stream() + .noneMatch(transport -> transport.mode() == UserDefinedTransport.Mode.STDIO); + } } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java index 800841336..c3e1ef8f8 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java @@ -818,8 +818,9 @@ public void setWorkspaceFolderStrategyConfiguration(@Nullable String workspaceFo /** * Returns every problem of the fields this panel shows, each on its field: an empty server name, no command for a * language server which LSP4IJ starts, a port which can't be used, and content which isn't valid JSON. A socket for - * a language server whose template lists only the standard input and output is a warning. The New Language Server - * dialog and the settings page both check with it. + * a language server whose template lists only the standard input and output is a warning, and so is the standard + * input and output for one whose template lists only socket modes. The New Language Server dialog and the settings + * page both check with it. * * @return every problem of the shown fields, empty if there is none. */ diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java index e0f51c28f..903e8bcb8 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java @@ -66,6 +66,7 @@ final class TransportFields { private final JBLabel portHint = new JBLabel(message("language.server.socket.port.hint", UserDefinedTransport.PORT_MACRO)); private final JBLabel attachPortHint = new JBLabel(message("language.server.socket.port.attach.hint")); private final JBLabel stdioOnlyHint = new JBLabel(message("language.server.transport.stdio.only")); + private final JBLabel socketOnlyHint = new JBLabel(message("language.server.transport.socket.only")); private final JBLabel templateSocketHint = new JBLabel(); private final JPanel transportRow = row(GAP, stdio, socket); // Auto and Fixed have a row of their own, whose gaps hide with them, so the port lines up with the host without them @@ -84,6 +85,7 @@ final class TransportFields { portHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); attachPortHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); stdioOnlyHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); + socketOnlyHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); templateSocketHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); typedPortCommits(); setTransport(UserDefinedTransport.STDIO); @@ -106,6 +108,7 @@ final class TransportFields { void addTo(@NotNull FormBuilder builder) { builder.addLabeledComponent(message("language.server.transport"), transportRow); builder.addComponent(stdioOnlyHint, 0); + builder.addComponent(socketOnlyHint, 0); builder.addLabeledComponent(socketLabel, attach); builder.addLabeledComponent(startAndConnectLabel, startAndConnect); builder.addLabeledComponent(startAndAcceptLabel, startAndAccept); @@ -127,9 +130,10 @@ void addChangeListener(@NotNull Runnable listener) { /** * Shows what the template of the language server says of its transports: a hint says that a language server talks - * only over its standard input and output, and another names the mode, and the argument, of a socket which it - * supports. Every transport can still be chosen, as a template is also the starting point of a language server of - * one's own; a socket for a language server which talks only over its standard input and output is a warning of + * only over its standard input and output, or only over a socket, and another names the mode, and the argument, of + * a socket which it supports. Every transport can still be chosen, as a template is also the starting point of a + * language server of one's own; a socket for a language server which talks only over its standard input and output, + * and the standard input and output for one which talks only over a socket, are warnings of * {@link #validateForTemplate()}. Without a word from the template, no hint shows. * * @param supportedTransports the transports which the template lists, empty if it lists none. @@ -249,19 +253,26 @@ ValidationInfo validateForServerInstances() { /** * Returns a warning when a socket is chosen for a language server whose template lists only the standard input and - * output. It is a warning, which doesn't block OK or Apply: the template may be the starting point of a language - * server of one's own, which does talk over a socket, and saved settings with a socket keep working. + * output, or the standard input and output for one whose template lists only socket modes. It is a warning, which + * doesn't block OK or Apply: the template may be the starting point of a language server of one's own, which does + * talk over the other transport, and saved settings with it keep working. * - * @return the warning, or null for stdio, or for a template which lists a socket mode or no transports. + * @return the warning, or null for a transport which the template lists, or for a template which lists both stdio + * and a socket mode, or no transports. */ @Nullable ValidationInfo validateForTemplate() { - if (!socket.isSelected() || !listsOnlyStdio()) { - return null; + if (socket.isSelected() && listsOnlyStdio()) { + return new ValidationInfo(message("language.server.validation.socket.stdio.only"), socket) + .asWarning() + .withOKEnabled(); + } + if (stdio.isSelected() && TransportSupport.talksOnlyOverASocket(supportedTransports)) { + return new ValidationInfo(message("language.server.validation.stdio.socket.only"), stdio) + .asWarning() + .withOKEnabled(); } - return new ValidationInfo(message("language.server.validation.socket.stdio.only"), socket) - .asWarning() - .withOKEnabled(); + return null; } /** @@ -295,6 +306,7 @@ private void update() { .filter(transport -> transport.mode() != UserDefinedTransport.Mode.STDIO) .findFirst(); stdioOnlyHint.setVisible(listsOnlyStdio()); + socketOnlyHint.setVisible(TransportSupport.talksOnlyOverASocket(supportedTransports)); socketSupport.ifPresent(transport -> templateSocketHint.setText(transport.argument() != null ? message("language.server.socket.template.hint", transport.argument(), modeButton(transport.mode()).getText()) : message("language.server.socket.template.mode.hint", modeButton(transport.mode()).getText()))); diff --git a/src/main/resources/messages/LanguageServerBundle.properties b/src/main/resources/messages/LanguageServerBundle.properties index 8225b2c6c..0c8582a37 100644 --- a/src/main/resources/messages/LanguageServerBundle.properties +++ b/src/main/resources/messages/LanguageServerBundle.properties @@ -23,6 +23,7 @@ language.server.transport=Transport: language.server.transport.stdio=Standard input and output (stdio) language.server.transport.socket=Socket language.server.transport.stdio.only=This language server talks LSP only over its standard input and output. +language.server.transport.socket.only=This language server talks LSP only over a socket. language.server.socket=Socket: language.server.socket.attach=Attach to a running language server language.server.socket.startAndConnect=Start the language server, LSP4IJ connects to it @@ -47,6 +48,7 @@ language.server.validation.socket.port.fixed=A fixed port can't be 0: choose Aut language.server.validation.socket.port.macro=With an Auto port, the command must take the port as {0} language.server.validation.stdio.port.macro=The command takes {0}, but stdio gives no port, so the language server gets {0} as it is. Did you mean Socket? language.server.validation.socket.stdio.only=The template says this language server talks LSP only over its standard input and output +language.server.validation.stdio.socket.only=The template says this language server talks LSP only over a socket language.server.instances.noInstance=No instance of this language server serves {0}: the file has no root in its workspace folder configuration language.server.installer.transport.stdio=The installer set a command without ${port}, so this language server now runs over its standard input and output instead of a socket. To use a socket again, choose Socket in its settings, with a command which takes ${port}. language.server.validation.socket.instances.attach=Several language server instances can't share one running language server: let LSP4IJ start it, or remove serverInstances from the workspace folders diff --git a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java index a2768d905..34d9d7709 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java @@ -48,6 +48,10 @@ public class NewLanguageServerDialogTransportTest extends BasePlatformTestCase { // The transports of the TypeScript template private static final List STDIO_ONLY = List.of( new TransportSupport(UserDefinedTransport.Mode.STDIO, "--stdio")); + // The transports of a language server which talks only over a socket, which it listens on or connects to + private static final List SOCKET_ONLY = List.of( + new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}")); public void testTheTransportWhichTheUserChoosesSwapsItsArgumentInTheCommand() { var dialog = dialog(template("node css.js --stdio", UserDefinedTransport.STDIO, CSS)); @@ -269,6 +273,79 @@ public void testTheSocketOfATemplateWhichListsOnlyStdioIsKeptWithAWarning() { } } + public void testATemplateWhichListsOnlySocketModesStartsInTheFirstOfThem() { + var dialog = dialog(template("server --port ${port}", UserDefinedTransport.STDIO, SOCKET_ONLY)); + try { + assertTrue(radio(dialog, "language.server.transport.socket").isSelected()); + assertTrue(radio(dialog, "language.server.socket.startAndConnect").isSelected()); + assertTrue("LSP4IJ chooses the port", radio(dialog, "language.server.socket.port.auto").isSelected()); + assertEquals("the command is the template's", "server --port ${port}", command(dialog)); + assertEmpty(dialog.doValidateAll()); + } finally { + dialog.close(DialogWrapper.CANCEL_EXIT_CODE); + } + + // The template's own socket stays, and a template which lists stdio too starts with it, as before + var withASocket = dialog(template("", RUNNING, List.of( + new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"), + new TransportSupport(UserDefinedTransport.Mode.ATTACH, null)))); + try { + assertTrue(radio(withASocket, "language.server.socket.attach").isSelected()); + } finally { + withASocket.close(DialogWrapper.CANCEL_EXIT_CODE); + } + var alsoStdio = dialog(template("node css.js --stdio", UserDefinedTransport.STDIO, CSS)); + try { + assertTrue(radio(alsoStdio, "language.server.transport.stdio").isSelected()); + } finally { + alsoStdio.close(DialogWrapper.CANCEL_EXIT_CODE); + } + } + + public void testTheLanguageServerAddedFromATemplateWhichListsOnlySocketModesHasTheSocket() { + var dialog = dialog(template("server --port ${port}", UserDefinedTransport.STDIO, SOCKET_ONLY)); + dialog.doOKAction(); + + var added = LanguageServersRegistry.getInstance().getServerDefinitions().stream() + .filter(definition -> definition instanceof UserDefinedLanguageServerDefinition + && "Socket server".equals(definition.getDisplayName())) + .map(UserDefinedLanguageServerDefinition.class::cast) + .findFirst() + .orElseThrow(); + try { + assertEquals(new UserDefinedTransport(UserDefinedTransport.Mode.START_AND_CONNECT, null, 0), added.getTransport()); + } finally { + LanguageServersRegistry.getInstance().removeServerDefinition(getProject(), added); + } + } + + public void testStdioForATemplateWhichListsOnlySocketModesIsAWarningWhichDoesNotBlockOk() { + var dialog = dialog(template("server --port ${port}", UserDefinedTransport.STDIO, SOCKET_ONLY)); + try { + var stdio = radio(dialog, "language.server.transport.stdio"); + assertTrue("the template may be the start of a language server which does talk over stdio", + stdio.isEnabled()); + + click(dialog, "language.server.transport.stdio"); + String message = LanguageServerBundle.message("language.server.validation.stdio.socket.only"); + List problems = dialog.doValidateAll(); + // On stdio, and listed below the fields, next to the warning of the command, which still takes ${port}, as + // the template has no argument for stdio to swap in + String portWarning = LanguageServerBundle.message("language.server.validation.stdio.port.macro", + UserDefinedTransport.PORT_MACRO); + assertEquals(List.of(portWarning, message, portWarning, message), + problems.stream().map(problem -> problem.message).toList()); + assertSame(stdio, problems.get(1).component); + assertNull(problems.get(3).component); + assertTrue("OK stays enabled", problems.stream().allMatch(problem -> problem.warning && problem.okEnabled)); + + click(dialog, "language.server.transport.socket"); + assertEmpty(dialog.doValidateAll()); + } finally { + dialog.close(DialogWrapper.CANCEL_EXIT_CODE); + } + } + public void testAStdioCommandWhichTakesThePortIsAWarningWhichDoesNotBlockOk() { var dialog = dialog(template("node server.js --socket=${port}", UserDefinedTransport.STDIO)); try { diff --git a/src/test/java/com/redhat/devtools/lsp4ij/settings/LanguageServerViewTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/settings/LanguageServerViewTransportTest.java index ec53b7722..1b09800b9 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/settings/LanguageServerViewTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/settings/LanguageServerViewTransportTest.java @@ -18,8 +18,10 @@ import com.redhat.devtools.lsp4ij.LanguageServerBundle; import com.redhat.devtools.lsp4ij.LanguageServersRegistry; import com.redhat.devtools.lsp4ij.launching.UserDefinedLanguageServerSettings; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate; import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; import com.redhat.devtools.lsp4ij.mock.MockLanguageServerDefinition; +import com.redhat.devtools.lsp4ij.server.definition.launching.TransportSupport; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedTransport; import org.jetbrains.annotations.NotNull; @@ -37,6 +39,8 @@ public class LanguageServerViewTransportTest extends BasePlatformTestCase { private static final UserDefinedTransport ACCEPT = new UserDefinedTransport(UserDefinedTransport.Mode.START_AND_ACCEPT, null, 0); // typescript-language-server talks only over stdio private static final String TYPESCRIPT_TEMPLATE = "typescript-language-server"; + // No bundled template lists only socket modes, so the tests add one + private static final String SOCKET_ONLY_TEMPLATE = "socket-only-test-server"; private UserDefinedLanguageServerDefinition definition; private LanguageServerView view; @@ -256,6 +260,43 @@ public void testASocketChosenForALanguageServerWhoseTemplateListsOnlyStdioIsAWar } } + public void testASavedStdioOfALanguageServerWhoseTemplateListsOnlySocketModesIsShownWithAWarningAndSaved() + throws ConfigurationException { + var fromTemplate = fromSocketOnlyTemplate("node my-server.js --stdio", UserDefinedTransport.STDIO); + var page = new LanguageServerView(fromTemplate, null, false, getProject()); + try { + page.reset(); + + assertSame(UserDefinedTransport.STDIO, page.getTransport()); + assertFalse(page.isModified()); + var stdio = radio(page, "language.server.transport.stdio"); + assertTrue(stdio.isSelected()); + assertTrue(label(page, "language.server.transport.socket.only").isVisible()); + var problems = page.validateFields(); + assertEquals(1, problems.size()); + assertEquals(LanguageServerBundle.message("language.server.validation.stdio.socket.only"), + problems.get(0).message); + assertSame(stdio, problems.get(0).component); + assertTrue(problems.get(0).warning); + + // a warning, which doesn't block Apply + page.setCommandLine("node my-server.js --stdio --log"); + page.apply(); + + assertSame(UserDefinedTransport.STDIO, settings(fromTemplate).getTransport()); + assertEquals("node my-server.js --stdio --log", settings(fromTemplate).getCommandLine()); + + // Socket, in the mode which the template lists, gives none + radio(page, "language.server.transport.socket").doClick(); + assertEquals(new UserDefinedTransport(UserDefinedTransport.Mode.START_AND_CONNECT, null, 0), page.getTransport()); + page.setCommandLine("node my-server.js --port ${port}"); + assertEmpty(page.validateFields()); + } finally { + Disposer.dispose(page); + LanguageServersRegistry.getInstance().removeServerDefinition(getProject(), fromTemplate); + } + } + public void testASocketOfALanguageServerWithoutATemplateIsNoWarning() { assertEquals(ACCEPT, view.getTransport()); assertEmpty(view.validateFields()); @@ -267,12 +308,7 @@ public void testASocketOfALanguageServerWithoutATemplateIsNoWarning() { */ private @NotNull UserDefinedLanguageServerDefinition fromStdioOnlyTemplate( @NotNull String command, @NotNull UserDefinedTransport transport) { - // The bundled templates are read from the jar of LSP4IJ in the test sandbox, which is outside the roots that a - // test may read when the checkout isn't in the user's home directory - String templates = PathManager.getResourceRoot(LanguageServerTemplateManager.class, "/templates/lsp"); - assertNotNull(templates); - VfsRootAccess.allowRootAccess(getTestRootDisposable(), templates); - var template = LanguageServerTemplateManager.getInstance().findTemplateById(TYPESCRIPT_TEMPLATE); + var template = templateManager().findTemplateById(TYPESCRIPT_TEMPLATE); assertNotNull(template); assertTrue("the template lists only stdio", template.getSupportedTransports().stream() .allMatch(support -> support.mode() == UserDefinedTransport.Mode.STDIO)); @@ -284,6 +320,41 @@ public void testASocketOfALanguageServerWithoutATemplateIsNoWarning() { return fromTemplate; } + /** + * Adds a language server created from a template which lists only socket modes, with the given command and + * transport, and returns it. The template is one of the bundled ones, among which the settings find it, until the + * test ends. + */ + private @NotNull UserDefinedLanguageServerDefinition fromSocketOnlyTemplate( + @NotNull String command, @NotNull UserDefinedTransport transport) { + var template = new LanguageServerTemplate(); + template.setId(SOCKET_ONLY_TEMPLATE); + template.setName("Socket-only server"); + template.setSupportedTransports(List.of( + new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"))); + var bundled = templateManager().getTemplates(); + bundled.add(template); + Disposer.register(getTestRootDisposable(), () -> bundled.remove(template)); + var fromTemplate = new UserDefinedLanguageServerDefinition(UUID.randomUUID().toString(), SOCKET_ONLY_TEMPLATE, + "My socket server", null, null, command, null, Map.of(), false, null, false, null, null, null, null, + null, null); + fromTemplate.setTransport(transport); + LanguageServersRegistry.getInstance().addServerDefinition(getProject(), fromTemplate, List.of()); + return fromTemplate; + } + + /** + * Returns the manager of the bundled templates. + */ + private @NotNull LanguageServerTemplateManager templateManager() { + // The bundled templates are read from the jar of LSP4IJ in the test sandbox, which is outside the roots that a + // test may read when the checkout isn't in the user's home directory + String templates = PathManager.getResourceRoot(LanguageServerTemplateManager.class, "/templates/lsp"); + assertNotNull(templates); + VfsRootAccess.allowRootAccess(getTestRootDisposable(), templates); + return LanguageServerTemplateManager.getInstance(); + } + private static @NotNull JBRadioButton radio(@NotNull LanguageServerView page, @NotNull String key) { String text = LanguageServerBundle.message(key); return UIUtil.findComponentsOfType(page.getComponent(), JBRadioButton.class).stream() diff --git a/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java b/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java index e59961321..9f77d58ee 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java @@ -207,6 +207,57 @@ public void testATemplateWhichListsOnlyASocketGivesNoWarningForTheSocket() { assertNull(fields.validateForTemplate()); } + public void testStdioForALanguageServerWhichTalksOnlyOverASocketIsAWarningWhichDoesNotBlockOk() { + assertFalse(label("language.server.transport.socket.only").isVisible()); + + fields.setSupportedTransports(List.of(new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"))); + + // the template may be the starting point of a language server of one's own, which does talk over stdio + assertTrue(radio("language.server.transport.stdio").isEnabled()); + assertTrue(label("language.server.transport.socket.only").isVisible()); + assertFalse(label("language.server.transport.stdio.only").isVisible()); + var warning = fields.validateForTemplate(); + assertNotNull("stdio is chosen", warning); + assertEquals(message("language.server.validation.stdio.socket.only"), warning.message); + assertSame(radio("language.server.transport.stdio"), warning.component); + assertTrue(warning.warning); + assertTrue(warning.okEnabled); + + radio("language.server.transport.socket").doClick(); + + assertNull("the socket is what the template lists", fields.validateForTemplate()); + assertTrue("the hint stays", label("language.server.transport.socket.only").isVisible()); + assertTrue(radio("language.server.socket.startAndConnect").isSelected()); + + radio("language.server.transport.stdio").doClick(); + + assertNotNull(fields.validateForTemplate()); + + // a template which lists stdio too, or no transports, says nothing of it + for (var supportedTransports : List.of( + List.of(new TransportSupport(UserDefinedTransport.Mode.STDIO, "--stdio"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}")), + List.of())) { + fields.setSupportedTransports(supportedTransports); + assertFalse(label("language.server.transport.socket.only").isVisible()); + assertNull(fields.validateForTemplate()); + } + } + + public void testASavedStdioOfALanguageServerWhichTalksOnlyOverASocketStaysChosenWithAWarning() { + fields.setTransport(UserDefinedTransport.STDIO); + + // as when the settings of a language server open: the saved transport first, then its template's transports + fields.setSupportedTransports(List.of(new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}"))); + + assertSame(UserDefinedTransport.STDIO, fields.getTransport()); + assertTrue(radio("language.server.transport.stdio").isSelected()); + assertNull(fields.validate()); + var warning = fields.validateForTemplate(); + assertNotNull(warning); + assertEquals(message("language.server.validation.stdio.socket.only"), warning.message); + } + public void testWithoutAWordFromTheTemplateEveryTransportCanBeChosenWithoutAHint() { fields.setSupportedTransports(List.of()); radio("language.server.transport.socket").doClick(); From 5c84f3ffb93e835686d92190f5d8e4e4215aeee6 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:38:02 +0100 Subject: [PATCH 2/3] fix: an installer no longer switches a language server whose template lists only socket modes to stdio An installer's command is taken to be for the standard input and output unless it takes ${port}, so a language server which LSP4IJ starts over a socket was switched to stdio by one without it. A language server whose template lists only socket modes can't talk over stdio, so it couldn't start at all afterwards. - forInstalledCommand(command, supportedTransports) keeps the transport of such a language server, and switches any other as before. The one-argument forInstalledCommand is unchanged. - The installer of a language server, at its start or from its settings, finds its template by its id among the bundled ones, as the settings page does. The New Language Server dialog uses the template it loaded, imported ones included. - With an Auto port, the kept socket can't start until the command takes ${port}: a warning says so, with the action which opens the settings, and the dialog shows its error "With an Auto port, the command must take the port as ${port}". A fixed port, which the command may name itself, gets no warning. Part of #1947. --- docs/UserDefinedLanguageServer.md | 4 +- docs/UserDefinedLanguageServerTemplate.md | 7 +- .../launching/ui/NewLanguageServerDialog.java | 19 ++++-- .../launching/ui/UICommandLineUpdater.java | 42 +++++++++++- .../launching/UserDefinedTransport.java | 17 +++++ .../messages/LanguageServerBundle.properties | 1 + .../UICommandLineUpdaterTransportTest.java | 68 +++++++++++++++++++ .../NewLanguageServerDialogTransportTest.java | 29 ++++++++ .../launching/UserDefinedTransportTest.java | 23 +++++++ 9 files changed, 199 insertions(+), 11 deletions(-) diff --git a/docs/UserDefinedLanguageServer.md b/docs/UserDefinedLanguageServer.md index dd2a181c4..098d07eeb 100644 --- a/docs/UserDefinedLanguageServer.md +++ b/docs/UserDefinedLanguageServer.md @@ -157,7 +157,9 @@ An [installer](#installer-tab) which sets the `Command` writes the command of it input and output unless it takes the port as `${port}`. A language server which LSP4IJ starts over a socket is then switched back to the standard input and output, and a warning says so, with an action which opens its settings; set the socket again with a command that takes the port. A language server which runs already, and to which LSP4IJ -attaches, keeps its socket, since LSP4IJ doesn't use its command. +attaches, keeps its socket, since LSP4IJ doesn't use its command. So does a language server whose template lists only +socket modes in its [`transports`](./UserDefinedLanguageServerTemplate.md#transports), since it can't talk over its +standard input and output: with an `Auto` port, a warning says that its command needs `${port}`, with the same action. A [template](./UserDefinedLanguageServerTemplate.md#template-descriptor) sets the transport in its [`socket`](./UserDefinedLanguageServerTemplate.md#socket), and lists the transports which its language server supports diff --git a/docs/UserDefinedLanguageServerTemplate.md b/docs/UserDefinedLanguageServerTemplate.md index a6bfd1c3e..7f8438dd5 100644 --- a/docs/UserDefinedLanguageServerTemplate.md +++ b/docs/UserDefinedLanguageServerTemplate.md @@ -217,7 +217,9 @@ Without `transports`, no hint shows. A language server which lists only socket modes is the other way round: `Standard input and output (stdio)` gives the warning "The template says this language server talks LSP only over a socket", which blocks neither OK nor Apply either. Without a `socket`, the New Language Server dialog selects `Socket` for it, in the first mode of the list, with -an `Auto` port, or, for `attach`, no port yet. +an `Auto` port, or, for `attach`, no port yet. An [installer](./UserDefinedLanguageServer.md#transport) whose command +has no `${port}` leaves it on its socket, rather than switching it to its standard input and output, where it couldn't +talk at all; with an `Auto` port, a warning then says that its command needs `${port}`. | Field | Value | Required | |------------|-----------------------------------------------------------------------|----------| @@ -251,7 +253,8 @@ When the user changes the transport, in the New Language Server dialog or in the from the template, the argument of the chosen transport replaces that of another listed transport in the command. The arguments are matched as whole words, and the one which comes last in the command is replaced. A command with none of the listed arguments stays as it is, and so does the command when LSP4IJ loads a template or saved settings. The -settings find the template by its `id` among the bundled templates, so a language server created from a +settings, and the installer of a language server created from the template, find the template by its `id` among the +bundled templates, so a language server created from a [custom template](./UserDefinedLanguageServer.md#custom-template) gets this in the New Language Server dialog only. #### Socket and transports diff --git a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java index c12ca31d8..9d42f5438 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java @@ -114,11 +114,7 @@ public String getCommandLine() { @Override public void setCommandLine(String commandLine) { - ApplicationManager.getApplication().invokeLater(() -> { - languageServerPanel.setCommandLine(commandLine); - // The installer's command is written for the standard input and output, unless it takes the port - languageServerPanel.setTransport(languageServerPanel.getTransport().forInstalledCommand(commandLine)); - }); + ApplicationManager.getApplication().invokeLater(() -> setInstalledCommandLine(commandLine)); } }); @@ -135,6 +131,19 @@ public void setCommandLine(String commandLine) { return panel; } + /** + * Sets the command which an installer gives the language server, and the transport to use with it: the installer's + * command is written for the standard input and output, unless it takes the port, or the template says that the + * language server talks LSP only over a socket. + * + * @param commandLine the command which the installer sets. + */ + void setInstalledCommandLine(@Nullable String commandLine) { + languageServerPanel.setCommandLine(commandLine); + var supportedTransports = currentTemplate != null ? currentTemplate.getSupportedTransports() : List.of(); + languageServerPanel.setTransport(languageServerPanel.getTransport().forInstalledCommand(commandLine, supportedTransports)); + } + private UIConfiguration createUIConfiguration() { UIConfiguration configuration = new UIConfiguration(); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/UICommandLineUpdater.java b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/UICommandLineUpdater.java index b63ee6be2..09fac922b 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/UICommandLineUpdater.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/UICommandLineUpdater.java @@ -24,13 +24,18 @@ import com.redhat.devtools.lsp4ij.console.explorer.actions.EditServerAction; import com.redhat.devtools.lsp4ij.installation.CommandLineUpdater; import com.redhat.devtools.lsp4ij.launching.UserDefinedLanguageServerSettings; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinitionListener; +import com.redhat.devtools.lsp4ij.server.definition.launching.TransportSupport; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedTransport; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Objects; +import java.util.Optional; /** * UI command line updater. @@ -54,9 +59,10 @@ public String getCommandLine() { @Override public void setCommandLine(String commandLine) { - // The installer's command is written for the standard input and output, unless it takes the port + // The installer's command is written for the standard input and output, unless it takes the port, or the + // template says that the language server talks LSP only over a socket var previousTransport = definition.getTransport(); - var transport = previousTransport.forInstalledCommand(commandLine); + var transport = previousTransport.forInstalledCommand(commandLine, getSupportedTransports()); if (Objects.equals(commandLine, getCommandLine()) && transport.equals(previousTransport)) { return; } @@ -82,17 +88,47 @@ public void setCommandLine(String commandLine) { } if (previousTransport.mode() != UserDefinedTransport.Mode.STDIO && transport.mode() == UserDefinedTransport.Mode.STDIO) { warnOfTheSwitchToStandardInputAndOutput(); + } else if (transport.mode() != UserDefinedTransport.Mode.STDIO && transport.startsLanguageServer() + && transport.port() == 0 && (commandLine == null || !commandLine.contains(UserDefinedTransport.PORT_MACRO))) { + warnOfTheMissingPort(); } } + /** + * Returns the transports which the template of the language server lists: a template which LSP4IJ bundles, as the + * settings of the language server find it by its id, and none for a language server without one. + */ + private @NotNull List getSupportedTransports() { + return Optional.ofNullable(definition.getTemplateId()) + .map(templateId -> LanguageServerTemplateManager.getInstance().findTemplateById(templateId)) + .map(LanguageServerTemplate::getSupportedTransports) + .orElse(List.of()); + } + /** * Tells the user that the installer's command doesn't take the port, so that the language server now runs over its * standard input and output instead of the socket it had. */ private void warnOfTheSwitchToStandardInputAndOutput() { + warn(LanguageServerBundle.message("language.server.installer.transport.stdio")); + } + + /** + * Tells the user that the installer's command doesn't take the port, which LSP4IJ chooses, of a language server + * which keeps its socket, as its template says that it talks LSP only over a socket: it can't start until its + * command takes the port. + */ + private void warnOfTheMissingPort() { + warn(LanguageServerBundle.message("language.server.installer.transport.socket.only")); + } + + /** + * Shows the given warning of the transport, with an action which opens the settings of the language server. + */ + private void warn(@NotNull String message) { var notification = new Notification(LSPNotificationConstants.LSP4IJ_GENERAL_NOTIFICATIONS_ID, definition.getDisplayName(), - LanguageServerBundle.message("language.server.installer.transport.stdio"), + message, NotificationType.WARNING); notification.addAction(new EditServerAction(definition)); Notifications.Bus.notify(notification, project); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransport.java b/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransport.java index 0ec02ea00..e05c6a105 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransport.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransport.java @@ -15,6 +15,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; +import java.util.List; /** * How LSP4IJ talks to a user-defined language server: over the standard input and output of its process, by default, @@ -142,4 +143,20 @@ public boolean startsLanguageServer() { boolean startedOverSocket = mode == Mode.START_AND_CONNECT || mode == Mode.START_AND_ACCEPT; return startedOverSocket && (command == null || !command.contains(PORT_MACRO)) ? STDIO : this; } + + /** + * Returns the transport to use with a command which an installer sets, for a language server whose template lists + * the given transports: as {@link #forInstalledCommand(String)}, except that a language server which the template + * says talks LSP only over a socket keeps its transport, since it could never talk over its standard input and + * output. Its command then needs {@value #PORT_MACRO} for a port which LSP4IJ chooses, as the settings say. + * + * @param command the command which the installer sets. + * @param supportedTransports the transports which the template of the language server lists, empty if it lists + * none or isn't known. + * @return the transport to use with that command. + */ + public @NotNull UserDefinedTransport forInstalledCommand(@Nullable String command, + @NotNull List supportedTransports) { + return TransportSupport.talksOnlyOverASocket(supportedTransports) ? this : forInstalledCommand(command); + } } diff --git a/src/main/resources/messages/LanguageServerBundle.properties b/src/main/resources/messages/LanguageServerBundle.properties index 0c8582a37..1c0435cf5 100644 --- a/src/main/resources/messages/LanguageServerBundle.properties +++ b/src/main/resources/messages/LanguageServerBundle.properties @@ -51,6 +51,7 @@ language.server.validation.socket.stdio.only=The template says this language ser language.server.validation.stdio.socket.only=The template says this language server talks LSP only over a socket language.server.instances.noInstance=No instance of this language server serves {0}: the file has no root in its workspace folder configuration language.server.installer.transport.stdio=The installer set a command without ${port}, so this language server now runs over its standard input and output instead of a socket. To use a socket again, choose Socket in its settings, with a command which takes ${port}. +language.server.installer.transport.socket.only=The installer set a command without ${port}. The template says this language server talks LSP only over a socket, so it keeps its socket, but it can't learn the port which LSP4IJ chooses: give its command ${port} in its settings. language.server.validation.socket.instances.attach=Several language server instances can't share one running language server: let LSP4IJ start it, or remove serverInstances from the workspace folders language.server.validation.socket.instances.port=Several language server instances can't share one fixed port: choose Auto, or remove serverInstances from the workspace folders language.server.validation.environment.variable=Write the environment variable {0} as NAME=value, or it is ignored diff --git a/src/test/java/com/redhat/devtools/lsp4ij/UICommandLineUpdaterTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/UICommandLineUpdaterTransportTest.java index a3c1f0e43..a74315780 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/UICommandLineUpdaterTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/UICommandLineUpdaterTransportTest.java @@ -10,14 +10,21 @@ import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.notification.Notifications; +import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess; import com.intellij.testFramework.LightPlatformTestCase; import com.intellij.util.ui.UIUtil; import com.redhat.devtools.lsp4ij.launching.UserDefinedLanguageServerSettings; import com.redhat.devtools.lsp4ij.launching.UserDefinedLanguageServerSettings.UserDefinedLanguageServerItemSettings; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; import com.redhat.devtools.lsp4ij.launching.ui.UICommandLineUpdater; +import com.redhat.devtools.lsp4ij.server.definition.launching.TransportSupport; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedTransport; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -31,6 +38,8 @@ public class UICommandLineUpdaterTransportTest extends LightPlatformTestCase { private static final String SERVER_ID = "installer-transport-test"; + // No bundled template lists only socket modes, so the tests add one + private static final String SOCKET_ONLY_TEMPLATE = "socket-only-test-server"; private UserDefinedLanguageServerItemSettings settings; private UserDefinedLanguageServerDefinition definition; @@ -105,9 +114,68 @@ public void testALanguageServerWhichRunsAlreadyKeepsItsSocket() { assertEquals(running, settings.getTransport()); } + public void testAnInstallerCommandWithoutThePortKeepsTheSocketOfALanguageServerWhichTalksOnlyOverASocket() { + var connect = new UserDefinedTransport(UserDefinedTransport.Mode.START_AND_CONNECT, null, 0); + var updater = updater(connect, "server --port ${port}", socketOnlyTemplate()); + + updater.setCommandLine("/opt/server/bin/server"); + + assertEquals("/opt/server/bin/server", definition.getCommandLine()); + assertEquals("/opt/server/bin/server", settings.getCommandLine()); + // over its standard input and output, it couldn't talk at all + assertEquals(connect, definition.getTransport()); + assertEquals(connect, settings.getTransport()); + // but it can't learn the port which LSP4IJ chooses, and the user is told so + UIUtil.dispatchAllInvocationEvents(); + assertEquals(1, warnings.size()); + assertEquals(LanguageServerBundle.message("language.server.installer.transport.socket.only"), + warnings.get(0).getContent()); + } + + public void testAFixedPortOfALanguageServerWhichTalksOnlyOverASocketIsKeptWithoutAWarning() { + // the command may name the port itself + var connect = new UserDefinedTransport(UserDefinedTransport.Mode.START_AND_CONNECT, null, 4389); + var updater = updater(connect, "server --port 4389", socketOnlyTemplate()); + + updater.setCommandLine("/opt/server/bin/server --port 4389"); + + assertEquals("/opt/server/bin/server --port 4389", settings.getCommandLine()); + assertEquals(connect, definition.getTransport()); + assertEquals(connect, settings.getTransport()); + UIUtil.dispatchAllInvocationEvents(); + assertEmpty(warnings); + } + + /** + * Adds a template which lists only socket modes to the bundled ones, among which LSP4IJ finds the template of a + * language server by its id, until the test ends, and returns its id. + */ + private @NotNull String socketOnlyTemplate() { + // The bundled templates are read from the jar of LSP4IJ in the test sandbox, which is outside the roots that a + // test may read when the checkout isn't in the user's home directory + String templates = PathManager.getResourceRoot(LanguageServerTemplateManager.class, "/templates/lsp"); + assertNotNull(templates); + VfsRootAccess.allowRootAccess(getTestRootDisposable(), templates); + var template = new LanguageServerTemplate(); + template.setId(SOCKET_ONLY_TEMPLATE); + template.setName("Socket-only server"); + template.setSupportedTransports(List.of( + new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"))); + var bundled = LanguageServerTemplateManager.getInstance().getTemplates(); + bundled.add(template); + Disposer.register(getTestRootDisposable(), () -> bundled.remove(template)); + return SOCKET_ONLY_TEMPLATE; + } + private @NotNull UICommandLineUpdater updater(@NotNull UserDefinedTransport transport, @NotNull String commandLine) { + return updater(transport, commandLine, null); + } + + private @NotNull UICommandLineUpdater updater(@NotNull UserDefinedTransport transport, @NotNull String commandLine, + @Nullable String templateId) { settings = new UserDefinedLanguageServerItemSettings(); settings.setServerId(SERVER_ID); + settings.setTemplateId(templateId); settings.setServerName("Installer transport test"); settings.setCommandLine(commandLine); settings.setUserEnvironmentVariables(Map.of()); diff --git a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java index 34d9d7709..55a2e4f42 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java @@ -346,6 +346,35 @@ public void testStdioForATemplateWhichListsOnlySocketModesIsAWarningWhichDoesNot } } + public void testAnInstallerCommandWithoutThePortKeepsTheSocketOfATemplateWhichListsOnlySocketModes() { + var dialog = dialog(template("server --port ${port}", UserDefinedTransport.STDIO, SOCKET_ONLY)); + try { + dialog.setInstalledCommandLine("/opt/server/bin/server"); + + assertEquals("/opt/server/bin/server", command(dialog)); + // over its standard input and output, it couldn't talk at all + assertTrue(radio(dialog, "language.server.transport.socket").isSelected()); + assertTrue(radio(dialog, "language.server.socket.startAndConnect").isSelected()); + // and OK waits for a command which takes the port + assertEquals(List.of(LanguageServerBundle.message("language.server.validation.socket.port.macro", + UserDefinedTransport.PORT_MACRO)), + fieldProblems(dialog).stream().map(problem -> problem.message).toList()); + } finally { + dialog.close(DialogWrapper.CANCEL_EXIT_CODE); + } + + // A template which lists stdio too switches to it, as before + var alsoStdio = dialog(template("node css.js --stdio", UserDefinedTransport.STDIO, CSS)); + try { + click(alsoStdio, "language.server.transport.socket"); + alsoStdio.setInstalledCommandLine("node /opt/css.js --stdio"); + + assertTrue(radio(alsoStdio, "language.server.transport.stdio").isSelected()); + } finally { + alsoStdio.close(DialogWrapper.CANCEL_EXIT_CODE); + } + } + public void testAStdioCommandWhichTakesThePortIsAWarningWhichDoesNotBlockOk() { var dialog = dialog(template("node server.js --socket=${port}", UserDefinedTransport.STDIO)); try { diff --git a/src/test/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransportTest.java index 68e50dbaf..d4fdf6d39 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedTransportTest.java @@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; @@ -71,6 +73,27 @@ void anInstallerCommandWithoutThePortStartsTheLanguageServerOverItsStandardInput } } + @Test + void anInstallerCommandWithoutThePortKeepsTheSocketOfALanguageServerWhichTalksOnlyOverASocket() { + var socketOnly = List.of(new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}")); + var alsoStdio = List.of(new TransportSupport(UserDefinedTransport.Mode.STDIO, "--stdio"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}")); + for (var mode : new UserDefinedTransport.Mode[]{UserDefinedTransport.Mode.START_AND_CONNECT, UserDefinedTransport.Mode.START_AND_ACCEPT}) { + for (int port : new int[]{0, 4389}) { + var transport = new UserDefinedTransport(mode, null, port); + // over its standard input and output, it couldn't talk at all + assertSame(transport, transport.forInstalledCommand("/opt/server/bin/server", socketOnly)); + assertSame(transport, transport.forInstalledCommand(null, socketOnly)); + // a template which lists stdio too, or no transports, switches it as before + assertSame(UserDefinedTransport.STDIO, transport.forInstalledCommand("node server.js --stdio", alsoStdio)); + assertSame(UserDefinedTransport.STDIO, transport.forInstalledCommand("node server.js --stdio", List.of())); + assertSame(transport, transport.forInstalledCommand("node server.js --socket=${port}", alsoStdio)); + } + } + assertSame(UserDefinedTransport.STDIO, UserDefinedTransport.STDIO.forInstalledCommand("/opt/server/bin/server", socketOnly)); + } + @Test void anInstallerCommandLeavesTheTransportOfALanguageServerWhichDoesNotUseIt() { var running = new UserDefinedTransport(UserDefinedTransport.Mode.ATTACH, "localhost", 6005); From e2b13afb6545f74d8c93a3fae7ea01d4d4d2c008 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:43:26 +0100 Subject: [PATCH 3/3] fix: show a hint for every socket mode which a template lists, not only the first The hint below the socket modes took the first entry of `transports` which isn't stdio, so a template which lists both startAndConnect and startAndAccept, each with its own argument, named only the first: after choosing the other mode, whose argument the command then gets, the hint still named the first one's. - Each socket mode which the template lists has its hint, in the order of the list, with the wording of the hint as before: "This language server takes the port as {argument}, with "{mode}".", or "This language server supports "{mode}"." without an argument. - A mode listed twice has the hint of its first entry, whose argument the command gets when the mode is chosen. - A template which lists one socket mode shows the same hint as before. Part of #1947. --- docs/UserDefinedLanguageServerTemplate.md | 7 +-- .../lsp4ij/settings/ui/TransportFields.java | 51 ++++++++++++++----- .../NewLanguageServerDialogTransportTest.java | 20 ++++++++ .../settings/ui/TransportFieldsTest.java | 40 +++++++++++++++ 4 files changed, 101 insertions(+), 17 deletions(-) diff --git a/docs/UserDefinedLanguageServerTemplate.md b/docs/UserDefinedLanguageServerTemplate.md index 7f8438dd5..6bebad63c 100644 --- a/docs/UserDefinedLanguageServerTemplate.md +++ b/docs/UserDefinedLanguageServerTemplate.md @@ -207,7 +207,7 @@ does instead: each with its `mode` (`stdio` or one of the socket modes above) and the `argument` of the command for it. The New Language Server dialog then helps with the transport: for a language server which lists only `stdio`, a hint says that it talks LSP only over its standard input and output, for one which lists only socket modes, a -hint says that it talks LSP only over a socket, and for one which lists a socket mode, a hint below the socket modes +hint says that it talks LSP only over a socket, and for each socket mode which it lists, a hint below the socket modes names the mode and the argument. Every transport can still be chosen, as the template may be the starting point of a language server of your own which does talk over a socket: Socket, for a language server which lists only `stdio`, gives the warning "The template says this language server talks LSP only over its standard input and output", which @@ -227,8 +227,9 @@ talk at all; with an `Auto` port, a warning then says that its command needs `${ | `argument` | the argument of the command for this mode, such as `--socket=${port}` | no | An entry with a missing or unknown `mode` is skipped, and shown as a warning in the New Language Server dialog. Without -an `argument`, the hint names only the mode. The hint shows the first socket mode of the list. The CSS language server, -for instance, is started over its standard input and output, and also talks over a socket it connects to: +an `argument`, the hint names only the mode. The hints follow the order of the list, and Socket starts in the first +socket mode of the list. The CSS language server, for instance, is started over its standard input and output, and also +talks over a socket it connects to: ```json { diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java index 903e8bcb8..b842bf347 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFields.java @@ -31,8 +31,11 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static com.redhat.devtools.lsp4ij.LanguageServerBundle.message; @@ -67,7 +70,11 @@ final class TransportFields { private final JBLabel attachPortHint = new JBLabel(message("language.server.socket.port.attach.hint")); private final JBLabel stdioOnlyHint = new JBLabel(message("language.server.transport.stdio.only")); private final JBLabel socketOnlyHint = new JBLabel(message("language.server.transport.socket.only")); - private final JBLabel templateSocketHint = new JBLabel(); + // A hint for each socket mode which the template lists, in the order of the list + private final List templateSocketHints = Arrays.stream(UserDefinedTransport.Mode.values()) + .filter(mode -> mode != UserDefinedTransport.Mode.STDIO) + .map(mode -> new JBLabel()) + .toList(); private final JPanel transportRow = row(GAP, stdio, socket); // Auto and Fixed have a row of their own, whose gaps hide with them, so the port lines up with the host without them private final JPanel portChoice = row(GAP, autoPort, fixedPort); @@ -86,7 +93,7 @@ final class TransportFields { attachPortHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); stdioOnlyHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); socketOnlyHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); - templateSocketHint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND); + templateSocketHints.forEach(hint -> hint.setForeground(JBUI.CurrentTheme.ContextHelp.FOREGROUND)); typedPortCommits(); setTransport(UserDefinedTransport.STDIO); for (var button : List.of(stdio, socket, attach, startAndConnect, startAndAccept, autoPort, fixedPort)) { @@ -112,7 +119,7 @@ void addTo(@NotNull FormBuilder builder) { builder.addLabeledComponent(socketLabel, attach); builder.addLabeledComponent(startAndConnectLabel, startAndConnect); builder.addLabeledComponent(startAndAcceptLabel, startAndAccept); - builder.addComponent(templateSocketHint, 0); + templateSocketHints.forEach(hint -> builder.addComponent(hint, 0)); builder.addLabeledComponent(hostLabel, host); builder.addLabeledComponent(portLabel, portRow); builder.addComponent(portHint, 0); @@ -130,10 +137,10 @@ void addChangeListener(@NotNull Runnable listener) { /** * Shows what the template of the language server says of its transports: a hint says that a language server talks - * only over its standard input and output, or only over a socket, and another names the mode, and the argument, of - * a socket which it supports. Every transport can still be chosen, as a template is also the starting point of a - * language server of one's own; a socket for a language server which talks only over its standard input and output, - * and the standard input and output for one which talks only over a socket, are warnings of + * only over its standard input and output, or only over a socket, and one for each socket mode which it supports + * names the mode, and the argument. Every transport can still be chosen, as a template is also the starting point + * of a language server of one's own; a socket for a language server which talks only over its standard input and + * output, and the standard input and output for one which talks only over a socket, are warnings of * {@link #validateForTemplate()}. Without a word from the template, no hint shows. * * @param supportedTransports the transports which the template lists, empty if it lists none. @@ -302,15 +309,21 @@ private void update() { port.setEnabled(isAttach || fixedPort.isSelected()); portHint.setVisible(isSocket && !isAttach); attachPortHint.setVisible(isAttach && port.getNumber() == 0); - var socketSupport = supportedTransports.stream() - .filter(transport -> transport.mode() != UserDefinedTransport.Mode.STDIO) - .findFirst(); stdioOnlyHint.setVisible(listsOnlyStdio()); socketOnlyHint.setVisible(TransportSupport.talksOnlyOverASocket(supportedTransports)); - socketSupport.ifPresent(transport -> templateSocketHint.setText(transport.argument() != null - ? message("language.server.socket.template.hint", transport.argument(), modeButton(transport.mode()).getText()) - : message("language.server.socket.template.mode.hint", modeButton(transport.mode()).getText()))); - templateSocketHint.setVisible(isSocket && socketSupport.isPresent()); + // The first entry of each socket mode, whose argument the command gets + List socketHints = supportedTransports.stream() + .filter(transport -> transport.mode() != UserDefinedTransport.Mode.STDIO) + .collect(Collectors.toMap(TransportSupport::mode, transport -> transport, (first, other) -> first, + LinkedHashMap::new)) + .values().stream() + .map(this::templateSocketHint) + .toList(); + for (int i = 0; i < templateSocketHints.size(); i++) { + var hint = templateSocketHints.get(i); + hint.setText(i < socketHints.size() ? socketHints.get(i) : ""); + hint.setVisible(isSocket && i < socketHints.size()); + } var parent = transportRow.getParent(); if (parent != null) { parent.revalidate(); @@ -360,6 +373,16 @@ private static boolean isHost(@NotNull String text) { return HOST_NAME.matcher(text).matches() && !lastLabel.chars().allMatch(Character::isDigit); } + /** + * Returns the hint of a socket mode which the template lists: the mode, and the argument of the command for it. + */ + private @NotNull String templateSocketHint(@NotNull TransportSupport transport) { + String mode = modeButton(transport.mode()).getText(); + return transport.argument() != null + ? message("language.server.socket.template.hint", transport.argument(), mode) + : message("language.server.socket.template.mode.hint", mode); + } + private @NotNull JBRadioButton modeButton(@NotNull UserDefinedTransport.Mode mode) { return switch (mode) { case STDIO -> stdio; diff --git a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java index 55a2e4f42..1fb6cdeb8 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialogTransportTest.java @@ -11,6 +11,7 @@ import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.ValidationInfo; import com.intellij.testFramework.fixtures.BasePlatformTestCase; +import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBRadioButton; import com.redhat.devtools.lsp4ij.LanguageServerBundle; import com.redhat.devtools.lsp4ij.LanguageServersRegistry; @@ -375,6 +376,25 @@ public void testAnInstallerCommandWithoutThePortKeepsTheSocketOfATemplateWhichLi } } + public void testEverySocketModeOfTheTemplateHasAHint() { + var dialog = dialog(template("server --port ${port}", UserDefinedTransport.STDIO, SOCKET_ONLY)); + try { + List hints = form(dialog).stream() + .filter(component -> component instanceof JBLabel label && label.isVisible() + && label.getText().startsWith("This language server takes")) + .map(component -> ((JBLabel) component).getText()) + .toList(); + assertEquals(List.of( + LanguageServerBundle.message("language.server.socket.template.hint", "--port ${port}", + LanguageServerBundle.message("language.server.socket.startAndConnect")), + LanguageServerBundle.message("language.server.socket.template.hint", "--socket=${port}", + LanguageServerBundle.message("language.server.socket.startAndAccept"))), + hints); + } finally { + dialog.close(DialogWrapper.CANCEL_EXIT_CODE); + } + } + public void testAStdioCommandWhichTakesThePortIsAWarningWhichDoesNotBlockOk() { var dialog = dialog(template("node server.js --socket=${port}", UserDefinedTransport.STDIO)); try { diff --git a/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java b/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java index 9f77d58ee..3319c127a 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/settings/ui/TransportFieldsTest.java @@ -198,6 +198,35 @@ public void testASocketWhichTheTemplateListsHasAHint() { assertNull("the template lists the socket", fields.validateForTemplate()); } + public void testEverySocketModeWhichTheTemplateListsHasAHint() { + fields.setSupportedTransports(List.of( + new TransportSupport(UserDefinedTransport.Mode.STDIO, "--stdio"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, "--port ${port}"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}"), + new TransportSupport(UserDefinedTransport.Mode.ATTACH, null))); + String connect = message("language.server.socket.template.hint", "--port ${port}", + message("language.server.socket.startAndConnect")); + String accept = message("language.server.socket.template.hint", "--socket=${port}", + message("language.server.socket.startAndAccept")); + String attach = message("language.server.socket.template.mode.hint", message("language.server.socket.attach")); + assertEmpty("only a socket has the hints", templateSocketHints()); + + radio("language.server.transport.socket").doClick(); + + // in the order of the list, whichever mode is chosen + assertTrue("the first socket mode of the list", radio("language.server.socket.startAndConnect").isSelected()); + assertEquals(List.of(connect, accept, attach), templateSocketHints()); + radio("language.server.socket.startAndAccept").doClick(); + assertEquals(List.of(connect, accept, attach), templateSocketHints()); + + // A mode listed twice has the hint of its first entry, whose argument the command gets, and the hints of the + // modes which another template doesn't list go + fields.setSupportedTransports(List.of( + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--socket=${port}"), + new TransportSupport(UserDefinedTransport.Mode.START_AND_ACCEPT, "--listen=${port}"))); + assertEquals(List.of(accept), templateSocketHints()); + } + public void testATemplateWhichListsOnlyASocketGivesNoWarningForTheSocket() { fields.setSupportedTransports(List.of(new TransportSupport(UserDefinedTransport.Mode.START_AND_CONNECT, null))); @@ -338,6 +367,17 @@ public void testATransportIsShownAsItIs() { .orElseThrow(() -> new AssertionError("No label " + text)); } + /** + * Returns the texts of the hints which show for the socket modes of the template, in the order of the form. + */ + private @NotNull List templateSocketHints() { + return UIUtil.findComponentsOfType(form, JBLabel.class).stream() + .filter(label -> label.isVisible() && (label.getText().startsWith("This language server takes") + || label.getText().startsWith("This language server supports"))) + .map(JBLabel::getText) + .toList(); + } + private @NotNull JBTextField host() { return UIUtil.findComponentOfType(form, JBTextField.class); }