Skip to content

Commit 797708a

Browse files
springcompMaxime LABELLE
authored and
Maxime LABELLE
committed
Refactored key chords handling
1 parent e06e5bc commit 797708a

13 files changed

+788
-738
lines changed

PSReadLine/Completion.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,8 @@ private void MenuCompleteImpl(Menu menu, CommandCompletion completions)
10941094
prependNextKey = true;
10951095

10961096
// without this branch experience doesn't look naturally
1097-
if (_dispatchTable.TryGetValue(nextKey, out var handler) &&
1097+
if (_dispatchTable.TryGetValue(nextKey, out var handlerOrChordDispatchTable) &&
1098+
handlerOrChordDispatchTable.TryGetKeyHandler(out var handler) &&
10981099
(
10991100
handler.Action == CopyOrCancelLine ||
11001101
handler.Action == Cut ||

PSReadLine/ConsoleKeyChordConverter.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ public static ConsoleKeyInfo[] Convert(string chord)
3636
throw new ArgumentNullException(nameof(chord));
3737
}
3838

39-
int start = 0;
40-
var first = GetOneKey(chord, ref start);
39+
var keyChord = new List<ConsoleKeyInfo>(2);
4140

42-
if (start >= chord.Length)
43-
return new [] { first };
41+
int index = 0;
4442

45-
if (chord[start++] != ',')
46-
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
43+
while (true)
44+
{
45+
keyChord.Add(GetOneKey(chord, ref index));
46+
47+
if (index >= chord.Length)
48+
break;
4749

48-
var second = GetOneKey(chord, ref start);
49-
if (start < chord.Length)
50-
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
50+
if (chord[index++] != ',')
51+
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
52+
}
5153

52-
return new [] { first, second };
54+
return keyChord.ToArray();
5355
}
5456

5557
private static ConsoleKeyInfo GetOneKey(string chord, ref int start)

PSReadLine/History.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,9 +1171,13 @@ private void InteractiveHistorySearchLoop(int direction)
11711171
var toMatch = new StringBuilder(64);
11721172
while (true)
11731173
{
1174+
Action<ConsoleKeyInfo?, object> function = null;
1175+
11741176
var key = ReadKey();
1175-
_dispatchTable.TryGetValue(key, out var handler);
1176-
var function = handler?.Action;
1177+
_dispatchTable.TryGetValue(key, out var handlerOrChordDispatchTable);
1178+
if (handlerOrChordDispatchTable.TryGetKeyHandler(out var handler))
1179+
function = handler.Action;
1180+
11771181
if (function == ReverseSearchHistory)
11781182
{
11791183
UpdateHistoryDuringInteractiveSearch(toMatch.ToString(), -1, ref searchFromPoint);

PSReadLine/KeyBindings.cs

Lines changed: 229 additions & 68 deletions
Large diffs are not rendered by default.

PSReadLine/KeyBindings.vi.cs

Lines changed: 234 additions & 257 deletions
Large diffs are not rendered by default.

PSReadLine/Movement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ private static bool CheckIsBound(Action<ConsoleKeyInfo?, object> action)
370370
{
371371
foreach (var entry in _singleton._dispatchTable)
372372
{
373-
if (entry.Value.Action == action)
373+
if (entry.Value.TryGetKeyHandler(out var handler) && handler.Action == action)
374374
return true;
375375
}
376376
return false;

0 commit comments

Comments
 (0)