Java Code Examples for com.intellij.openapi.vfs.ReadonlyStatusHandler#OperationStatus

The following examples show how to use com.intellij.openapi.vfs.ReadonlyStatusHandler#OperationStatus . 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: PatchApplier.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
protected ApplyPatchStatus executeWritable() {
  final ReadonlyStatusHandler.OperationStatus readOnlyFilesStatus = getReadOnlyFilesStatus(myVerifier.getWritableFiles());
  if (readOnlyFilesStatus.hasReadonlyFiles()) {
    showError(myProject, readOnlyFilesStatus.getReadonlyFilesMessage(), true);
    return ApplyPatchStatus.ABORT;
  }
  myFailedPatches.addAll(myVerifier.filterBadFileTypePatches());
  ApplyPatchStatus result = myFailedPatches.isEmpty() ? null : ApplyPatchStatus.FAILURE;
  final List<Pair<VirtualFile, ApplyTextFilePatch>> textPatches = myVerifier.getTextPatches();
  try {
    markInternalOperation(textPatches, true);
    return ApplyPatchStatus.and(result, actualApply(textPatches, myVerifier.getBinaryPatches(), myCommitContext));
  }
  finally {
    markInternalOperation(textPatches, false);
  }
}
 
Example 2
Source File: CodeInsightUtilBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean preparePsiElementsForWrite(@Nonnull Collection<? extends PsiElement> elements) {
  if (elements.isEmpty()) return true;
  Set<VirtualFile> files = new THashSet<VirtualFile>();
  Project project = null;
  for (PsiElement element : elements) {
    if (element == null) continue;
    PsiFile file = element.getContainingFile();
    if (file == null || !file.isPhysical()) continue;
    project = file.getProject();
    VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null) continue;
    files.add(virtualFile);
  }
  if (!files.isEmpty()) {
    VirtualFile[] virtualFiles = VfsUtilCore.toVirtualFileArray(files);
    ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(virtualFiles);
    return !status.hasReadonlyFiles();
  }
  return true;
}
 
Example 3
Source File: InjectTextAction.java    From PackageTemplates with Apache License 2.0 5 votes vote down vote up
private void injectText(PsiFile psiFile) {
    ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(psiFile.getVirtualFile());
    if (status.hasReadonlyFiles()) {
        ReportHelper.setState(ExecutionState.FAILED);
        ReportHelper.putReport(new FailedActionReport(this, Localizer.get("error.ReadOnlyFile"), "InjectTextAction ReadOnlyFile"));
        Logger.log("InjectTextAction ReadOnlyFile");
        return;
    }

    Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
    if (document == null) {
        ReportHelper.setState(ExecutionState.FAILED);
        ReportHelper.putReport(new FailedActionReport(this, Localizer.get("error.internalError"), "InjectTextAction document==nul"));
        Logger.log("InjectTextAction document==nul");
        return;
    }

    int offset = getOffset(document, textInjection.getInjectDirection());
    if (offset == -1) {
        return;
    }

    String textToInsert = fromVelocity(textInjection.getTextToInject());
    if (textToInsert == null) {
        return;
    }

    // Replace
    if (textInjection.getInjectDirection() == InjectDirection.REPLACE) {
        doReplace(document, textToInsert);
    }
    // Insert
    else {
        document.insertString(offset, textToInsert);
    }
}
 
Example 4
Source File: AbstractIntention.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
protected static boolean isFileReadOnly( @NotNull Project project, @NotNull PsiFile file )
{
    VirtualFile virtualFile = file.getVirtualFile();
    ReadonlyStatusHandler readonlyStatusHandler = ReadonlyStatusHandler.getInstance( project );
    ReadonlyStatusHandler.OperationStatus operationStatus =
        readonlyStatusHandler.ensureFilesWritable( virtualFile );
    return operationStatus.hasReadonlyFiles();
}
 
Example 5
Source File: ProjectStoreImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doSave(@Nullable List<SaveSession> saveSessions, @Nonnull List<Pair<SaveSession, File>> readonlyFiles) {
  ProjectStorageUtil.UnableToSaveProjectNotification[] notifications = NotificationsManager.getNotificationsManager().getNotificationsOfType(ProjectStorageUtil.UnableToSaveProjectNotification.class, myProject);
  if (notifications.length > 0) {
    throw new SaveCancelledException();
  }

  beforeSave(readonlyFiles);

  super.doSave(saveSessions, readonlyFiles);

  if (!readonlyFiles.isEmpty()) {
    ReadonlyStatusHandler.OperationStatus status = AccessRule.read(() -> {
      List<File> filesList = getFilesList(readonlyFiles);
      VirtualFile[] files = filesList.stream().map(file -> LocalFileSystem.getInstance().findFileByIoFile(file)).toArray(VirtualFile[]::new);
      return ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(files);
    });

    if (status.hasReadonlyFiles()) {
      ProjectStorageUtil.dropUnableToSaveProjectNotification(myProject, VfsUtil.virtualToIoFiles(Arrays.asList(status.getReadonlyFiles())));
      throw new SaveCancelledException();
    }
    else {
      List<Pair<SaveSession, File>> oldList = new ArrayList<>(readonlyFiles);
      readonlyFiles.clear();
      for (Pair<SaveSession, File> entry : oldList) {
        executeSave(entry.first, readonlyFiles);
      }

      if (!readonlyFiles.isEmpty()) {
        ProjectStorageUtil.dropUnableToSaveProjectNotification(myProject, getFilesList(readonlyFiles));
        throw new SaveCancelledException();
      }
    }
  }
}
 
Example 6
Source File: MergeOperations.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void perform(final Project project) {
  if (myPerformed) return;
  myPerformed = true;
  if (!myDocument.isWritable()) {
    final VirtualFile file = FileDocumentManager.getInstance().getFile(myDocument);
    final ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(file);
    if (status.hasReadonlyFiles()) return;
  }
  ApplicationManager.getApplication().runWriteAction(new Runnable(){
    public void run() {
      CommandProcessor.getInstance().executeCommand(project, myModification, getName(), null);
    }
  });
}
 
Example 7
Source File: CommonRefactoringUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean checkReadOnlyStatus(@Nonnull Project project,
                                           @Nonnull Collection<? extends PsiElement> recursive,
                                           @Nonnull Collection<? extends PsiElement> flat,
                                           @Nonnull String messagePrefix,
                                           boolean notifyOnFail) {
  Collection<VirtualFile> readonly = new THashSet<>();  // not writable, but could be checked out
  Collection<VirtualFile> failed = new THashSet<>();  // those located in read-only filesystem

  boolean seenNonWritablePsiFilesWithoutVirtualFile =
          checkReadOnlyStatus(flat, false, readonly, failed) || checkReadOnlyStatus(recursive, true, readonly, failed);

  ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(readonly);
  ContainerUtil.addAll(failed, status.getReadonlyFiles());

  if (notifyOnFail && (!failed.isEmpty() || seenNonWritablePsiFilesWithoutVirtualFile && readonly.isEmpty())) {
    StringBuilder message = new StringBuilder(messagePrefix).append('\n');
    int i = 0;
    for (VirtualFile virtualFile : failed) {
      String subj = RefactoringBundle.message(virtualFile.isDirectory() ? "directory.description" : "file.description", virtualFile.getPresentableUrl());
      if (virtualFile.getFileSystem().isReadOnly()) {
        message.append(RefactoringBundle.message("0.is.located.in.a.archive.file", subj)).append('\n');
      }
      else {
        message.append(RefactoringBundle.message("0.is.read.only", subj)).append('\n');
      }
      if (i++ > 20) {
        message.append("...\n");
        break;
      }
    }
    showErrorMessage(RefactoringBundle.message("error.title"), message.toString(), null, project);
    return false;
  }

  return failed.isEmpty();
}
 
Example 8
Source File: QuickFixAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if immediate UI update needed.
 */
protected boolean applyFix(@Nonnull RefEntity[] refElements) {
  Set<VirtualFile> readOnlyFiles = getReadOnlyFiles(refElements);
  if (!readOnlyFiles.isEmpty()) {
    final Project project = refElements[0].getRefManager().getProject();
    final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(
      VfsUtilCore.toVirtualFileArray(readOnlyFiles));
    if (operationStatus.hasReadonlyFiles()) return false;
  }
  return true;
}
 
Example 9
Source File: UndoRedo.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean execute(boolean drop, boolean isInsideStartFinishGroup) {
  if (!myUndoableGroup.isUndoable()) {
    reportCannotUndo(CommonBundle.message("cannot.undo.error.contains.nonundoable.changes.message"),
                     myUndoableGroup.getAffectedDocuments());
    return false;
  }

  Set<DocumentReference> clashing = getStackHolder().collectClashingActions(myUndoableGroup);
  if (!clashing.isEmpty()) {
    reportCannotUndo(CommonBundle.message("cannot.undo.error.other.affected.files.changed.message"), clashing);
    return false;
  }


  if (!isInsideStartFinishGroup && myUndoableGroup.shouldAskConfirmation(isRedo())) {
    if (!askUser()) return false;
  }
  else {
    if (restore(getBeforeState(), true)) {
      setBeforeState(new EditorAndState(myEditor, myEditor.getState(FileEditorStateLevel.UNDO)));
      return true;
    }
  }

  Collection<VirtualFile> readOnlyFiles = collectReadOnlyAffectedFiles();
  if (!readOnlyFiles.isEmpty()) {
    final Project project = myManager.getProject();
    final VirtualFile[] files = VfsUtil.toVirtualFileArray(readOnlyFiles);

    if (project == null) {
      return false;
    }

    final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files);
    if (operationStatus.hasReadonlyFiles()) {
      return false;
    }
  }

  Collection<Document> readOnlyDocuments = collectReadOnlyDocuments();
  if (!readOnlyDocuments.isEmpty()) {
    for (Document document : readOnlyDocuments) {
      document.fireReadOnlyModificationAttempt();
    }
    return false;
  }

  getStackHolder().removeFromStacks(myUndoableGroup);
  if (!drop) {
    getReverseStackHolder().addToStacks(myUndoableGroup);
  }

  performAction();

  restore(getAfterState(), false);

  return true;
}
 
Example 10
Source File: PatchApplier.java    From consulo with Apache License 2.0 4 votes vote down vote up
private ReadonlyStatusHandler.OperationStatus getReadOnlyFilesStatus(@Nonnull final List<VirtualFile> filesToMakeWritable) {
  final VirtualFile[] fileArray = VfsUtilCore.toVirtualFileArray(filesToMakeWritable);
  return ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(fileArray);
}
 
Example 11
Source File: CodeInsightUtilBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean prepareVirtualFilesForWrite(@Nonnull Project project, @Nonnull Collection<VirtualFile> files) {
  ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files);
  return !status.hasReadonlyFiles();
}