From 6b6e01d7b22053d1aaa9031f063d2c4288bb138e Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 7 Dec 2023 16:06:33 +0100 Subject: [PATCH] fix: fill LSP consoles with server which have no language mappings. Signed-off-by: azerr --- .../lsp4ij/LanguageServersRegistry.java | 47 ++++++++++++------- .../lsp4ij/commands/CommandExecutor.java | 2 +- .../explorer/LanguageServerExplorer.java | 7 ++- .../LanguageServerListConfigurable.java | 9 ++-- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java index 7b1996a19..df79a61f8 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java @@ -183,6 +183,8 @@ public static LanguageServersRegistry getInstance() { return INSTANCE; } + private final Map serverDefinitions = new HashMap<>(); + private final List connections = new ArrayList<>(); private Map serverIcons = new HashMap<>(); @@ -192,11 +194,10 @@ private LanguageServersRegistry() { } private void initialize() { - Map servers = new HashMap<>(); List languageMappings = new ArrayList<>(); for (ServerExtensionPointBean server : ServerExtensionPointBean.EP_NAME.getExtensions()) { if (server.id != null && !server.id.isEmpty()) { - servers.put(server.id, new ExtensionLanguageServerDefinition(server)); + serverDefinitions.put(server.id, new ExtensionLanguageServerDefinition(server)); } } for (LanguageMappingExtensionPointBean extension : LanguageMappingExtensionPointBean.EP_NAME.getExtensions()) { @@ -211,7 +212,7 @@ private void initialize() { } for (LanguageMapping mapping : languageMappings) { - LanguageServerDefinition lsDefinition = servers.get(mapping.languageId); + LanguageServerDefinition lsDefinition = serverDefinitions.get(mapping.languageId); if (lsDefinition != null) { registerAssociation(lsDefinition, mapping); } else { @@ -248,16 +249,6 @@ public void registerAssociation(@NotNull LanguageServerDefinition serverDefiniti connections.add(new ContentTypeToLanguageServerDefinition(language, serverDefinition, mapping.getDocumentMatcher())); } - public @Nullable - LanguageServerDefinition getDefinition(@NonNull String languageServerId) { - for (ContentTypeToLanguageServerDefinition mapping : this.connections) { - if (mapping.getValue().id.equals(languageServerId)) { - return mapping.getValue(); - } - } - return null; - } - /** * internal class to capture content-type mappings for language servers */ @@ -284,13 +275,33 @@ public DocumentMatcher getDocumentMatcher() { } } - public Set getAllDefinitions() { - return connections - .stream() - .map(AbstractMap.SimpleEntry::getValue) - .collect(Collectors.toSet()); + /** + * Returns the language server definition for the given language server id and null otherwise. + * + * @param languageServerId the language server id. + * + * @return the language server definition for the given language server id and null otherwise. + */ + public @Nullable LanguageServerDefinition getServerDefinition(@NonNull String languageServerId) { + return serverDefinitions.get(languageServerId); } + /** + * Returns the registered server definitions. + * + * @return the registered server definitions. + */ + public Collection getServerDefinitions() { + return serverDefinitions.values(); + } + + /** + * Returns true if the given language is supported by a language server and false otherwise. + * + * @param language the IJ language + * + * @return true if the given language is supported by a language server and false otherwise. + */ public boolean isLanguageSupported(@NotNull Language language) { return connections .stream() diff --git a/src/main/java/com/redhat/devtools/lsp4ij/commands/CommandExecutor.java b/src/main/java/com/redhat/devtools/lsp4ij/commands/CommandExecutor.java index 39d339b1a..2498d4b81 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/commands/CommandExecutor.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/commands/CommandExecutor.java @@ -109,7 +109,7 @@ private static boolean executeCommandServerSide(@NotNull Command command, @Nulla return false; } LanguageServersRegistry.LanguageServerDefinition languageServerDefinition = LanguageServersRegistry.getInstance() - .getDefinition(languageServerId); + .getServerDefinition(languageServerId); if (languageServerDefinition == null) { return false; } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/console/explorer/LanguageServerExplorer.java b/src/main/java/com/redhat/devtools/lsp4ij/console/explorer/LanguageServerExplorer.java index 898936d0e..3424f2c3c 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/console/explorer/LanguageServerExplorer.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/console/explorer/LanguageServerExplorer.java @@ -49,7 +49,7 @@ public class LanguageServerExplorer extends SimpleToolWindowPanel implements Dis private final LanguageServerExplorerLifecycleListener listener; private boolean disposed; - private TreeSelectionListener treeSelectionListener = event -> { + private final TreeSelectionListener treeSelectionListener = event -> { if (isDisposed()) { return; } @@ -101,7 +101,7 @@ private Tree buildTree() { tree.setRootVisible(false); // Fill tree will all language server definitions, ordered alphabetically - LanguageServersRegistry.getInstance().getAllDefinitions().stream() + LanguageServersRegistry.getInstance().getServerDefinitions().stream() .sorted(Comparator.comparing(LanguageServersRegistry.LanguageServerDefinition::getDisplayName)) .map(LanguageServerTreeNode::new) .forEach(top::add); @@ -120,8 +120,7 @@ public void invokePopup(Component comp, int x, int y) { if (path != null) { DefaultActionGroup group = null; Object node = path.getLastPathComponent(); - if (node instanceof LanguageServerProcessTreeNode) { - LanguageServerProcessTreeNode processTreeNode = (LanguageServerProcessTreeNode) node; + if (node instanceof LanguageServerProcessTreeNode processTreeNode) { switch (processTreeNode.getServerStatus()) { case starting: case started: diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerListConfigurable.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerListConfigurable.java index 19d050d6e..7979d1557 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerListConfigurable.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerListConfigurable.java @@ -26,8 +26,8 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.Collection; import java.util.Objects; -import java.util.Set; /** * UI settings which show: @@ -43,13 +43,13 @@ public class LanguageServerListConfigurable extends MasterDetailsComponent imple private static final String ID = "LanguageServers"; private final Project project; - private final Set languageServeDefinitions; + private final Collection languageServeDefinitions; private boolean isTreeInitialized; public LanguageServerListConfigurable(Project project) { this.project = project; - this.languageServeDefinitions = LanguageServersRegistry.getInstance().getAllDefinitions(); + this.languageServeDefinitions = LanguageServersRegistry.getInstance().getServerDefinitions(); } @Override @@ -85,10 +85,9 @@ protected void initTree() { .installTreeSpeedSearch(myTree, treePath -> ((MyNode) treePath.getLastPathComponent()).getDisplayName(), true); } - private MyNode addLanguageServerDefinitionNode(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) { + private void addLanguageServerDefinitionNode(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) { MyNode node = new MyNode(new LanguageServerConfigurable(languageServerDefinition, TREE_UPDATER, project)); addNode(node, myRoot); - return node; } private void reloadTree() {