Search in sources :

Example 26 with WriteAction

use of com.intellij.openapi.application.WriteAction 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 27 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class XDebuggerTestUtil method removeAllBreakpoints.

public static void removeAllBreakpoints(@NotNull final Project project) {
    final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
    XBreakpoint<?>[] breakpoints = getBreakpoints(breakpointManager);
    for (final XBreakpoint b : breakpoints) {
        new WriteAction() {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                breakpointManager.removeBreakpoint(b);
            }
        }.execute();
    }
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) FutureResult(com.intellij.util.concurrency.FutureResult) Result(com.intellij.openapi.application.Result)

Example 28 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class CompilerTestUtil method enableExternalCompiler.

@TestOnly
public static void enableExternalCompiler() {
    final JavaAwareProjectJdkTableImpl table = JavaAwareProjectJdkTableImpl.getInstanceEx();
    new WriteAction() {

        @Override
        protected void run(@NotNull final Result result) {
            table.addJdk(table.getInternalJdk());
        }
    }.execute();
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) JavaAwareProjectJdkTableImpl(com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl) Result(com.intellij.openapi.application.Result) TestOnly(org.jetbrains.annotations.TestOnly)

Example 29 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class PsiTestCase method createFile.

@NotNull
protected PsiFile createFile(@NotNull final Module module, @NotNull final VirtualFile vDir, @NotNull final String fileName, @NotNull final String text) throws IOException {
    return new WriteAction<PsiFile>() {

        @Override
        protected void run(@NotNull Result<PsiFile> result) throws Throwable {
            if (!ModuleRootManager.getInstance(module).getFileIndex().isInSourceContent(vDir)) {
                addSourceContentToRoots(module, vDir);
            }
            final VirtualFile vFile = vDir.createChildData(vDir, fileName);
            VfsUtil.saveText(vFile, text);
            assertNotNull(vFile);
            final PsiFile file = myPsiManager.findFile(vFile);
            assertNotNull(file);
            result.setResult(file);
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with WriteAction

use of com.intellij.openapi.application.WriteAction in project intellij-community by JetBrains.

the class ArtifactsStructureConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    myPackagingEditorContext.saveEditorSettings();
    checkForEmptyAndDuplicatedNames("Artifact", CommonBundle.getErrorTitle(), ArtifactConfigurableBase.class);
    super.apply();
    myPackagingEditorContext.getManifestFilesInfo().saveManifestFiles();
    final ModifiableArtifactModel modifiableModel = myPackagingEditorContext.getActualModifiableModel();
    if (modifiableModel != null) {
        new WriteAction() {

            @Override
            protected void run(@NotNull final Result result) {
                modifiableModel.commit();
            }
        }.execute();
        myPackagingEditorContext.resetModifiableModel();
    }
    // TODO: fix to not reset on apply!
    reset();
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) Result(com.intellij.openapi.application.Result)

Aggregations

Result (com.intellij.openapi.application.Result)85 WriteAction (com.intellij.openapi.application.WriteAction)85 NotNull (org.jetbrains.annotations.NotNull)38 VirtualFile (com.intellij.openapi.vfs.VirtualFile)37 File (java.io.File)20 IOException (java.io.IOException)16 Module (com.intellij.openapi.module.Module)10 PsiFile (com.intellij.psi.PsiFile)7 Sdk (com.intellij.openapi.projectRoots.Sdk)5 RunResult (com.intellij.openapi.application.RunResult)4 Document (com.intellij.openapi.editor.Document)4 Project (com.intellij.openapi.project.Project)4 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)4 Library (com.intellij.openapi.roots.libraries.Library)4 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)4 ModifiableFacetModel (com.intellij.facet.ModifiableFacetModel)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 FacetManager (com.intellij.facet.FacetManager)2 ProjectFacetManager (com.intellij.facet.ProjectFacetManager)2