From 093196d7db1eedf8aa98fdb5dadf4b90ed9fcbb8 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 23 Feb 2012 16:02:24 +0100 Subject: [PATCH] hide "find in path" and "replace in path" from context menu if the item under cursor is not a valid search scope (IDEA-81700) --- .../find/actions/FindInPathAction.java | 21 ++++++++++++++++++- .../find/actions/ReplaceInPathAction.java | 6 ++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/find/actions/FindInPathAction.java b/platform/lang-impl/src/com/intellij/find/actions/FindInPathAction.java index 81f8dc59d76e..f31bbd2829f0 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/FindInPathAction.java +++ b/platform/lang-impl/src/com/intellij/find/actions/FindInPathAction.java @@ -1,6 +1,6 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,10 @@ import com.intellij.notification.NotificationType; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindowId; +import com.intellij.psi.PsiDirectoryContainer; +import com.intellij.psi.PsiElement; public class FindInPathAction extends AnAction implements DumbAware { static final NotificationGroup NOTIFICATION_GROUP = NotificationGroup.toolWindowGroup("FindInPath", ToolWindowId.FIND, false); @@ -49,8 +52,24 @@ public class FindInPathAction extends AnAction implements DumbAware { @Override public void update(AnActionEvent e){ + doUpdate(e); + } + + static void doUpdate(AnActionEvent e) { Presentation presentation = e.getPresentation(); Project project = e.getData(PlatformDataKeys.PROJECT); presentation.setEnabled(project != null); + if (ActionPlaces.isPopupPlace(e.getPlace())) { + presentation.setVisible(isValidSearchScope(e)); + } + } + + private static boolean isValidSearchScope(AnActionEvent e) { + final PsiElement[] elements = e.getData(LangDataKeys.PSI_ELEMENT_ARRAY); + if (elements != null && elements.length == 1 && elements[0] instanceof PsiDirectoryContainer) { + return true; + } + final VirtualFile[] virtualFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); + return virtualFiles != null && virtualFiles.length == 1 && virtualFiles[0].isDirectory(); } } diff --git a/platform/lang-impl/src/com/intellij/find/actions/ReplaceInPathAction.java b/platform/lang-impl/src/com/intellij/find/actions/ReplaceInPathAction.java index 4e603990b349..a5b34b6aeb13 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/ReplaceInPathAction.java +++ b/platform/lang-impl/src/com/intellij/find/actions/ReplaceInPathAction.java @@ -1,6 +1,6 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,8 +38,6 @@ public class ReplaceInPathAction extends AnAction { @Override public void update(AnActionEvent event){ - Presentation presentation = event.getPresentation(); - Project project = event.getData(PlatformDataKeys.PROJECT); - presentation.setEnabled(project != null); + FindInPathAction.doUpdate(event); } }