Skip to content

Commit

Permalink
fix: synchronize LSP console trace level with settings page
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Aug 10, 2023
1 parent e56076d commit 0f6c2bf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;

/**
* LSP consoles
Expand Down Expand Up @@ -139,6 +141,8 @@ private class ConsoleContentPanel extends SimpleCardLayoutPanel<JComponent> {

private ConsoleView consoleView;

private final Set<Runnable> settingsChangeListeners = new HashSet<>();

public ConsoleContentPanel(DefaultMutableTreeNode key) {
if (key instanceof LanguageServerTreeNode) {
add(createDetailPanel((LanguageServerTreeNode) key), NAME_VIEW_DETAIL);
Expand Down Expand Up @@ -185,6 +189,13 @@ private JComponent createDetailPanel(LanguageServerTreeNode key) {
settings.setServerTrace(serverTrace);
UserDefinedLanguageServerSettings.getInstance(project).setLanguageServerSettings(serverDefinition.id, settings);
});

// Add a settings listener to keep serverTraceComboBox in sync with changes coming from the LSP settings page
// See https://github.com/redhat-developer/intellij-quarkus/issues/1062
Runnable settingsChangeListener = createSettingsChangeListener(serverDefinition.id, serverTraceComboBox);
UserDefinedLanguageServerSettings.getInstance(getProject()).addChangeHandler(settingsChangeListener);
settingsChangeListeners.add(settingsChangeListener);

return FormBuilder.createFormBuilder()
.setFormLeftIndent(10)
.addComponent(createTitleComponent(serverDefinition), 1)
Expand All @@ -193,6 +204,23 @@ private JComponent createDetailPanel(LanguageServerTreeNode key) {
.getPanel();
}


private Runnable createSettingsChangeListener(String id, ComboBox<ServerTrace> serverTraceComboBox) {
return new Runnable() {
@Override
public void run() {
if (isDisposed()) {
return;
}
UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(id);
ServerTrace newServerTrace = settings.getServerTrace();
if (newServerTrace != null && !newServerTrace.equals(serverTraceComboBox.getSelectedItem())) {
serverTraceComboBox.setSelectedItem(newServerTrace);
}
}
};
}

private JComponent createTitleComponent(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) {
JLabel title = new JLabel(languageServerDefinition.getDisplayName());
String description = languageServerDefinition.description;
Expand Down Expand Up @@ -224,6 +252,10 @@ public void showError(Throwable exception) {

@Override
public void dispose() {
for (Runnable settingsChangeListener : settingsChangeListeners) {
UserDefinedLanguageServerSettings.getInstance(getProject()).removeChangeHandler(settingsChangeListener);
}
settingsChangeListeners.clear();
super.dispose();
if (consoleView != null) {
consoleView.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.XCollection;
import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.intellij.lsp4mp4ij.settings.UserDefinedMicroProfileSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -42,6 +45,8 @@ public class UserDefinedLanguageServerSettings implements PersistentStateCompone

private volatile MyState myState = new MyState();

private final List<Runnable> myChangeHandlers = ContainerUtil.createConcurrentList();

public static UserDefinedLanguageServerSettings getInstance(@NotNull Project project) {
return project.getService(UserDefinedLanguageServerSettings.class);
}
Expand All @@ -63,6 +68,7 @@ public LanguageServerDefinitionSettings getLanguageServerSettings(String languag

public void setLanguageServerSettings(String languageSeverId, LanguageServerDefinitionSettings settings) {
myState.myState.put(languageSeverId, settings);
fireStateChanged();
}

public static class LanguageServerDefinitionSettings {
Expand Down Expand Up @@ -108,4 +114,29 @@ public static class MyState {

}

/**
* Adds the given changeHandler to the list of registered change handlers
* @param changeHandler the changeHandler to remove
*/
public void addChangeHandler(@NotNull Runnable changeHandler) {
myChangeHandlers.add(changeHandler);
}

/**
* Removes the given changeHandler from the list of registered change handlers
* @param changeHandler the changeHandler to remove
*/
public void removeChangeHandler(@NotNull Runnable changeHandler) {
myChangeHandlers.remove(changeHandler);
}

/**
* Notifies all registered change handlers when the state changed
*/
public void fireStateChanged() {
for (Runnable handler : myChangeHandlers) {
handler.run();
}
}

}

0 comments on commit 0f6c2bf

Please sign in to comment.