use of com.intellij.ide.util.gotoByName.ChooseByNamePopup in project buck by facebook.
the class ChooseTargetContributor method addPathSugestions.
public void addPathSugestions(final List<String> names, final Project project) {
String currentText = project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY).getEnteredText();
// Remove the begining //
currentText = currentText.replaceFirst("^/*", "");
// check if we have as input a proper target
int currentTargetSeparatorIndex = currentText.lastIndexOf(TARGET_NAME_SEPARATOR);
if (currentTargetSeparatorIndex != -1) {
currentText = currentText.substring(0, currentText.lastIndexOf(TARGET_NAME_SEPARATOR));
}
// Try to get the relative path to the current input folder
VirtualFile baseDir = project.getBaseDir().findFileByRelativePath(currentText + "/");
if (baseDir == null) {
// Try to get the relative path to the previous input folder
if (currentText.lastIndexOf("/") != -1) {
currentText = currentText.substring(0, currentText.lastIndexOf("/"));
} else {
// Get the base path if there is no previous folder
currentText = "";
}
baseDir = project.getBaseDir().findFileByRelativePath(currentText);
// If the base dir is still null, then we have a bad relative path
if (baseDir == null) {
return;
}
}
// get the files under the base folder
VirtualFile[] files = baseDir.getChildren();
if (!currentText.isEmpty()) {
currentText += "/";
}
for (VirtualFile file : files) {
// if the file is a directory we add it to the targets
if (file.isDirectory()) {
names.add("//" + currentText + file.getName());
}
//if the file is a buck file we parse it and add its target names to the list
if (file.getName().equals("BUCK")) {
int end = currentText.length() - 1;
// Handle if the BUCK file is the main directory of the project
if (currentText.isEmpty()) {
end = 0;
}
String target = "//" + currentText.substring(0, end) + ":";
// we need the aliases now
names.addAll(BuckQueryAction.execute(project, target, new Function<List<String>, Void>() {
@Nullable
@Override
public Void apply(@Nullable List<String> strings) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
ChooseByNamePopup chooseByNamePopup = project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
// the user might have closed the window
if (chooseByNamePopup != null) {
// if we don't have them, just refresh the view when we do, if the
// window is still open
chooseByNamePopup.rebuildList(true);
}
}
});
return null;
}
}));
}
}
}
use of com.intellij.ide.util.gotoByName.ChooseByNamePopup in project buck by facebook.
the class ChooseTargetAction method gotoActionPerformed.
@Override
protected void gotoActionPerformed(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null) {
return;
}
final ChooseTargetModel model = new ChooseTargetModel(project);
GotoActionCallback<String> callback = new GotoActionCallback<String>() {
@Override
public void elementChosen(ChooseByNamePopup chooseByNamePopup, Object element) {
if (element == null) {
return;
}
BuckSettingsProvider buckSettingsProvider = BuckSettingsProvider.getInstance();
if (buckSettingsProvider == null || buckSettingsProvider.getState() == null) {
return;
}
ChooseTargetItem item = (ChooseTargetItem) element;
// if the target selected isn't an alias, then it has to have :
if (item.getName().contains("//") && !item.getName().contains(":")) {
return;
}
if (buckSettingsProvider.getState().lastAlias != null) {
buckSettingsProvider.getState().lastAlias.put(project.getBasePath(), item.getBuildTarget());
}
BuckToolWindowFactory.updateBuckToolWindowTitle(project);
}
};
showNavigationPopup(e, model, callback, "Choose Build Target", true, false);
// Add navigation listener for auto complete
final ChooseByNamePopup chooseByNamePopup = project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
chooseByNamePopup.getTextField().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_RIGHT == e.getKeyCode()) {
ChooseTargetItem obj = (ChooseTargetItem) chooseByNamePopup.getChosenElement();
if (obj != null) {
chooseByNamePopup.getTextField().setText(obj.getName());
chooseByNamePopup.getTextField().repaint();
}
} else {
super.keyPressed(e);
}
String adText = chooseByNamePopup.getAdText();
if (adText != null) {
chooseByNamePopup.setAdText(adText + " and " + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 2)) + " to use autocomplete");
}
}
});
}
use of com.intellij.ide.util.gotoByName.ChooseByNamePopup in project intellij-community by JetBrains.
the class GotoActionAction method createPopup.
@Nullable
private static ChooseByNamePopup createPopup(@Nullable Project project, @NotNull final GotoActionModel model, String initialText, int initialIndex, final Component component, final AnActionEvent e) {
ChooseByNamePopup oldPopup = project == null ? null : project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
if (oldPopup != null) {
oldPopup.close(false);
}
final Disposable disposable = Disposer.newDisposable();
final ChooseByNamePopup popup = new ChooseByNamePopup(project, model, new GotoActionItemProvider(model), oldPopup, initialText, false, initialIndex) {
private boolean myPaintInternalInfo;
@Override
protected void initUI(Callback callback, ModalityState modalityState, boolean allowMultipleSelection) {
super.initUI(callback, modalityState, allowMultipleSelection);
myList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object value = myList.getSelectedValue();
String text = getText(value);
if (text != null && myDropdownPopup != null) {
myDropdownPopup.setAdText(text, SwingConstants.LEFT);
}
String description = getValueDescription(value);
ActionMenu.showDescriptionInStatusBar(true, myList, description);
}
@Nullable
private String getText(@Nullable Object o) {
if (o instanceof GotoActionModel.MatchedValue) {
GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) o;
if (myPaintInternalInfo) {
if (mv.value instanceof GotoActionModel.ActionWrapper) {
AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
String actionId = ActionManager.getInstance().getId(action);
return StringUtil.notNullize(actionId, "class: " + action.getClass().getName());
}
}
if (mv.value instanceof BooleanOptionDescription || mv.value instanceof GotoActionModel.ActionWrapper && ((GotoActionModel.ActionWrapper) mv.value).getAction() instanceof ToggleAction) {
return "Press " + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) + " to toggle option";
}
}
return getAdText();
}
});
myList.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int index = myList.locationToIndex(e.getPoint());
if (index == -1)
return;
Object value = myList.getModel().getElementAt(index);
String description = getValueDescription(value);
ActionMenu.showDescriptionInStatusBar(true, myList, description);
}
});
if (Registry.is("show.configurables.ids.in.settings")) {
new HeldDownKeyListener() {
@Override
protected void heldKeyTriggered(JComponent component, boolean pressed) {
myPaintInternalInfo = pressed;
// an easy way to repaint the AdText
ListSelectionEvent event = new ListSelectionEvent(this, -1, -1, false);
for (ListSelectionListener listener : myList.getListSelectionListeners()) {
listener.valueChanged(event);
}
}
}.installOn(myTextField);
}
}
@Nullable
private String getValueDescription(@Nullable Object value) {
if (value instanceof GotoActionModel.MatchedValue) {
GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) value;
if (mv.value instanceof GotoActionModel.ActionWrapper) {
AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
return action.getTemplatePresentation().getDescription();
}
}
return null;
}
@NotNull
@Override
protected Set<Object> filter(@NotNull Set<Object> elements) {
return super.filter(model.sortItems(elements));
}
@Override
protected boolean closeForbidden(boolean ok) {
if (!ok)
return false;
Object element = getChosenElement();
return element instanceof GotoActionModel.MatchedValue && processOptionInplace(((GotoActionModel.MatchedValue) element).value, this, component, e) || super.closeForbidden(true);
}
@Override
public void setDisposed(boolean disposedFlag) {
super.setDisposed(disposedFlag);
Disposer.dispose(disposable);
ActionMenu.showDescriptionInStatusBar(true, myList, null);
for (ListSelectionListener listener : myList.getListSelectionListeners()) {
myList.removeListSelectionListener(listener);
}
UIUtil.dispose(myList);
}
};
ApplicationManager.getApplication().getMessageBus().connect(disposable).subscribe(ProgressWindow.TOPIC, new ProgressWindow.Listener() {
@Override
public void progressWindowCreated(ProgressWindow pw) {
Disposer.register(pw, new Disposable() {
@Override
public void dispose() {
if (!popup.checkDisposed()) {
popup.repaintList();
}
}
});
}
});
if (project != null) {
project.putUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY, popup);
}
popup.addMouseClickListener(new MouseAdapter() {
@Override
public void mouseClicked(@NotNull MouseEvent me) {
Object element = popup.getSelectionByPoint(me.getPoint());
if (element instanceof GotoActionModel.MatchedValue) {
if (processOptionInplace(((GotoActionModel.MatchedValue) element).value, popup, component, e)) {
me.consume();
}
}
}
});
CustomShortcutSet shortcutSet = new CustomShortcutSet(KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_INTENTION_ACTIONS));
new DumbAwareAction() {
@Override
public void actionPerformed(AnActionEvent e) {
Object o = popup.getChosenElement();
if (o instanceof GotoActionModel.MatchedValue) {
Comparable value = ((GotoActionModel.MatchedValue) o).value;
if (value instanceof GotoActionModel.ActionWrapper) {
GotoActionModel.ActionWrapper aw = (GotoActionModel.ActionWrapper) value;
boolean available = aw.isAvailable();
if (available) {
AnAction action = aw.getAction();
String id = ActionManager.getInstance().getId(action);
KeymapManagerImpl km = ((KeymapManagerImpl) KeymapManager.getInstance());
Keymap k = km.getActiveKeymap();
if (!k.canModify())
return;
KeymapPanel.addKeyboardShortcut(id, ActionShortcutRestrictions.getInstance().getForActionId(id), k, component);
popup.repaintListImmediate();
}
}
}
}
}.registerCustomShortcutSet(shortcutSet, popup.getTextField(), disposable);
return popup;
}
use of com.intellij.ide.util.gotoByName.ChooseByNamePopup in project intellij-community by JetBrains.
the class GotoFileAction method gotoActionPerformed.
@Override
public void gotoActionPerformed(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null)
return;
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.file");
final GotoFileModel gotoFileModel = new GotoFileModel(project);
GotoActionCallback<FileType> callback = new GotoActionCallback<FileType>() {
@Override
protected ChooseByNameFilter<FileType> createFilter(@NotNull ChooseByNamePopup popup) {
return new GotoFileFilter(popup, gotoFileModel, project);
}
@Override
public void elementChosen(final ChooseByNamePopup popup, final Object element) {
if (element == null)
return;
ApplicationManager.getApplication().assertIsDispatchThread();
Navigatable n = (Navigatable) element;
//this is for better cursor position
if (element instanceof PsiFile) {
VirtualFile file = ((PsiFile) element).getVirtualFile();
if (file == null)
return;
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
}
if (n.canNavigate()) {
n.navigate(true);
}
}
};
GotoFileItemProvider provider = new GotoFileItemProvider(project, getPsiContext(e), gotoFileModel);
showNavigationPopup(e, gotoFileModel, callback, IdeBundle.message("go.to.file.toolwindow.title"), true, true, provider);
}
use of com.intellij.ide.util.gotoByName.ChooseByNamePopup in project intellij-community by JetBrains.
the class RunInspectionAction method gotoActionPerformed.
@Override
protected void gotoActionPerformed(final AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null)
return;
PsiDocumentManager.getInstance(project).commitAllDocuments();
final PsiElement psiElement = CommonDataKeys.PSI_ELEMENT.getData(e.getDataContext());
final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.goto.inspection");
final GotoInspectionModel model = new GotoInspectionModel(project);
showNavigationPopup(e, model, new GotoActionCallback<Object>() {
@Override
protected ChooseByNameFilter<Object> createFilter(@NotNull ChooseByNamePopup popup) {
popup.setSearchInAnyPlace(true);
return super.createFilter(popup);
}
@Override
public void elementChosen(ChooseByNamePopup popup, final Object element) {
ApplicationManager.getApplication().invokeLater(() -> runInspection(project, ((InspectionToolWrapper) element).getShortName(), virtualFile, psiElement, psiFile));
}
}, false);
}
Aggregations