Skip to content

Commit

Permalink
feat: associate JSON Schema for language server configuration
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Dec 12, 2024
1 parent e5ca369 commit 2e44c67
Show file tree
Hide file tree
Showing 24 changed files with 2,220 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private void loadServersAndMappingFromSettings() {
launch.getUserEnvironmentVariables(),
launch.isIncludeSystemEnvironmentVariables(),
launch.getConfigurationContent(),
launch.getConfigurationSchemaContent(),
launch.getInitializationOptionsContent(),
launch.getClientConfigurationContent()),
mappings);
Expand Down Expand Up @@ -365,6 +366,7 @@ private void addServerDefinitionWithoutNotification(@NotNull LanguageServerDefin
settings.setMappings(toServerMappingSettings(mappings));
}
settings.setConfigurationContent(definitionFromSettings.getConfigurationContent());
settings.setConfigurationSchemaContent(definitionFromSettings.getConfigurationSchemaContent());
settings.setInitializationOptionsContent(definitionFromSettings.getInitializationOptionsContent());
settings.setClientConfigurationContent(definitionFromSettings.getClientConfigurationContent());
UserDefinedLanguageServerSettings.getInstance().setLaunchConfigSettings(languageServerId, settings);
Expand Down Expand Up @@ -493,6 +495,7 @@ private void removeAssociationsFor(LanguageServerDefinition definition) {
settings.setUserEnvironmentVariables(request.userEnvironmentVariables());
settings.setIncludeSystemEnvironmentVariables(request.includeSystemEnvironmentVariables());
settings.setConfigurationContent(request.configurationContent());
settings.setConfigurationSchemaContent(request.configurationSchemaContent());
settings.setInitializationOptionsContent(request.initializationOptionsContent());
settings.setClientConfigurationContent(request.clientConfigurationContent);
settings.setMappings(request.mappings());
Expand Down Expand Up @@ -583,6 +586,7 @@ public record UpdateServerDefinitionRequest(@NotNull Project project,
boolean includeSystemEnvironmentVariables,
@NotNull List<ServerMappingSettings> mappings,
@Nullable String configurationContent,
@Nullable String configurationSchemaContent,
@Nullable String initializationOptionsContent,
@Nullable String clientConfigurationContent) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public static class UserDefinedLanguageServerItemSettings {

private String configurationContent;

private String configurationSchemaContent;

private String initializationOptionsContent;

private String clientConfigurationContent = "{}";
Expand Down Expand Up @@ -201,6 +203,14 @@ public void setConfigurationContent(String configurationContent) {
this.configurationContent = configurationContent;
}

public String getConfigurationSchemaContent() {
return configurationSchemaContent;
}

public void setConfigurationSchemaContent(String configurationSchemaContent) {
this.configurationSchemaContent = configurationSchemaContent;
}

public String getInitializationOptionsContent() {
return initializationOptionsContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public String getName() {
public static final String TEMPLATE_FILE_NAME = "template.json";
public static final String INITIALIZATION_OPTIONS_FILE_NAME = "initializationOptions.json";
public static final String SETTINGS_FILE_NAME = "settings.json";
public static final String SETTINGS_SCHEMA_FILE_NAME = "settings.schema.json";
public static final String CLIENT_SETTINGS_FILE_NAME = "clientSettings.json";
public static final String README_FILE_NAME = "README.md";

Expand Down Expand Up @@ -70,6 +71,7 @@ public String getName() {
private String description;

private String configuration;
private String configurationSchema;
private String initializationOptions;
private String clientConfiguration;

Expand Down Expand Up @@ -144,6 +146,14 @@ public void setConfiguration(String configuration) {
this.configuration = configuration;
}

public String getConfigurationSchema() {
return configurationSchema;
}

public void setConfigurationSchema(String configurationSchema) {
this.configurationSchema = configurationSchema;
}

public String getInitializationOptions() {
return initializationOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public LanguageServerTemplate importLsTemplate(@NotNull VirtualFile templateFold
public LanguageServerTemplate createLsTemplate(@NotNull VirtualFile templateFolder) throws IOException {
String templateJson = null;
String settingsJson = null;
String settingsSchemaJson = null;
String initializationOptionsJson = null;
String clientSettingsJson = null;
String description = null;
Expand All @@ -149,6 +150,9 @@ public LanguageServerTemplate createLsTemplate(@NotNull VirtualFile templateFold
case SETTINGS_FILE_NAME:
settingsJson = VfsUtilCore.loadText(file);
break;
case SETTINGS_SCHEMA_FILE_NAME:
settingsSchemaJson = VfsUtilCore.loadText(file);
break;
case INITIALIZATION_OPTIONS_FILE_NAME:
initializationOptionsJson = VfsUtilCore.loadText(file);
break;
Expand Down Expand Up @@ -183,6 +187,7 @@ public LanguageServerTemplate createLsTemplate(@NotNull VirtualFile templateFold

LanguageServerTemplate template = gson.fromJson(templateJson, LanguageServerTemplate.class);
template.setConfiguration(settingsJson);
template.setConfigurationSchema(settingsSchemaJson);
template.setInitializationOptions(initializationOptionsJson);
template.setClientConfiguration(clientSettingsJson);
if (StringUtils.isNotBlank(description)) {
Expand Down Expand Up @@ -230,12 +235,14 @@ private SimpleEntry<Integer, byte[]> createZipFromLanguageServers(@NotNull List<
String template = gson.toJson(lsDefinition);
String initializationOptions = ((UserDefinedLanguageServerDefinition) lsDefinition).getInitializationOptionsContent();
String settings = ((UserDefinedLanguageServerDefinition) lsDefinition).getConfigurationContent();
String settingsSchema = ((UserDefinedLanguageServerDefinition) lsDefinition).getConfigurationSchemaContent();
String clientSettings = ((UserDefinedLanguageServerDefinition) lsDefinition).getClientConfigurationContent();
lsName = lsDefinition.getDisplayName();

writeToZip(TEMPLATE_FILE_NAME, template, zos);
writeToZip(INITIALIZATION_OPTIONS_FILE_NAME, initializationOptions, zos);
writeToZip(SETTINGS_FILE_NAME, settings, zos);
writeToZip(SETTINGS_SCHEMA_FILE_NAME, settingsSchema, zos);
writeToZip(CLIENT_SETTINGS_FILE_NAME, clientSettings, zos);
zos.closeEntry();
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ private void loadFromTemplate(LanguageServerTemplate template) {
configuration.setText(template.getConfiguration() != null ? template.getConfiguration() : "");
configuration.setCaretPosition(0);

// Update server configuration JSON Schema
this.languageServerPanel.setConfigurationSchemaContent(template.getConfigurationSchema() != null ? template.getConfigurationSchema() : "");

// Update initialization options
var initializationOptions = this.languageServerPanel.getInitializationOptionsWidget();
initializationOptions.setText(template.getInitializationOptions() != null ? template.getInitializationOptions() : "");
Expand Down Expand Up @@ -293,6 +296,7 @@ protected void doOKAction() {
Map<String, String> userEnvironmentVariables = this.languageServerPanel.getEnvironmentVariables().getEnvs();
boolean includeSystemEnvironmentVariables = this.languageServerPanel.getEnvironmentVariables().isPassParentEnvs();
String configuration = this.languageServerPanel.getConfiguration().getText();
String configurationSchema = this.languageServerPanel.getConfigurationSchemaContent();
String initializationOptions = this.languageServerPanel.getInitializationOptionsWidget().getText();
String clientConfiguration = this.languageServerPanel.getClientConfigurationWidget().getText();
UserDefinedLanguageServerDefinition definition = new UserDefinedLanguageServerDefinition(serverId,
Expand All @@ -302,6 +306,7 @@ protected void doOKAction() {
userEnvironmentVariables,
includeSystemEnvironmentVariables,
configuration,
configurationSchema,
initializationOptions,
clientConfiguration);
LanguageServersRegistry.getInstance().addServerDefinition(project, definition, mappingSettings);
Expand All @@ -315,4 +320,10 @@ protected void textChanged(@NotNull DocumentEvent e) {
}
});
}

@Override
protected void dispose() {
this.languageServerPanel.dispose();
super.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class UserDefinedLanguageServerDefinition extends LanguageServerDefinitio
private boolean includeSystemEnvironmentVariables;
private String configurationContent;
private Object configuration;
private String configurationSchemaContent;
private String initializationOptionsContent;
private Object initializationOptions;
private String clientConfigurationContent;
Expand All @@ -57,6 +58,7 @@ public UserDefinedLanguageServerDefinition(@NotNull String id,
@NotNull Map<String, String> userEnvironmentVariables,
boolean includeSystemEnvironmentVariables,
@Nullable String configurationContent,
@Nullable String configurationSchemaContent,
@Nullable String initializationOptionsContent,
@Nullable String clientConfigurationContent) {
super(id, name, description, true, null, false);
Expand All @@ -65,6 +67,7 @@ public UserDefinedLanguageServerDefinition(@NotNull String id,
this.userEnvironmentVariables = userEnvironmentVariables;
this.includeSystemEnvironmentVariables = includeSystemEnvironmentVariables;
this.configurationContent = configurationContent;
this.configurationSchemaContent = configurationSchemaContent;
this.initializationOptionsContent = initializationOptionsContent;
this.clientConfigurationContent = clientConfigurationContent;
}
Expand All @@ -85,6 +88,7 @@ public UserDefinedLanguageServerDefinition(@NotNull String id,
userEnvironmentVariables,
includeSystemEnvironmentVariables,
configurationContent,
null,
initializationOptionsContent,
null);
}
Expand Down Expand Up @@ -157,6 +161,14 @@ public void setConfigurationContent(String configurationContent) {
this.configuration = null;
}

public String getConfigurationSchemaContent() {
return configurationSchemaContent;
}

public void setConfigurationSchemaContent(String configurationSchemaContent) {
this.configurationSchemaContent = configurationSchemaContent;
}

public String getInitializationOptionsContent() {
return initializationOptionsContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ && isEquals(this.getCommandLine(), settings.getCommandLine())
&& this.getEnvData().isPassParentEnvs() == settings.isIncludeSystemEnvironmentVariables()
&& Objects.equals(this.getMappings(), settings.getMappings())
&& isEquals(this.getConfigurationContent(), settings.getConfigurationContent())
&& isEquals(this.getConfigurationSchemaContent(), settings.getConfigurationSchemaContent())
&& isEquals(this.getInitializationOptionsContent(), settings.getInitializationOptionsContent())
&& isEquals(this.getClientConfigurationContent(), settings.getClientConfigurationContent()))) {
return true;
Expand Down Expand Up @@ -171,6 +172,7 @@ public void reset() {
userDefinedLanguageServerSettings.getUserEnvironmentVariables(),
userDefinedLanguageServerSettings.isIncludeSystemEnvironmentVariables()));
this.setConfigurationContent(userDefinedLanguageServerSettings.getConfigurationContent());
this.setConfigurationSchemaContent(userDefinedLanguageServerSettings.getConfigurationSchemaContent());
this.setInitializationOptionsContent(userDefinedLanguageServerSettings.getInitializationOptionsContent());
this.setClientConfigurationContent(userDefinedLanguageServerSettings.getClientConfigurationContent());

Expand Down Expand Up @@ -256,7 +258,18 @@ public void apply() {
// Update user-defined language server settings
var serverChangedEvent = LanguageServersRegistry.getInstance()
.updateServerDefinition(
new LanguageServersRegistry.UpdateServerDefinitionRequest(project, launch, getDisplayName(), getCommandLine(), getEnvData().getEnvs(), getEnvData().isPassParentEnvs(), getMappings(), getConfigurationContent(), getInitializationOptionsContent(), getClientConfigurationContent()), false);
new LanguageServersRegistry.UpdateServerDefinitionRequest(project,
launch,
getDisplayName(),
getCommandLine(),
getEnvData().getEnvs(),
getEnvData().isPassParentEnvs(),
getMappings(),
getConfigurationContent(),
getConfigurationSchemaContent(),
getInitializationOptionsContent(),
getClientConfigurationContent()),
false);
if (settingsChangedEvent != null) {
// Settings has changed, fire the event
com.redhat.devtools.lsp4ij.settings.UserDefinedLanguageServerSettings
Expand Down Expand Up @@ -400,6 +413,14 @@ public void setConfigurationContent(String configurationContent) {
configuration.setCaretPosition(0);
}

public String getConfigurationSchemaContent() {
return languageServerPanel.getConfigurationSchemaContent();
}

public void setConfigurationSchemaContent(String configurationSchemaContent) {
languageServerPanel.setConfigurationSchemaContent(configurationSchemaContent);
}

public String getInitializationOptionsContent() {
return languageServerPanel.getInitializationOptionsWidget().getText();
}
Expand All @@ -422,6 +443,7 @@ public void setClientConfigurationContent(String configurationContent) {

@Override
public void dispose() {
languageServerPanel.dispose();
}

public List<ServerMappingSettings> getMappings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,30 @@
*******************************************************************************/
package com.redhat.devtools.lsp4ij.settings.jsonSchema;

import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.impl.PsiManagerEx;
import com.intellij.psi.impl.file.impl.FileManagerImpl;
import com.intellij.util.ModalityUiUtil;
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider;
import com.jetbrains.jsonSchema.extension.SchemaType;
import com.jetbrains.jsonSchema.impl.JsonSchemaVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URL;

/**
* Abstract base class for JSON schema file providers that are based on JSON schema files bundled in the plugin distribution.
*/
abstract class AbstractLSPJsonSchemaFileProvider implements JsonSchemaFileProvider {
private final String jsonSchemaPath;

private final String jsonFilename;
private VirtualFile jsonSchemaFile = null;

protected AbstractLSPJsonSchemaFileProvider(@NotNull String jsonSchemaPath, @NotNull String jsonFilename) {
this.jsonSchemaPath = jsonSchemaPath;
protected AbstractLSPJsonSchemaFileProvider(@NotNull String jsonFilename) {
this.jsonFilename = jsonFilename;
}

@Nullable
@Override
public final VirtualFile getSchemaFile() {
if (jsonSchemaFile == null) {
URL jsonSchemaUrl = getClass().getResource(jsonSchemaPath);
String jsonSchemaFileUrl = jsonSchemaUrl != null ? VfsUtil.convertFromUrl(jsonSchemaUrl) : null;
jsonSchemaFile = jsonSchemaFileUrl != null ? VirtualFileManager.getInstance().findFileByUrl(jsonSchemaFileUrl) : null;
// Make sure that the IDE is using the absolute latest version of the JSON schema
if (jsonSchemaFile != null) {
jsonSchemaFile.refresh(true, false);
}
}
return jsonSchemaFile;
}

@Override
public boolean isAvailable(@NotNull VirtualFile file) {
return StringUtil.equalsIgnoreCase(jsonFilename, file.getName());
Expand Down Expand Up @@ -80,4 +64,19 @@ public final JsonSchemaVersion getSchemaVersion() {
public final String getPresentableName() {
return getName();
}

@Override
public boolean isUserVisible() {
return false;
}

protected static void reloadPsi(@NotNull VirtualFile file,
@NotNull Project project) {
final FileManagerImpl fileManager = (FileManagerImpl) PsiManagerEx.getInstanceEx(project).getFileManager();
if (fileManager.findCachedViewProvider(file) != null) {
ModalityUiUtil.invokeLaterIfNeeded(ModalityState.defaultModalityState(), project.getDisposed(),
() -> WriteAction.run(() -> fileManager.forceReload(file))
);
}
}
}
Loading

0 comments on commit 2e44c67

Please sign in to comment.