remove duplication from goto actions

This commit is contained in:
peter
2011-02-21 16:03:46 +01:00
parent 05bf505396
commit 3770f00bae
6 changed files with 92 additions and 72 deletions
@@ -27,20 +27,16 @@ import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.actions.GotoActionBase;
import com.intellij.ide.util.gotoByName.ChooseByNameFilter;
import com.intellij.ide.util.gotoByName.ChooseByNamePopup;
import com.intellij.ide.util.gotoByName.ChooseByNamePopupComponent;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.*;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
@@ -57,26 +53,30 @@ public class RunInspectionAction extends GotoActionBase {
@Override
protected void gotoActionPerformed(final AnActionEvent e) {
final Project project = e.getData(PlatformDataKeys.PROJECT);
if (project == null) return;
PsiDocumentManager.getInstance(project).commitAllDocuments();
final PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(e.getDataContext());
final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext());
final VirtualFile virtualFile = LangDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
if (project == null || virtualFile == null) return;
if (virtualFile == null) return;
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.goto.inspection");
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, new GotoInspectionModel(project), getPsiContext(e));
popup.setSearchInAnyPlace(true);
popup.invoke(new ChooseByNamePopupComponent.Callback() {
public void onClose() {
if (RunInspectionAction.class.equals(myInAction)) {
myInAction = null;
}
final GotoInspectionModel model = new GotoInspectionModel(project);
showNavigationPopup(e, model, new GotoActionCallback<Object>() {
@Override
protected ChooseByNameFilter<Object> createFilter(ChooseByNamePopup popup) {
popup.setSearchInAnyPlace(true);
return super.createFilter(popup);
}
public void elementChosen(Object element) {
final InspectionProfileEntry profileEntry = (InspectionProfileEntry)element;
runInspection(project, profileEntry, virtualFile, psiElement, psiFile);
@Override
public void elementChosen(ChooseByNamePopup popup, Object element) {
runInspection(project, (InspectionProfileEntry)element, virtualFile, psiElement, psiFile);
}
}, ModalityState.current(), true);
});
}
private static void runInspection(@NotNull Project project,
@@ -19,7 +19,6 @@ package com.intellij.ide.actions;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.DataManager;
import com.intellij.ide.util.gotoByName.ChooseByNamePopup;
import com.intellij.ide.util.gotoByName.ChooseByNamePopupComponent;
import com.intellij.ide.util.gotoByName.GotoActionModel;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
@@ -38,15 +37,9 @@ public class GotoActionAction extends GotoActionBase implements DumbAware {
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.action");
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, new GotoActionModel(project, component), getPsiContext(e));
popup.invoke(new ChooseByNamePopupComponent.Callback() {
public void onClose() {
if (GotoActionAction.class.equals(myInAction)) {
myInAction = null;
}
}
public void elementChosen(Object element) {
showNavigationPopup(e, new GotoActionModel(project, component), new GotoActionCallback<Object>() {
@Override
public void elementChosen(ChooseByNamePopup popup, Object element) {
final AnAction action = (AnAction)((Map.Entry)element).getKey();
if (action != null) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@@ -62,7 +55,7 @@ public class GotoActionAction extends GotoActionBase implements DumbAware {
}, ModalityState.NON_MODAL);
}
}
}, ModalityState.current(), true);
});
}
}
@@ -16,7 +16,9 @@
package com.intellij.ide.actions;
import com.intellij.ide.util.gotoByName.*;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
@@ -25,6 +27,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
/**
* Author: msk
@@ -71,6 +74,7 @@ public abstract class GotoActionBase extends AnAction {
return true;
}
@Nullable
public static PsiElement getPsiContext(final AnActionEvent e) {
PsiFile file = e.getData(LangDataKeys.PSI_FILE);
if (file != null) return file;
@@ -78,6 +82,7 @@ public abstract class GotoActionBase extends AnAction {
return getPsiContext(project);
}
@Nullable
public static PsiElement getPsiContext(final Project project) {
if (project == null) return null;
Editor selectedEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
@@ -85,4 +90,39 @@ public abstract class GotoActionBase extends AnAction {
Document document = selectedEditor.getDocument();
return PsiDocumentManager.getInstance(project).getPsiFile(document);
}
protected static abstract class GotoActionCallback<T> {
@Nullable
protected ChooseByNameFilter<T> createFilter(ChooseByNamePopup popup) {
return null;
}
public abstract void elementChosen(ChooseByNamePopup popup, Object element);
}
protected static <T> void showNavigationPopup(AnActionEvent e, ChooseByNameModel model, final GotoActionCallback<T> callback) {
final Project project = e.getData(PlatformDataKeys.PROJECT);
final Class startedAction = myInAction;
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, model, getPsiContext(e), getInitialText(e.getData(PlatformDataKeys.EDITOR)));
final ChooseByNameFilter<T> filter = callback.createFilter(popup);
popup.invoke(new ChooseByNamePopupComponent.Callback() {
@Override
public void onClose() {
if (startedAction.equals(myInAction)) {
myInAction = null;
}
if (filter != null) {
filter.close();
}
}
@Override
public void elementChosen(Object element) {
callback.elementChosen(popup, element);
}
}, ModalityState.current(), true);
}
}
@@ -19,12 +19,12 @@ package com.intellij.ide.actions;
import com.intellij.codeInsight.navigation.NavigationUtil;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.util.gotoByName.*;
import com.intellij.lang.Language;
import com.intellij.navigation.ChooseByNameRegistry;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
@@ -43,23 +43,18 @@ public class GotoClassAction extends GotoActionBase implements DumbAware {
return;
}
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
PsiDocumentManager.getInstance(project).commitAllDocuments();
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
final GotoClassModel2 model = new GotoClassModel2(project);
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, model, getPsiContext(e),
getInitialText(e.getData(PlatformDataKeys.EDITOR)));
final ChooseByNameFilter filterUI = new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
popup.invoke(new ChooseByNamePopupComponent.Callback() {
public void onClose() {
if (GotoClassAction.class.equals(myInAction)) {
myInAction = null;
}
filterUI.close();
showNavigationPopup(e, model, new GotoActionCallback<Language>() {
@Override
protected ChooseByNameFilter<Language> createFilter(ChooseByNamePopup popup) {
return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
}
public void elementChosen(Object element) {
@Override
public void elementChosen(ChooseByNamePopup popup, Object element) {
if (element instanceof PsiElement) {
NavigationUtil.activateFileWithPsiElement((PsiElement)element);
}
@@ -67,7 +62,7 @@ public class GotoClassAction extends GotoActionBase implements DumbAware {
((NavigationItem)element).navigate(true);
}
}
}, ModalityState.current(), true);
});
}
protected boolean hasContributors(DataContext dataContext) {
@@ -17,7 +17,10 @@
package com.intellij.ide.actions;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.util.gotoByName.*;
import com.intellij.ide.util.gotoByName.ChooseByNameFilter;
import com.intellij.ide.util.gotoByName.ChooseByNamePopup;
import com.intellij.ide.util.gotoByName.GotoFileConfiguration;
import com.intellij.ide.util.gotoByName.GotoFileModel;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
@@ -52,18 +55,14 @@ public class GotoFileAction extends GotoActionBase implements DumbAware {
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.file");
final Project project = e.getData(PlatformDataKeys.PROJECT);
final GotoFileModel gotoFileModel = new GotoFileModel(project);
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, gotoFileModel, getPsiContext(e),
getInitialText(e.getData(PlatformDataKeys.EDITOR)));
final ChooseByNameFilter filterUI = new GotoFileFilter(popup, gotoFileModel, project);
popup.invoke(new ChooseByNamePopupComponent.Callback() {
public void onClose() {
if (GotoFileAction.class.equals(myInAction)) {
myInAction = null;
}
filterUI.close();
showNavigationPopup(e, gotoFileModel, new GotoActionCallback<FileType>() {
@Override
protected ChooseByNameFilter<FileType> createFilter(ChooseByNamePopup popup) {
return new GotoFileFilter(popup, gotoFileModel, project);
}
public void elementChosen(final Object element) {
@Override
public void elementChosen(final ChooseByNamePopup popup, final Object element) {
if (element == null) return;
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
@@ -81,7 +80,7 @@ public class GotoFileAction extends GotoActionBase implements DumbAware {
}
}, ModalityState.NON_MODAL);
}
}, ModalityState.current(), true);
});
}
protected static class GotoFileFilter extends ChooseByNameFilter<FileType> {
@@ -18,12 +18,12 @@ package com.intellij.ide.actions;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.util.gotoByName.*;
import com.intellij.navigation.NavigationItem;
import com.intellij.lang.Language;
import com.intellij.navigation.ChooseByNameRegistry;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
@@ -32,26 +32,19 @@ public class GotoSymbolAction extends GotoActionBase {
public void gotoActionPerformed(AnActionEvent e) {
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.symbol");
final Project project = e.getData(PlatformDataKeys.PROJECT);
PsiDocumentManager.getInstance(project).commitAllDocuments();
final GotoSymbolModel2 model = new GotoSymbolModel2(project);
final ChooseByNamePopup popup = ChooseByNamePopup.createPopup(project, model, getPsiContext(e),
getInitialText(e.getData(PlatformDataKeys.EDITOR)));
final ChooseByNameFilter filterUI = new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project),
project);
popup.invoke(new ChooseByNamePopupComponent.Callback() {
public void onClose() {
if (GotoSymbolAction.class.equals(myInAction)) {
myInAction = null;
}
filterUI.close();
PsiDocumentManager.getInstance(project).commitAllDocuments();
showNavigationPopup(e, model, new GotoActionCallback<Language>() {
@Override
protected ChooseByNameFilter<Language> createFilter(ChooseByNamePopup popup) {
return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
}
public void elementChosen(Object element) {
@Override
public void elementChosen(ChooseByNamePopup popup, Object element) {
((NavigationItem)element).navigate(true);
}
}, ModalityState.current(), true);
});
}
protected boolean hasContributors(DataContext dataContext) {