|
| 1 | +package org.nixos.idea.lsp; |
| 2 | + |
| 3 | +import com.intellij.openapi.options.Configurable; |
| 4 | +import com.intellij.openapi.options.ConfigurationException; |
| 5 | +import com.intellij.openapi.options.SearchableConfigurable; |
| 6 | +import com.intellij.openapi.project.Project; |
| 7 | +import com.intellij.openapi.project.ProjectManager; |
| 8 | +import com.intellij.openapi.util.NlsContexts; |
| 9 | +import com.intellij.platform.lsp.api.LspServerManager; |
| 10 | +import com.intellij.ui.RawCommandLineEditor; |
| 11 | +import com.intellij.ui.TitledSeparator; |
| 12 | +import com.intellij.ui.components.JBCheckBox; |
| 13 | +import com.intellij.util.ui.FormBuilder; |
| 14 | +import org.jetbrains.annotations.NonNls; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | +import org.nixos.idea.lsp.ui.CommandSuggestionsPopup; |
| 18 | + |
| 19 | +import javax.swing.JComponent; |
| 20 | +import javax.swing.JPanel; |
| 21 | + |
| 22 | +public class NixLspSettingsConfigurable implements SearchableConfigurable, Configurable.Beta { |
| 23 | + |
| 24 | + private @Nullable JBCheckBox myEnabled; |
| 25 | + private @Nullable RawCommandLineEditor myCommand; |
| 26 | + |
| 27 | + @Override |
| 28 | + public @NotNull @NonNls String getId() { |
| 29 | + return "org.nixos.idea.lsp.NixLspSettings"; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public @NlsContexts.ConfigurableName String getDisplayName() { |
| 34 | + return "Language Server (LSP)"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public @Nullable JComponent createComponent() { |
| 39 | + myEnabled = new JBCheckBox("Enable language server"); |
| 40 | + myEnabled.addChangeListener(e -> updateUiState()); |
| 41 | + |
| 42 | + myCommand = new RawCommandLineEditor(); |
| 43 | + myCommand.getEditorField().getEmptyText().setText("Command to start Language Server"); |
| 44 | + myCommand.getEditorField().getAccessibleContext().setAccessibleName("Command to start Language Server"); |
| 45 | + myCommand.getEditorField().setMargin(myEnabled.getMargin()); |
| 46 | + new CommandSuggestionsPopup(myCommand, NixLspSettings.getInstance().getCommandHistory()).install(); |
| 47 | + |
| 48 | + return FormBuilder.createFormBuilder() |
| 49 | + .addComponent(myEnabled) |
| 50 | + .addComponent(new TitledSeparator("Language Server Configuration")) |
| 51 | + .addLabeledComponent("Command: ", myCommand) |
| 52 | + .addComponentFillVertically(new JPanel(), 0) |
| 53 | + .getPanel(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void reset() { |
| 58 | + assert myEnabled != null; |
| 59 | + assert myCommand != null; |
| 60 | + |
| 61 | + NixLspSettings settings = NixLspSettings.getInstance(); |
| 62 | + myEnabled.setSelected(settings.isEnabled()); |
| 63 | + myCommand.setText(settings.getCommand()); |
| 64 | + |
| 65 | + updateUiState(); |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("UnstableApiUsage") |
| 69 | + @Override |
| 70 | + public void apply() throws ConfigurationException { |
| 71 | + assert myEnabled != null; |
| 72 | + assert myCommand != null; |
| 73 | + |
| 74 | + NixLspSettings settings = NixLspSettings.getInstance(); |
| 75 | + settings.setEnabled(myEnabled.isSelected()); |
| 76 | + settings.setCommand(myCommand.getText()); |
| 77 | + |
| 78 | + for (Project project : ProjectManager.getInstance().getOpenProjects()) { |
| 79 | + LspServerManager.getInstance(project).stopAndRestartIfNeeded(NixLspServerSupportProvider.class); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean isModified() { |
| 85 | + assert myEnabled != null; |
| 86 | + assert myCommand != null; |
| 87 | + |
| 88 | + NixLspSettings settings = NixLspSettings.getInstance(); |
| 89 | + return Configurable.isCheckboxModified(myEnabled, settings.isEnabled()) || |
| 90 | + Configurable.isFieldModified(myCommand.getTextField(), settings.getCommand()); |
| 91 | + } |
| 92 | + |
| 93 | + private void updateUiState() { |
| 94 | + assert myEnabled != null; |
| 95 | + assert myCommand != null; |
| 96 | + |
| 97 | + myCommand.setEnabled(myEnabled.isSelected()); |
| 98 | + } |
| 99 | +} |
0 commit comments