Skip to content

Commit

Permalink
feat: Add text field for initializationOptions of manually defined
Browse files Browse the repository at this point in the history
language server settings

Fixes redhat-developer#154

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr authored and fbricon committed Mar 11, 2024
1 parent 69554f8 commit cfc2d01
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 54 deletions.
4 changes: 2 additions & 2 deletions docs/LSPSupport.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Current state of [Language Features]( https://microsoft.github.io/language-serve
*[textDocument/moniker](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_moniker).
*[textDocument/pullDiagnostics](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics).
*[textDocument/formatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting) (see [implementation details](#formatting))
*[textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rangeFormatting).
*[textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rangeFormatting) (see [implementation details](#formatting)).
*[textDocument/onTypeFormatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_onTypeFormatting).
*[textDocument/rename](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rename).
*[textDocument/prepareRename](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_prepareRename).
Expand Down Expand Up @@ -281,7 +281,7 @@ Here is an example with the [Qute language server](https://github.com/redhat-dev

#### Formatting

[textDocument/formatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentColor) is implemented with the `formattingService` extension point.
[textDocument/formatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentColor) and [textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_rangeFormatting) are implemented with the `formattingService` extension point.

#### Show Message

Expand Down
4 changes: 2 additions & 2 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ to configure language servers debugging and tracing:

## Actions

### Reformat Code
### Formatting

The LSP formatting support can be done with the standard `Reformat Code` menu.
The LSP formatting support can be done with the standard `Reformat Code` and `Reformat File` actions.

### Find Usages

Expand Down
Binary file added docs/images/user-defined-ls/CSSServerDemo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/user-defined-ls/CSSServerDialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/user-defined-ls/typescript-language-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ It will install:

After that you can create the TypeScript Language Server with the [language server template](../UserDefinedLanguageServer.md#using-template):

![TypeScript demo](../images/user-defined-ls/TypeScriptServerDialog.png)
![TypeScript server dialog](../images/user-defined-ls/TypeScriptServerDialog.png)
13 changes: 11 additions & 2 deletions docs/user-defined-ls/vscode-css-language-server.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# CSS Language Server

You can use [Visual Studio Code's CSS language server](https://github.com/microsoft/vscode-css-languageservice) by following these instructions:
You can leverage the [Visual Studio Code CSS Language Server](https://github.com/microsoft/vscode-css-languageservice) to enhance your development experience with support for `CSS`, `LESS`, and `SCSS`.

![CSS demo](../images/user-defined-ls/CSSServerDemo.gif)

You can use it by following these instructions:
* [Install Node.js](https://nodejs.org/en/download)
* [Download and install Visual Studio Code](https://code.visualstudio.com/download)

Once Visual Studio Code is installed, it will store the CSS language server in:
> **${BASE_DIR}/resources/app/extensions/css-language-features/server/dist/node/cssServerMain.js**
[LSP4IJ](https://github.com/redhat-developer/lsp4ij) tries to generate the proper **cssServerMain.js** file path according to your OS, but you may have to adjust it.
After that you can create the CSS Language Server with the [language server template](../UserDefinedLanguageServer.md#using-template):

![CSS server dialog](../images/user-defined-ls/CSSServerDialog.png)

[LSP4IJ](https://github.com/redhat-developer/lsp4ij) tries to generate the proper **cssServerMain.js** file path according to your OS, but you may have to adjust it
in the command text field.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ private void loadServersAndMappingFromSettings() {
String serverId = launch.getServerId();
List<ServerMapping> mappings = toServerMappings(serverId, launch.getMappings());
// Register server definition from settings
addServerDefinitionWithoutNotification(new UserDefinedLanguageServerDefinition(serverId, launch.getServerName(), "", launch.getCommandLine(), launch.getConfigurationContent()), mappings);
addServerDefinitionWithoutNotification(new UserDefinedLanguageServerDefinition(
serverId,
launch.getServerName(),
"",
launch.getCommandLine(),
launch.getConfigurationContent(),
launch.getInitializationOptionsContent()),
mappings);
}
} catch (Exception e) {
LOGGER.error("Error while loading user defined language servers from settings", e);
Expand Down Expand Up @@ -282,6 +289,7 @@ private void addServerDefinitionWithoutNotification(@NotNull LanguageServerDefin
settings.setMappings(toServerMappingSettings(mappings));
}
settings.setConfigurationContent(definitionFromSettings.getConfigurationContent());
settings.setInitializationOptionsContent(definitionFromSettings.getInitializationOptionsContent());
UserDefinedLanguageServerSettings.getInstance().setLaunchConfigSettings(definition.id, settings);
}
}
Expand Down Expand Up @@ -381,10 +389,12 @@ public void updateServerDefinition(@NotNull UserDefinedLanguageServerDefinition
@Nullable String name,
@Nullable String commandLine,
@NotNull List<ServerMappingSettings> mappings,
@Nullable String configurationContent) {
@Nullable String configurationContent,
@Nullable String initializationOptionsContent) {
definition.setName(name);
definition.setCommandLine(commandLine);
definition.setConfigurationContent(configurationContent);
definition.setInitializationOptionsContent(initializationOptionsContent);

// remove associations
removeAssociationsFor(definition);
Expand All @@ -396,15 +406,17 @@ public void updateServerDefinition(@NotNull UserDefinedLanguageServerDefinition
boolean commandChanged = !Objects.equals(settings.getCommandLine(), commandLine);
boolean mappingsChanged = !Objects.deepEquals(settings.getMappings(), mappings);
boolean configurationContentChanged = !Objects.equals(settings.getConfigurationContent(), configurationContent);
boolean initializationOptionsContentChanged = !Objects.equals(settings.getInitializationOptionsContent(), initializationOptionsContent);

settings.setServerName(name);
settings.setCommandLine(commandLine);
settings.setConfigurationContent(configurationContent);
settings.setInitializationOptionsContent(initializationOptionsContent);
settings.setMappings(mappings);

if (nameChanged || commandChanged || mappingsChanged || configurationContentChanged) {
if (nameChanged || commandChanged || mappingsChanged || configurationContentChanged || initializationOptionsContentChanged) {
// Notifications
LanguageServerDefinitionListener.LanguageServerChangedEvent event = new LanguageServerDefinitionListener.LanguageServerChangedEvent(definition, nameChanged, commandChanged, mappingsChanged, configurationContentChanged);
LanguageServerDefinitionListener.LanguageServerChangedEvent event = new LanguageServerDefinitionListener.LanguageServerChangedEvent(definition, nameChanged, commandChanged, mappingsChanged, configurationContentChanged, initializationOptionsContentChanged);
for (LanguageServerDefinitionListener listener : this.listeners) {
try {
listener.handleChanged(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void handleRemoved(@NotNull LanguageServerDefinitionListener.LanguageServ
}

@Override
public void handleChanged(LanguageServerChangedEvent event) {
public void handleChanged(@NotNull LanguageServerChangedEvent event) {
if (event.commandChanged || event.mappingsChanged) {
// Stop all servers where command or mappings has changed
List<LanguageServerWrapper> serversToStop = startedServers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void handleRemoved(@NotNull LanguageServerDefinitionListener.LanguageServ
}

@Override
public void handleChanged(LanguageServerChangedEvent event) {
public void handleChanged(@NotNull LanguageServerChangedEvent event) {
if (event.nameChanged) {
// A server definition name has changed, rename the proper tree node label of the explorer
DefaultTreeModel treeModel = (DefaultTreeModel)tree.getModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public static class UserDefinedLanguageServerItemSettings {

private String configurationContent;

private String initializationOptionsContent;

@XCollection(elementTypes = ServerMappingSettings.class)
private List<ServerMappingSettings> mappings;

public String getServerId() {
return serverId;
}
Expand Down Expand Up @@ -155,6 +156,14 @@ public String getConfigurationContent() {
public void setConfigurationContent(String configurationContent) {
this.configurationContent = configurationContent;
}

public String getInitializationOptionsContent() {
return initializationOptionsContent;
}

public void setInitializationOptionsContent(String initializationOptionsContent) {
this.initializationOptionsContent = initializationOptionsContent;
}
}

public static class MyState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public String getName() {
private String description;

private String configuration;
private String initializationOptions;

public String getId() {
return id;
Expand Down Expand Up @@ -123,7 +124,7 @@ public String getDescription() {
return null;
}
String docPath = getId() + "/README.md";
String docContent = loadDocContent(docPath);
String docContent = loadTemplateResourceContent(docPath);
if (docContent == null) {
description = "";
} else {
Expand All @@ -146,16 +147,31 @@ public String getConfiguration() {
return null;
}
String configurationPath = getId() + "/settings.json";
String configurationContent = loadDocContent(configurationPath);
String configurationContent = loadTemplateResourceContent(configurationPath);
configuration = configurationContent != null ? configurationContent : "";
return configuration;
}

private static String loadDocContent(@NotNull String docPath) {
try (Reader reader = LanguageServerTemplateManager.loadTemplateReader(docPath)) {

public String getInitializationOptions() {
if (initializationOptions != null) {
return initializationOptions.isEmpty() ? null : initializationOptions;
}
String id = getId();
if (id == null) {
return null;
}
String initializationOptionsPath = getId() + "/initializationOptions.json";
String initializationOptionsContent = loadTemplateResourceContent(initializationOptionsPath);
initializationOptions = initializationOptionsContent != null ? initializationOptionsContent : "";
return initializationOptions;
}

private static String loadTemplateResourceContent(@NotNull String resourcePath) {
try (Reader reader = LanguageServerTemplateManager.loadTemplateReader(resourcePath)) {
return reader != null ? StreamUtil.readText(reader) : null;
} catch (Exception e) {
LOGGER.warn("Error while loading language server template documentation '" + docPath + "'", e);
LOGGER.warn("Error while loading language server template resource '" + resourcePath + "'", e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.intellij.openapi.application.ApplicationManager;
import com.redhat.devtools.lsp4ij.internal.IntelliJPlatformUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -60,9 +61,9 @@ public List<LanguageServerTemplate> getTemplates() {
return templates;
}

@Nullable
static Reader loadTemplateReader(@NotNull String path) {
var is = LanguageServerTemplateManager.class.getClassLoader().getResourceAsStream(TEMPLATES_DIR + "/" + path);
assert is != null;
return new InputStreamReader(new BufferedInputStream(is));
return is !=null? new InputStreamReader(new BufferedInputStream(is)) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ private void loadFromTemplate(LanguageServerTemplate template) {
var configuration = this.languageServerPanel.getConfiguration();
configuration.setText(template.getConfiguration() != null ? template.getConfiguration() : "");
configuration.setCaretPosition(0);

// Update initialize options
var initializationOptions = this.languageServerPanel.getInitializationOptionsWidget();
initializationOptions.setText(template.getInitializationOptions() != null ? template.getInitializationOptions() : "");
initializationOptions.setCaretPosition(0);
}

private static String getCommandLine(LanguageServerTemplate entry) {
Expand Down Expand Up @@ -202,7 +207,8 @@ protected void doOKAction() {
String serverName = this.languageServerPanel.getServerName().getText();
String commandLine = this.languageServerPanel.getCommandLine().getText();
String configuration = this.languageServerPanel.getConfiguration().getText();
UserDefinedLanguageServerDefinition definition = new UserDefinedLanguageServerDefinition(serverId, serverName, "", commandLine, configuration);
String initializationOptions = this.languageServerPanel.getInitializationOptionsWidget().getText();
UserDefinedLanguageServerDefinition definition = new UserDefinedLanguageServerDefinition(serverId, serverName, "", commandLine, configuration, initializationOptions);
LanguageServersRegistry.getInstance().addServerDefinition(definition, mappingSettings);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Predicate;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void format(@Nullable Editor editor, @Nullable TextRange textRange, @NotN
}

private static void handleError(@NotNull AsyncFormattingRequest formattingRequest, Throwable error) {
if (error instanceof ProcessCanceledException) {
if (error instanceof ProcessCanceledException || error instanceof CancellationException) {
// Ignore the error
formattingRequest.onTextReady(formattingRequest.getDocumentText());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public interface LanguageServerDefinitionListener {

public class LanguageServerAddedEvent {
class LanguageServerAddedEvent {

public final Collection<LanguageServerDefinition> serverDefinitions;

Expand All @@ -31,7 +31,7 @@ public LanguageServerAddedEvent(@NotNull Collection<LanguageServerDefinition> se
}
}

public class LanguageServerRemovedEvent {
class LanguageServerRemovedEvent {

public final Collection<LanguageServerDefinition> serverDefinitions;

Expand All @@ -40,7 +40,7 @@ public LanguageServerRemovedEvent(@NotNull Collection<LanguageServerDefinition>
}
}

public class LanguageServerChangedEvent {
class LanguageServerChangedEvent {

public final LanguageServerDefinition serverDefinition;

Expand All @@ -50,13 +50,15 @@ public class LanguageServerChangedEvent {

public final boolean mappingsChanged;
public final boolean configurationChanged;
public final boolean initializationOptionsContentChanged;

public LanguageServerChangedEvent(@NotNull LanguageServerDefinition serverDefinition, boolean nameChanged, boolean commandChanged, boolean mappingsChanged, boolean configurationContentChanged) {
public LanguageServerChangedEvent(@NotNull LanguageServerDefinition serverDefinition, boolean nameChanged, boolean commandChanged, boolean mappingsChanged, boolean configurationContentChanged, boolean initializationOptionsContentChanged) {
this.serverDefinition = serverDefinition;
this.nameChanged = nameChanged;
this.commandChanged = commandChanged;
this.mappingsChanged = mappingsChanged;
this.configurationChanged = configurationContentChanged;
this.initializationOptionsContentChanged = initializationOptionsContentChanged;
}
}
void handleAdded(@NotNull LanguageServerDefinitionListener.LanguageServerAddedEvent event);
Expand Down
Loading

0 comments on commit cfc2d01

Please sign in to comment.