Search in sources :

Example 1 with XLocalAttachDebuggerProvider

use of com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider in project intellij-community by JetBrains.

the class AttachToLocalProcessAction method collectAttachItems.

@NotNull
public static List<AttachItem> collectAttachItems(@NotNull final Project project, @NotNull ProcessInfo[] processList, @NotNull ProgressIndicator indicator, @NotNull XLocalAttachDebuggerProvider... providers) {
    MultiMap<XLocalAttachGroup, Pair<ProcessInfo, ArrayList<XLocalAttachDebugger>>> groupWithItems = new MultiMap<>();
    UserDataHolderBase dataHolder = new UserDataHolderBase();
    for (ProcessInfo eachInfo : processList) {
        MultiMap<XLocalAttachGroup, XLocalAttachDebugger> groupsWithDebuggers = new MultiMap<>();
        for (XLocalAttachDebuggerProvider eachProvider : providers) {
            indicator.checkCanceled();
            groupsWithDebuggers.putValues(eachProvider.getAttachGroup(), eachProvider.getAvailableDebuggers(project, eachInfo, dataHolder));
        }
        for (XLocalAttachGroup eachGroup : groupsWithDebuggers.keySet()) {
            Collection<XLocalAttachDebugger> debuggers = groupsWithDebuggers.get(eachGroup);
            if (!debuggers.isEmpty()) {
                groupWithItems.putValue(eachGroup, Pair.create(eachInfo, new ArrayList<>(debuggers)));
            }
        }
    }
    ArrayList<XLocalAttachGroup> sortedGroups = new ArrayList<>(groupWithItems.keySet());
    sortedGroups.sort(Comparator.comparingInt(XLocalAttachGroup::getOrder));
    List<AttachItem> currentItems = new ArrayList<>();
    for (final XLocalAttachGroup eachGroup : sortedGroups) {
        List<Pair<ProcessInfo, ArrayList<XLocalAttachDebugger>>> sortedItems = new ArrayList<>(groupWithItems.get(eachGroup));
        sortedItems.sort((a, b) -> eachGroup.compare(project, a.first, b.first, dataHolder));
        boolean first = true;
        for (Pair<ProcessInfo, ArrayList<XLocalAttachDebugger>> eachItem : sortedItems) {
            currentItems.add(new AttachItem(eachGroup, first, eachItem.first, eachItem.second, dataHolder));
            first = false;
        }
    }
    List<AttachItem> currentHistoryItems = new ArrayList<>();
    List<HistoryItem> history = getHistory(project);
    for (int i = history.size() - 1; i >= 0; i--) {
        HistoryItem eachHistoryItem = history.get(i);
        for (AttachItem eachCurrentItem : currentItems) {
            boolean isSuitableItem = eachHistoryItem.getGroup().equals(eachCurrentItem.getGroup()) && eachHistoryItem.getProcessInfo().getCommandLine().equals(eachCurrentItem.getProcessInfo().getCommandLine());
            if (!isSuitableItem)
                continue;
            List<XLocalAttachDebugger> debuggers = eachCurrentItem.getDebuggers();
            int selectedDebugger = -1;
            for (int j = 0; j < debuggers.size(); j++) {
                XLocalAttachDebugger eachDebugger = debuggers.get(j);
                if (eachDebugger.getDebuggerDisplayName().equals(eachHistoryItem.getDebuggerName())) {
                    selectedDebugger = j;
                    break;
                }
            }
            if (selectedDebugger == -1)
                continue;
            currentHistoryItems.add(new AttachItem(eachCurrentItem.getGroup(), currentHistoryItems.isEmpty(), XDebuggerBundle.message("xdebugger.attach.toLocal.popup.recent"), eachCurrentItem.getProcessInfo(), debuggers, selectedDebugger, dataHolder));
        }
    }
    currentHistoryItems.addAll(currentItems);
    return currentHistoryItems;
}
Also used : UserDataHolderBase(com.intellij.openapi.util.UserDataHolderBase) ProcessInfo(com.intellij.execution.process.ProcessInfo) XLocalAttachDebugger(com.intellij.xdebugger.attach.XLocalAttachDebugger) MultiMap(com.intellij.util.containers.MultiMap) XLocalAttachGroup(com.intellij.xdebugger.attach.XLocalAttachGroup) XLocalAttachDebuggerProvider(com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XLocalAttachDebuggerProvider

use of com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider in project intellij-community by JetBrains.

the class AttachToLocalProcessAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = getEventProject(e);
    if (project == null)
        return;
    XLocalAttachDebuggerProvider[] providers = Extensions.getExtensions(XLocalAttachDebuggerProvider.EP);
    new Task.Backgroundable(project, XDebuggerBundle.message("xdebugger.attach.toLocal.action.collectingProcesses"), true, PerformInBackgroundOption.DEAF) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            ProcessInfo[] processList = OSProcessUtil.getProcessList();
            List<AttachItem> items = collectAttachItems(project, processList, indicator, providers);
            ApplicationManager.getApplication().invokeLater(() -> {
                if (project.isDisposed()) {
                    return;
                }
                ProcessListStep step = new ProcessListStep(items, project);
                final ListPopup popup = JBPopupFactory.getInstance().createListPopup(step);
                final JList mainList = ((ListPopupImpl) popup).getList();
                ListSelectionListener listener = event -> {
                    if (event.getValueIsAdjusting())
                        return;
                    Object item = ((JList) event.getSource()).getSelectedValue();
                    if (item == null) {
                        item = mainList.getSelectedValue();
                    }
                    if (item instanceof AttachItem) {
                        String debuggerName = ((AttachItem) item).getSelectedDebugger().getDebuggerDisplayName();
                        debuggerName = StringUtil.shortenTextWithEllipsis(debuggerName, 50, 0);
                        ((ListPopupImpl) popup).setCaption(XDebuggerBundle.message("xdebugger.attach.toLocal.popup.title", debuggerName));
                    }
                };
                popup.addListSelectionListener(listener);
                // force first valueChanged event
                listener.valueChanged(new ListSelectionEvent(mainList, mainList.getMinSelectionIndex(), mainList.getMaxSelectionIndex(), false));
                popup.showCenteredInCurrentWindow(project);
            });
        }
    }.queue();
}
Also used : Task(com.intellij.openapi.progress.Task) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ListSelectionListener(javax.swing.event.ListSelectionListener) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) XLocalAttachDebuggerProvider(com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider)

Aggregations

XLocalAttachDebuggerProvider (com.intellij.xdebugger.attach.XLocalAttachDebuggerProvider)2 ProcessInfo (com.intellij.execution.process.ProcessInfo)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 UserDataHolderBase (com.intellij.openapi.util.UserDataHolderBase)1 ListPopupImpl (com.intellij.ui.popup.list.ListPopupImpl)1 MultiMap (com.intellij.util.containers.MultiMap)1 XLocalAttachDebugger (com.intellij.xdebugger.attach.XLocalAttachDebugger)1 XLocalAttachGroup (com.intellij.xdebugger.attach.XLocalAttachGroup)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1 NotNull (org.jetbrains.annotations.NotNull)1