Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve LSP settings UI #970

Merged
merged 2 commits into from
Jun 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

import com.intellij.openapi.Disposable;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.PortField;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UI;
import com.redhat.devtools.intellij.lsp4ij.LanguageServerBundle;
import com.redhat.devtools.intellij.lsp4ij.LanguageServersRegistry;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
* UI settings view to configure a given language server:
Expand All @@ -35,45 +39,65 @@
public class LanguageServerView implements Disposable {

private final JPanel myMainPanel;
private JBTextField debugPortField = new JBTextField();

private JBCheckBox debugSuspendCheckBox = new JBCheckBox(LanguageServerBundle.message("language.server.debug.suspend"));

private ComboBox<ServerTrace> serverTraceComboBox = new ComboBox<>(new DefaultComboBoxModel<>(ServerTrace.values()));
private final PortField debugPortField = new PortField();
private final JBCheckBox debugSuspendCheckBox = new JBCheckBox(LanguageServerBundle.message("language.server.debug.suspend"));
private final ComboBox<ServerTrace> serverTraceComboBox = new ComboBox<>(new DefaultComboBoxModel<>(ServerTrace.values()));

public LanguageServerView(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) {
this.myMainPanel = FormBuilder.createFormBuilder()
.setFormLeftIndent(10)
.addComponent(createTitleComponent(languageServerDefinition), 1)
.addLabeledComponent(LanguageServerBundle.message("language.server.debug.port"), debugPortField, 1)
.addComponent(debugSuspendCheckBox, 1)
.addLabeledComponent(LanguageServerBundle.message("language.server.trace"), serverTraceComboBox, 1)
JComponent descriptionPanel = createDescription(languageServerDefinition.description.trim());
JPanel settingsPanel = createSettings(descriptionPanel);
TitledBorder title = IdeBorderFactory.createTitledBorder(languageServerDefinition.getDisplayName());
settingsPanel.setBorder(title);
JPanel wrapper = JBUI.Panels.simplePanel(settingsPanel);
wrapper.setBorder(JBUI.Borders.emptyLeft(10));
this.myMainPanel = wrapper;
}

private JPanel createSettings(JComponent description) {
return FormBuilder.createFormBuilder()
.addComponent(description, 0)
.addLabeledComponent(LanguageServerBundle.message("language.server.debug.port"), debugPortField, 5)
.addComponent(debugSuspendCheckBox, 5)
.addLabeledComponent(LanguageServerBundle.message("language.server.trace"), serverTraceComboBox, 5)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

private JComponent createTitleComponent(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) {
JLabel title = new JLabel(languageServerDefinition.getDisplayName());
String description = languageServerDefinition.description;
if (description != null && description.length() > 0) {
// @See com.intellij.internal.ui.ComponentPanelTestAction for more details on how to create comment panels
return UI.PanelFactory.panel(title)
.withComment(description)
.createPanel();
private JComponent createDescription(String description) {
/**
* Normally comments are below the controls.
* Here we want the comments to precede the controls, we therefore create an empty, 0-sized panel.
*/
JPanel titledComponent = UI.PanelFactory.grid().createPanel();
titledComponent.setMinimumSize(JBUI.emptySize());
titledComponent.setPreferredSize(JBUI.emptySize());
if (description != null && !description.isBlank()) {
titledComponent = UI.PanelFactory.panel(titledComponent)
.withComment(description)
.resizeX(true)
.resizeY(true)
.createPanel();
}
return title;
return titledComponent;
}

public JComponent getComponent() {
return myMainPanel;
}

public String getDebugPort() {
return debugPortField.getText();
return debugPortField.getNumber() <= 0? "": Integer.toString(debugPortField.getNumber());
}

public void setDebugPort(String debugPort) {
debugPortField.setText(debugPort);
int port = 0;
try {
port = Integer.parseInt(debugPort);
if (port < debugPortField.getMin() || port > debugPortField.getMax()) {
port = 0;
}
} catch (Exception ignore) {}
debugPortField.setNumber(port);
}

public boolean isDebugSuspend() {
Expand Down