private static boolean isQuickFixOnReadOnlyFile(PsiElement problemElement) { final PsiFile containingPsiFile = problemElement.getContainingFile(); if (containingPsiFile == null) { return false; } final VirtualFile virtualFile = containingPsiFile.getVirtualFile(); final Project project = problemElement.getProject(); final ReadonlyStatusHandler handler = ReadonlyStatusHandler.getInstance(project); final ReadonlyStatusHandler.OperationStatus status; if (virtualFile != null) { status = handler.ensureFilesWritable(virtualFile); return status.hasReadonlyFiles(); } return false; }
/** * Applies the text from the original marker to the target marker. * @return the resulting TextRange from the target document, or null if the document if not writable. */ @Nullable private static TextRange modifyDocument(@Nullable Project project, @NotNull RangeMarker original, @NotNull RangeMarker target) { Document document = target.getDocument(); if (project != null && !ReadonlyStatusHandler.ensureDocumentWritable(project, document)) { return null; } if (DocumentUtil.isEmpty(original)) { int offset = target.getStartOffset(); document.deleteString(offset, target.getEndOffset()); } CharSequence text = original.getDocument().getImmutableCharSequence().subSequence(original.getStartOffset(), original.getEndOffset()); int startOffset = target.getStartOffset(); if (DocumentUtil.isEmpty(target)) { document.insertString(startOffset, text); } else { document.replaceString(startOffset, target.getEndOffset(), text); } return new TextRange(startOffset, startOffset + text.length()); }
private static void execute(@NotNull final LineStatusTracker tracker, @NotNull final Runnable task) { CommandProcessor.getInstance().executeCommand(tracker.getProject(), new Runnable() { public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { if (!tracker.getDocument().isWritable()) { final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler .getInstance(tracker.getProject()).ensureFilesWritable(tracker.getVirtualFile()); if (operationStatus.hasReadonlyFiles()) return; } task.run(); } }); } }, VcsBundle.message("command.name.rollback.change"), null); }
@Override public boolean prepareFileForWrite(@Nullable final PsiFile psiFile) { if (psiFile == null) return false; final VirtualFile file = psiFile.getVirtualFile(); final Project project = psiFile.getProject(); if (ReadonlyStatusHandler.ensureFilesWritable(project, file)) { return true; } ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file), true); if (editor != null && editor.getComponent().isDisplayable()) { HintManager.getInstance().showErrorHint(editor, CodeInsightBundle.message("error.hint.file.is.readonly", file.getPresentableUrl())); } } }, project.getDisposed()); return false; }
@Override public boolean preparePsiElementsForWrite(@NotNull 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; }
@Override public void actionPerformed(AnActionEvent e) { final Document document = myEditor.getDocument(); final VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null) { ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(file); } String selection = myEditor.getSelectionModel().getSelectedText(true); if (selection != null) { selection = selection.trim(); PsiDocumentManager.getInstance(myFile.getProject()).commitAllDocuments(); myTemplate.wrap(selection, new CustomTemplateCallback(myEditor, myFile)); } }
private void commitToOriginal(final DocumentEvent e) { VirtualFile origVirtualFile = PsiUtilCore.getVirtualFile(myNewFile.getContext()); myCommittingToOriginal = true; try { if (origVirtualFile == null || !ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(origVirtualFile).hasReadonlyFiles()) { PostprocessReformattingAspect.getInstance(myProject).disablePostprocessFormattingInside(new Runnable() { @Override public void run() { if (myAltFullRange != null) { altCommitToOriginal(e); return; } commitToOriginalInner(); } }); PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myOrigDocument); } } finally { myCommittingToOriginal = false; } }
private boolean ensureUsagesWritable(ReplaceContext replaceContext, Collection<Usage> selectedUsages) { Set<VirtualFile> readOnlyFiles = null; for (final Usage usage : selectedUsages) { final VirtualFile file = ((UsageInFile)usage).getFile(); if (file != null && !file.isWritable()) { if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>(); readOnlyFiles.add(file); } } if (readOnlyFiles != null) { ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles)); } if (hasReadOnlyUsages(selectedUsages)) { int result = Messages.showOkCancelDialog(replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"), FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon()); if (result != Messages.OK) { return false; } } return true; }
public void performReplaceAll(Editor e) { if (!ReadonlyStatusHandler.ensureDocumentWritable(e.getProject(), e.getDocument())) return; if (mySearchResults.getFindModel() != null) { final FindModel copy = new FindModel(); copy.copyFrom(mySearchResults.getFindModel()); final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel(); final int offset; if (!selectionModel.hasSelection() || copy.isGlobal()) { copy.setGlobal(true); offset = 0; } else { offset = selectionModel.getBlockSelectionStarts()[0]; } FindUtil.replace(e.getProject(), e, offset, copy, this); } }
@Override protected void executeWithReparse(ThrowableRunnable<Exception> operation) { if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) { return; } try { myPsiChangeListener.stop(); operation.run(); myPsiChangeListener.start(); reparseFile(); } catch (Throwable e) { showError("Execute command", e); myPsiChangeListener.start(); } }
@Override protected void execute(List<EditOperation> operations) { if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) { return; } try { myPsiChangeListener.stop(); for (EditOperation operation : operations) { operation.execute(); } updateRenderer(true); } catch (Throwable e) { showError("Execute command", e); } finally { myPsiChangeListener.start(); } }
public static boolean saveCreatedProperty(final PropertiesFile bundle, final String name, final String value, final PsiFile formFile) { final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(bundle.getProject()).ensureFilesWritable(bundle.getVirtualFile()); if (operationStatus.hasReadonlyFiles()) { return false; } CommandProcessor.getInstance().executeCommand( bundle.getProject(), new Runnable() { public void run() { UndoUtil.markPsiFileForUndo(formFile); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { try { bundle.addProperty(name, value); } catch (IncorrectOperationException e1) { LOG.error(e1); } } }); } }, UIDesignerBundle.message("command.create.property"), null); return true; }
public boolean ensureEditable() { if (isEditable()) { return true; } VirtualFile sourceFileToCheckOut = null; if (!GuiDesignerConfiguration.getInstance(getProject()).INSTRUMENT_CLASSES) { final String classToBind = myRootContainer.getClassToBind(); if (classToBind != null && classToBind.length() > 0) { PsiClass psiClass = FormEditingUtil.findClassToBind(getModule(), classToBind); if (psiClass != null) { sourceFileToCheckOut = psiClass.getContainingFile().getVirtualFile(); } } } final ReadonlyStatusHandler.OperationStatus status; if (sourceFileToCheckOut != null) { status = ReadonlyStatusHandler.getInstance(getProject()).ensureFilesWritable(myFile, sourceFileToCheckOut); } else { status = ReadonlyStatusHandler.getInstance(getProject()).ensureFilesWritable(myFile); } return !status.hasReadonlyFiles(); }
public void actionPerformed(AnActionEvent e) { try { final VcsContext context = CvsContextWrapper.createCachedInstance(e); final VirtualFile[] files = context.getSelectedFiles(); if (files.length == 0) return; final Project project = context.getProject(); final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(files); if (operationStatus.hasReadonlyFiles()) { return; } AbstractVcsHelper.getInstance(project).showMergeDialog(Arrays.asList(files), new CvsMergeProvider()); } catch (Exception e1) { LOG.error(e1); } }
private void handleFiles(final List<PsiFile> files) { final List<VirtualFile> vFiles = new ArrayList<VirtualFile>(); for (PsiFile psiFile : files) { vFiles.add(psiFile.getVirtualFile()); } if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtil.toVirtualFileArray(vFiles)) .hasReadonlyFiles()) { if (!files.isEmpty()) { final Runnable[] resultRunnable = new Runnable[1]; execute(new Runnable() { @Override public void run() { resultRunnable[0] = prepareFiles(files); } }, new Runnable() { @Override public void run() { if (resultRunnable[0] != null) { resultRunnable[0].run(); } } }); } } }
/** * Applies the text from the original marker to the target marker. * @return the resulting TextRange from the target document, or null if the document if not writable. */ @Nullable private static TextRange modifyDocument(@NotNull Project project, @NotNull RangeMarker original, @NotNull RangeMarker target) { Document document = target.getDocument(); if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) { return null; } if (DocumentUtil.isEmpty(original)) { int offset = target.getStartOffset(); document.deleteString(offset, target.getEndOffset()); } String text = DocumentUtil.getText(original); int startOffset = target.getStartOffset(); if (DocumentUtil.isEmpty(target)) { document.insertString(startOffset, text); } else { document.replaceString(startOffset, target.getEndOffset(), text); } return new TextRange(startOffset, startOffset + text.length()); }
public static boolean ensureFilesWritable(@NotNull final Project project, @NotNull final Collection<PsiFile> psiFiles) { if (!psiFiles.isEmpty()) { List<VirtualFile> list = new SmartList<VirtualFile>(); for (final PsiFile psiFile : psiFiles) { if (psiFile == null) continue; final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null) { list.add(virtualFile); } } if (!list.isEmpty()) { if (ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(list)).hasReadonlyFiles()) { return false; } } } return true; }
public void actionPerformed(final AnActionEvent e) { CommandProcessor.getInstance().executeCommand(myLineStatusTracker.getProject(), new Runnable() { public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { if (!myLineStatusTracker.getDocument().isWritable()) { final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler .getInstance(myLineStatusTracker.getProject()).ensureFilesWritable(myLineStatusTracker.getVirtualFile()); if (operationStatus.hasReadonlyFiles()) return; } myLineStatusTracker.rollbackChanges(myRange); } }); } }, VcsBundle.message("command.name.rollback.change"), null); }
@Override public boolean prepareFileForWrite(@Nullable final PsiFile psiFile) { if (psiFile == null) return false; final VirtualFile file = psiFile.getVirtualFile(); final Project project = psiFile.getProject(); final Editor editor = psiFile.isWritable() ? null : FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file), true); if (!ReadonlyStatusHandler.ensureFilesWritable(project, file)) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { if (editor != null && editor.getComponent().isDisplayable()) { HintManager.getInstance() .showErrorHint(editor, CodeInsightBundle.message("error.hint.file.is.readonly", file.getPresentableUrl())); } } }); return false; } return true; }
@Override public boolean preparePsiElementsForWrite(@NotNull Collection<? extends PsiElement> elements) { if (elements.isEmpty()) return true; Set<VirtualFile> files = new THashSet<VirtualFile>(); Project project = null; for (PsiElement element : elements) { PsiFile file = element.getContainingFile(); if (file == null) 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; }
@Override public void actionPerformed(AnActionEvent e) { final Document document = myEditor.getDocument(); final VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null) { ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(file); } String selection = myEditor.getSelectionModel().getSelectedText(); if (selection != null) { selection = selection.trim(); PsiDocumentManager.getInstance(myFile.getProject()).commitAllDocuments(); myTemplate.wrap(selection, new CustomTemplateCallback(myEditor, myFile, true)); } }
private void commitToOriginal() { if (!isValid()) return; final PsiFile origFile = (PsiFile)myNewFile.getUserData(FileContextUtil.INJECTED_IN_ELEMENT).getElement(); VirtualFile origFileVirtualFile = origFile != null? origFile.getVirtualFile() : null; myCommittingToOriginal = true; try { if (origFileVirtualFile == null || !ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(origFileVirtualFile).hasReadonlyFiles()) { PostprocessReformattingAspect.getInstance(myProject).disablePostprocessFormattingInside(new Runnable() { @Override public void run() { if (myAltFullRange != null) { altCommitToOriginal(); return; } commitToOriginalInner(); } }); } } finally { myCommittingToOriginal = false; } }
private boolean ensureUsagesWritable(ReplaceContext replaceContext, Collection<Usage> selectedUsages) { Set<VirtualFile> readOnlyFiles = null; for (final Usage usage : selectedUsages) { final VirtualFile file = ((UsageInFile)usage).getFile(); if (file != null && !file.isWritable()) { if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>(); readOnlyFiles.add(file); } } if (readOnlyFiles != null) { ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles)); } if (hasReadOnlyUsages(selectedUsages)) { int result = Messages.showOkCancelDialog(replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"), FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon()); if (result != 0) { return false; } } return true; }
public void performReplaceAll(Editor e) { if (!ReadonlyStatusHandler.ensureDocumentWritable(e.getProject(), e.getDocument())) return; if (mySearchResults.getFindModel() != null) { final FindModel copy = new FindModel(); copy.copyFrom(mySearchResults.getFindModel()); final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel(); final int offset; if ((!selectionModel.hasSelection() && !selectionModel.hasBlockSelection()) || copy.isGlobal()) { copy.setGlobal(true); offset = 0; } else { offset = selectionModel.getBlockSelectionStarts()[0]; } FindUtil.replace(e.getProject(), e, offset, copy, this); } }
public void actionPerformed(AnActionEvent e) { try { final VcsContext context = CvsContextWrapper.createCachedInstance(e); final VirtualFile[] files = context.getSelectedFiles(); if (files == null || files.length == 0) return; final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(context.getProject()).ensureFilesWritable(files); if (operationStatus.hasReadonlyFiles()) { return; } final Project project = context.getProject(); AbstractVcsHelper.getInstance(project).showMergeDialog(Arrays.asList(files), new CvsMergeProvider()); } catch (Exception e1) { LOG.error(e1); } }
private void handleFiles(final List<PsiFile> files) { final List<VirtualFile> vFiles = new ArrayList<VirtualFile>(); for (PsiFile psiFile : files) { vFiles.add(psiFile.getVirtualFile()); } if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtil.toVirtualFileArray(vFiles)) .hasReadonlyFiles()) { if (!files.isEmpty()) { final Runnable[] resultRunnable = new Runnable[1]; execute(new Runnable() { public void run() { resultRunnable[0] = prepareFiles(files); } }, new Runnable() { public void run() { if (resultRunnable[0] != null) { resultRunnable[0].run(); } } }); } } }
protected void execute(java.util.List<EditOperation> operations) { if (!ReadonlyStatusHandler.ensureFilesWritable(getProject(), myFile)) { return; } try { myPsiChangeListener.stop(); for (EditOperation operation : operations) { operation.execute(); } updateRenderer(true); } catch (Throwable e) { showError("Execute command", e); } finally { myPsiChangeListener.start(); } }
@Override public void save() { if (context.project == null) { Flog.info("Document: %s can not be saved.", document); return; } if (!ReadonlyStatusHandler.ensureDocumentWritable(context.project, document)) { Flog.info("Document: %s is not writable, can not save.", document); return; } setReadOnly(false); try { FileDocumentManager.getInstance().saveDocument(document); } catch (Throwable e) { Flog.error(e); } }
/** * Applies the text from the original marker to the target marker. * @return the resulting TextRange from the target document, or null if the document if not writable. */ @Nullable private static TextRange modifyDocument(@Nonnull Project project, @Nonnull RangeMarker original, @Nonnull RangeMarker target) { Document document = target.getDocument(); if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) { return null; } if (DocumentUtil.isEmpty(original)) { int offset = target.getStartOffset(); document.deleteString(offset, target.getEndOffset()); } String text = DocumentUtil.getText(original); int startOffset = target.getStartOffset(); if (DocumentUtil.isEmpty(target)) { document.insertString(startOffset, text); } else { document.replaceString(startOffset, target.getEndOffset(), text); } return new TextRange(startOffset, startOffset + text.length()); }
@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); } }
private void handleFiles(final List<PsiFile> files) { final List<VirtualFile> vFiles = new ArrayList<VirtualFile>(); for (PsiFile psiFile : files) { vFiles.add(psiFile.getVirtualFile()); } if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtil.toVirtualFileArray(vFiles)).hasReadonlyFiles()) { if (!files.isEmpty()) { final Runnable[] resultRunnable = new Runnable[1]; execute(new Runnable() { public void run() { resultRunnable[0] = prepareFiles(files); } }, new Runnable() { public void run() { if (resultRunnable[0] != null) { resultRunnable[0].run(); } } } ); } } }
@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; }
private boolean ensureUsagesWritable(ReplaceContext replaceContext, Collection<Usage> selectedUsages) { Set<VirtualFile> readOnlyFiles = null; for (final Usage usage : selectedUsages) { final VirtualFile file = ((UsageInFile)usage).getFile(); if (file != null && !file.isWritable()) { if (readOnlyFiles == null) readOnlyFiles = new HashSet<>(); readOnlyFiles.add(file); } } if (readOnlyFiles != null) { ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles)); } if (hasReadOnlyUsages(selectedUsages)) { int result = Messages.showOkCancelDialog(replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"), FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon()); if (result != Messages.OK) { return false; } } return true; }