Search in sources :

Example 11 with ApplicationImpl

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

the class JoinLinesHandler method doExecute.

@Override
public void doExecute(@NotNull final Editor editor, @Nullable Caret caret, final DataContext dataContext) {
    assert caret != null;
    if (!(editor.getDocument() instanceof DocumentEx)) {
        myOriginalHandler.execute(editor, caret, dataContext);
        return;
    }
    final DocumentEx doc = (DocumentEx) editor.getDocument();
    final Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editor.getContentComponent()));
    if (project == null)
        return;
    final PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
    PsiFile psiFile = docManager.getPsiFile(doc);
    if (psiFile == null) {
        myOriginalHandler.execute(editor, caret, dataContext);
        return;
    }
    LogicalPosition caretPosition = caret.getLogicalPosition();
    int startLine = caretPosition.line;
    int endLine = startLine + 1;
    if (caret.hasSelection()) {
        startLine = doc.getLineNumber(caret.getSelectionStart());
        endLine = doc.getLineNumber(caret.getSelectionEnd());
        if (doc.getLineStartOffset(endLine) == caret.getSelectionEnd())
            endLine--;
    }
    final int startReformatOffset = CharArrayUtil.shiftBackward(doc.getCharsSequence(), doc.getLineEndOffset(startLine), " \t");
    // joining lines, several times if selection is multiline
    int lineCount = endLine - startLine;
    int line = startLine;
    ((ApplicationImpl) ApplicationManager.getApplication()).runWriteActionWithProgressInDispatchThread("Join Lines", project, null, IdeBundle.message("action.stop"), indicator -> {
        Ref<Integer> caretRestoreOffset = new Ref<>(-1);
        CodeEditUtil.setNodeReformatStrategy(node -> node.getTextRange().getStartOffset() >= startReformatOffset);
        try {
            for (int count = 0; count < lineCount; count++) {
                indicator.checkCanceled();
                indicator.setFraction(((double) count) / lineCount);
                ProgressManager.getInstance().executeNonCancelableSection(() -> doJoinTwoLines(doc, project, docManager, psiFile, line, caretRestoreOffset));
            }
        } finally {
            CodeEditUtil.setNodeReformatStrategy(null);
        }
        positionCaret(editor, caret, caretRestoreOffset.get());
    });
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl)

Example 12 with ApplicationImpl

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

the class BaseRefactoringProcessor method doRefactoring.

private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet) {
    for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext(); ) {
        UsageInfo usageInfo = iterator.next();
        final PsiElement element = usageInfo.getElement();
        if (element == null || !isToBeChanged(usageInfo)) {
            iterator.remove();
        }
    }
    LocalHistoryAction action = LocalHistory.getInstance().startAction(getCommandName());
    final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(new UsageInfo[usageInfoSet.size()]);
    try {
        PsiDocumentManager.getInstance(myProject).commitAllDocuments();
        RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl) RefactoringListenerManager.getInstance(myProject);
        myTransaction = listenerManager.startTransaction();
        final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
        final Runnable prepareHelpersRunnable = new Runnable() {

            @Override
            public void run() {
                for (final RefactoringHelper helper : Extensions.getExtensions(RefactoringHelper.EP_NAME)) {
                    Object operation = ApplicationManager.getApplication().runReadAction(new Computable<Object>() {

                        @Override
                        public Object compute() {
                            return helper.prepareOperation(writableUsageInfos);
                        }
                    });
                    preparedData.put(helper, operation);
                }
            }
        };
        ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable, "Prepare ...", false, myProject);
        Runnable performRefactoringRunnable = () -> {
            final String refactoringId = getRefactoringId();
            if (refactoringId != null) {
                RefactoringEventData data = getBeforeData();
                if (data != null) {
                    data.addUsages(usageInfoSet);
                }
                myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
            }
            try {
                if (refactoringId != null) {
                    UndoableAction action1 = new UndoRefactoringAction(myProject, refactoringId);
                    UndoManager.getInstance(myProject).undoableActionPerformed(action1);
                }
                performRefactoring(writableUsageInfos);
            } finally {
                if (refactoringId != null) {
                    myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
                }
            }
        };
        ApplicationImpl app = (ApplicationImpl) ApplicationManagerEx.getApplicationEx();
        if (Registry.is("run.refactorings.under.progress")) {
            app.runWriteActionWithProgressInDispatchThread(getCommandName(), myProject, null, null, indicator -> performRefactoringRunnable.run());
        } else {
            app.runWriteAction(performRefactoringRunnable);
        }
        DumbService.getInstance(myProject).completeJustSubmittedTasks();
        for (Map.Entry<RefactoringHelper, Object> e : preparedData.entrySet()) {
            //noinspection unchecked
            e.getKey().performOperation(myProject, e.getValue());
        }
        myTransaction.commit();
        app.runWriteAction(() -> performPsiSpoilingRefactoring());
    } finally {
        action.finish();
    }
    int count = writableUsageInfos.length;
    if (count > 0) {
        StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
    } else {
        if (!isPreviewUsages(writableUsageInfos)) {
            StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
        }
    }
}
Also used : ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) LocalHistoryAction(com.intellij.history.LocalHistoryAction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) MultiMap(com.intellij.util.containers.MultiMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 13 with ApplicationImpl

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

the class VfsRootAccess method assertAccessInTests.

@TestOnly
static void assertAccessInTests(@NotNull VirtualFileSystemEntry child, @NotNull NewVirtualFileSystem delegate) {
    final Application application = ApplicationManager.getApplication();
    if (SHOULD_PERFORM_ACCESS_CHECK && application.isUnitTestMode() && application instanceof ApplicationImpl && ((ApplicationImpl) application).isComponentsCreated() && !ApplicationInfoImpl.isInStressTest()) {
        if (delegate != LocalFileSystem.getInstance() && delegate != JarFileSystem.getInstance()) {
            return;
        }
        // root' children are loaded always
        if (child.getParent() == null || child.getParent().getParent() == null) {
            return;
        }
        Set<String> allowed = ApplicationManager.getApplication().runReadAction(new Computable<Set<String>>() {

            @Override
            public Set<String> compute() {
                return allowedRoots();
            }
        });
        boolean isUnder = allowed == null || allowed.isEmpty();
        if (!isUnder) {
            String childPath = child.getPath();
            if (delegate == JarFileSystem.getInstance()) {
                VirtualFile local = JarFileSystem.getInstance().getVirtualFileForJar(child);
                assert local != null : child;
                childPath = local.getPath();
            }
            for (String root : allowed) {
                if (FileUtil.startsWith(childPath, root)) {
                    isUnder = true;
                    break;
                }
                if (root.startsWith(JarFileSystem.PROTOCOL_PREFIX)) {
                    String rootLocalPath = FileUtil.toSystemIndependentName(PathUtil.toPresentableUrl(root));
                    isUnder = FileUtil.startsWith(childPath, rootLocalPath);
                    if (isUnder)
                        break;
                }
            }
        }
        assert isUnder : "File accessed outside allowed roots: " + child + ";\nAllowed roots: " + new ArrayList<>(allowed);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) Set(java.util.Set) ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) Application(com.intellij.openapi.application.Application) TestOnly(org.jetbrains.annotations.TestOnly)

Example 14 with ApplicationImpl

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

the class TestWriteActionUnderProgress method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    ApplicationImpl app = (ApplicationImpl) ApplicationManager.getApplication();
    boolean success = app.runWriteActionWithProgressInDispatchThread("Progress", null, null, null, TestWriteActionUnderProgress::runIndeterminateProgress);
    assert success;
    app.runWriteActionWithProgressInBackgroundThread("Cancellable Progress", null, null, "Stop", TestWriteActionUnderProgress::runDeterminateProgress);
}
Also used : ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl)

Aggregations

ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)14 Application (com.intellij.openapi.application.Application)6 Project (com.intellij.openapi.project.Project)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 LafManagerImpl (com.intellij.ide.ui.laf.LafManagerImpl)1 DarculaLookAndFeelInfo (com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo)1 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 KeymapManagerImpl (com.intellij.openapi.keymap.impl.KeymapManagerImpl)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)1 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)1 Ref (com.intellij.openapi.util.Ref)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 IdeFrame (com.intellij.openapi.wm.IdeFrame)1 PsiElement (com.intellij.psi.PsiElement)1