[mod-command] IJPL-181933 Improve preview for ModCreateFile.Directory commands

GitOrigin-RevId: f0e686fe627a2671c64b28cee2a21d9c9f533814
This commit is contained in:
Tagir Valeev
2025-03-31 12:46:30 +02:00
committed by intellij-monorepo-bot
parent fdfde8d5cb
commit f6d4202b5d
3 changed files with 32 additions and 4 deletions

View File

@@ -42,7 +42,7 @@ public final class ModCommandTest extends LightPlatformCodeInsightTestCase {
assertEquals(ModCommandExecutor.Result.INTERACTIVE, result);
}
public void testCreateDirectories() throws IOException {
public void testCreateDirectories() {
configureFromFileText("dummy.txt", "");
ModCommand command = ModCommand.psiUpdate(getFile(), (f, u) -> {
PsiDirectory d = u.getWritable(getFile().getContainingDirectory());
@@ -69,4 +69,18 @@ public final class ModCommandTest extends LightPlatformCodeInsightTestCase {
PsiFile targetFile = PsiManager.getInstance(getProject()).findFile(target);
assertEquals("hello", targetFile.getFileDocument().getCharsSequence().toString());
}
public void testCreateDirectoriesPreview() {
configureFromFileText("dummy.txt", "");
ModCommand command = ModCommand.psiUpdate(getFile(), (f, u) -> {
PsiDirectory d = u.getWritable(getFile().getContainingDirectory());
PsiDirectory dir1 = d.createSubdirectory("a");
dir1.createSubdirectory("b");
dir1.createSubdirectory("c");
});
IntentionPreviewInfo preview = ModCommandExecutor.getInstance().getPreview(command, ActionContext.from(null, getFile()));
IntentionPreviewInfo.Html html = assertInstanceOf(preview, IntentionPreviewInfo.Html.class);
String actual = html.content().toString();
assertEquals("<p>Create directories:<ul><li>a</li><li>a/b</li><li>a/c</li></ul></p>", actual);
}
}

View File

@@ -242,6 +242,7 @@ progress.title.refresh=Refresh
preview.cannot.perform.action=Cannot perform action:
preview.copy.to.clipboard=Copy to clipboard the string "{0}"
preview.create.directory=Create directory ''{0}''
preview.create.directories=Create directories:
preview.open.url=Browse "{0}"
modcommand.result.action.completed.successfully=Action completed successfully
modcommand.result.action.is.interactive.only.cannot.be.executed.in.batch=Action is interactive only; cannot be executed in batch

View File

@@ -14,6 +14,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.text.HtmlBuilder;
import com.intellij.openapi.util.text.HtmlChunk;
import com.intellij.openapi.util.text.StringUtil;
@@ -293,6 +294,7 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
PsiFile file = context.file();
List<IntentionPreviewInfo.CustomDiff> customDiffList = new ArrayList<>();
IntentionPreviewInfo navigateInfo = IntentionPreviewInfo.EMPTY;
List<@NlsSafe String> createdDirs = new ArrayList<>();
for (ModCommand command : modCommand.unpack()) {
if (command instanceof ModUpdateFileText modFile) {
VirtualFile vFile = modFile.file();
@@ -308,7 +310,7 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
else if (command instanceof ModCreateFile createFile) {
VirtualFile vFile = createFile.file();
if (createFile.content() instanceof ModCreateFile.Directory) {
navigateInfo = new IntentionPreviewInfo.Html(text(AnalysisBundle.message("preview.create.directory", vFile.getPath())));
createdDirs.add(getFileNamePresentation(project, vFile));
} else {
String content =
createFile.content() instanceof ModCreateFile.Text text ? text.text() : AnalysisBundle.message("preview.binary.content");
@@ -369,8 +371,19 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
}
}
customDiffList.sort(Comparator.comparing(diff -> diff.fileName() != null));
return customDiffList.isEmpty() ? navigateInfo :
customDiffList.size() == 1 ? customDiffList.get(0) :
if (customDiffList.isEmpty()) {
if (!createdDirs.isEmpty()) {
if (createdDirs.size() == 1) {
return new IntentionPreviewInfo.Html(text(AnalysisBundle.message("preview.create.directory", createdDirs.get(0))));
}
return new IntentionPreviewInfo.Html(
tag("p").addText(AnalysisBundle.message("preview.create.directories"))
.child(tag("ul").children(ContainerUtil.map(createdDirs, text -> tag("li").addText(text))))
);
}
return navigateInfo;
}
return customDiffList.size() == 1 ? customDiffList.get(0) :
new IntentionPreviewInfo.MultiFileDiff(customDiffList);
}