Java Code Examples for com.intellij.openapi.application.Application#runWriteAction()

The following examples show how to use com.intellij.openapi.application.Application#runWriteAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OkJsonUpdateNavigationHandler.java    From NutzCodeInsight with Apache License 2.0 6 votes vote down vote up
@Override
public void navigate(MouseEvent mouseEvent, PsiElement psiElement) {
    JsonFormat jsonFormat = new JsonFormat();
    if (value.startsWith(JSON_PREFIX)) {
        try {
            jsonFormat = GSON.fromJson(value.substring(JSON_PREFIX.length()), JsonFormat.class);
        } catch (Exception e) {
            Messages.showErrorDialog("json格式化信息书写错误!", "错误提示");
        }
    }
    Application applicationManager = ApplicationManager.getApplication();
    CommandProcessor processor = CommandProcessor.getInstance();
    Project project = psiElement.getProject();
    PsiFile psiFile = psiElement.getContainingFile();
    Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
    int startOffset = psiElement.getTextRange().getStartOffset();
    int endOffset = psiElement.getTextRange().getEndOffset();
    OkJsonFastEditor dialog = new OkJsonFastEditor(jsonFormat, (json) ->
            applicationManager.runWriteAction(() ->
                    processor.executeCommand(project, new DocumentReplace(document, startOffset, endOffset, json), "", document)
            )
    );
    dialog.pack();
    dialog.setVisible(true);
}
 
Example 2
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static VirtualFile waitForTheFile(@Nullable final String path) {
  if (path == null) return null;

  final VirtualFile[] file = new VirtualFile[1];
  final Application app = ApplicationManager.getApplication();
  Runnable action = new Runnable() {
    public void run() {
      app.runWriteAction(new Runnable() {
        public void run() {
          file[0] = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
        }
      });
    }
  };
  if (app.isDispatchThread()) {
    action.run();
  }
  else {
    app.invokeAndWait(action, ModalityState.defaultModalityState());
  }
  return file[0];
}
 
Example 3
Source File: VirtualFileManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void notifyPropertyChanged(@Nonnull final VirtualFile virtualFile, @Nonnull final String property, final Object oldValue, final Object newValue) {
  final Application application = ApplicationManager.getApplication();
  final Runnable runnable = new Runnable() {
    @Override
    public void run() {
      if (virtualFile.isValid() && !application.isDisposed()) {
        application.runWriteAction(new Runnable() {
          @Override
          public void run() {
            List<VFilePropertyChangeEvent> events = Collections.singletonList(new VFilePropertyChangeEvent(this, virtualFile, property, oldValue, newValue, false));
            BulkFileListener listener = application.getMessageBus().syncPublisher(VirtualFileManager.VFS_CHANGES);
            listener.before(events);
            listener.after(events);
          }
        });
      }
    }
  };
  application.invokeLater(runnable, ModalityState.NON_MODAL);
}
 
Example 4
Source File: VcsUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static VirtualFile waitForTheFile(final String path) {
  final VirtualFile[] file = new VirtualFile[1];
  final Application app = ApplicationManager.getApplication();
  Runnable action = new Runnable() {
    @Override
    public void run() {
      app.runWriteAction(new Runnable() {
        @Override
        public void run() {
          file[0] = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
        }
      });
    }
  };

  app.invokeAndWait(action, ModalityState.defaultModalityState());

  return file[0];
}
 
Example 5
Source File: DispatchActions.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Perform a write action.  If the current thread is the event dispatch thread,
 * then the run action will block until all other write actions complete.
 *
 * @param run function to run that contains a write action.
 */
public static void writeAction(@NotNull Runnable run) {
    final Application app = ApplicationManager.getApplication();
    if (app.isDispatchThread() && !app.isWriteAccessAllowed()) {
        app.runWriteAction(run);
    } else {
        run.run();
    }
}
 
Example 6
Source File: OSSSdkRefreshActionTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public void testRefreshNonPantsSdk() {
  String customJdkName = "Custom-JDK";

  try {
    Application application = ApplicationManager.getApplication();
    ProjectRootManager rootManager = ProjectRootManager.getInstance(myProject);

    Sdk customSdk = createDummySdk(customJdkName);

    doImport("examples/src/java/org/pantsbuild/example/hello");

    // set custom SDK
    application.runWriteAction(() -> {
      NewProjectUtil.applyJdkToProject(myProject, customSdk);
    });
    assertEquals(customJdkName, rootManager.getProjectSdk().getName());

    refreshProjectSdk();

    // refreshing changes the project SDK
    Sdk pantsSdk = rootManager.getProjectSdk();
    assertNotSame(customSdk, pantsSdk);

    // previously used custom SDK is not removed
    HashSet<Sdk> jdks = Sets.newHashSet(ProjectJdkTable.getInstance().getAllJdks());
    assertContainsElements(jdks, customSdk, pantsSdk);
  }
  finally {
    removeJdks(jdk -> jdk.getName().equals(customJdkName));
  }
}
 
Example 7
Source File: BuckCopyPasteProcessorTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private void doTest(String pasteText, String expected, String... testdata) throws Exception {
  if (testdata != null && testdata.length > 0) {
    PsiFile[] files = myFixture.configureByFiles(testdata);
    if (files != null) {
      Application application = ApplicationManager.getApplication();

      for (PsiFile file : files) {
        String name = file.getName();
        String newName = maybeRenameFixture(name);
        if (name != newName) {
          // Rename, without the .fixture extension
          VirtualFile fixture = file.getVirtualFile();
          Exception[] inWriteAction = new Exception[1]; // we (may) set [0] in a lambda
          application.runWriteAction(
              () -> {
                try {
                  fixture.rename(this, newName);
                } catch (IOException e) {
                  inWriteAction[0] = e;
                }
              });
          if (inWriteAction[0] != null) {
            throw inWriteAction[0];
          }
        }
      }
    }
  } else {
    String testPath = "formatter/paste/environment.BUCK";
    myFixture.configureByFile(testPath);
  }

  BuckCopyPasteProcessor processor = new BuckCopyPasteProcessor();
  String actual =
      processor.preprocessOnPaste(
          getProject(), myFixture.getFile(), myFixture.getEditor(), pasteText, null);
  assertEquals(expected, actual);
}
 
Example 8
Source File: PatchApplier.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private ApplyPatchStatus createFiles() {
  final Application application = ApplicationManager.getApplication();
  Boolean isSuccess = application.runWriteAction(new Computable<Boolean>() {
    @Override
    public Boolean compute() {
      final List<FilePatch> filePatches = myVerifier.execute();
      myFailedPatches.addAll(filePatches);
      myPatches.removeAll(filePatches);
      return myFailedPatches.isEmpty();
    }
  });
  return isSuccess ? ApplyPatchStatus.SUCCESS : ApplyPatchStatus.FAILURE;
}
 
Example 9
Source File: UpdatePsiFileCopyright.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void processActions() throws IncorrectOperationException {
  Application app = ApplicationManager.getApplication();
  app.runWriteAction(new Runnable() {
    @Override
    public void run() {
      Document doc = FileDocumentManager.getInstance().getDocument(myPsiFile.getVirtualFile());
      PsiDocumentManager.getInstance(myPsiFile.getProject()).doPostponedOperationsAndUnblockDocument(doc);
      for (CommentAction action : myActions) {
        int start = action.getStart();
        int end = action.getEnd();

        switch (action.getType()) {
          case CommentAction.ACTION_INSERT:
            String comment = getCommentText(action.getPrefix(), action.getSuffix());
            if (!comment.isEmpty()) {
              doc.insertString(start, comment);
            }
            break;
          case CommentAction.ACTION_REPLACE:
            doc.replaceString(start, end, getCommentText("", ""));
            break;
          case CommentAction.ACTION_DELETE:
            doc.deleteString(start, end);
            break;
        }
      }
    }
  });
}
 
Example 10
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void decrementPostponedCounter() {
  Application application = ApplicationManager.getApplication();
  application.assertIsWriteThread();
  if (--getContext().myPostponedCounter == 0) {
    if (application.isWriteAccessAllowed()) {
      doPostponedFormatting();
    }
    else {
      application.runWriteAction((Runnable)this::doPostponedFormatting);
    }
  }
}