Search in sources :

Example 6 with AsyncPromise

use of org.jetbrains.concurrency.AsyncPromise in project intellij-community by JetBrains.

the class XDebuggerUtilImpl method toggleAndReturnLineBreakpoint.

@NotNull
public static <P extends XBreakpointProperties> Promise<XLineBreakpoint> toggleAndReturnLineBreakpoint(@NotNull final Project project, @NotNull final XLineBreakpointType<P> type, @NotNull final XSourcePosition position, final boolean temporary, @Nullable final Editor editor, boolean canRemove) {
    return new WriteAction<Promise<XLineBreakpoint>>() {

        @Override
        protected void run(@NotNull Result<Promise<XLineBreakpoint>> result) throws Throwable {
            final VirtualFile file = position.getFile();
            final int line = position.getLine();
            final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
            XLineBreakpoint<P> breakpoint = breakpointManager.findBreakpointAtLine(type, file, line);
            if (breakpoint != null) {
                if (!temporary && canRemove) {
                    breakpointManager.removeBreakpoint(breakpoint);
                }
            } else {
                List<? extends XLineBreakpointType<P>.XLineBreakpointVariant<P>> variants = type.computeVariants(project, position);
                if (!variants.isEmpty() && editor != null) {
                    RelativePoint relativePoint = DebuggerUIUtil.getPositionForPopup(editor, line);
                    if (variants.size() > 1 && relativePoint != null) {
                        final AsyncPromise<XLineBreakpoint> res = new AsyncPromise<>();
                        class MySelectionListener implements ListSelectionListener {

                            RangeHighlighter myHighlighter = null;

                            @Override
                            public void valueChanged(ListSelectionEvent e) {
                                if (!e.getValueIsAdjusting()) {
                                    updateHighlighter(((JList) e.getSource()).getSelectedValue());
                                }
                            }

                            public void initialSet(Object value) {
                                if (myHighlighter == null) {
                                    updateHighlighter(value);
                                }
                            }

                            void updateHighlighter(Object value) {
                                clearHighlighter();
                                if (value instanceof XLineBreakpointType.XLineBreakpointVariant) {
                                    TextRange range = ((XLineBreakpointType.XLineBreakpointVariant) value).getHighlightRange();
                                    TextRange lineRange = DocumentUtil.getLineTextRange(editor.getDocument(), line);
                                    if (range != null) {
                                        range = range.intersection(lineRange);
                                    } else {
                                        range = lineRange;
                                    }
                                    if (range != null && !range.isEmpty()) {
                                        EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
                                        TextAttributes attributes = scheme.getAttributes(DebuggerColors.BREAKPOINT_ATTRIBUTES);
                                        myHighlighter = editor.getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), DebuggerColors.BREAKPOINT_HIGHLIGHTER_LAYER, attributes, HighlighterTargetArea.EXACT_RANGE);
                                    }
                                }
                            }

                            private void clearHighlighter() {
                                if (myHighlighter != null) {
                                    myHighlighter.dispose();
                                }
                            }
                        }
                        // calculate default item
                        int caretOffset = editor.getCaretModel().getOffset();
                        XLineBreakpointType<P>.XLineBreakpointVariant<P> defaultVariant = null;
                        for (XLineBreakpointType<P>.XLineBreakpointVariant<P> variant : variants) {
                            TextRange range = variant.getHighlightRange();
                            if (range != null && range.contains(caretOffset)) {
                                //noinspection ConstantConditions
                                if (defaultVariant == null || defaultVariant.getHighlightRange().getLength() > range.getLength()) {
                                    defaultVariant = variant;
                                }
                            }
                        }
                        final int defaultIndex = defaultVariant != null ? variants.indexOf(defaultVariant) : 0;
                        final MySelectionListener selectionListener = new MySelectionListener();
                        ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<XLineBreakpointType.XLineBreakpointVariant>("Set Breakpoint", variants) {

                            @NotNull
                            @Override
                            public String getTextFor(XLineBreakpointType.XLineBreakpointVariant value) {
                                return value.getText();
                            }

                            @Override
                            public Icon getIconFor(XLineBreakpointType.XLineBreakpointVariant value) {
                                return value.getIcon();
                            }

                            @Override
                            public void canceled() {
                                selectionListener.clearHighlighter();
                            }

                            @Override
                            public PopupStep onChosen(final XLineBreakpointType.XLineBreakpointVariant selectedValue, boolean finalChoice) {
                                selectionListener.clearHighlighter();
                                ApplicationManager.getApplication().runWriteAction(() -> {
                                    P properties = (P) selectedValue.createProperties();
                                    res.setResult(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary));
                                });
                                return FINAL_CHOICE;
                            }

                            @Override
                            public int getDefaultOptionIndex() {
                                return defaultIndex;
                            }
                        }) {

                            @Override
                            protected void afterShow() {
                                super.afterShow();
                                selectionListener.initialSet(getList().getSelectedValue());
                            }
                        };
                        DebuggerUIUtil.registerExtraHandleShortcuts(popup, IdeActions.ACTION_TOGGLE_LINE_BREAKPOINT);
                        popup.setAdText(DebuggerUIUtil.getSelectionShortcutsAdText(IdeActions.ACTION_TOGGLE_LINE_BREAKPOINT));
                        popup.addListSelectionListener(selectionListener);
                        popup.show(relativePoint);
                        result.setResult(res);
                        return;
                    } else {
                        P properties = variants.get(0).createProperties();
                        result.setResult(Promise.resolve(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary)));
                        return;
                    }
                }
                P properties = type.createBreakpointProperties(file, line);
                result.setResult(Promise.resolve(breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary)));
                return;
            }
            result.setResult(rejectedPromise());
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ListSelectionEvent(javax.swing.event.ListSelectionEvent) RelativePoint(com.intellij.ui.awt.RelativePoint) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) WriteAction(com.intellij.openapi.application.WriteAction) TextRange(com.intellij.openapi.util.TextRange) RelativePoint(com.intellij.ui.awt.RelativePoint) ListSelectionListener(javax.swing.event.ListSelectionListener) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with AsyncPromise

use of org.jetbrains.concurrency.AsyncPromise in project intellij-community by JetBrains.

the class ChooseComponentsToExportDialog method chooseSettingsFile.

@NotNull
public static Promise<String> chooseSettingsFile(String oldPath, Component parent, final String title, final String description) {
    FileChooserDescriptor chooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor();
    chooserDescriptor.setDescription(description);
    chooserDescriptor.setHideIgnored(false);
    chooserDescriptor.setTitle(title);
    VirtualFile initialDir;
    if (oldPath != null) {
        final File oldFile = new File(oldPath);
        initialDir = LocalFileSystem.getInstance().findFileByIoFile(oldFile);
        if (initialDir == null && oldFile.getParentFile() != null) {
            initialDir = LocalFileSystem.getInstance().findFileByIoFile(oldFile.getParentFile());
        }
    } else {
        initialDir = null;
    }
    final AsyncPromise<String> result = new AsyncPromise<>();
    FileChooser.chooseFiles(chooserDescriptor, null, parent, initialDir, new FileChooser.FileChooserConsumer() {

        @Override
        public void consume(List<VirtualFile> files) {
            VirtualFile file = files.get(0);
            if (file.isDirectory()) {
                result.setResult(file.getPath() + '/' + new File(DEFAULT_PATH).getName());
            } else {
                result.setResult(file.getPath());
            }
        }

        @Override
        public void cancelled() {
            result.setError("");
        }
    });
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) FileChooser(com.intellij.openapi.fileChooser.FileChooser) AsyncPromise(org.jetbrains.concurrency.AsyncPromise) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with AsyncPromise

use of org.jetbrains.concurrency.AsyncPromise in project intellij-community by JetBrains.

the class MavenProjectsManager method scheduleResolve.

private AsyncPromise<List<Module>> scheduleResolve() {
    final AsyncPromise<List<Module>> result = new AsyncPromise<>();
    runWhenFullyOpen(() -> {
        LinkedHashSet<MavenProject> toResolve;
        synchronized (myImportingDataLock) {
            toResolve = new LinkedHashSet<>(myProjectsToResolve);
            myProjectsToResolve.clear();
        }
        if (toResolve.isEmpty())
            return;
        final ResolveContext context = new ResolveContext();
        Runnable onCompletion = () -> {
            if (hasScheduledProjects()) {
                scheduleImport().processed(result);
            } else {
                result.setResult(Collections.emptyList());
            }
        };
        final boolean useSinglePomResolver = Boolean.getBoolean("idea.maven.use.single.pom.resolver");
        if (useSinglePomResolver) {
            Iterator<MavenProject> it = toResolve.iterator();
            while (it.hasNext()) {
                MavenProject each = it.next();
                myResolvingProcessor.scheduleTask(new MavenProjectsProcessorResolvingTask(Collections.singleton(each), myProjectsTree, getGeneralSettings(), it.hasNext() ? null : onCompletion, context));
            }
        } else {
            myResolvingProcessor.scheduleTask(new MavenProjectsProcessorResolvingTask(toResolve, myProjectsTree, getGeneralSettings(), onCompletion, context));
        }
    });
    return result;
}
Also used : DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable) AsyncPromise(org.jetbrains.concurrency.AsyncPromise)

Aggregations

AsyncPromise (org.jetbrains.concurrency.AsyncPromise)8 NotNull (org.jetbrains.annotations.NotNull)7 TextRange (com.intellij.openapi.util.TextRange)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 GoApplicationRunningState (com.goide.runconfig.application.GoApplicationRunningState)1 GoHistoryProcessListener (com.goide.util.GoHistoryProcessListener)1 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)1 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)1 SuspendContextCommandImpl (com.intellij.debugger.engine.events.SuspendContextCommandImpl)1 RunProfileStarter (com.intellij.execution.RunProfileStarter)1 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)1 ProcessEvent (com.intellij.execution.process.ProcessEvent)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 FileChooser (com.intellij.openapi.fileChooser.FileChooser)1