Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/UserDefinedLanguageServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions docs/UserDefinedLanguageServerTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -206,21 +206,30 @@ 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 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
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. 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 |
|------------|-----------------------------------------------------------------------|----------|
| `mode` | `stdio`, `attach`, `startAndConnect` or `startAndAccept` | yes |
| `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
{
Expand All @@ -245,7 +254,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,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));
}
});

Expand All @@ -133,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.<TransportSupport>of();
languageServerPanel.setTransport(languageServerPanel.getTransport().forInstalledCommand(commandLine, supportedTransports));
}

private UIConfiguration createUIConfiguration() {
UIConfiguration configuration = new UIConfiguration();

Expand Down Expand Up @@ -268,7 +279,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();

Expand Down Expand Up @@ -301,6 +312,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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<TransportSupport> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<TransportSupport> supportedTransports) {
return !supportedTransports.isEmpty() && supportedTransports.stream()
.noneMatch(transport -> transport.mode() == UserDefinedTransport.Mode.STDIO);
}
}
Loading
Loading