Refactor: Remove StringWithSymbol

It's replaced with a macro containing one key.
This commit is contained in:
Jules Aguillon 2025-02-28 00:17:28 +01:00
parent 77b0c30728
commit b7d1508d7b
5 changed files with 13 additions and 53 deletions

View File

@ -99,7 +99,6 @@ public final class KeyEventHandler
case Editing: handle_editing_key(key.getEditing()); break;
case Compose_pending: _recv.set_compose_pending(true); break;
case Slider: handle_slider(key.getSlider(), key.getSliderRepeat()); break;
case StringWithSymbol: send_text(key.getStringWithSymbol()); break;
case Macro: evaluate_macro(key.getMacro()); break;
}
update_meta_state(old_mods);

View File

@ -95,7 +95,6 @@ public final class KeyValue implements Comparable<KeyValue>
Modifier, Editing, Placeholder,
String, // [_payload] is also the string to output, value is unused.
Slider, // [_payload] is a [KeyValue.Slider], value is slider repeatition.
StringWithSymbol, // [_payload] is a [KeyValue.StringWithSymbol], value is unused.
Macro, // [_payload] is a [KeyValue.Macro], value is unused.
}
@ -225,12 +224,6 @@ public final class KeyValue implements Comparable<KeyValue>
return ((int)(short)(_code & VALUE_BITS));
}
/** Defined only when [getKind() == Kind.StringWithSymbol]. */
public String getStringWithSymbol()
{
return ((StringWithSymbol)_payload).str;
}
/** Defined only when [getKind() == Kind.Macro]. */
public KeyValue[] getMacro()
{
@ -250,7 +243,7 @@ public final class KeyValue implements Comparable<KeyValue>
public KeyValue withFlags(int f)
{
return new KeyValue(_payload, (_code & ~FLAGS_BITS) | (f & FLAGS_BITS));
return new KeyValue(_payload, _code, _code, f);
}
public KeyValue withSymbol(String symbol)
@ -266,7 +259,9 @@ public final class KeyValue implements Comparable<KeyValue>
case Modifier:
case Editing:
case Placeholder:
return new KeyValue(symbol, _code);
return new KeyValue(symbol, _code, _code, getFlags());
case Macro:
return makeMacro(symbol, getMacro(), 0);
default:
return makeMacro(symbol, new KeyValue[]{ this }, 0);
}
@ -313,17 +308,12 @@ public final class KeyValue implements Comparable<KeyValue>
return "[KeyValue " + getKind().toString() + "+" + getFlags() + "+" + value + " \"" + getString() + "\"]";
}
private KeyValue(Comparable p, int code)
private KeyValue(Comparable p, int kind, int value, int flags)
{
if (p == null)
throw new NullPointerException("KeyValue payload cannot be null");
_payload = p;
_code = code;
}
private KeyValue(Comparable p, int kind, int value, int flags)
{
this(p, (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS));
_code = (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS);
}
public KeyValue(Comparable p, Kind k, int v, int f)
@ -485,12 +475,6 @@ public final class KeyValue implements Comparable<KeyValue>
return new KeyValue(str, Kind.String, 0, flags | FLAG_SMALLER_FONT);
}
public static KeyValue makeStringKeyWithSymbol(String str, String symbol, int flags)
{
return new KeyValue(new StringWithSymbol(str, symbol),
Kind.StringWithSymbol, 0, flags);
}
public static KeyValue makeMacro(String symbol, KeyValue[] keys, int flags)
{
return new KeyValue(new Macro(keys, symbol), Kind.Macro, 0, flags);
@ -781,29 +765,6 @@ public final class KeyValue implements Comparable<KeyValue>
throw new RuntimeException("Assertion failure");
}
public static final class StringWithSymbol implements Comparable<StringWithSymbol>
{
public final String str;
final String _symbol;
public StringWithSymbol(String _str, String _sym)
{
str = _str;
_symbol = _sym;
}
@Override
public String toString() { return _symbol; }
@Override
public int compareTo(StringWithSymbol snd)
{
int d = str.compareTo(snd.str);
if (d != 0) return d;
return _symbol.compareTo(snd._symbol);
}
};
public static enum Slider
{
Cursor_left(0xE008),

View File

@ -184,7 +184,7 @@ public final class KeyValueParser
payload = parseSingleQuotedString(m);
if (symbol == null)
return KeyValue.makeStringKey(payload, flags);
return KeyValue.makeStringKeyWithSymbol(payload, symbol, flags);
return KeyValue.makeStringKey(payload, flags).withSymbol(symbol);
case "char":
payload = parsePayloadWord(m);

View File

@ -26,9 +26,6 @@ public class KeyValueParserTest
@Test
public void parse_macro() throws Exception
{
Utils.parse("symbol:abc", KeyValue.makeMacro("symbol", new KeyValue[]{
KeyValue.makeStringKey("abc")
}, 0));
Utils.parse("copy:ctrl,a,ctrl,c", KeyValue.makeMacro("copy", new KeyValue[]{
KeyValue.getSpecialKeyByName("ctrl"),
KeyValue.makeStringKey("a"),
@ -59,6 +56,7 @@ public class KeyValueParserTest
public void parse_non_macro() throws Exception
{
Utils.parse("a:b", KeyValue.makeCharKey('b', "a", 0));
Utils.parse("symbol:abc", KeyValue.makeStringKey("abc").withSymbol("symbol"));
}
@Test
@ -94,6 +92,7 @@ public class KeyValueParserTest
@Test
public void parse_key_event() throws Exception
{
Utils.parse("a:keyevent:85", KeyValue.keyeventKey("a", 85, 0));
Utils.parse("symbol:keyevent:85", KeyValue.keyeventKey("symbol", 85, 0));
Utils.parse("macro:keyevent:85,abc", KeyValue.makeMacro("macro", new KeyValue[]{
KeyValue.keyeventKey("", 85, 0),
@ -112,8 +111,8 @@ public class KeyValueParserTest
{
Utils.parse(":str:'Foo'", KeyValue.makeStringKey("Foo"));
Utils.parse(":str flags='dim':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY));
Utils.parse(":str symbol='Symbol':'Foo'", KeyValue.makeStringKeyWithSymbol("Foo", "Symbol", 0));
Utils.parse(":str symbol='Symbol' flags='dim':'Foo'", KeyValue.makeStringKeyWithSymbol("Foo", "Symbol", KeyValue.FLAG_SECONDARY));
Utils.parse(":str symbol='Symbol':'Foo'", KeyValue.makeStringKey("Foo").withSymbol("Symbol"));
Utils.parse(":str symbol='Symbol' flags='dim':'f'", KeyValue.makeStringKey("f").withSymbol("Symbol").withFlags(KeyValue.FLAG_SECONDARY));
Utils.parse(":str flags='dim,small':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY | KeyValue.FLAG_SMALLER_FONT));
Utils.parse(":str flags=',,':'Foo'", KeyValue.makeStringKey("Foo")); // Unintentional
Utils.expect_error(":unknown:Foo"); // Unknown kind

View File

@ -11,7 +11,8 @@ public class KeyValueTest
@Test
public void equals()
{
assertEquals(KeyValue.makeStringKeyWithSymbol("Foo", "Symbol", 0), KeyValue.makeStringKeyWithSymbol("Foo", "Symbol", 0));
assertEquals(KeyValue.makeStringKey("Foo").withSymbol("Symbol"),
KeyValue.makeMacro("Symbol", new KeyValue[] { KeyValue.makeStringKey("Foo") }, 0));
}
@Test