Fix exception after AcceptAndGetNext

The "current" line cleared after accepting via AcceptAndGetNext
and we would crash trying to recall this current line.

I've changed how history search works after AcceptAndGetNext
to work in a more natural way so that arrow keys can be used
to recall commands near the accepted command instead of
searching for other commands with the same prefix.

Fixes #152.
This commit is contained in:
jason@truewheels.net
2014-09-26 18:19:23 -07:00
parent d6be68e8d8
commit 3bb3e30bef
5 changed files with 51 additions and 8 deletions
+1
View File
@@ -10,6 +10,7 @@ New features:
Set-PSReadlineKeyHandler -Key Enter -Function AcceptLine
Bug fixes:
* Occasional exception with AcceptAndGetNext (Ctrl+O) followed by something other than Enter
New functions:
* ValidateAndAcceptLine
+11 -4
View File
@@ -62,9 +62,16 @@ namespace PSConsoleUtilities
{
ClearDemoWindow();
}
_savedCurrentLine._line = null;
_savedCurrentLine._edits = null;
_savedCurrentLine._undoEditIndex = 0;
// Clear the saved line unless we used AcceptAndGetNext in which
// case we're really still in middle of history and might want
// to recall the saved line.
if (_getNextHistoryIndex == 0)
{
_savedCurrentLine._line = null;
_savedCurrentLine._edits = null;
_savedCurrentLine._undoEditIndex = 0;
}
return result;
}
@@ -284,7 +291,7 @@ namespace PSConsoleUtilities
// to check if we need to load history from another sessions now.
MaybeReadHistoryFile();
if (_singleton._currentHistoryIndex == _history.Count)
if (_savedCurrentLine._line == null)
{
_savedCurrentLine._line = _buffer.ToString();
_savedCurrentLine._edits = _edits;
+12 -1
View File
@@ -528,7 +528,6 @@ namespace PSConsoleUtilities
_yankCommandCount = 0;
_yankLastArgCommandCount = 0;
_tabCommandCount = 0;
_searchHistoryCommandCount = 0;
_recallHistoryCommandCount = 0;
_visualSelectionCommandCount = 0;
_hashedHistory = null;
@@ -538,6 +537,18 @@ namespace PSConsoleUtilities
_currentHistoryIndex = _getNextHistoryIndex;
UpdateFromHistory(moveCursor: true);
_getNextHistoryIndex = 0;
if (_searchHistoryCommandCount > 0)
{
_searchHistoryPrefix = "";
if (Options.HistoryNoDuplicates)
{
_hashedHistory = new Dictionary<string, int>();
}
}
}
else
{
_searchHistoryCommandCount = 0;
}
}
+4 -3
View File
@@ -106,13 +106,13 @@ namespace TestPSReadLine
PSConsoleReadLine.SetOptions(new SetPSReadlineOption
{
EditMode = EditMode.Windows,
EditMode = EditMode.Emacs,
HistoryNoDuplicates = true,
});
PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+LeftArrow"}, PSConsoleReadLine.ShellBackwardWord, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+RightArrow"}, PSConsoleReadLine.ShellNextWord, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"UpArrow"}, PSConsoleReadLine.HistorySearchBackward, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"DownArrow"}, PSConsoleReadLine.HistorySearchForward, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"F4"}, PSConsoleReadLine.HistorySearchBackward, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"F5"}, PSConsoleReadLine.HistorySearchForward, "", "");
//PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+E"}, PSConsoleReadLine.EnableDemoMode, "", "");
//PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+D"}, PSConsoleReadLine.DisableDemoMode, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"Ctrl+D,Ctrl+C"}, PSConsoleReadLine.CaptureScreen, "", "");
@@ -121,6 +121,7 @@ namespace TestPSReadLine
PSConsoleReadLine.SetKeyHandler(new[] {"F6"}, PSConsoleReadLine.PreviousLine, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"F7"}, PSConsoleReadLine.NextLine, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"F2"}, PSConsoleReadLine.ValidateAndAcceptLine, "", "");
PSConsoleReadLine.SetKeyHandler(new[] {"Enter"}, PSConsoleReadLine.AcceptLine, "", "");
EngineIntrinsics executionContext;
+23
View File
@@ -166,6 +166,29 @@ namespace UnitTestPSReadLine
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys(_.UpArrow, _.UpArrow, _.CtrlO, InputAcceptedNow));
Test("echo 2", Keys(_.Enter));
// Test that the current saved line is saved after AcceptAndGetNext
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.CtrlO, InputAcceptedNow));
Test("e", Keys(_.DownArrow, _.DownArrow, _.Enter));
// Test that we can edit after recalling the current line
SetHistory("echo 1", "echo 2");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.CtrlO, InputAcceptedNow));
Test("eee", Keys(_.DownArrow, _.DownArrow, "ee", _.Enter));
}
[TestMethod]
public void TestAcceptAndGetNextWithHistorySearch()
{
TestSetup(KeyMode.Emacs,
new KeyHandler("UpArrow", PSConsoleReadLine.HistorySearchBackward),
new KeyHandler("DownArrow", PSConsoleReadLine.HistorySearchForward));
// Test that after AcceptAndGetNext, the previous search is not applied
SetHistory("echo 1", "echo 2", "zzz");
Test("echo 1", Keys("e", _.UpArrow, _.UpArrow, _.CtrlO, InputAcceptedNow));
Test("zzz", Keys(_.DownArrow, _.Enter));
}
[TestMethod]