Skip to content

Commit

Permalink
fix handling of completions that rely on nested commands (#2727)
Browse files Browse the repository at this point in the history
* fix handling of completions that rely on nested commands

* add logic to compensate for #2728
  • Loading branch information
jonsequitur authored Feb 14, 2023
1 parent 4e85881 commit 7dd1eb9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
15 changes: 12 additions & 3 deletions NotebookTestScript.dib
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!meta

{"kernelInfo":{"defaultKernelName":null,"items":[{"name":"csharp","languageName":"C#","aliases":["c#","cs"]},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"html","languageName":"HTML"},{"name":"sql","languageName":"SQL"},{"name":"kql","languageName":"KQL"},{"name":"mermaid","languageName":"Mermaid"},{"name":"http","languageName":"HTTP"},{"name":"value"}]}}
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp","aliases":[]}]}}

#!markdown

Expand Down Expand Up @@ -188,7 +188,7 @@ Console.Error.Write("\x1b[0m");
#!markdown

# vscode kernel and user input prompts
This cell should prompt the user for input
This cell should prompt the user for input. Type something into the input field. Whatever you typed should be displayed in the output.

#!csharp

Expand Down Expand Up @@ -221,7 +221,16 @@ value_from_input

#!markdown

Now check the variable explorer. Is the value there in the `#!value` kernel?
Now check the variable explorer. Is the value there in the `value` kernel?

#!markdown

This should prompt you for input. Type something into the input field. Whatever you typed should be displayed in the output.

#!csharp

#!set --name csharpVarFromInput --value @input:"Please enter a value"
csharpVarFromInput

#!markdown

Expand Down
33 changes: 23 additions & 10 deletions src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,38 @@ private static void ConfigureAndAddSetMagicCommand<T>(T kernel) where T : Kernel

valueOption.AddCompletions(_ =>
{
// FIX: (ConfigureAndAddSetMagicCommand) why is this getting invoked this option wasn't specified?

if (kernel.ParentKernel is { } composite)
{
var getValueTasks = composite.ChildKernels
.Where(
k => k != kernel &&
k.KernelInfo.SupportsCommand(nameof(RequestValueInfos)))
.Select(async k => await k.SendAsync(new RequestValueInfos()));
.Select(async k => await k.SendAsync(new RequestValueInfos(k.Name)));

var tasks = Task.WhenAll(getValueTasks).GetAwaiter().GetResult();

return tasks
.Select(t => t.Events.OfType<ValueInfosProduced>())
.SelectMany(events => events.Select(e => new { e.Command.TargetKernelName, e.ValueInfos }))
.SelectMany(x => x.ValueInfos.Select(i => $"@{x.TargetKernelName}:{i.Name}"))
.OrderBy(x => x)
.Select(n => new CompletionItem(n))
.ToArray();
var x = tasks
.Select(t => t.Events.OfType<ValueInfosProduced>())
.SelectMany(events => events.Select(e => new { e.Command, e.ValueInfos }))
.SelectMany(x =>
{
var kernelName = x.Command.TargetKernelName;

// TODO: (ConfigureAndAddSetMagicCommand) this is compensating for https://github.com/dotnet/interactive/issues/2728
if (kernelName is null &&
kernel.RootKernel is CompositeKernel root &&
root.ChildKernels.TryGetByUri(x.Command.DestinationUri, out var k))
{
kernelName = k.Name;
}

return x.ValueInfos.Select(i => $"@{kernelName}:{i.Name}");
})
.OrderBy(x => x)
.Select(n => new CompletionItem(n))
.ToArray();

return x;
}

return Array.Empty<CompletionItem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class InteractiveClient {
token = token || this.getNextToken();
const id = Guid.create().toString();
let disposable = this.subscribeToKernelTokenEvents(token, eventEnvelope => {
if (eventEnvelope.command?.token === token) {
if (eventEnvelope.command?.token === token && eventEnvelope.eventType === expectedEventType) {
switch (eventEnvelope.eventType) {
case CommandFailedType:
if (!handled) {
Expand Down

0 comments on commit 7dd1eb9

Please sign in to comment.