[mod-commands] ModOpenUrl command

GitOrigin-RevId: 164a69f1d570e08a8e7af108c24e6c3f312f405e
This commit is contained in:
Tagir Valeev
2024-06-27 13:12:46 +02:00
committed by intellij-monorepo-bot
parent 86e193d79f
commit edec77df83
7 changed files with 58 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package com.intellij.modcommand;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.openapi.fileTypes.UserBinaryFileType;
import com.intellij.openapi.util.text.HtmlChunk;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
@@ -25,4 +26,15 @@ public final class ModCommandTest extends LightPlatformCodeInsightTestCase {
assertNotNull(child);
assertOrderedEquals(content, child.contentsToByteArray());
}
public void testBrowse() {
configureFromFileText("dummy.txt", "");
ActionContext context = ActionContext.from(null, getFile());
ModCommand command = ModCommand.openUrl("https://example.com");
ModCommandExecutor executor = ModCommandExecutor.getInstance();
IntentionPreviewInfo preview = executor.getPreview(command, context);
assertEquals(new IntentionPreviewInfo.Html(HtmlChunk.text("Browse \"https://example.com\"")), preview);
ModCommandExecutor.BatchExecutionResult result = executor.executeInBatch(context, command);
assertEquals(ModCommandExecutor.Result.INTERACTIVE, result);
}
}

View File

@@ -2591,6 +2591,7 @@ com.intellij.modcommand.ModCommand
- *s:moveCaretAfter(com.intellij.modcommand.ModCommand,com.intellij.psi.PsiFile,I,Z):com.intellij.modcommand.ModCommand
- s:moveTo(com.intellij.psi.PsiElement):com.intellij.modcommand.ModCommand
- s:nop():com.intellij.modcommand.ModCommand
- s:openUrl(java.lang.String):com.intellij.modcommand.ModCommand
- s:psiBasedStep(com.intellij.psi.PsiElement,java.lang.String,java.util.function.Function,java.util.function.Function):com.intellij.modcommand.ModCommandAction
- s:psiUpdate(com.intellij.modcommand.ActionContext,java.util.function.Consumer):com.intellij.modcommand.ModCommand
- s:psiUpdate(com.intellij.psi.PsiElement,java.util.function.BiConsumer):com.intellij.modcommand.ModCommand
@@ -2764,6 +2765,13 @@ f:com.intellij.modcommand.ModNothing
- f:hashCode():I
- isEmpty():Z
- unpack():java.util.List
f:com.intellij.modcommand.ModOpenUrl
- java.lang.Record
- com.intellij.modcommand.ModCommand
- <init>(java.lang.String):V
- f:equals(java.lang.Object):Z
- f:hashCode():I
- url():java.lang.String
*:com.intellij.modcommand.ModPsiNavigator
- a:getCaretOffset():I
- a:moveCaretTo(I):V

View File

@@ -239,6 +239,7 @@ progress.title.submit.request=Submit
progress.title.refresh=Refresh
preview.cannot.perform.action=Cannot perform action:
preview.copy.to.clipboard=Copy to clipboard the string "{0}"
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
modcommand.result.action.has.no.effect=Action has no effect

View File

@@ -34,8 +34,8 @@ import java.util.function.Function;
*/
public sealed interface ModCommand
permits ModChooseAction, ModChooseMember, ModCompositeCommand, ModCopyToClipboard, ModCreateFile, ModDeleteFile, ModDisplayMessage,
ModHighlight, ModNavigate, ModNothing, ModStartRename, ModShowConflicts, ModStartTemplate, ModUpdateReferences,
ModUpdateFileText, ModUpdateSystemOptions {
ModHighlight, ModNavigate, ModNothing, ModOpenUrl, ModShowConflicts, ModStartRename, ModStartTemplate, ModUpdateFileText,
ModUpdateReferences, ModUpdateSystemOptions {
/**
* @return true if the command does nothing
@@ -85,6 +85,14 @@ public sealed interface ModCommand
return new ModCopyToClipboard(content);
}
/**
* @param url the URL to open
* @return a ModCommand instance representing the action of opening the URL
*/
static @NotNull ModCommand openUrl(@NotNull String url) {
return new ModOpenUrl(url);
}
/**
* @param message error message to display
* @return a command that displays the specified error message in the editor

View File

@@ -0,0 +1,12 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.modcommand;
import org.jetbrains.annotations.NotNull;
/**
* An interactive command that allows to open an URL in the browser.
*
* @param url URL to open
*/
public record ModOpenUrl(@NotNull String url) implements ModCommand {
}

View File

@@ -107,7 +107,7 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
if (command instanceof ModNavigate || command instanceof ModHighlight ||
command instanceof ModCopyToClipboard || command instanceof ModStartRename ||
command instanceof ModStartTemplate || command instanceof ModUpdateSystemOptions ||
command instanceof ModUpdateReferences) {
command instanceof ModUpdateReferences || command instanceof ModOpenUrl) {
return Result.INTERACTIVE;
}
if (command instanceof ModShowConflicts) {
@@ -290,9 +290,13 @@ public class ModCommandBatchExecutorImpl implements ModCommandExecutor {
}
}
else if (command instanceof ModCopyToClipboard copy) {
navigateInfo = new IntentionPreviewInfo.Html(HtmlChunk.text(
navigateInfo = new IntentionPreviewInfo.Html(text(
AnalysisBundle.message("preview.copy.to.clipboard", StringUtil.shortenTextWithEllipsis(copy.content(), 50, 10))));
}
else if (command instanceof ModOpenUrl openUrl) {
navigateInfo = new IntentionPreviewInfo.Html(text(
AnalysisBundle.message("preview.open.url", StringUtil.shortenTextWithEllipsis(openUrl.url(), 50, 10))));
}
else if (command instanceof ModUpdateSystemOptions options) {
HtmlChunk preview = createOptionsPreview(context, options);
navigateInfo = preview.isEmpty() ? IntentionPreviewInfo.EMPTY : new IntentionPreviewInfo.Html(preview);

View File

@@ -18,6 +18,7 @@ import com.intellij.codeInspection.options.OptionControllerProvider;
import com.intellij.diff.comparison.ComparisonManager;
import com.intellij.diff.comparison.ComparisonPolicy;
import com.intellij.diff.fragments.DiffFragment;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.DataManager;
import com.intellij.ide.util.MemberChooser;
import com.intellij.injected.editor.EditorWindow;
@@ -116,6 +117,9 @@ public class ModCommandExecutorImpl extends ModCommandBatchExecutorImpl {
if (command instanceof ModCopyToClipboard copyToClipboard) {
return executeCopyToClipboard(copyToClipboard);
}
if (command instanceof ModOpenUrl openUrl) {
return executeOpenUrl(project, openUrl);
}
if (command instanceof ModNothing) {
return true;
}
@@ -318,6 +322,11 @@ public class ModCommandExecutorImpl extends ModCommandBatchExecutorImpl {
return true;
}
private static boolean executeOpenUrl(@NotNull Project project, @NotNull ModOpenUrl openUrl) {
BrowserUtil.browse(openUrl.url(), project);
return true;
}
private static boolean executeRename(@NotNull Project project, @NotNull ModStartRename rename, @Nullable Editor editor) {
VirtualFile file = actualize(rename.file());
if (file == null) return false;