Merge pull request #569 from PowerShell/tab4

Fix case where tab completion is not at end of command
This commit is contained in:
Andy Schwartzmeyer 2016-02-21 16:50:13 -08:00
commit 666e924aad

View File

@ -85,6 +85,11 @@ namespace Microsoft.PowerShell.Linux.Host
/// </summary> /// </summary>
private bool newHistory = false; private bool newHistory = false;
/// <summary>
/// Save buffer at time we hit <tab>
/// </summary>
private string preTabBuffer;
/// <summary> /// <summary>
/// Initializes a new instance of the ConsoleReadLine class. /// Initializes a new instance of the ConsoleReadLine class.
/// </summary> /// </summary>
@ -235,7 +240,8 @@ namespace Microsoft.PowerShell.Linux.Host
//if the buffer has been modified in any way, get the new command completion //if the buffer has been modified in any way, get the new command completion
if (previousKeyPress.Key != ConsoleKey.Tab) if (previousKeyPress.Key != ConsoleKey.Tab)
{ {
cmdCompleteOpt = CommandCompletion.CompleteInput(this.buffer.ToString(), this.current, options, powershell); this.preTabBuffer = this.buffer.ToString();
cmdCompleteOpt = CommandCompletion.CompleteInput(this.preTabBuffer, this.current, options, powershell);
} }
if (cmdCompleteOpt.CompletionMatches.Count == 0) if (cmdCompleteOpt.CompletionMatches.Count == 0)
@ -263,26 +269,30 @@ namespace Microsoft.PowerShell.Linux.Host
if (!String.IsNullOrEmpty(tabResult)) if (!String.IsNullOrEmpty(tabResult))
{ {
var replaceIndex = cmdCompleteOpt.ReplacementIndex; var replaceIndex = cmdCompleteOpt.ReplacementIndex;
string replaceBuffer = this.buffer.ToString(); string replaceBuffer = this.preTabBuffer;
if (replaceBuffer.Length < replaceIndex) if (replaceBuffer.Length < replaceIndex)
{ {
replaceIndex = replaceBuffer.Length; // something is wrong
return;
} }
if (replaceBuffer.Length == replaceIndex) if (replaceBuffer.Length == replaceIndex)
{ {
tabResult = replaceBuffer + tabResult; replaceBuffer = replaceBuffer + tabResult;
} }
else else
{ {
replaceBuffer = replaceBuffer.Remove(replaceIndex); replaceBuffer = replaceBuffer.Remove(replaceIndex, cmdCompleteOpt.ReplacementLength).Insert(replaceIndex, tabResult);
tabResult = replaceBuffer + tabResult;
} }
BufferFromString(tabResult); BufferFromString(replaceBuffer);
this.Render(); this.Render();
int newPosition = replaceIndex + tabResult.Length;
this.cursor.Place(newPosition);
this.current = newPosition;
if (moveLeftOneSpace) if (moveLeftOneSpace)
{ {
MoveLeft(); MoveLeft();
@ -523,9 +533,8 @@ namespace Microsoft.PowerShell.Linux.Host
{ {
if (this.current < this.buffer.Length) if (this.current < this.buffer.Length)
{ {
char c = this.buffer[this.current];
this.current++; this.current++;
Cursor.Move(1); this.cursor.Move(1);
} }
} }
@ -534,11 +543,10 @@ namespace Microsoft.PowerShell.Linux.Host
/// </summary> /// </summary>
private void MoveLeft() private void MoveLeft()
{ {
if (this.current > 0 && (this.current - 1 < this.buffer.Length)) if (this.current > 0)
{ {
this.current--; this.current--;
char c = this.buffer[this.current]; this.cursor.Move(-1);
Cursor.Move(-1);
} }
} }
@ -685,7 +693,7 @@ namespace Microsoft.PowerShell.Linux.Host
/// Moves the cursor. /// Moves the cursor.
/// </summary> /// </summary>
/// <param name="delta">The number of characters to move.</param> /// <param name="delta">The number of characters to move.</param>
internal static void Move(int delta) internal void Move(int delta)
{ {
int position = Console.CursorTop * Console.BufferWidth + Console.CursorLeft + delta; int position = Console.CursorTop * Console.BufferWidth + Console.CursorLeft + delta;