[Keyboard, Web] Map from "Esc" to the Escape key (#106133)

* Impl

* Fix build

* Add test
This commit is contained in:
Tong Mu
2022-06-21 13:27:12 -07:00
committed by GitHub
parent df55dbb9d9
commit 0ac05f1c5c
13 changed files with 125 additions and 51 deletions

View File

@@ -233,12 +233,24 @@ void addNameValue(List<String> names, List<int> values, String name, int value)
}
}
enum DeduplicateBehavior {
// Warn the duplicate entry.
kWarn,
// Skip the latter duplicate entry.
kSkip,
// Keep all duplicate entries.
kKeep,
}
/// The information for a line used by [OutputLines].
class OutputLine<T extends Comparable<Object>> {
const OutputLine(this.key, this.value);
OutputLine(this.key, String value)
: values = <String>[value];
final T key;
final String value;
final List<String> values;
}
/// A utility class to build join a number of lines in a sorted order.
@@ -247,41 +259,43 @@ class OutputLine<T extends Comparable<Object>> {
/// get the joined string of these lines joined sorting them in the order of the
/// index.
class OutputLines<T extends Comparable<Object>> {
OutputLines(this.mapName, {this.checkDuplicate = true});
OutputLines(this.mapName, {this.behavior = DeduplicateBehavior.kWarn});
/// If true, then lines with duplicate keys will be warned and discarded.
///
/// Default to true.
final bool checkDuplicate;
/// What to do if there are entries with the same key.
final DeduplicateBehavior behavior;
/// The name for this map.
///
/// Used in warning messages.
final String mapName;
final Set<T> keys = <T>{};
final List<OutputLine<T>> lines = <OutputLine<T>>[];
final Map<T, OutputLine<T>> lines = <T, OutputLine<T>>{};
void add(T code, String line) {
if (checkDuplicate) {
if (keys.contains(code)) {
final OutputLine<T> existing = lines.firstWhere((OutputLine<T> line) => line.key == code);
print('Warn: $mapName is requested to add line $code as:\n $line\n but it already exists as:\n ${existing.value}');
return;
void add(T key, String line) {
final OutputLine<T>? existing = lines[key];
if (existing != null) {
switch (behavior) {
case DeduplicateBehavior.kWarn:
print('Warn: Request to add $key to map "$mapName" as:\n $line\n but it already exists as:\n ${existing.values[0]}');
return;
case DeduplicateBehavior.kSkip:
return;
case DeduplicateBehavior.kKeep:
existing.values.add(line);
return;
}
keys.add(code);
}
lines.add(OutputLine<T>(code, line));
lines[key] = OutputLine<T>(key, line);
}
String join() {
return lines.map((OutputLine<T> line) => line.value).join('\n');
return lines.values.map((OutputLine<T> line) => line.values.join('\n')).join('\n');
}
String sortedJoin() {
return (lines.sublist(0)
return (lines.values.toList()
..sort((OutputLine<T> a, OutputLine<T> b) => a.key.compareTo(b.key)))
.map((OutputLine<T> line) => line.value)
.map((OutputLine<T> line) => line.values.join('\n'))
.join('\n');
}
}