Skip to content

Commit 531e3bf

Browse files
committed
Add #[doc(alias = ...)] attributes
For certain keys and codes where it makes sense, including those we already have alternatives for when parsing.
1 parent b82feeb commit 531e3bf

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

convert.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -46,49 +46,60 @@ def parse(text):
4646

4747
comment = re.sub(r"[ \t][ \t]+", "\n", typical_usage.decode_contents())
4848

49-
display.append([name, comment, deprecated, []])
49+
display.append([name, comment, deprecated, [], []])
5050
return display
5151

5252

5353
def emit_enum_entries(display, file):
54-
for [key, doc_comment, deprecated, alternatives] in display:
54+
for [key, doc_comment, deprecated, alternatives, aliases] in display:
5555
for line in doc_comment.split('\n'):
5656
line = line.strip()
5757
if len(line) == 0:
5858
continue
5959
print(f" /// {line}", file=file)
6060
if deprecated:
6161
print(" #[deprecated = \"marked as legacy in the spec, use Meta instead\"]", file=file)
62+
for alias in aliases:
63+
print(f" #[doc(alias = \"{alias}\")]", file=file)
6264
print(f" {key},", file=file)
6365

6466

6567
def print_display_entries(display, file):
66-
for [key, doc_comment, deprecated, alternatives] in display:
68+
for [key, doc_comment, deprecated, alternatives, aliases] in display:
6769
print(" {0} => f.write_str(\"{0}\"),".format(
6870
key), file=file)
6971

7072

7173
def print_from_str_entries(display, file):
72-
for [key, doc_comment, deprecated, alternatives] in display:
74+
for [key, doc_comment, deprecated, alternatives, aliases] in display:
7375
print(" \"{0}\"".format(key), file=file, end='')
7476
for alternative in alternatives:
7577
print(" | \"{0}\"".format(alternative), file=file, end='')
7678
print(" => Ok({0}),".format(key), file=file)
7779

7880

7981
def add_comment_to(display, key, comment):
80-
for (i, [found_key, doc_comment, deprecated, alternatives]) in enumerate(display):
82+
for (i, [found_key, doc_comment, deprecated, alternatives, aliases]) in enumerate(display):
8183
if found_key != key:
8284
continue
8385
doc_comment = doc_comment + "\n" + comment
84-
display[i] = [found_key, doc_comment, deprecated, alternatives]
86+
display[i] = [found_key, doc_comment, deprecated, alternatives, aliases]
87+
88+
89+
def add_alias_for(display, key, alias):
90+
for [found_key, doc_comment, deprecated, alternatives, aliases] in display:
91+
if found_key != key:
92+
continue
93+
aliases.append(alias)
8594

8695

8796
def add_alternative_for(display, key, alternative):
88-
for [found_key, doc_comment, deprecated, alternatives] in display:
97+
for [found_key, doc_comment, deprecated, alternatives, aliases] in display:
8998
if found_key != key:
9099
continue
91100
alternatives.append(alternative)
101+
# Alternatives are also listed as aliases
102+
aliases.append(alternative)
92103

93104

94105
def convert_key(text, file):
@@ -117,9 +128,13 @@ def convert_key(text, file):
117128
'F{}'.format(i),
118129
'The F{0} key, a general purpose function key, as index {0}.'.format(i),
119130
False,
120-
[]
131+
[],
132+
[],
121133
])
122134

135+
add_alias_for(display, 'Meta', 'Super')
136+
add_alias_for(display, 'Enter', 'Return')
137+
123138
emit_enum_entries(display, file)
124139
print("}", file=file)
125140

@@ -191,7 +206,8 @@ def convert_code(text, file):
191206
'F{}'.format(i),
192207
'<kbd>F{}</kbd>'.format(i),
193208
False,
194-
[]
209+
[],
210+
[],
195211
])
196212

197213
chromium_key_codes = [
@@ -223,12 +239,20 @@ def convert_code(text, file):
223239
chromium_only,
224240
'Non-standard code value supported by Chromium.',
225241
False,
226-
[]
242+
[],
243+
[],
227244
])
228245

229246
add_comment_to(display, 'Backquote', 'This is also called a backtick or grave.')
230247
add_comment_to(display, 'Quote', 'This is also called an apostrophe.')
231248

249+
add_alias_for(display, 'Backquote', 'Backtick')
250+
add_alias_for(display, 'Backquote', 'Grave')
251+
add_alias_for(display, 'Quote', 'Apostrophe')
252+
add_alias_for(display, 'MetaLeft', 'SuperLeft')
253+
add_alias_for(display, 'MetaRight', 'SuperRight')
254+
add_alias_for(display, 'Enter', 'Return')
255+
232256
add_alternative_for(display, 'MetaLeft', 'OSLeft')
233257
add_alternative_for(display, 'MetaRight', 'OSRight')
234258
add_alternative_for(display, 'AudioVolumeDown', 'VolumeDown')

src/code.rs

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use std::error::Error;
2222
pub enum Code {
2323
/// <kbd>`~</kbd> on a US keyboard. This is the <kbd>半角/全角/漢字</kbd> (<span class="unicode">hankaku/zenkaku/kanji</span>) key on Japanese keyboards
2424
/// This is also called a backtick or grave.
25+
#[doc(alias = "Backtick")]
26+
#[doc(alias = "Grave")]
2527
Backquote,
2628
/// Used for both the US <kbd>\|</kbd> (on the 101-key layout) and also for the key
2729
/// located between the <kbd>"</kbd> and <kbd>Enter</kbd> keys on row C of the 102-,
@@ -130,6 +132,7 @@ pub enum Code {
130132
Period,
131133
/// <kbd>'"</kbd> on a US keyboard.
132134
/// This is also called an apostrophe.
135+
#[doc(alias = "Apostrophe")]
133136
Quote,
134137
/// <kbd>;:</kbd> on a US keyboard.
135138
Semicolon,
@@ -152,10 +155,15 @@ pub enum Code {
152155
/// <kbd>Control</kbd> or <kbd>⌃</kbd>
153156
ControlRight,
154157
/// <kbd>Enter</kbd> or <kbd>↵</kbd>. Labelled <kbd>Return</kbd> on Apple keyboards.
158+
#[doc(alias = "Return")]
155159
Enter,
156160
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd> or other OS symbol key.
161+
#[doc(alias = "SuperLeft")]
162+
#[doc(alias = "OSLeft")]
157163
MetaLeft,
158164
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd> or other OS symbol key.
165+
#[doc(alias = "SuperRight")]
166+
#[doc(alias = "OSRight")]
159167
MetaRight,
160168
/// <kbd>Shift</kbd> or <kbd>⇧</kbd>
161169
ShiftLeft,
@@ -310,6 +318,7 @@ pub enum Code {
310318
LaunchApp2,
311319
LaunchMail,
312320
MediaPlayPause,
321+
#[doc(alias = "LaunchMediaPlayer")]
313322
MediaSelect,
314323
MediaStop,
315324
MediaTrackNext,
@@ -318,8 +327,11 @@ pub enum Code {
318327
/// replacing the <kbd>Eject</kbd> key.
319328
Power,
320329
Sleep,
330+
#[doc(alias = "VolumeDown")]
321331
AudioVolumeDown,
332+
#[doc(alias = "VolumeMute")]
322333
AudioVolumeMute,
334+
#[doc(alias = "VolumeUp")]
323335
AudioVolumeUp,
324336
WakeUp,
325337
#[deprecated = "marked as legacy in the spec, use Meta instead"]

src/named_key.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub enum NamedKey {
4040
FnLock,
4141
/// The <kbd>Meta</kbd> key, to enable meta modifier function for interpreting concurrent or subsequent keyboard input.
4242
/// This key value is used for the <q>Windows Logo</q> key and the Apple <kbd>Command</kbd> or <kbd>⌘</kbd> key.
43+
#[doc(alias = "Super")]
4344
Meta,
4445
/// The <kbd>NumLock</kbd> or Number Lock key, to toggle numpad mode function for interpreting subsequent keyboard input.
4546
NumLock,
@@ -58,6 +59,7 @@ pub enum NamedKey {
5859
#[deprecated = "marked as legacy in the spec, use Meta instead"]
5960
Super,
6061
/// The <kbd>Enter</kbd> or <kbd>↵</kbd> key, to activate current selection or accept current input.<br/> This key value is also used for the <kbd>Return</kbd> (Macintosh numpad) key.<br/> This key value is also used for the Android <code class="android">KEYCODE_DPAD_CENTER</code>.
62+
#[doc(alias = "Return")]
6163
Enter,
6264
/// The Horizontal Tabulation <kbd>Tab</kbd> key.
6365
Tab,

0 commit comments

Comments
 (0)