Skip to content
New issue

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

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

Already on GitHub? # to your account

Add backwards AutoComplete #12

Merged
merged 3 commits into from
Feb 19, 2017
Merged
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
40 changes: 36 additions & 4 deletions src/ReadLine/KeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void MoveCursorHome()

private string BuildKeyInput()
{
return _keyInfo.Modifiers != ConsoleModifiers.Control ?
return (_keyInfo.Modifiers != ConsoleModifiers.Control && _keyInfo.Modifiers != ConsoleModifiers.Shift) ?
_keyInfo.Key.ToString() : _keyInfo.Modifiers.ToString() + _keyInfo.Key.ToString();
}

Expand Down Expand Up @@ -133,16 +133,40 @@ private void Backspace()
}
}

private void AutoComplete()
private void StartAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();

_completionsIndex = 0;

WriteString(_completions[_completionsIndex]);
}

private void NextAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();

_completionsIndex++;

if (_completionsIndex == _completions.Length)
_completionsIndex = 0;

WriteString(_completions[_completionsIndex]);
}

private void PreviousAutoComplete()
{
while (_cursorPos > _completionStart)
Backspace();

_completionsIndex--;

if (_completionsIndex == -1)
_completionsIndex = _completions.Length - 1;

WriteString(_completions[_completionsIndex]);
}

private void PrevHistory()
Expand Down Expand Up @@ -226,7 +250,7 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
{
if (IsInAutoCompleteMode())
{
AutoComplete();
NextAutoComplete();
}
else
{
Expand All @@ -243,7 +267,15 @@ public KeyHandler(IConsole console, List<string> history, Func<string, int, stri
if (_completions == null || _completions.Length == 0)
return;

AutoComplete();
StartAutoComplete();
}
};

_keyActions["ShiftTab"] = () =>
{
if (IsInAutoCompleteMode())
{
PreviousAutoComplete();
}
};
}
Expand Down
33 changes: 33 additions & 0 deletions test/ReadLine.Tests/KeyHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,38 @@ public void TestTab()
Assert.Equal($"Hi {_completions[i]}", _keyHandler.Text);
}
}

[Fact]
public void TestBackwardsTab()
{
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false);
_keyHandler.Handle(_keyInfo);

// Nothing happens when no auto complete handler is set
Assert.Equal("Hello", _keyHandler.Text);

_keyHandler = new KeyHandler(new Console2(), _history, (t, s) => _completions);

_keyInfo = new ConsoleKeyInfo('H', ConsoleKey.H, false, false, false);
_keyHandler.Handle(_keyInfo);

_keyInfo = new ConsoleKeyInfo('i', ConsoleKey.I, false, false, false);
_keyHandler.Handle(_keyInfo);

_keyInfo = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false);
_keyHandler.Handle(_keyInfo);

// Bring up the first Autocomplete
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, false, false, false);
_keyHandler.Handle(_keyInfo);

for (int i = _completions.Length - 1; i >= 0; i--)
{
_keyInfo = new ConsoleKeyInfo('\0', ConsoleKey.Tab, true, false, false);
_keyHandler.Handle(_keyInfo);

Assert.Equal($"Hi {_completions[i]}", _keyHandler.Text);
}
}
}
}