Refactor CompletionHelpers HandleDoubleAndSingleQuote to have less nesting logic (#25179)

This commit is contained in:
Armaan Mcleod
2025-03-17 21:47:01 +05:00
committed by GitHub
parent e8bf560787
commit 9f45f9a855
@@ -55,36 +55,70 @@ namespace System.Management.Automation
}
}
/// <summary>
/// Removes wrapping quotes from a string and returns the quote used, if present.
/// </summary>
/// <param name="wordToComplete">
/// The string to process, potentially surrounded by single or double quotes.
/// This parameter is updated in-place to exclude the removed quotes.
/// </param>
/// <returns>
/// The type of quote detected (single or double), or an empty string if no quote is found.
/// </returns>
/// <remarks>
/// This method checks for single or double quotes at the start and end of the string.
/// If wrapping quotes are detected and match, both are removed; otherwise, only the front quote is removed.
/// The string is updated in-place, and only matching front-and-back quotes are stripped.
/// If no quotes are detected or the input is empty, the original string remains unchanged.
/// </remarks>
internal static string HandleDoubleAndSingleQuote(ref string wordToComplete)
{
string quote = string.Empty;
if (!string.IsNullOrEmpty(wordToComplete) && (wordToComplete[0].IsSingleQuote() || wordToComplete[0].IsDoubleQuote()))
if (string.IsNullOrEmpty(wordToComplete))
{
char frontQuote = wordToComplete[0];
int length = wordToComplete.Length;
if (length == 1)
{
wordToComplete = string.Empty;
quote = frontQuote.IsSingleQuote() ? "'" : "\"";
}
else if (length > 1)
{
if ((wordToComplete[length - 1].IsDoubleQuote() && frontQuote.IsDoubleQuote()) || (wordToComplete[length - 1].IsSingleQuote() && frontQuote.IsSingleQuote()))
{
wordToComplete = wordToComplete.Substring(1, length - 2);
quote = frontQuote.IsSingleQuote() ? "'" : "\"";
}
else if (!wordToComplete[length - 1].IsDoubleQuote() && !wordToComplete[length - 1].IsSingleQuote())
{
wordToComplete = wordToComplete.Substring(1);
quote = frontQuote.IsSingleQuote() ? "'" : "\"";
}
}
return string.Empty;
}
return quote;
char frontQuote = wordToComplete[0];
bool hasFrontSingleQuote = frontQuote.IsSingleQuote();
bool hasFrontDoubleQuote = frontQuote.IsDoubleQuote();
if (!hasFrontSingleQuote && !hasFrontDoubleQuote)
{
return string.Empty;
}
string quoteInUse = hasFrontSingleQuote ? "'" : "\"";
int length = wordToComplete.Length;
if (length == 1)
{
wordToComplete = string.Empty;
return quoteInUse;
}
char backQuote = wordToComplete[length - 1];
bool hasBackSingleQuote = backQuote.IsSingleQuote();
bool hasBackDoubleQuote = backQuote.IsDoubleQuote();
bool hasBothFrontAndBackQuotes =
(hasFrontSingleQuote && hasBackSingleQuote) || (hasFrontDoubleQuote && hasBackDoubleQuote);
if (hasBothFrontAndBackQuotes)
{
wordToComplete = wordToComplete.Substring(1, length - 2);
return quoteInUse;
}
bool hasFrontQuoteAndNoBackQuote =
(hasFrontSingleQuote || hasFrontDoubleQuote) && !hasBackSingleQuote && !hasBackDoubleQuote;
if (hasFrontQuoteAndNoBackQuote)
{
wordToComplete = wordToComplete.Substring(1);
return quoteInUse;
}
return string.Empty;
}
internal static bool CompletionRequiresQuotes(string completion, bool escape)