import com.intellij.openapi.actionSystem.impl.PresentationFactory;
import com.intellij.openapi.actionSystem.impl.Utils;
import com.intellij.openapi.util.IconLoader;
-import com.intellij.openapi.util.text.TextWithMnemonic;
import com.intellij.ui.SizedIcon;
import com.intellij.util.ObjectUtils;
import com.intellij.util.ui.EmptyIcon;
import java.util.List;
import java.util.Objects;
+import static com.intellij.openapi.actionSystem.Presentation.restoreTextWithMnemonic;
+
class ActionStepBuilder {
private final List<PopupFactoryImpl.ActionItem> myListModel;
private final DataContext myDataContext;
myCurrentNumber++;
}
else if (myHonorActionMnemonics) {
- if (text != null) {
- text = TextWithMnemonic.fromPlainText(text, (char)action.getTemplatePresentation().getMnemonic()).toString();
- }
+ text = restoreTextWithMnemonic(text, action.getTemplatePresentation().getMnemonic());
}
boolean hideIcon = Boolean.TRUE.equals(presentation.getClientProperty(MenuItemPresentationFactory.HIDE_ICON));
+++ /dev/null
-// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
-package com.intellij.util.text;
-
-import com.intellij.openapi.util.text.TextWithMnemonic;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-public class TextWithMnemonicTest {
- @Test
- public void noMnemonic() {
- TextWithMnemonic hello = TextWithMnemonic.fromPlainText("hello");
- assertEquals("hello", hello.getText());
- assertEquals("hello", hello.toString());
- assertEquals(0, hello.getMnemonic());
- assertEquals(-1, hello.getMnemonicIndex());
- }
-
- @Test
- public void mnemonicInString() {
- TextWithMnemonic hello = TextWithMnemonic.fromPlainText("hello", 'e');
- assertEquals("hello", hello.getText());
- assertEquals("h_ello", hello.toString());
- assertEquals('E', hello.getMnemonic());
- assertEquals(1, hello.getMnemonicIndex());
- }
-
- @Test
- public void parse() {
- TextWithMnemonic hello = TextWithMnemonic.parse("h&ello");
- assertEquals("hello", hello.getText());
- assertEquals("h_ello", hello.toString());
- assertEquals('E', hello.getMnemonic());
- assertEquals(1, hello.getMnemonicIndex());
- }
-
- @Test
- public void parseUnderscore() {
- TextWithMnemonic hello = TextWithMnemonic.parse("h_ello");
- assertEquals("hello", hello.getText());
- assertEquals("h_ello", hello.toString());
- assertEquals('E', hello.getMnemonic());
- assertEquals(1, hello.getMnemonicIndex());
- }
-
- @Test
- public void parseJapanese() {
- TextWithMnemonic hello = TextWithMnemonic.parse("hello(&H)");
- assertEquals("hello", hello.getText());
- assertEquals("hello(_H)", hello.toString());
- assertEquals('H', hello.getMnemonic());
- assertEquals(6, hello.getMnemonicIndex());
- }
-
- @Test
- public void append() {
- assertEquals("H_ello world!", TextWithMnemonic.parse("H&ello").append(" world!").toString());
- assertEquals("Hello _world!(H)", TextWithMnemonic.parse("Hello(&H)").append(" world!").toString());
- }
-
- @Test
- public void replaceFirst() {
- assertEquals("_Hello wonderful world!", TextWithMnemonic.parse("&Hello {0} world!").replaceFirst("{0}", "wonderful").toString());
- assertEquals("Hello wonderful _world!", TextWithMnemonic.parse("Hello {0} &world!").replaceFirst("{0}", "wonderful").toString());
- assertEquals("Hello wonderful world!(_W)", TextWithMnemonic.parse("Hello {0} world!(&W)").replaceFirst("{0}", "wonderful").toString());
- }
-}
public static final Pattern MNEMONIC = Pattern.compile(" ?\\(_?[A-Z]\\)");
@NotNull private final String myText;
- /**
- * Mnemonic index (-1 = no mnemonic)
- */
private final int myMnemonicIndex;
- /**
- * A text that should be appended to myText to display a mnemonic
- */
- private final String myMnemonicSuffix;
- private TextWithMnemonic(@NotNull String text, int mnemonicIndex, String mnemonicSuffix) {
+ private TextWithMnemonic(@NotNull String text, int mnemonicIndex) {
myText = StringUtil.internEmptyString(text);
myMnemonicIndex = mnemonicIndex;
- myMnemonicSuffix = mnemonicSuffix;
}
/**
* @return a mnemonic character (upper-cased) if mnemonic is set; 0 otherwise
*/
public int getMnemonic() {
- return hasMnemonic() ? Character.toUpperCase((myText + myMnemonicSuffix).charAt(myMnemonicIndex)) : 0;
+ return hasMnemonic() ? Character.toUpperCase(myText.charAt(myMnemonicIndex)) : 0;
}
/**
* @return a TextWithMnemonic object with mnemonic set at given index
*/
public TextWithMnemonic setMnemonicAt(int index) {
- if (index < 0 || index >= myText.length() + myMnemonicSuffix.length()) {
+ if (index < 0 || index >= myText.length()) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
- return index == myMnemonicIndex ? this : new TextWithMnemonic(myText, index, myMnemonicSuffix);
+ return index == myMnemonicIndex ? this : new TextWithMnemonic(myText, index);
}
/**
* @return TextWithMnemonic object which text is the concatenation of this object text and supplied text.
*/
public TextWithMnemonic append(@NotNull String textToAppend) {
- return new TextWithMnemonic(myText + textToAppend, myMnemonicIndex, myMnemonicSuffix);
+ return new TextWithMnemonic(myText + textToAppend, myMnemonicIndex);
}
/**
int resultIndex = myMnemonicIndex < index ? myMnemonicIndex :
myMnemonicIndex >= index + target.length() ? myMnemonicIndex - target.length() + replacement.length() :
-1;
- return new TextWithMnemonic(resultText, resultIndex, myMnemonicSuffix);
+ return new TextWithMnemonic(resultText, resultIndex);
}
/**
@NotNull
@Contract(pure = true)
public static TextWithMnemonic fromPlainText(@NotNull String text) {
- return new TextWithMnemonic(text, -1, "");
- }
-
- /**
- * Creates a TextWithMnemonic object from a plain text without mnemonic.
- * @param text a plain text to create a TextWithMnemonic object from
- * @param mnemonicChar mnemonic character
- * @return new TextWithMnemonic object which has given mnemonic character.
- * If the text doesn't contain the supplied character then mnemonicChar is appended in parentheses.
- */
- @NotNull
- @Contract(pure = true)
- public static TextWithMnemonic fromPlainText(@NotNull String text, char mnemonicChar) {
- mnemonicChar = Character.toUpperCase(mnemonicChar);
- for (int i = 0; i < text.length(); i++) {
- if (Character.toUpperCase(text.charAt(i)) == mnemonicChar) {
- return new TextWithMnemonic(text, i, "");
- }
- }
- return new TextWithMnemonic(text, text.length() + 2, "(" + mnemonicChar + ")");
+ return new TextWithMnemonic(text, -1);
}
/**
}
plainText.append(ch);
}
- String plain = plainText.toString();
- int length = plain.length();
- if (length > 3 && mnemonicIndex == length - 2 && plain.charAt(length - 1) == ')' && plain.charAt(length - 3) == '(') {
- return new TextWithMnemonic(plain.substring(0, length - 3), mnemonicIndex, plain.substring(length - 3));
- }
- return new TextWithMnemonic(plain, mnemonicIndex, "");
+ return new TextWithMnemonic(plainText.toString(), mnemonicIndex);
}
return fromPlainText(text);
}
if (o == null || getClass() != o.getClass()) return false;
TextWithMnemonic mnemonic = (TextWithMnemonic)o;
return myMnemonicIndex == mnemonic.myMnemonicIndex &&
- myText.equals(mnemonic.myText) &&
- myMnemonicSuffix.equals(mnemonic.myMnemonicSuffix);
+ myText.equals(mnemonic.myText);
}
@Override
public int hashCode() {
- return (myText.hashCode() * 31 + myMnemonicIndex) * 31 + myMnemonicSuffix.hashCode();
+ return myText.hashCode() * 31 + myMnemonicIndex;
}
/**
@Override
public String toString() {
if (myMnemonicIndex > -1) {
- String completeText = myText + myMnemonicSuffix;
- String prefix = StringUtil.escapeMnemonics(completeText.substring(0, myMnemonicIndex));
- String suffix = completeText.substring(myMnemonicIndex);
+ String prefix = StringUtil.escapeMnemonics(myText.substring(0, myMnemonicIndex));
+ String suffix = myText.substring(myMnemonicIndex);
return prefix + "_" + suffix;
}
return StringUtil.escapeMnemonics(myText);