mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 09:03:31 +07:00
IDEA-89869 $Prompt$ macro: add a label / text to give the user a hint what to enter in the prompt
GitOrigin-RevId: 9384839ecbddd7321f7f4b6b851a7edf9b61c30f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
3df2d7ec61
commit
cbf62dd988
@@ -21,7 +21,14 @@ class MacroExpandingTest: LightPlatformTestCase() {
|
||||
assertEquals("", expand("\$WithParams()\$"))
|
||||
}
|
||||
|
||||
private fun expand(input: String) = MacroManager.expandMacros(input, listOf(regular, withParams)) {
|
||||
fun testPrompt() {
|
||||
assertEquals("\$Prompt", expand("\$Prompt"))
|
||||
assertEquals("Prompt null null", expand("\$Prompt\$"))
|
||||
assertEquals("Prompt Title null", expand("\$Prompt:Title\$"))
|
||||
assertEquals("Prompt Title value", expand("\$Prompt:Title:value\$"))
|
||||
}
|
||||
|
||||
private fun expand(input: String) = MacroManager.expandMacros(input, listOf(regular, withParams, prompt)) {
|
||||
macro, occurence -> macro.expandOccurence(DataContext.EMPTY_CONTEXT, occurence)
|
||||
}
|
||||
|
||||
@@ -37,4 +44,11 @@ class MacroExpandingTest: LightPlatformTestCase() {
|
||||
override fun expand(dataContext: DataContext) = "foo"
|
||||
override fun expand(dataContext: DataContext, vararg args: String?) = args.firstOrNull()
|
||||
}
|
||||
|
||||
private val prompt: Macro = object : PromptingMacro() {
|
||||
override fun getName() = "Prompt"
|
||||
override fun getDescription() = ""
|
||||
override fun expand(dataContext: DataContext, vararg args: String?) = args.firstOrNull()
|
||||
override fun promptUser(dataContext: DataContext, label: String?, defaultValue: String?) = "Prompt $label $defaultValue"
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
||||
public class FilePromptMacro extends PromptingMacro implements SecondQueueExpandMacro, PathMacro {
|
||||
@@ -27,9 +29,13 @@ public class FilePromptMacro extends PromptingMacro implements SecondQueueExpand
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String promptUser(DataContext dataContext) {
|
||||
protected String promptUser(@NotNull DataContext dataContext,
|
||||
@Nls @Nullable String label, @Nullable String defaultValue) {
|
||||
Project project = CommonDataKeys.PROJECT.getData(dataContext);
|
||||
final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor();
|
||||
if (label != null) {
|
||||
descriptor.setTitle(label);
|
||||
}
|
||||
final VirtualFile[] result = IdeUiService.getInstance().chooseFiles(descriptor, project, null);
|
||||
return result.length == 1? FileUtil.toSystemDependentName(result[0].getPath()) : null;
|
||||
}
|
||||
|
||||
@@ -78,14 +78,19 @@ public abstract class Macro {
|
||||
|
||||
public @Nullable TextRange findOccurence(@NotNull CharSequence s, int offset) {
|
||||
String prefix = "$" + getName();
|
||||
int i = Strings.indexOf(s, prefix, offset);
|
||||
int next = i + prefix.length();
|
||||
if (i < 0 || next >= s.length()) return null;
|
||||
int start = Strings.indexOf(s, prefix, offset);
|
||||
int next = start + prefix.length();
|
||||
if (start < 0 || next >= s.length()) return null;
|
||||
return getRangeForSuffix(s, start, next);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected TextRange getRangeForSuffix(@NotNull CharSequence s, int start, int next) {
|
||||
return switch (s.charAt(next)) {
|
||||
case '$' -> TextRange.create(i, next + 1);
|
||||
case '$' -> TextRange.create(start, next + 1);
|
||||
case '(' -> {
|
||||
int end = Strings.indexOf(s, ")$", next);
|
||||
yield end < 0 ? null : TextRange.create(i, end + 2);
|
||||
yield end < 0 ? null : TextRange.create(start, end + 2);
|
||||
}
|
||||
default -> null;
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.intellij.ide.macro;
|
||||
import com.intellij.ide.IdeCoreBundle;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.ui.messages.MessagesService;
|
||||
import com.intellij.openapi.util.NlsContexts;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -38,12 +39,10 @@ public final class PromptMacro extends PromptingMacro implements SecondQueueExpa
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected String promptUser(DataContext dataContext) {
|
||||
return promptUser();
|
||||
}
|
||||
|
||||
public String promptUser() {
|
||||
return MessagesService.getInstance().showInputDialog(null, null, IdeCoreBundle.message("prompt.enter.parameters"), IdeCoreBundle.message("title.input"),
|
||||
UIUtil.getQuestionIcon(), null, null, null, null);
|
||||
protected String promptUser(@NotNull DataContext dataContext,
|
||||
@NlsContexts.DialogMessage @Nullable String label, @Nullable String defaultValue) {
|
||||
String message = label == null ? IdeCoreBundle.message("prompt.enter.parameters") : label + ":";
|
||||
return MessagesService.getInstance().showInputDialog(null, null, message, IdeCoreBundle.message("title.input"),
|
||||
UIUtil.getQuestionIcon(), defaultValue, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ package com.intellij.ide.macro;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.util.text.Strings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -14,15 +17,39 @@ import org.jetbrains.annotations.Nullable;
|
||||
public abstract class PromptingMacro extends Macro {
|
||||
|
||||
@Override
|
||||
public final String expand(@NotNull DataContext dataContext) throws ExecutionCancelledException {
|
||||
protected @Nullable TextRange getRangeForSuffix(@NotNull CharSequence s, int start, int next) {
|
||||
return switch (s.charAt(next)) {
|
||||
case '$' -> TextRange.create(start, next + 1);
|
||||
case ':' -> {
|
||||
int end = Strings.indexOf(s, '$', next);
|
||||
yield end < 0 ? null : TextRange.create(start, end + 1);
|
||||
}
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String expandOccurence(@NotNull DataContext context, @NotNull String occurence) throws ExecutionCancelledException {
|
||||
String[] strings = StringUtil.trimEnd(occurence, "$").split(":");
|
||||
String label = strings.length > 1 ? strings[1] : null;
|
||||
String defaultValue = strings.length > 2 ? strings[2] : null;
|
||||
return expand(context, label, defaultValue);
|
||||
}
|
||||
|
||||
private String expand(@NotNull DataContext dataContext, @Nullable String label, @Nullable String defaultValue) throws ExecutionCancelledException {
|
||||
Ref<String> userInput = Ref.create();
|
||||
ApplicationManager.getApplication().invokeAndWait(() -> userInput.set(promptUser(dataContext)));
|
||||
ApplicationManager.getApplication().invokeAndWait(() -> userInput.set(promptUser(dataContext, label, defaultValue)));
|
||||
if (userInput.isNull()) {
|
||||
throw new ExecutionCancelledException();
|
||||
}
|
||||
return userInput.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String expand(@NotNull DataContext dataContext) throws ExecutionCancelledException {
|
||||
return expand(dataContext, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String preview(@NotNull DataContext dataContext) {
|
||||
return "<params>";
|
||||
@@ -35,5 +62,5 @@ public abstract class PromptingMacro extends Macro {
|
||||
* If {@code null} is returned, {@code ExecutionCancelledException} is thrown by the {@link #expand} method.
|
||||
*/
|
||||
@Nullable
|
||||
protected abstract String promptUser(DataContext dataContext);
|
||||
protected abstract String promptUser(@NotNull DataContext dataContext, @Nullable String label, @Nullable String defaultValue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user