From 466e0b82189352ee931916b22494f638fabc9aa3 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 27 Feb 2025 23:53:42 +0100 Subject: KeyValue: Don't wrap keys into a macro when possible Many kind of KeyValues don't need to be wrapped into a Macro to show a specific symbol. This is especially useful as Macro keys are not affected by modifiers. The parser is changed to have a fast-path when a key value is not a macro. --- srcs/juloo.keyboard2/KeyValue.java | 31 ++++++++++++++++++++++++---- srcs/juloo.keyboard2/KeyValueParser.java | 10 +++++---- test/juloo.keyboard2/KeyValueParserTest.java | 11 +++++++--- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index b2ef5db..7cad9eb 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -250,7 +250,26 @@ public final class KeyValue implements Comparable public KeyValue withFlags(int f) { - return new KeyValue(_payload, (_code & KIND_BITS), (_code & VALUE_BITS), f); + return new KeyValue(_payload, (_code & ~FLAGS_BITS) | (f & FLAGS_BITS)); + } + + public KeyValue withSymbol(String symbol) + { + switch (getKind()) + { + case Char: + case Keyevent: + case Event: + case Compose_pending: + case Hangul_initial: + case Hangul_medial: + case Modifier: + case Editing: + case Placeholder: + return new KeyValue(symbol, _code); + default: + return makeMacro(symbol, new KeyValue[]{ this }, 0); + } } @Override @@ -294,13 +313,17 @@ public final class KeyValue implements Comparable return "[KeyValue " + getKind().toString() + "+" + getFlags() + "+" + value + " \"" + getString() + "\"]"; } - /** [value] is an unsigned integer. */ - private KeyValue(Comparable p, int kind, int value, int flags) + private KeyValue(Comparable p, int code) { if (p == null) throw new NullPointerException("KeyValue payload cannot be null"); _payload = p; - _code = (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS); + _code = code; + } + + private KeyValue(Comparable p, int kind, int value, int flags) + { + this(p, (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS)); } public KeyValue(Comparable p, Kind k, int v, int f) diff --git a/srcs/juloo.keyboard2/KeyValueParser.java b/srcs/juloo.keyboard2/KeyValueParser.java index 488f5d3..92d1ee5 100644 --- a/srcs/juloo.keyboard2/KeyValueParser.java +++ b/srcs/juloo.keyboard2/KeyValueParser.java @@ -41,15 +41,17 @@ public final class KeyValueParser if (symbol_ends == input_len) // String key return KeyValue.makeStringKey(input); String symbol = input.substring(0, symbol_ends); - ArrayList keydefs = new ArrayList(); init(); Matcher m = KEYDEF_TOKEN.matcher(input); m.region(symbol_ends + 1, input_len); + KeyValue first_key = parse_key_def(m); + if (!parse_comma(m)) // Input is a single key def with a specified symbol + return first_key.withSymbol(symbol); + // Input is a macro + ArrayList keydefs = new ArrayList(); + keydefs.add(first_key); do { keydefs.add(parse_key_def(m)); } while (parse_comma(m)); - for (KeyValue k : keydefs) - if (k == null) - parseError("Contains null key", m); return KeyValue.makeMacro(symbol, keydefs.toArray(new KeyValue[]{}), 0); } diff --git a/test/juloo.keyboard2/KeyValueParserTest.java b/test/juloo.keyboard2/KeyValueParserTest.java index a041e8a..7ece1a7 100644 --- a/test/juloo.keyboard2/KeyValueParserTest.java +++ b/test/juloo.keyboard2/KeyValueParserTest.java @@ -54,6 +54,13 @@ public class KeyValueParserTest Utils.expect_error("unexpected_quote:,"); } + @Test + /* Using the [symbol:..] syntax but not resulting in a macro. */ + public void parse_non_macro() throws Exception + { + Utils.parse("a:b", KeyValue.makeCharKey('b', "a", 0)); + } + @Test public void parse_string_key() throws Exception { @@ -87,9 +94,7 @@ public class KeyValueParserTest @Test public void parse_key_event() throws Exception { - Utils.parse("symbol:keyevent:85", KeyValue.makeMacro("symbol", new KeyValue[]{ - KeyValue.keyeventKey("", 85, 0) - }, 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), KeyValue.makeStringKey("abc") -- cgit v1.2.3