Skip to content
Merged
Show file tree
Hide file tree
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
590 changes: 590 additions & 0 deletions CosmosDBShell.Tests/Shell/HotkeyCommandTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public partial class ShellInterpreter : IHighlighter

/// <inheritdoc/>
IRenderable IHighlighter.BuildHighlightedText(string text)
{
return new Markup(this.BuildHighlightedMarkup(text));
}

/// <summary>
/// Builds the syntax-highlighted Spectre.Console markup string for the given shell input.
/// Returns escaped plain text on parse errors.
/// </summary>
internal string BuildHighlightedMarkup(string text)
{
var parser = new StatementParser(text);
Statement? statement = null;
Expand All @@ -49,7 +58,8 @@ IRenderable IHighlighter.BuildHighlightedText(string text)
statement.Accept(highlighter);
var result = highlighter.GetResult();
Comment thread
mkrueger marked this conversation as resolved.
this.oldHighlightStatement = statement;
return new Markup(result);
this.oldHighlightedText = text;
return result;
}
catch
{
Expand All @@ -66,7 +76,7 @@ IRenderable IHighlighter.BuildHighlightedText(string text)
this.oldHighlightedText = text;
this.oldHighlightStatement.Accept(highlighter);
var result = highlighter.GetResult();
return new Markup(result);
return result;
}
}
catch
Expand All @@ -77,12 +87,13 @@ IRenderable IHighlighter.BuildHighlightedText(string text)
#pragma warning restore CZ0001 // Empty Catch Clause

// fall back to non highlighted text in case of any errors.
return new Markup(Markup.Escape(text));
return Markup.Escape(text);
}

private void ClearHighlightStatement()
{
this.oldHighlightStatement = null;
this.oldHighlightedText = null;
}

internal class HighlightingVisitor : IAstVisitor
Expand Down
15 changes: 15 additions & 0 deletions CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Azure.Data.Cosmos.Shell.Core;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Data.Cosmos.Shell.Commands;
using Azure.Data.Cosmos.Shell.KeyBindings;
using Azure.Data.Cosmos.Shell.Parser;
using Azure.Data.Cosmos.Shell.States;
using Azure.Data.Cosmos.Shell.Util;
Expand Down Expand Up @@ -119,6 +120,8 @@ internal static char CSVSeparator

internal string HistoryFile { get; private set; }

internal IReadOnlyList<string> History => this.history;

internal string? LastBuffer { get; set; }

internal string? OriginalString { get; set; }
Expand Down Expand Up @@ -1107,6 +1110,18 @@ private LineEditor CreateLineEditor()

lineEditor.KeyBindings.Add<ClearCurrentLineCommand>(ConsoleKey.Escape);
lineEditor.KeyBindings.Add<ClearScreenCommand>(ConsoleKey.L, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<MoveToStartOfLineCommand>(ConsoleKey.A, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<MoveToEndOfLineCommand>(ConsoleKey.E, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<DeleteToStartOfLineCommand>(ConsoleKey.U, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<DeleteToEndOfLineCommand>(ConsoleKey.K, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<DeletePreviousWordCommand>(ConsoleKey.W, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<PreviousHistoryCommand>(ConsoleKey.P, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<NextHistoryCommand>(ConsoleKey.N, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<MoveCursorLeftCommand>(ConsoleKey.B, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add<MoveCursorRightCommand>(ConsoleKey.F, ConsoleModifiers.Control);
lineEditor.KeyBindings.Add(ConsoleKey.D, ConsoleModifiers.Control, () => new ExitShellCommand(this));
lineEditor.KeyBindings.Add(ConsoleKey.R, ConsoleModifiers.Control, () => new ReverseSearchHistoryCommand(this));
lineEditor.KeyBindings.Add(ConsoleKey.S, ConsoleModifiers.Control, () => new ReverseSearchHistoryCommand(this, startsForward: true));
lineEditor.KeyBindings.Add(ConsoleKey.Tab, () => new CosmosCompleteCommand(this, AutoComplete.Next));
lineEditor.KeyBindings.Add(ConsoleKey.Tab, ConsoleModifiers.Control, () => new CosmosCompleteCommand(this, AutoComplete.Previous));
foreach (var line in this.history)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.Core;
namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.Core;
namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;
using Spectre.Console;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class DeletePreviousWordCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
var end = context.Buffer.Position;
if (end <= 0)
{
return;
}

var content = context.Buffer.Content;
var start = end;

while (start > 0 && char.IsWhiteSpace(content[start - 1]))
{
start--;
}

while (start > 0 && !char.IsWhiteSpace(content[start - 1]))
{
start--;
}

var length = end - start;
if (length <= 0)
{
return;
}

context.Buffer.Clear(start, length);
context.Buffer.Move(start);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class DeleteToEndOfLineCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
var position = context.Buffer.Position;
var length = context.Buffer.Length - position;
if (length <= 0)
{
return;
}

context.Buffer.Clear(position, length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class DeleteToStartOfLineCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
var position = context.Buffer.Position;
if (position <= 0)
{
return;
}

context.Buffer.Clear(0, position);
context.Buffer.Move(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using Azure.Data.Cosmos.Shell.Core;
using RadLine;

internal class ExitShellCommand(ShellInterpreter shell) : LineEditorCommand
{
private readonly ShellInterpreter shell = shell;

public override void Execute(LineEditorContext context)
{
if (context.Buffer.Length == 0)
{
this.shell.IsRunning = false;
context.Submit(SubmitAction.Cancel);
return;
}

var position = context.Buffer.Position;
if (position < context.Buffer.Length)
{
context.Buffer.Clear(position, 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class MoveCursorLeftCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
context.Buffer.MoveLeft(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class MoveCursorRightCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
context.Buffer.MoveRight(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class MoveToEndOfLineCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
context.Buffer.MoveEnd();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Azure.Data.Cosmos.Shell.KeyBindings;

using RadLine;

internal class MoveToStartOfLineCommand : LineEditorCommand
{
public override void Execute(LineEditorContext context)
{
context.Buffer.MoveHome();
}
}
Loading
Loading