From 71f54717d06e64f0aea64eb7afde1a82cfde7ea0 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 10 Jan 2025 13:35:13 +0100 Subject: [PATCH] [mod-commands] ModCommandExecutorImpl.ActionContextPointer: do not rely on PsiFile validity It can be invalidated easily. Get the fresh one from VirtualFile Fixes IDEA-365492 File is invalid, when invoking some ModCommand based intention from property tests GitOrigin-RevId: 24a7d42f4d774f19ce8e118b22aeaa7d9d7dc2bc --- .../lang/impl/modcommand/ModCommandExecutorImpl.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java b/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java index 91a6c12f34ff..1945dedf82f7 100644 --- a/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java +++ b/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java @@ -76,6 +76,7 @@ import java.awt.datatransfer.StringSelection; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import java.util.Objects; import java.util.concurrent.Callable; import static java.util.Objects.requireNonNullElse; @@ -540,7 +541,7 @@ public class ModCommandExecutorImpl extends ModCommandBatchExecutorImpl { private static class ActionContextPointer { private final @NotNull Project myProject; - private final @NotNull PsiFile myFile; + private final @NotNull VirtualFile myFile; private final @Nullable SmartPsiElementPointer myElementPointer; private final @NotNull RangeMarker myOffsetMarker; private final @NotNull RangeMarker mySelectionMarker; @@ -548,9 +549,9 @@ public class ModCommandExecutorImpl extends ModCommandBatchExecutorImpl { ActionContextPointer(@NotNull ActionContext context) { myProject = context.project(); - myFile = context.file(); + myFile = Objects.requireNonNull(context.file().getVirtualFile()); myElementPointer = context.element() != null ? SmartPointerManager.createPointer(context.element()) : null; - Document document = myFile.getFileDocument(); + Document document = context.file().getFileDocument(); myOffsetMarker = document.createRangeMarker(context.offset(), context.offset()); mySelectionMarker = document.createRangeMarker(context.selection()); } @@ -565,7 +566,9 @@ public class ModCommandExecutorImpl extends ModCommandBatchExecutorImpl { @Nullable ActionContext restore() { if (!isValid()) return null; - return new ActionContext(myProject, myFile, myOffsetMarker.getStartOffset(), + PsiFile file = PsiManager.getInstance(myProject).findFile(myFile); + if (file == null) return null; + return new ActionContext(myProject, file, myOffsetMarker.getStartOffset(), mySelectionMarker.getTextRange(), myElementPointer != null ? myElementPointer.getElement() : null); }