Search in sources :

Example 1 with ChooseByNamePopup

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;
                }
            }));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Function(com.google.common.base.Function) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(javax.annotation.Nullable)

Example 2 with ChooseByNamePopup

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");
            }
        }
    });
}
Also used : KeyEvent(java.awt.event.KeyEvent) Project(com.intellij.openapi.project.Project) ChooseTargetModel(com.facebook.buck.intellij.ideabuck.actions.choosetargets.ChooseTargetModel) KeyAdapter(java.awt.event.KeyAdapter) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) BuckSettingsProvider(com.facebook.buck.intellij.ideabuck.config.BuckSettingsProvider) ChooseTargetItem(com.facebook.buck.intellij.ideabuck.actions.choosetargets.ChooseTargetItem)

Example 3 with ChooseByNamePopup

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;
}
Also used : Set(java.util.Set) ListSelectionEvent(javax.swing.event.ListSelectionEvent) NotNull(org.jetbrains.annotations.NotNull) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) GotoActionItemProvider(com.intellij.ide.util.gotoByName.GotoActionItemProvider) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) Disposable(com.intellij.openapi.Disposable) GotoActionModel(com.intellij.ide.util.gotoByName.GotoActionModel) HeldDownKeyListener(com.intellij.ui.HeldDownKeyListener) ListSelectionListener(javax.swing.event.ListSelectionListener) ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) BooleanOptionDescription(com.intellij.ide.ui.search.BooleanOptionDescription) ModalityState(com.intellij.openapi.application.ModalityState) Nullable(org.jetbrains.annotations.Nullable) KeymapManagerImpl(com.intellij.openapi.keymap.impl.KeymapManagerImpl) Keymap(com.intellij.openapi.keymap.Keymap) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ChooseByNamePopup

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) FileType(com.intellij.openapi.fileTypes.FileType) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) GotoFileModel(com.intellij.ide.util.gotoByName.GotoFileModel) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 5 with ChooseByNamePopup

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) ChooseByNameFilter(com.intellij.ide.util.gotoByName.ChooseByNameFilter)

Aggregations

ChooseByNamePopup (com.intellij.ide.util.gotoByName.ChooseByNamePopup)7 Project (com.intellij.openapi.project.Project)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 GotoActionModel (com.intellij.ide.util.gotoByName.GotoActionModel)2 PsiFile (com.intellij.psi.PsiFile)2 ChooseTargetItem (com.facebook.buck.intellij.ideabuck.actions.choosetargets.ChooseTargetItem)1 ChooseTargetModel (com.facebook.buck.intellij.ideabuck.actions.choosetargets.ChooseTargetModel)1 BuckSettingsProvider (com.facebook.buck.intellij.ideabuck.config.BuckSettingsProvider)1 Function (com.google.common.base.Function)1 BooleanOptionDescription (com.intellij.ide.ui.search.BooleanOptionDescription)1 ChooseByNameFilter (com.intellij.ide.util.gotoByName.ChooseByNameFilter)1 ChooseByNamePopupComponent (com.intellij.ide.util.gotoByName.ChooseByNamePopupComponent)1 GotoActionItemProvider (com.intellij.ide.util.gotoByName.GotoActionItemProvider)1 GotoFileModel (com.intellij.ide.util.gotoByName.GotoFileModel)1 ListChooseByNameModel (com.intellij.ide.util.gotoByName.ListChooseByNameModel)1 Disposable (com.intellij.openapi.Disposable)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1