Any way to delete a file before create

Answered

I am trying to create a file by psiFileFactory:

val project = e.getData(PlatformDataKeys.PROJECT)
var psiFileFactory = PsiFileFactory.getInstance(project)
val createFileFromText = psiFileFactory.createFileFromText("soc.md", "fadsfdsaf")
var directory = modFile.containingDirectory
directory.add(createFileFromText)

But it could generate error if there is the same file. So is there any way to delete the same name file before create?

0
5 comments

Assuming `directory` is a `com.intellij.psi.PsiDirectory`, `com.intellij.psi.PsiDirectory#checkCreateFile`

0

Yann Cebron

What I want is to overwrite the same file if the file exists. But `checkCreateFile`  is utilized to check if it's possible to crate a file.

0

PsiDirectory#findFile(), if found, PsiFile#delete, then proceed with adding new file like before

0

If try to delete file like this, it seems the delete operation was executed. But it would generate the below error.

val createFileFromText = psiFileFactory.createFileFromText("sca.md", result)
var directory = modFile.containingDirectory
directory.findFile("sca.md")?.delete()
directory.add(createFileFromText)

2022-05-04 04:24:52,241 [ 154720]  ERROR - plication.impl.ApplicationImpl - Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction()) 
java.lang.Throwable: Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
    at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:218)
    at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:1256)
    at com.intellij.openapi.editor.impl.DocumentImpl.assertWriteAccess(DocumentImpl.java:689)
    at com.intellij.openapi.editor.impl.DocumentImpl.replaceString(DocumentImpl.java:607)
    at com.intellij.openapi.editor.impl.DocumentImpl.lambda$setText$2(DocumentImpl.java:1085)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:219)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:174)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:164)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:150)
    at com.intellij.openapi.editor.impl.DocumentImpl.setText(DocumentImpl.java:1090)
    at com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:414)
    at com.github.madneal.secdog.DependencyCheck.actionPerformed(DependencyCheck.kt:42)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.lambda$performActionDumbAwareWithCallbacks$4(ActionUtil.java:244)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.performDumbAwareWithCallbacks(ActionUtil.java:265)
    at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAwareWithCallbacks(ActionUtil.java:244)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:300)
    at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:236)
    at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:67)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:292)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem.lambda$fireActionPerformed$3(ActionMenuItem.java:131)
    at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:94)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:131)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:401)
    at java.desktop/javax.swing.AbstractButton.doClick(AbstractButton.java:369)
    at java.desktop/com.apple.laf.ScreenMenuItemCheckbox.itemStateChanged(ScreenMenuItemCheckbox.java:204)
    at java.desktop/java.awt.CheckboxMenuItem.processItemEvent(CheckboxMenuItem.java:396)
    at java.desktop/java.awt.CheckboxMenuItem.processEvent(CheckboxMenuItem.java:364)
    at java.desktop/java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:375)
    at java.desktop/java.awt.MenuComponent.dispatchEvent(MenuComponent.java:363)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:781)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:891)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:760)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$6(IdeEventQueue.java:447)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:818)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$7(IdeEventQueue.java:446)
    at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:805)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:498)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

0

Please sign in to leave a comment.