Skip to content

Allow argument completion on 'cd' #20

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

Merged
merged 1 commit into from
Apr 5, 2023
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
55 changes: 41 additions & 14 deletions src/CompletionPredictor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public partial class CompletionPredictor : ICommandPredictor, IDisposable
"ForEach-Object",
"?",
"where",
"Where-Object"
"Where-Object",
"cd",
};

internal CompletionPredictor(string guid)
Expand Down Expand Up @@ -62,33 +63,59 @@ public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContex
return default;
}

return GetFromTabCompletion(context, cancellationToken);
}

private SuggestionPackage GetFromTabCompletion(PredictionContext context, CancellationToken cancellationToken)
{
// Call into PowerShell tab completion to get completion results.
// The runspace may be held by another call, or the call may take too long and exceed the timeout.
CommandCompletion? result = GetCompletionResults(context.InputAst, context.InputTokens, context.CursorPosition);
if (result is null || cancellationToken.IsCancellationRequested)
if (result is null || result.CompletionMatches.Count == 0 || cancellationToken.IsCancellationRequested)
{
return default;
}

int count = result.CompletionMatches.Count;
if (count > 0)
{
count = count > 10 ? 10 : count;
var list = new List<PredictiveSuggestion>(count);
int count = result.CompletionMatches.Count > 30 ? 30 : result.CompletionMatches.Count;
List<PredictiveSuggestion>? list = null;

int replaceIndex = result.ReplacementIndex;
string input = context.InputAst.Extent.Text;

string input = context.InputAst.Extent.Text;
var head = result.ReplacementIndex == 0 ? ReadOnlySpan<char>.Empty : input.AsSpan(0, result.ReplacementIndex);
ReadOnlySpan<char> head = replaceIndex == 0 ? ReadOnlySpan<char>.Empty : input.AsSpan(0, replaceIndex);
ReadOnlySpan<char> diff = input.AsSpan(replaceIndex);

for (int i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{
CompletionResult completion = result.CompletionMatches[i];
ReadOnlySpan<char> text = completion.CompletionText.AsSpan();
string? suggestion = null;

switch (completion.ResultType)
{
var completion = result.CompletionMatches[i];
list.Add(new PredictiveSuggestion(string.Concat(head, completion.CompletionText), completion.ToolTip));
case CompletionResultType.ProviderItem or CompletionResultType.ProviderContainer when !diff.IsEmpty:
// For local paths, if the input doesn't contain the prefix, then we stripe it from the suggestion.
bool removeLocalPathPrefix =
text.IndexOf(diff, StringComparison.OrdinalIgnoreCase) == 2 &&
text[0] == '.' && text[1] == Path.DirectorySeparatorChar;
ReadOnlySpan<char> newPart = removeLocalPathPrefix ? text.Slice(2) : text;
suggestion = string.Concat(head, newPart);

break;

default:
break;
}

return new SuggestionPackage(list);
suggestion ??= string.Concat(head, text);
if (!string.Equals(input, suggestion, StringComparison.OrdinalIgnoreCase))
{
list ??= new List<PredictiveSuggestion>(count);
list.Add(new PredictiveSuggestion(suggestion, completion.ToolTip));
}
}

return default;
return list is null ? default : new SuggestionPackage(list);
}

private CommandCompletion? GetCompletionResults(Ast inputAst, IReadOnlyCollection<Token> inputTokens, IScriptPosition cursorPosition)
Expand Down