Skip to content
Merged
Changes from all 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
34 changes: 21 additions & 13 deletions src/Microsoft.PowerShell.Linux.Host/readline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ internal class ConsoleReadLine
/// </summary>
private bool newHistory = false;

/// <summary>
/// Save buffer at time we hit <tab>
/// </summary>
private string preTabBuffer;

/// <summary>
/// Initializes a new instance of the ConsoleReadLine class.
/// </summary>
Expand Down Expand Up @@ -235,7 +240,8 @@ private void OnTab()
//if the buffer has been modified in any way, get the new command completion
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)
Expand Down Expand Up @@ -263,26 +269,30 @@ private void OnTab()
if (!String.IsNullOrEmpty(tabResult))
{
var replaceIndex = cmdCompleteOpt.ReplacementIndex;
string replaceBuffer = this.buffer.ToString();
string replaceBuffer = this.preTabBuffer;

if (replaceBuffer.Length < replaceIndex)
{
replaceIndex = replaceBuffer.Length;
// something is wrong
return;
}

if (replaceBuffer.Length == replaceIndex)
{
tabResult = replaceBuffer + tabResult;
replaceBuffer = replaceBuffer + tabResult;
}
else
{
replaceBuffer = replaceBuffer.Remove(replaceIndex);
tabResult = replaceBuffer + tabResult;
replaceBuffer = replaceBuffer.Remove(replaceIndex, cmdCompleteOpt.ReplacementLength).Insert(replaceIndex, tabResult);
}

BufferFromString(tabResult);
BufferFromString(replaceBuffer);
this.Render();

int newPosition = replaceIndex + tabResult.Length;
this.cursor.Place(newPosition);
this.current = newPosition;

if (moveLeftOneSpace)
{
MoveLeft();
Expand Down Expand Up @@ -523,9 +533,8 @@ private void MoveRight()
{
if (this.current < this.buffer.Length)
{
char c = this.buffer[this.current];
this.current++;
Cursor.Move(1);
this.cursor.Move(1);
}
}

Expand All @@ -534,11 +543,10 @@ private void MoveRight()
/// </summary>
private void MoveLeft()
{
if (this.current > 0 && (this.current - 1 < this.buffer.Length))
if (this.current > 0)
{
this.current--;
char c = this.buffer[this.current];
Cursor.Move(-1);
this.cursor.Move(-1);
}
}

Expand Down Expand Up @@ -685,7 +693,7 @@ public Cursor()
/// Moves the cursor.
/// </summary>
/// <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;

Expand Down