[mod-commands] Better preview for create directory + move

GitOrigin-RevId: 846f75946e1bc26ce562f21e70bed0e2c9f3810d
This commit is contained in:
Tagir Valeev
2025-04-04 18:53:29 +02:00
committed by intellij-monorepo-bot
parent 0c604ad7e0
commit 6bb5aeda34
2 changed files with 36 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;
@@ -83,4 +84,18 @@ public final class ModCommandTest extends LightPlatformCodeInsightTestCase {
String actual = html.content().toString();
assertEquals("<p>Create directories:<br/>&bull; a<br/>&bull; a/b<br/>&bull; a/c</p>", actual);
}
public void testCreateDirectoriesAndMovePreview() {
configureFromFileText("dummy.txt", "");
VirtualFile vFile = getFile().getVirtualFile();
FutureVirtualFile target = new FutureVirtualFile(vFile.getParent(), "a", null);
ModCommand command = new ModCreateFile(target, new ModCreateFile.Directory())
.andThen(new ModMoveFile(vFile, new FutureVirtualFile(target, vFile.getName(), vFile.getFileType())));
IntentionPreviewInfo preview = ModCommandExecutor.getInstance().getPreview(command, ActionContext.from(null, getFile()));
IntentionPreviewInfo.Html html = assertInstanceOf(preview, IntentionPreviewInfo.Html.class);
String actual = html.content().toString();
assertEquals("""
Create directory &#39;a&#39;<br/><br/><p><icon src="file"/>&nbsp;dummy.txt &rarr; \
<icon src="dir"/>&nbsp;$src$a</p>""".replace("$", File.separator), actual);
}
}

View File

@@ -295,6 +295,7 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
List<IntentionPreviewInfo.CustomDiff> customDiffList = new ArrayList<>();
IntentionPreviewInfo navigateInfo = IntentionPreviewInfo.EMPTY;
List<@NlsSafe String> createdDirs = new ArrayList<>();
List<HtmlChunk> fsActions = new ArrayList<>();
for (ModCommand command : modCommand.unpack()) {
if (command instanceof ModUpdateFileText modFile) {
VirtualFile vFile = modFile.file();
@@ -359,11 +360,13 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
}
else if (command instanceof ModMoveFile moveFile) {
FutureVirtualFile targetFile = moveFile.targetFile();
IntentionPreviewInfo.Html html;
if (targetFile.getName().equals(moveFile.file().getName())) {
navigateInfo = IntentionPreviewInfo.moveToDirectory(moveFile.file(), targetFile.getParent());
html = (IntentionPreviewInfo.Html)IntentionPreviewInfo.moveToDirectory(moveFile.file(), targetFile.getParent());
} else {
navigateInfo = IntentionPreviewInfo.rename(moveFile.file(), targetFile.getName());
html = (IntentionPreviewInfo.Html)IntentionPreviewInfo.rename(moveFile.file(), targetFile.getName());
}
fsActions.add(html.content());
}
else if (command instanceof ModUpdateSystemOptions options) {
HtmlChunk preview = createOptionsPreview(context, options);
@@ -372,17 +375,25 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
}
customDiffList.sort(Comparator.comparing(diff -> diff.fileName() != null));
if (customDiffList.isEmpty()) {
HtmlBuilder builder = new HtmlBuilder();
if (!createdDirs.isEmpty()) {
if (createdDirs.size() == 1) {
return new IntentionPreviewInfo.Html(text(AnalysisBundle.message("preview.create.directory", createdDirs.get(0))));
builder.append(AnalysisBundle.message("preview.create.directory", createdDirs.get(0))).br();
} else {
builder.append(tag("p").addText(AnalysisBundle.message("preview.create.directories")).children(
ContainerUtil.map(createdDirs, text -> new HtmlBuilder().br()
.appendRaw("&bull; ") //NON-NLS
.append(text)
.toFragment()))
);
}
return new IntentionPreviewInfo.Html(
tag("p").addText(AnalysisBundle.message("preview.create.directories")).children(
ContainerUtil.map(createdDirs, text -> new HtmlBuilder().br()
.appendRaw("&bull; ") //NON-NLS
.append(text)
.toFragment()))
);
}
if (!fsActions.isEmpty()) {
if (!builder.isEmpty()) builder.br();
fsActions.forEach(builder::append);
}
if (!builder.isEmpty()) {
return new IntentionPreviewInfo.Html(builder.toFragment());
}
return navigateInfo;
}