Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Key Chords Handling #3853

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion PSReadLine/Completion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,8 @@ private void MenuCompleteImpl(Menu menu, CommandCompletion completions)
prependNextKey = true;

// without this branch experience doesn't look naturally
if (_dispatchTable.TryGetValue(nextKey, out var handler) &&
if (_dispatchTable.TryGetValue(nextKey, out var handlerOrChordDispatchTable) &&
handlerOrChordDispatchTable.TryGetKeyHandler(out var handler) &&
(
handler.Action == CopyOrCancelLine ||
handler.Action == Cut ||
Expand Down
22 changes: 12 additions & 10 deletions PSReadLine/ConsoleKeyChordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ public static ConsoleKeyInfo[] Convert(string chord)
throw new ArgumentNullException(nameof(chord));
}

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

if (start >= chord.Length)
return new [] { first };
int index = 0;

if (chord[start++] != ',')
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
while (true)
{
keyChord.Add(GetOneKey(chord, ref index));

if (index >= chord.Length)
break;

var second = GetOneKey(chord, ref start);
if (start < chord.Length)
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
if (chord[index++] != ',')
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
}

return new [] { first, second };
return keyChord.ToArray();
}

private static ConsoleKeyInfo GetOneKey(string chord, ref int start)
Expand Down
8 changes: 6 additions & 2 deletions PSReadLine/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,9 +1171,13 @@ private void InteractiveHistorySearchLoop(int direction)
var toMatch = new StringBuilder(64);
while (true)
{
Action<ConsoleKeyInfo?, object> function = null;

var key = ReadKey();
_dispatchTable.TryGetValue(key, out var handler);
var function = handler?.Action;
_dispatchTable.TryGetValue(key, out var handlerOrChordDispatchTable);
if (handlerOrChordDispatchTable.TryGetKeyHandler(out var handler))
function = handler.Action;

if (function == ReverseSearchHistory)
{
UpdateHistoryDuringInteractiveSearch(toMatch.ToString(), -1, ref searchFromPoint);
Expand Down
299 changes: 231 additions & 68 deletions PSReadLine/KeyBindings.cs

Large diffs are not rendered by default.

491 changes: 234 additions & 257 deletions PSReadLine/KeyBindings.vi.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion PSReadLine/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private static bool CheckIsBound(Action<ConsoleKeyInfo?, object> action)
{
foreach (var entry in _singleton._dispatchTable)
{
if (entry.Value.Action == action)
if (entry.Value.TryGetKeyHandler(out var handler) && handler.Action == action)
return true;
}
return false;
Expand Down
Loading