-
Notifications
You must be signed in to change notification settings - Fork 28
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: Validate preferences in lsp console #222
base: main
Are you sure you want to change the base?
Changes from all commits
be8a0fd
1527f02
a43d3ab
d76179e
36990bf
922296b
734dad2
b52251b
cd623f8
cf6779f
6c8f0c0
2de6a9f
80a1f95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,12 @@ | |
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
* Mitja Leino <[email protected]> - Extend ValidatableDialog for validations | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.launching.ui; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.ComboBox; | ||
import com.intellij.openapi.ui.DialogWrapper; | ||
import com.intellij.openapi.ui.ValidationInfo; | ||
import com.intellij.ui.DocumentAdapter; | ||
import com.intellij.ui.SimpleListCellRenderer; | ||
|
@@ -25,6 +25,7 @@ | |
import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; | ||
import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; | ||
import com.redhat.devtools.lsp4ij.settings.ui.LanguageServerPanel; | ||
import com.redhat.devtools.lsp4ij.settings.ui.ValidatableDialog; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
|
@@ -40,7 +41,7 @@ | |
/** | ||
* New language server dialog. | ||
*/ | ||
public class NewLanguageServerDialog extends DialogWrapper { | ||
public class NewLanguageServerDialog extends ValidatableDialog { | ||
|
||
private final ComboBox<LanguageServerTemplate> templateCombo = new ComboBox<>(new DefaultComboBoxModel<>(getLanguageServerTemplates())); | ||
private final Project project; | ||
|
@@ -70,7 +71,7 @@ | |
// Template combo | ||
createTemplateCombo(builder); | ||
// Create server name, command line, mappings, configuration UI | ||
this.languageServerPanel = new LanguageServerPanel(builder, null, LanguageServerPanel.EditionMode.NEW_USER_DEFINED); | ||
this.languageServerPanel = new LanguageServerPanel(builder, null, LanguageServerPanel.EditionMode.NEW_USER_DEFINED, this); | ||
|
||
// Add validation | ||
addValidator(this.languageServerPanel.getServerName()); | ||
|
@@ -165,38 +166,9 @@ | |
|
||
@Override | ||
protected @NotNull List<ValidationInfo> doValidateAll() { | ||
List<ValidationInfo> validations = new ArrayList<>(); | ||
addValidationInfo(validateServerName(), validations); | ||
addValidationInfo(validateCommand(), validations); | ||
return validations; | ||
return languageServerPanel.doValidateAll(); | ||
Check warning on line 169 in src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java Codecov / codecov/patchsrc/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java#L169
|
||
} | ||
|
||
private void addValidationInfo(ValidationInfo validationInfo, List<ValidationInfo> validations) { | ||
if (validationInfo == null) { | ||
return; | ||
} | ||
validations.add((validationInfo)); | ||
} | ||
|
||
private ValidationInfo validateServerName() { | ||
var serverName = this.languageServerPanel.getServerName(); | ||
if (serverName.getText().isBlank()) { | ||
String errorMessage = LanguageServerBundle.message("new.language.server.dialog.validation.serverName.must.be.set"); | ||
return new ValidationInfo(errorMessage, serverName); | ||
} | ||
return null; | ||
} | ||
|
||
private ValidationInfo validateCommand() { | ||
var commandLine = this.languageServerPanel.getCommandLine(); | ||
if (commandLine.getText().isBlank()) { | ||
String errorMessage = LanguageServerBundle.message("new.language.server.dialog.validation.commandLine.must.be.set"); | ||
return new ValidationInfo(errorMessage, commandLine); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
@Override | ||
protected void doOKAction() { | ||
super.doOKAction(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
* Mitja Leino <[email protected]> - Extend ValidatableDialog for validations | ||
*******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.settings; | ||
|
||
|
@@ -19,6 +20,7 @@ | |
import com.intellij.openapi.fileTypes.FileNameMatcher; | ||
import com.intellij.openapi.fileTypes.FileType; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.ValidationInfo; | ||
import com.intellij.ui.IdeBorderFactory; | ||
import com.intellij.util.ui.FormBuilder; | ||
import com.intellij.util.ui.JBUI; | ||
|
@@ -31,13 +33,13 @@ | |
import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; | ||
import com.redhat.devtools.lsp4ij.settings.ui.LanguageServerPanel; | ||
import com.redhat.devtools.lsp4ij.settings.ui.ServerMappingsPanel; | ||
import com.redhat.devtools.lsp4ij.settings.ui.ValidatableDialog; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
import javax.swing.border.TitledBorder; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
|
@@ -50,7 +52,7 @@ | |
* <li>Suspend and wait for a debugger?</li> | ||
* </ul> | ||
*/ | ||
public class LanguageServerView implements Disposable { | ||
public class LanguageServerView extends ValidatableDialog implements Disposable { | ||
|
||
private final LanguageServerNameProvider languageServerNameProvider; | ||
|
||
|
@@ -70,6 +72,7 @@ | |
@Nullable LanguageServerNameProvider languageServerNameProvider, | ||
@NotNull Project project | ||
) { | ||
super(project); | ||
this.languageServerDefinition = languageServerDefinition; | ||
this.languageServerNameProvider = languageServerNameProvider; | ||
this.project = project; | ||
|
@@ -285,7 +288,7 @@ | |
this.languageServerPanel = new LanguageServerPanel(builder, | ||
description, | ||
launchingServerDefinition ? LanguageServerPanel.EditionMode.EDIT_USER_DEFINED : | ||
LanguageServerPanel.EditionMode.EDIT_EXTENSION); | ||
LanguageServerPanel.EditionMode.EDIT_EXTENSION, this); | ||
this.mappingPanel = languageServerPanel.getMappingsPanel(); | ||
return builder | ||
.addComponentFillVertically(new JPanel(), 50) | ||
|
@@ -410,6 +413,16 @@ | |
initializationOptions.setCaretPosition(0); | ||
} | ||
|
||
@Override | ||
protected @Nullable JComponent createCenterPanel() { | ||
return myMainPanel; | ||
} | ||
|
||
@Override | ||
public @NotNull List<ValidationInfo> doValidateAll() { | ||
return languageServerPanel.doValidateAll(); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,23 +10,49 @@ | |
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
* Mitja Leino <[email protected]> - Implement ValidatableConsoleWidget | ||
*******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.settings.ui; | ||
|
||
import com.intellij.openapi.ui.ValidationInfo; | ||
import com.intellij.ui.components.JBTextArea; | ||
import com.intellij.util.ui.JBFont; | ||
import com.redhat.devtools.lsp4ij.LanguageServerBundle; | ||
|
||
import javax.swing.border.Border; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Command line widget used to fill the command to start a language server. | ||
* Command line widget used to fill the command to start a language | ||
* server when creating a new or modifying an existing LS configuration | ||
*/ | ||
public class CommandLineWidget extends JBTextArea { | ||
public class CommandLineWidget extends JBTextArea implements ValidatableConsoleWidget { | ||
private final String errorMessage = LanguageServerBundle.message("new.language.server.dialog.validation.commandLine.must.be.set"); | ||
private final transient Border normalBorder; | ||
|
||
public CommandLineWidget() { | ||
super(5, 0); | ||
super.setLineWrap(true); | ||
super.setWrapStyleWord(true); | ||
super.setFont(JBFont.regular()); | ||
super.getEmptyText().setText(LanguageServerBundle.message("language.server.command.emptyText")); | ||
this.normalBorder = this.getBorder(); | ||
} | ||
|
||
@Override | ||
public void validate(List<ValidationInfo> validations) { | ||
List<ValidationInfo> widgetValidations = new ArrayList<>(); | ||
|
||
if (getDocument() != null && getText().isBlank()) { | ||
widgetValidations.add(new ValidationInfo(errorMessage, this)); | ||
} | ||
|
||
if (widgetValidations.isEmpty()) { | ||
this.setBorder(normalBorder); | ||
} else { | ||
validations.addAll(widgetValidations); | ||
setErrorBorder(this); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,18 @@ | |
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
* Mitja Leino <[email protected]> - Add DialogWrapper for validations | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.settings.ui; | ||
|
||
import com.intellij.execution.configuration.EnvironmentVariablesComponent; | ||
import com.intellij.ide.BrowserUtil; | ||
import com.intellij.openapi.ui.ComboBox; | ||
import com.intellij.ui.ContextHelpLabel; | ||
import com.intellij.ui.PortField; | ||
import com.intellij.ui.SimpleListCellRenderer; | ||
import com.intellij.openapi.ui.ValidationInfo; | ||
import com.intellij.ui.*; | ||
import com.intellij.ui.components.JBCheckBox; | ||
import com.intellij.ui.components.JBScrollPane; | ||
import com.intellij.ui.components.JBTabbedPane; | ||
import com.intellij.ui.components.JBTextField; | ||
import com.intellij.ui.scale.JBUIScale; | ||
import com.intellij.util.ui.FormBuilder; | ||
import com.intellij.util.ui.components.BorderLayoutPanel; | ||
|
@@ -29,7 +28,11 @@ | |
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import javax.swing.event.DocumentEvent; | ||
import javax.swing.text.JTextComponent; | ||
import java.awt.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Language server panel which show information about language server in several tabs: | ||
|
@@ -49,7 +52,7 @@ | |
EDIT_EXTENSION; | ||
} | ||
|
||
private JBTextField serverName; | ||
private ServerNameWidget serverName; | ||
private EnvironmentVariablesComponent environmentVariables; | ||
private CommandLineWidget commandLine; | ||
private ServerMappingsPanel mappingsPanel; | ||
|
@@ -61,8 +64,10 @@ | |
private LanguageServerConfigurationWidget configurationWidget; | ||
|
||
private LanguageServerInitializationOptionsWidget initializationOptionsWidget; | ||
private final ValidatableDialog dialogWrapper; | ||
|
||
public LanguageServerPanel(FormBuilder builder, JComponent description, EditionMode mode) { | ||
public LanguageServerPanel(FormBuilder builder, JComponent description, EditionMode mode, ValidatableDialog dialogWrapper) { | ||
this.dialogWrapper = dialogWrapper; | ||
createUI(builder, description, mode); | ||
} | ||
|
||
|
@@ -80,6 +85,16 @@ | |
} | ||
// Debug tab | ||
addDebugTab(tabbedPane, mode); | ||
|
||
// Add validation | ||
var serverNameWidget = getServerName(); | ||
if (serverNameWidget != null) { | ||
addValidator(serverNameWidget); | ||
} | ||
var commandLineWidget = getCommandLine(); | ||
if (commandLineWidget != null) { | ||
addValidator(getCommandLine()); | ||
} | ||
} | ||
|
||
private void addServerTab(JBTabbedPane tabbedPane, JComponent description, EditionMode mode) { | ||
|
@@ -176,7 +191,7 @@ | |
} | ||
|
||
private void createServerNameField(FormBuilder builder) { | ||
serverName = new JBTextField(); | ||
serverName = new ServerNameWidget(); | ||
builder.addLabeledComponent(LanguageServerBundle.message("language.server.serverName"), serverName); | ||
} | ||
|
||
|
@@ -201,7 +216,7 @@ | |
builder.addLabeledComponent(LanguageServerBundle.message("language.server.initializationOptions"), scrollPane, true); | ||
} | ||
|
||
public JBTextField getServerName() { | ||
public ServerNameWidget getServerName() { | ||
return serverName; | ||
} | ||
|
||
|
@@ -241,4 +256,25 @@ | |
return errorReportingKindCombo; | ||
} | ||
|
||
public @NotNull List<ValidationInfo> doValidateAll() { | ||
List<ValidationInfo> validations = new ArrayList<>(); | ||
var serverNameWidget = getServerName(); | ||
Check warning on line 261 in src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java Codecov / codecov/patchsrc/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java#L260-L261
|
||
MituuZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (serverNameWidget != null) { | ||
serverNameWidget.validate(validations); | ||
} | ||
var commandLineWidget = getCommandLine(); | ||
if (commandLineWidget != null) { | ||
commandLineWidget.validate(validations); | ||
} | ||
return validations; | ||
} | ||
|
||
private void addValidator(JTextComponent textComponent) { | ||
textComponent.getDocument().addDocumentListener(new DocumentAdapter() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do that here but in the ValidableConsoleWidget impl in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think addValidator should just add in a list CommandLineWidget, ServerNameWidget (list of ValidatableConsoleWidget) and in doValidateAll should just iterate from this list. private void addValidatableWidget(@Nullable ValidatableConsoleWidget validatableWidget) {
if (validatableWidget == null) {
return;
}
validatableWidget.initializeValidator(this);
this.validatableWidgets.add(validatableWidget)
} |
||
@Override | ||
protected void textChanged(@NotNull DocumentEvent e) { | ||
dialogWrapper.refreshValidation(); | ||
} | ||
Check warning on line 277 in src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java Codecov / codecov/patchsrc/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java#L276-L277
|
||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Mitja Leino <[email protected]> - Initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.settings.ui; | ||
MituuZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.intellij.openapi.ui.ValidationInfo; | ||
import com.intellij.ui.components.JBTextField; | ||
import com.redhat.devtools.lsp4ij.LanguageServerBundle; | ||
|
||
import javax.swing.border.Border; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Server name widget that contains the server name when creating a new LS configuration | ||
*/ | ||
public class ServerNameWidget extends JBTextField implements ValidatableConsoleWidget { | ||
MituuZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final String errorMessage = LanguageServerBundle.message("new.language.server.dialog.validation.serverName.must.be.set"); | ||
private final transient Border originalBorder; | ||
|
||
public ServerNameWidget() { | ||
this.originalBorder = this.getBorder(); | ||
} | ||
|
||
@Override | ||
public void validate(List<ValidationInfo> validations) { | ||
List<ValidationInfo> widgetValidations = new ArrayList<>(); | ||
|
||
if (getDocument() != null && getText().isBlank()) { | ||
widgetValidations.add(new ValidationInfo(errorMessage, this)); | ||
} | ||
|
||
if (widgetValidations.isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do that here, move this code in the new |
||
this.setBorder(originalBorder); | ||
} else { | ||
validations.addAll(widgetValidations); | ||
setErrorBorder(this); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isValid() { | ||
return getDocument() != null && !getText().isBlank(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should look like this: