Java Code Examples for com.intellij.openapi.application.WriteAction#compute()

The following examples show how to use com.intellij.openapi.application.WriteAction#compute() . 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: AbstractLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void updateLibraryContent(@NotNull Set<String> contentUrls) {
  if (!FlutterModuleUtils.declaresFlutter(project)) {
    // If we have a Flutter library, remove it.
    final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final Library existingLibrary = getLibraryByName(getLibraryName());
    if (existingLibrary != null) {
      WriteAction.compute(() -> {
        final LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
        libraryTableModel.removeLibrary(existingLibrary);
        libraryTableModel.commit();
        return null;
      });
    }
    return;
  }
  updateLibraryContent(getLibraryName(), contentUrls, null);
}
 
Example 2
Source File: AbstractLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void updateLibraryContent(@NotNull Set<String> contentUrls) {
  if (!FlutterModuleUtils.declaresFlutter(project)) {
    // If we have a Flutter library, remove it.
    final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final Library existingLibrary = getLibraryByName(getLibraryName());
    if (existingLibrary != null) {
      WriteAction.compute(() -> {
        final LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
        libraryTableModel.removeLibrary(existingLibrary);
        libraryTableModel.commit();
        return null;
      });
    }
    return;
  }
  updateLibraryContent(getLibraryName(), contentUrls, null);
}
 
Example 3
Source File: XDebuggerUtilImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static <P extends XBreakpointProperties> XLineBreakpoint toggleAndReturnLineBreakpoint(@Nonnull final Project project,
                                                                                              @Nonnull final XLineBreakpointType<P> type,
                                                                                              @Nonnull final VirtualFile file,
                                                                                              final int line,
                                                                                              final boolean temporary) {
  return WriteAction.compute(() -> {
    XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
    XLineBreakpoint<P> breakpoint = breakpointManager.findBreakpointAtLine(type, file, line);
    if (breakpoint != null) {
      breakpointManager.removeBreakpoint(breakpoint);
      return null;
    }
    else {
      P properties = type.createBreakpointProperties(file, line);
      return breakpointManager.addLineBreakpoint(type, file.getUrl(), line, properties, temporary);
    }
  });
}
 
Example 4
Source File: CreateFromTemplateDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void doCreate(@Nullable String fileName) {
  try {
    String newName = fileName;
    PsiDirectory directory = myDirectory;
    if (fileName != null) {
      final String finalFileName = fileName;
      CreateFileAction.MkDirs mkDirs = WriteAction.compute(() -> new CreateFileAction.MkDirs(finalFileName, myDirectory));
      newName = mkDirs.newName;
      directory = mkDirs.directory;
    }
    myCreatedElement = FileTemplateUtil.createFromTemplate(myTemplate, newName, myAttrPanel.getVariables(myDefaultProperties), directory);
  }
  catch (Exception e) {
    showErrorDialog(e);
  }
}
 
Example 5
Source File: NewProjectAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
private static void generateProjectAsync(Project project, @Nonnull NewProjectPanel panel) {
  // leave current step
  panel.finish();

  NewModuleWizardContext context = panel.getWizardContext();

  final File location = new File(context.getPath());
  final int childCount = location.exists() ? location.list().length : 0;
  if (!location.exists() && !location.mkdirs()) {
    Messages.showErrorDialog(project, "Cannot create directory '" + location + "'", "Create Project");
    return;
  }

  final VirtualFile baseDir = WriteAction.compute(() -> LocalFileSystem.getInstance().refreshAndFindFileByIoFile(location));
  baseDir.refresh(false, true);

  if (childCount > 0) {
    int rc = Messages.showYesNoDialog(project, "The directory '" + location + "' is not empty. Continue?", "Create New Project", Messages.getQuestionIcon());
    if (rc == Messages.NO) {
      return;
    }
  }

  RecentProjectsManager.getInstance().setLastProjectCreationLocation(location.getParent());

  UIAccess uiAccess = UIAccess.current();
  ProjectManager.getInstance().openProjectAsync(baseDir, uiAccess).doWhenDone((openedProject) -> {
    uiAccess.give(() -> NewOrImportModuleUtil.doCreate(panel, openedProject, baseDir));
  });
}
 
Example 6
Source File: ArtifactsTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected Module addModule(final String moduleName, final @Nullable VirtualFile sourceRoot) {
  return WriteAction.compute(() -> {
    final Module module = createModule(moduleName);
    if (sourceRoot != null) {
      PsiTestUtil.addSourceContentToRoots(module, sourceRoot);
    }
    //   ModuleRootModificationUtil.setModuleSdk(module, getTestProjectJdk());
    return module;
  });
}
 
Example 7
Source File: PackagingElementsTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
static Library addProjectLibrary(final Project project, final @javax.annotation.Nullable Module module, final String name, final DependencyScope scope, final VirtualFile[] jars) {
  return WriteAction.compute(() -> {
    final Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(project).createLibrary(name);
    final Library.ModifiableModel libraryModel = library.getModifiableModel();
    for (VirtualFile jar : jars) {
      libraryModel.addRoot(jar, BinariesOrderRootType.getInstance());
    }
    libraryModel.commit();
    if (module != null) {
      ModuleRootModificationUtil.addDependency(module, library, scope, false);
    }
    return library;
  });
}
 
Example 8
Source File: ArtifactsTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Artifact addArtifact(Project project, String name, ArtifactType type, CompositePackagingElement<?> root) {
  return WriteAction.compute(() -> {
    final ModifiableArtifactModel model = ArtifactManager.getInstance(project).createModifiableModel();
    final ModifiableArtifact artifact = model.addArtifact(name, type);
    if (root != null) {
      artifact.setRootElement(root);
    }
    model.commit();
    return artifact;
  });
}
 
Example 9
Source File: VfsUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testFindRootPerformance() throws Exception {
  File tempDir = WriteAction.compute(() -> {
    File res = createTempDirectory();
    new File(res, "x.jar").createNewFile();
    return res;
  });
  final VirtualFile vDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDir);
  final VirtualFile jar = vDir.findChild("x.jar");
  assertNotNull(jar);

  final NewVirtualFile root = ManagingFS.getInstance().findRoot(jar.getPath() + "!/", (NewVirtualFileSystem)StandardFileSystems.jar());
  PlatformTestUtil.startPerformanceTest("find root is slow", 500, new ThrowableRunnable() {
    @Override
    public void run() throws Throwable {
      final String path = jar.getPath() + "!/";
      final NewVirtualFileSystem fileSystem = (NewVirtualFileSystem)StandardFileSystems.jar();
      JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Collections.nCopies(500, null), null, false, new Processor<Object>() {
        @Override
        public boolean process(Object o) {
          for (int i = 0; i < 1000; i++) {
            NewVirtualFile rootJar = ManagingFS.getInstance().findRoot(path, fileSystem);
            assertNotNull(rootJar);
            assertSame(root, rootJar);
          }
          return true;
        }
      });
    }
  }).assertTiming();
}
 
Example 10
Source File: VfsUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testFindRootWithDenormalizedPath() throws Exception {
  File tempDir = WriteAction.compute(() -> {
    File res = createTempDirectory();
    new File(res, "x.jar").createNewFile();
    return res;
  });
  VirtualFile vDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDir);
  VirtualFile jar = vDir.findChild("x.jar");
  assertNotNull(jar);

  NewVirtualFile root1 = ManagingFS.getInstance().findRoot(jar.getPath() + "!/", (NewVirtualFileSystem)StandardFileSystems.jar());
  NewVirtualFile root2 = ManagingFS.getInstance().findRoot(jar.getParent().getPath() + "//" + jar.getName() + "!/", (NewVirtualFileSystem)StandardFileSystems.jar());
  assertNotNull(root1);
  assertSame(root1, root2);
}
 
Example 11
Source File: VfsUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testFindChildByNamePerformance() throws IOException {
  File tempDir = WriteAction.compute(() -> {
    File res = createTempDirectory();
    return res;
  });
  final VirtualFile vDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDir);
  assertNotNull(vDir);
  assertTrue(vDir.isDirectory());

  for (int i = 0; i < 10000; i++) {
    final String name = i + ".txt";
    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        vDir.createChildData(vDir, name);
      }
    }.execute().throwException();
  }
  final VirtualFile theChild = vDir.findChild("5111.txt");

  PlatformTestUtil.startPerformanceTest("find child is slow", 450, new ThrowableRunnable() {
    @Override
    public void run() throws Throwable {
      for (int i = 0; i < 1000000; i++) {
        VirtualFile child = vDir.findChild("5111.txt");
        assertSame(theChild, child);
      }
    }
  }).assertTiming();
}
 
Example 12
Source File: VfsUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testFindChildWithTrailingSpace() throws IOException {
  File tempDir = WriteAction.compute(() -> {
    File res = createTempDirectory();
    return res;
  });
  VirtualFile vDir = LocalFileSystem.getInstance().findFileByIoFile(tempDir);
  assertNotNull(vDir);
  assertTrue(vDir.isDirectory());

  VirtualFile child = vDir.findChild(" ");
  assertNull(child);

  UsefulTestCase.assertEmpty(vDir.getChildren());
}
 
Example 13
Source File: PlatformTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected static VirtualFile createChildData(@Nonnull final VirtualFile dir, @Nonnull @NonNls final String name) {
  try {
    return WriteAction.compute(() -> dir.createChildData(null, name));
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 14
Source File: PlatformTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected static Module doCreateRealModuleIn(final String moduleName, final Project project) {
  final VirtualFile baseDir = project.getBaseDir();
  assertNotNull(baseDir);
  final File moduleFile = new File(baseDir.getPath().replace('/', File.separatorChar), moduleName);
  FileUtil.createIfDoesntExist(moduleFile);
  myFilesToDelete.add(moduleFile);
  return WriteAction.compute(() -> {
    final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleFile);
    Module module = ModuleManager.getInstance(project).newModule(moduleName, virtualFile.getPath());
    module.getModuleDir();
    return module;
  });
}
 
Example 15
Source File: CreateFileAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
@Nonnull
protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
  MkDirs mkdirs = new MkDirs(newName, directory);
  return new PsiElement[]{WriteAction.compute(() -> mkdirs.directory.createFile(getFileName(mkdirs.newName)))};
}
 
Example 16
Source File: CreateFileAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PsiDirectory findOrCreateSubdirectory(@Nonnull PsiDirectory parent, @Nonnull String subdirName) {
  final PsiDirectory sub = parent.findSubdirectory(subdirName);
  return sub == null ? WriteAction.compute(() -> parent.createSubdirectory(subdirName)) : sub;
}
 
Example 17
Source File: VfsUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static VirtualFile createDirectories(@Nonnull final String directoryPath) throws IOException {
  return WriteAction.compute(() -> {
    VirtualFile res = createDirectoryIfMissing(directoryPath);
    return res;
  });
}
 
Example 18
Source File: RemoteFileInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void finished(@Nullable final FileType fileType) {
  final File localIOFile;

  synchronized (myLock) {
    LOG.debug("Downloading finished, size = " + myLocalFile.length() + ", file type=" + (fileType != null ? fileType.getName() : "null"));
    if (fileType != null) {
      String fileName = myLocalFile.getName();
      int dot = fileName.lastIndexOf('.');
      String extension = fileType.getDefaultExtension();
      if (dot == -1 || !extension.equals(fileName.substring(dot + 1))) {
        File newFile = FileUtil.findSequentNonexistentFile(myLocalFile.getParentFile(), fileName, extension);
        try {
          FileUtil.rename(myLocalFile, newFile);
          myLocalFile = newFile;
        }
        catch (IOException e) {
          LOG.debug(e);
        }
      }
    }

    localIOFile = myLocalFile;
  }

  VirtualFile localFile = WriteAction.compute(() -> {
    final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(localIOFile);
    if (file != null) {
      file.refresh(false, false);
    }
    return file;
  });
  LOG.assertTrue(localFile != null, "Virtual local file not found for " + localIOFile.getAbsolutePath());
  LOG.debug("Virtual local file: " + localFile + ", size = " + localFile.getLength());
  synchronized (myLock) {
    myLocalVirtualFile = localFile;
    myPrevLocalFile = null;
    myState = RemoteFileState.DOWNLOADED;
    myErrorMessage = null;
  }
  for (FileDownloadingListener listener : myListeners) {
    listener.fileDownloaded(localFile);
  }
}
 
Example 19
Source File: XDebuggerTestUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static <P extends XBreakpointProperties> XBreakpoint<P> insertBreakpoint(final Project project, final P properties, final Class<? extends XBreakpointType<XBreakpoint<P>, P>> typeClass) {
  return WriteAction.compute(() -> {
    return XDebuggerManager.getInstance(project).getBreakpointManager().addBreakpoint(XBreakpointType.EXTENSION_POINT_NAME.findExtension(typeClass), properties);
  });
}
 
Example 20
Source File: ModuleTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected Module createModule(final String path) {
  Module module = WriteAction.compute(() -> ModuleManager.getInstance(myProject).newModule(new File(path).getName(), path));

  myModulesToDispose.add(module);
  return module;
}