Skip to content

Commit

Permalink
fix: LSP settings at project scope
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Aug 9, 2023
1 parent d21a621 commit aefc4cb
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class LanguageServersRegistry {

public abstract static class LanguageServerDefinition {

enum Scope {
project, application
}
private static final int DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT = 5;

public final @Nonnull
Expand All @@ -51,13 +54,16 @@ public abstract static class LanguageServerDefinition {
public final int lastDocumentDisconnectedTimeout;
private boolean enabled;

public LanguageServerDefinition(@Nonnull String id, @Nonnull String label, String description, boolean isSingleton, Integer lastDocumentDisconnectedTimeout) {
final @Nonnull Scope scope;

public LanguageServerDefinition(@Nonnull String id, @Nonnull String label, String description, boolean isSingleton, Integer lastDocumentDisconnectedTimeout, String scope) {
this.id = id;
this.label = label;
this.description = description;
this.isSingleton = isSingleton;
this.lastDocumentDisconnectedTimeout = lastDocumentDisconnectedTimeout != null && lastDocumentDisconnectedTimeout > 0 ? lastDocumentDisconnectedTimeout : DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT;
this.languageIdMappings = new ConcurrentHashMap<>();
this.scope = scope == null || scope.isBlank()? Scope.application : Scope.valueOf(scope);
setEnabled(true);
}

Expand Down Expand Up @@ -108,7 +114,7 @@ static class ExtensionLanguageServerDefinition extends LanguageServerDefinition
private final ServerExtensionPointBean extension;

public ExtensionLanguageServerDefinition(ServerExtensionPointBean element) {
super(element.id, element.label, element.description, element.singleton, element.lastDocumentDisconnectedTimeout);
super(element.id, element.label, element.description, element.singleton, element.lastDocumentDisconnectedTimeout, element.scope);
this.extension = element;
}

Expand All @@ -120,8 +126,11 @@ public StreamConnectionProvider createConnectionProvider(Project project) {
"Exception occurred while creating an instance of the stream connection provider, you have to define server/@class attribute in the extension point."); //$NON-NLS-1$
}
try {
return (StreamConnectionProvider) project.instantiateClass(extension.getServerImpl(),
extension.getPluginDescriptor().getPluginId());
if (Scope.project == scope) {
return (StreamConnectionProvider) project.instantiateClassWithConstructorInjection(extension.getServerImpl(), project,
extension.getPluginDescriptor().getPluginId());
}
return (StreamConnectionProvider) project.instantiateClass(extension.getServerImpl(), extension.getPluginDescriptor().getPluginId());
} catch (Exception e) {
throw new RuntimeException(
"Exception occurred while creating an instance of the stream connection provider", e); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class ServerExtensionPointBean extends BaseKeyedLazyInstance<StreamConnec
public String serverInterface;
private Class serverClass;

@Attribute("scope")
public String scope;

@Attribute("singleton")
public boolean singleton;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,20 @@ private void configureToolbar(JComponent consoleComponent) {

private JComponent createDetailPanel(LanguageServerTreeNode key) {
LanguageServersRegistry.LanguageServerDefinition serverDefinition = key.getServerDefinition();
Project project = LSPConsoleToolWindowPanel.this.project;
ComboBox<ServerTrace> serverTraceComboBox = new ComboBox<>(new DefaultComboBoxModel<>(ServerTrace.values()));
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings initialSettings = UserDefinedLanguageServerSettings.getInstance().getLanguageServerSettings(serverDefinition.id);
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings initialSettings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(serverDefinition.id);
if (initialSettings != null && initialSettings.getServerTrace() != null) {
serverTraceComboBox.setSelectedItem(initialSettings.getServerTrace());
}
serverTraceComboBox.addItemListener(event -> {
ServerTrace serverTrace = (ServerTrace) event.getItem();
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance().getLanguageServerSettings(serverDefinition.id);
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(serverDefinition.id);
if (settings == null) {
settings = new UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings();
}
settings.setServerTrace(serverTrace);
UserDefinedLanguageServerSettings.getInstance().setLanguageServerSettings(serverDefinition.id, settings);
UserDefinedLanguageServerSettings.getInstance(project).setLanguageServerSettings(serverDefinition.id, settings);
});
return FormBuilder.createFormBuilder()
.setFormLeftIndent(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void updateFoldings(int startLine, int endLine) {
* @return true if language server settings is configured with "verbose" level trace for the language server and false otherwise.
*/
private boolean canApplyFolding() {
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance().getLanguageServerSettings(serverDefinition.id);
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(getProject()).getLanguageServerSettings(serverDefinition.id);
if (settings == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.redhat.devtools.intellij.lsp4ij.console.explorer;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.intellij.lsp4ij.ServerStatus;
import com.redhat.devtools.intellij.lsp4ij.settings.ServerTrace;
import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void handleLSPMessage(Message message, MessageConsumer messageConsumer, L
return;
}
LanguageServerProcessTreeNode processTreeNode = updateServerStatus(languageServer, null, false);
ServerTrace serverTrace = getServerTrace(languageServer.serverDefinition.id);
ServerTrace serverTrace = getServerTrace(explorer.getProject(), languageServer.serverDefinition.id);
if (serverTrace == ServerTrace.off) {
return;
}
Expand Down Expand Up @@ -93,9 +94,9 @@ private TracingMessageConsumer getLSPRequestCacheFor(LanguageServerWrapper langu
}


private static ServerTrace getServerTrace(String languageServerId) {
private static ServerTrace getServerTrace(Project project, String languageServerId) {
ServerTrace serverTrace = null;
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance().getLanguageServerSettings(languageServerId);
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(languageServerId);
if (settings != null) {
serverTrace = settings.getServerTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij.server;

import com.intellij.openapi.project.Project;
import com.redhat.devtools.intellij.lsp4ij.settings.UserDefinedLanguageServerSettings;

import java.io.File;
Expand All @@ -32,10 +33,10 @@ public class JavaProcessCommandBuilder {

private String cp;

public JavaProcessCommandBuilder(String languageId) {
public JavaProcessCommandBuilder(Project project, String languageId) {
this.languageId = languageId;
setJavaPath(computeJavaPath());
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance().getLanguageServerSettings(languageId);
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(languageId);
if (settings != null) {
setDebugPort(settings.getDebugPort());
setDebugSuspend(settings.isDebugSuspend());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.redhat.devtools.intellij.lsp4ij.settings;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.NamedConfigurable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.NlsContexts;
Expand All @@ -33,12 +34,14 @@
public class LanguageServerConfigurable extends NamedConfigurable<LanguageServersRegistry.LanguageServerDefinition> {

private final LanguageServersRegistry.LanguageServerDefinition languageServerDefinition;
private final Project project;

private LanguageServerView myView;

public LanguageServerConfigurable(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition, Runnable updater) {
public LanguageServerConfigurable(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition, Runnable updater, Project project) {
super(false, updater);
this.languageServerDefinition = languageServerDefinition;
this.project = project;
}

@Override
Expand Down Expand Up @@ -77,7 +80,7 @@ public JComponent createOptionsPanel() {

@Override
public boolean isModified() {
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance()
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project)
.getLanguageServerSettings(languageServerDefinition.id);
if (settings == null) {
return true;
Expand All @@ -93,13 +96,13 @@ public void apply() throws ConfigurationException {
settings.setDebugPort(myView.getDebugPort());
settings.setDebugSuspend(myView.isDebugSuspend());
settings.setServerTrace(myView.getServerTrace());
UserDefinedLanguageServerSettings.getInstance().setLanguageServerSettings(languageServerDefinition.id, settings);
UserDefinedLanguageServerSettings.getInstance(project).setLanguageServerSettings(languageServerDefinition.id, settings);
}

@Override
public void reset() {
ServerTrace serverTrace = ServerTrace.off;
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance()
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project)
.getLanguageServerSettings(languageServerDefinition.id);
if (settings != null) {
myView.setDebugPort(settings.getDebugPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.redhat.devtools.intellij.lsp4ij.settings;

import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MasterDetailsComponent;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.ui.TreeUIHelper;
Expand Down Expand Up @@ -41,12 +42,14 @@ public class LanguageServerListConfigurable extends MasterDetailsComponent imple
@NonNls
private static final String ID = "LanguageServers";

private final Project project;
private final Set<LanguageServersRegistry.LanguageServerDefinition> languageServeDefinitions;

private boolean isTreeInitialized;

public LanguageServerListConfigurable(Set<LanguageServersRegistry.LanguageServerDefinition> languageServeDefinitions) {
this.languageServeDefinitions = languageServeDefinitions;
public LanguageServerListConfigurable(Project project) {
this.project = project;
this.languageServeDefinitions = LanguageServersRegistry.getInstance().getAllDefinitions();
}

@Override
Expand Down Expand Up @@ -83,7 +86,7 @@ protected void initTree() {
}

private MyNode addLanguageServerDefinitionNode(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) {
MyNode node = new MyNode(new LanguageServerConfigurable(languageServerDefinition, TREE_UPDATER));
MyNode node = new MyNode(new LanguageServerConfigurable(languageServerDefinition, TREE_UPDATER, project));
addNode(node, myRoot);
return node;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij.settings;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.XCollection;
import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -36,14 +36,14 @@
*/
@State(
name = "LanguageServerSettingsState",
storages = {@Storage("LanguageServersSettings.xml")}
storages = @Storage("LanguageServersSettings.xml")
)
public class UserDefinedLanguageServerSettings implements PersistentStateComponent<UserDefinedLanguageServerSettings.MyState> {

public volatile MyState myState = new MyState();
private volatile MyState myState = new MyState();

public static UserDefinedLanguageServerSettings getInstance() {
return ServiceManager.getService(UserDefinedLanguageServerSettings.class);
public static UserDefinedLanguageServerSettings getInstance(@NotNull Project project) {
return project.getService(UserDefinedLanguageServerSettings.class);
}

@Nullable
Expand Down Expand Up @@ -98,7 +98,7 @@ public void setServerTrace(ServerTrace serverTrace) {
}
}

static class MyState {
public static class MyState {
@Tag("state")
@XCollection
public Map<String, LanguageServerDefinitionSettings> myState = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.intellij.quarkus.TelemetryService;
import com.redhat.devtools.intellij.lsp4ij.server.JavaProcessCommandBuilder;
import com.redhat.devtools.intellij.lsp4ij.server.ProcessStreamConnectionProvider;
Expand All @@ -30,12 +31,12 @@
*/
public class QuarkusServer extends ProcessStreamConnectionProvider {

public QuarkusServer() {
public QuarkusServer(Project project) {
IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId("com.redhat.devtools.intellij.quarkus"));
File lsp4mpServerPath = new File(descriptor.getPath(), "lib/server/org.eclipse.lsp4mp.ls-uber.jar");
File quarkusServerPath = new File(descriptor.getPath(), "lib/server/com.redhat.quarkus.ls.jar");

List<String> commands = new JavaProcessCommandBuilder("quarkus")
List<String> commands = new JavaProcessCommandBuilder(project,"quarkus")
.setJar(lsp4mpServerPath.getAbsolutePath())
.setCp(quarkusServerPath.getAbsolutePath())
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.intellij.quarkus.TelemetryService;
import com.redhat.devtools.intellij.lsp4ij.server.JavaProcessCommandBuilder;
import com.redhat.devtools.intellij.lsp4ij.server.ProcessStreamConnectionProvider;
Expand All @@ -29,11 +30,11 @@
*/
public class QuteServer extends ProcessStreamConnectionProvider {

public QuteServer() {
public QuteServer(Project project) {
IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId("com.redhat.devtools.intellij.quarkus"));
File quteServerPath = new File(descriptor.getPath(), "lib/server/com.redhat.qute.ls-uber.jar");

List<String> commands = new JavaProcessCommandBuilder("qute")
List<String> commands = new JavaProcessCommandBuilder(project, "qute")
.setJar(quteServerPath.getAbsolutePath())
.create();
commands.add("-DrunAsync=true");
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/lsp4ij-quarkus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
label="Tools for MicroProfile"
class="com.redhat.devtools.intellij.quarkus.lsp.QuarkusServer"
clientImpl="com.redhat.devtools.intellij.quarkus.lsp.QuarkusLanguageClient"
serverInterface="org.eclipse.lsp4mp.ls.api.MicroProfileLanguageServerAPI">
serverInterface="org.eclipse.lsp4mp.ls.api.MicroProfileLanguageServerAPI"
scope="project">
<description><![CDATA[
The <a href="https://github.com/eclipse/lsp4mp">MicroProfile language server</a> provides <a href="https://microprofile.io/">MicroProfile</a> support in:
<ul>
Expand Down
Loading

0 comments on commit aefc4cb

Please sign in to comment.