From 1682c1e4d49c4d66742825ba811845f79d7912e7 Mon Sep 17 00:00:00 2001 From: azerr Date: Mon, 26 Jun 2023 16:09:17 +0200 Subject: [PATCH] fix: Start/stop actions from language server explorer do nothing. Signed-off-by: azerr --- .../explorer/LanguageServerExplorer.java | 6 +- ...anguageServerExplorerTreeDataProvider.java | 48 ------------ .../explorer/actions/RestartServerAction.java | 13 ++-- .../explorer/actions/StopServerAction.java | 14 ++-- .../console/explorer/actions/TreeAction.java | 77 +++++++++++++++++++ 5 files changed, 91 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorerTreeDataProvider.java create mode 100644 src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/TreeAction.java diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorer.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorer.java index 90426a77d..45f98c554 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorer.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorer.java @@ -13,7 +13,6 @@ *******************************************************************************/ package com.redhat.devtools.intellij.lsp4ij.console.explorer; -import com.intellij.ide.DataManager; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.project.Project; @@ -48,7 +47,7 @@ public class LanguageServerExplorer extends SimpleToolWindowPanel implements Dis private boolean disposed; private TreeSelectionListener treeSelectionListener = event -> { - if(isDisposed()) { + if (isDisposed()) { return; } TreePath selectionPath = event.getPath(); @@ -107,9 +106,6 @@ private Tree buildTree() { tree.addTreeSelectionListener(treeSelectionListener); - DataProvider newDataProvider = new LanguageServerExplorerTreeDataProvider(tree); - DataManager.registerDataProvider(tree, newDataProvider); - tree.addMouseListener(new PopupHandler() { @Override public void invokePopup(Component comp, int x, int y) { diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorerTreeDataProvider.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorerTreeDataProvider.java deleted file mode 100644 index 6ee584f5a..000000000 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/LanguageServerExplorerTreeDataProvider.java +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2023 Red Hat Inc. and others. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - * - * Contributors: - * Red Hat Inc. - initial API and implementation - *******************************************************************************/ -package com.redhat.devtools.intellij.lsp4ij.console.explorer; - -import com.intellij.openapi.actionSystem.DataProvider; -import com.intellij.ui.treeStructure.Tree; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.tree.TreePath; - -/** - * Language server explorer tree data provider. - */ -public class LanguageServerExplorerTreeDataProvider implements DataProvider { - - public static final String LANGUAGE_SERVER_DATA_ID = "languageServer"; - - private final Tree tree; - - public LanguageServerExplorerTreeDataProvider(Tree tree) { - this.tree = tree; - } - - @Override - public @Nullable Object getData(@NotNull @NonNls String dataId) { - if (LANGUAGE_SERVER_DATA_ID.equals(dataId)) { - TreePath path = tree.getSelectionPath(); - Object node = path != null ? path.getLastPathComponent() : null; - if (node instanceof LanguageServerProcessTreeNode) { - return ((LanguageServerProcessTreeNode) node).getLanguageServer(); - } - } - return null; - } -} diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/RestartServerAction.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/RestartServerAction.java index 9661d99db..15864a522 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/RestartServerAction.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/RestartServerAction.java @@ -13,9 +13,8 @@ *******************************************************************************/ package com.redhat.devtools.intellij.lsp4ij.console.explorer.actions; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; -import com.redhat.devtools.intellij.lsp4ij.console.explorer.LanguageServerExplorerTreeDataProvider; +import com.intellij.ui.treeStructure.Tree; import com.redhat.devtools.intellij.lsp4ij.LanguageServerBundle; import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper; import org.jetbrains.annotations.NotNull; @@ -27,7 +26,7 @@ /** * Action to restart the selected language server process from the language explorer. */ -public class RestartServerAction extends AnAction { +public class RestartServerAction extends TreeAction { private static final Logger LOGGER = LoggerFactory.getLogger(RestartServerAction.class);//$NON-NLS-1$ @@ -38,11 +37,11 @@ public RestartServerAction() { } @Override - public void actionPerformed(@NotNull AnActionEvent e) { - Object lsData = e.getDataContext().getData(LanguageServerExplorerTreeDataProvider.LANGUAGE_SERVER_DATA_ID); - if (lsData instanceof LanguageServerWrapper) { + protected void actionPerformed(@NotNull Tree tree, @NotNull AnActionEvent e) { + LanguageServerWrapper languageServer = getSelectedLanguageServer(tree); + if (languageServer != null) { try { - ((LanguageServerWrapper)lsData).start(); + languageServer.start(); } catch (IOException ex) { LOGGER.error("Failed restarting server", ex); } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/StopServerAction.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/StopServerAction.java index 5b134d4ed..5776df5bb 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/StopServerAction.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/StopServerAction.java @@ -13,9 +13,8 @@ *******************************************************************************/ package com.redhat.devtools.intellij.lsp4ij.console.explorer.actions; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; -import com.redhat.devtools.intellij.lsp4ij.console.explorer.LanguageServerExplorerTreeDataProvider; +import com.intellij.ui.treeStructure.Tree; import com.redhat.devtools.intellij.lsp4ij.LanguageServerBundle; import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper; import org.jetbrains.annotations.NotNull; @@ -23,7 +22,7 @@ /** * Action to stop the selected language server process from the language explorer. */ -public class StopServerAction extends AnAction { +public class StopServerAction extends TreeAction { public static final String ACTION_ID = "com.redhat.devtools.intellij.lsp4ij.console.explorer.actions.StopServerAction"; @@ -32,10 +31,11 @@ public StopServerAction() { } @Override - public void actionPerformed(@NotNull AnActionEvent e) { - Object lsData = e.getDataContext().getData(LanguageServerExplorerTreeDataProvider.LANGUAGE_SERVER_DATA_ID); - if (lsData instanceof LanguageServerWrapper) { - ((LanguageServerWrapper)lsData).stop(); + protected void actionPerformed(@NotNull Tree tree, @NotNull AnActionEvent e) { + LanguageServerWrapper languageServer = getSelectedLanguageServer(tree); + if (languageServer != null) { + languageServer.stop(); } } + } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/TreeAction.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/TreeAction.java new file mode 100644 index 000000000..513539f9e --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/explorer/actions/TreeAction.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.lsp4ij.console.explorer.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.util.NlsActions; +import com.intellij.ui.treeStructure.Tree; +import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper; +import com.redhat.devtools.intellij.lsp4ij.console.explorer.LanguageServerProcessTreeNode; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.tree.TreePath; +import java.awt.*; + +/** + * Base class for Actions processed from the Language Server tree. + */ +public abstract class TreeAction extends AnAction { + + protected TreeAction(@Nullable @NlsActions.ActionText String text) { + super(text); + } + + public final void actionPerformed(@NotNull AnActionEvent e) { + Tree tree = getTree(e); + if (tree == null) { + return; + } + actionPerformed(tree, e); + } + + /** + * Returns the language server tree and null otherwise. + * + * @param e the action event. + * @return the language server tree and null otherwise. + */ + private Tree getTree(AnActionEvent e) { + Component component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT); + if (component instanceof Tree) { + return (Tree) component; + } + return null; + } + + /** + * Returns the selected language server tree node and null otherwise. + * + * @param tree the tree. + * @return the selected language server tree node and null otherwise. + */ + protected LanguageServerWrapper getSelectedLanguageServer(Tree tree) { + TreePath path = tree.getSelectionPath(); + Object node = path != null ? path.getLastPathComponent() : null; + if (node instanceof LanguageServerProcessTreeNode) { + return ((LanguageServerProcessTreeNode) node).getLanguageServer(); + } + return null; + } + + protected abstract void actionPerformed(@NotNull Tree tree, @NotNull AnActionEvent e); + +}