Skip to content

Commit

Permalink
Feat: upcasing, downcasing, and capitalizing word
Browse files Browse the repository at this point in the history
  • Loading branch information
3N4N committed Jul 12, 2022
1 parent 0ac6c07 commit ac0734d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
66 changes: 66 additions & 0 deletions PSReadLine/BasicEditing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,72 @@ public static void DeleteCharOrExit(ConsoleKeyInfo? key = null, object arg = nul
_singleton.DeleteCharImpl(1, orExit: true);
}

/// <summary>
/// A helper function to change the case of the current word.
/// </summary>
private static void UpdateWordCase(bool toUpper, ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._current >= _singleton._buffer.Length)
{
Ding();
return;
}

int endOfWord = _singleton.FindForwardWordPoint(_singleton.Options.WordDelimiters);
int wordlen = endOfWord - _singleton._current;

string word = _singleton._buffer.ToString(_singleton._current, wordlen);
word = toUpper ? word.ToUpper() : word.ToLower();

Replace(_singleton._current, wordlen, word);

_singleton.MoveCursor(endOfWord);
_singleton.Render();
}

/// <summary>
/// Upcase the current word and move to the next one.
/// </summary>
public static void UpcaseWord(ConsoleKeyInfo? key = null, object arg = null)
{
UpdateWordCase(true, key, arg);
}

/// <summary>
/// Downcase the current word and move to the next one.
/// </summary>
public static void DowncaseWord(ConsoleKeyInfo? key = null, object arg = null)
{
UpdateWordCase(false, key, arg);
}

/// <summary>
/// Capitalize the current word and move to the next one.
/// </summary>
public static void CapitalizeWord(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._current >= _singleton._buffer.Length)
{
Ding();
return;
}

int endOfWord = _singleton.FindForwardWordPoint(_singleton.Options.WordDelimiters);
int wordlen = endOfWord - _singleton._current;

char[] word = _singleton._buffer.ToString(_singleton._current, wordlen).ToLower().ToCharArray();
int idxFirstLetter = Array.FindIndex(word, x => char.IsLetter(x));

if (idxFirstLetter > 0)
{
word[idxFirstLetter] = Char.ToUpper(word[idxFirstLetter]);
Replace(_singleton._current, wordlen, new string(word));
}

_singleton.MoveCursor(endOfWord);
_singleton.Render();
}

private bool AcceptLineImpl(bool validate)
{
using var _ = _prediction.DisableScoped();
Expand Down
10 changes: 8 additions & 2 deletions PSReadLine/KeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ void SetDefaultEmacsBindings()
{ Keys.AltH, MakeKeyHandler(ShowParameterHelp, "ShowParameterHelp") },
{ Keys.F1, MakeKeyHandler(ShowCommandHelp, "ShowCommandHelp") },
{ Keys.F2, MakeKeyHandler(SwitchPredictionView, "SwitchPredictionView") },
{ Keys.AltU, MakeKeyHandler(UpcaseWord, "UpcaseWord") },
{ Keys.AltL, MakeKeyHandler(DowncaseWord, "DowncaseWord") },
{ Keys.AltC, MakeKeyHandler(CapitalizeWord, "CapitalizeWord") },
};

// Some bindings are not available on certain platforms
Expand Down Expand Up @@ -399,28 +402,30 @@ public static KeyHandlerGroup GetDisplayGrouping(string function)
case nameof(AcceptAndGetNext):
case nameof(AcceptLine):
case nameof(AddLine):
case nameof(BackwardDeleteInput):
case nameof(BackwardDeleteChar):
case nameof(BackwardDeleteInput):
case nameof(BackwardDeleteLine):
case nameof(BackwardDeleteWord):
case nameof(BackwardKillInput):
case nameof(BackwardKillLine):
case nameof(BackwardKillWord):
case nameof(CancelLine):
case nameof(CapitalizeWord):
case nameof(Copy):
case nameof(CopyOrCancelLine):
case nameof(Cut):
case nameof(DeleteChar):
case nameof(DeleteCharOrExit):
case nameof(DeleteEndOfBuffer):
case nameof(DeleteEndOfWord):
case nameof(DeleteRelativeLines):
case nameof(DeleteLine):
case nameof(DeleteLineToFirstChar):
case nameof(DeleteNextLines):
case nameof(DeletePreviousLines):
case nameof(DeleteRelativeLines):
case nameof(DeleteToEnd):
case nameof(DeleteWord):
case nameof(DowncaseWord):
case nameof(ForwardDeleteInput):
case nameof(ForwardDeleteLine):
case nameof(InsertLineAbove):
Expand All @@ -442,6 +447,7 @@ public static KeyHandlerGroup GetDisplayGrouping(string function)
case nameof(Undo):
case nameof(UndoAll):
case nameof(UnixWordRubout):
case nameof(UpcaseWord):
case nameof(ValidateAndAcceptLine):
case nameof(ViAcceptLine):
case nameof(ViAcceptLineOrExit):
Expand Down

0 comments on commit ac0734d

Please sign in to comment.