Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion for keywords #21171

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public CommandCompletion(Collection<CompletionResult> matches, int currentMatchI
/// <returns></returns>
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options)
{
if (input == null || input.Length == 0)
if (input == null)
{
return s_emptyCommandCompletion;
}
Expand Down Expand Up @@ -143,7 +143,7 @@ public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPo
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "powershell")]
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options, PowerShell powershell)
{
if (input == null || input.Length == 0)
if (input == null)
{
return s_emptyCommandCompletion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1143,19 +1143,19 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
}
}

if (result == null || result.Count == 0)
if (result is null || result.Count == 0)
{
// Handle special file completion scenarios: .\+file.txt -> +<tab>
string input = completionContext.RelatedAsts[0].Extent.Text;
if (Regex.IsMatch(input, @"^[\S]+$") && completionContext.RelatedAsts.Count > 0 && completionContext.RelatedAsts[0] is ScriptBlockAst)
result = CompletionCompleters.CompleteKeywords(completionContext, _tokens);
if (result.Count > 0 && (tokenAtCursor is null || (tokenAtCursor.Kind != TokenKind.Identifier && !tokenAtCursor.TokenFlags.HasFlag(TokenFlags.Keyword))))
{
replacementIndex = completionContext.RelatedAsts[0].Extent.StartScriptPosition.Offset;
replacementLength = completionContext.RelatedAsts[0].Extent.EndScriptPosition.Offset - replacementIndex;

completionContext.WordToComplete = input;
result = CompleteFileNameAsCommand(completionContext);
replacementIndex = completionContext.CursorPosition.Offset;
replacementLength = 0;
}
}
else
{
result.AddRange(CompletionCompleters.CompleteKeywords(completionContext, _tokens));
}

return result;
}
Expand Down