From 89596e68336d165c8b7eafd35995d8f4ec16fc01 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Mon, 13 Feb 2023 15:19:24 -0800 Subject: [PATCH 1/2] fix handling of completions that rely on nested commands --- NotebookTestScript.dib | 15 ++++++++++++--- .../KernelExtensions.cs | 18 +++++++++--------- .../src/interactiveClient.ts | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/NotebookTestScript.dib b/NotebookTestScript.dib index 82191e3c85..c4d39d5ac7 100644 --- a/NotebookTestScript.dib +++ b/NotebookTestScript.dib @@ -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 @@ -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 @@ -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 diff --git a/src/Microsoft.DotNet.Interactive/KernelExtensions.cs b/src/Microsoft.DotNet.Interactive/KernelExtensions.cs index d41726fe49..59b3aadf23 100644 --- a/src/Microsoft.DotNet.Interactive/KernelExtensions.cs +++ b/src/Microsoft.DotNet.Interactive/KernelExtensions.cs @@ -283,8 +283,6 @@ private static void ConfigureAndAddSetMagicCommand(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 @@ -295,13 +293,15 @@ private static void ConfigureAndAddSetMagicCommand(T kernel) where T : Kernel var tasks = Task.WhenAll(getValueTasks).GetAwaiter().GetResult(); - return tasks - .Select(t => t.Events.OfType()) - .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()) + .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(); + + return x; } return Array.Empty(); diff --git a/src/polyglot-notebooks-vscode-common/src/interactiveClient.ts b/src/polyglot-notebooks-vscode-common/src/interactiveClient.ts index 1ac9dee036..089cc308f1 100644 --- a/src/polyglot-notebooks-vscode-common/src/interactiveClient.ts +++ b/src/polyglot-notebooks-vscode-common/src/interactiveClient.ts @@ -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) { From cc406dda0ba0c390d9638bda2c6e569e1cce054d Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Mon, 13 Feb 2023 18:23:29 -0800 Subject: [PATCH 2/2] add logic to compensate for #2728 --- .../KernelExtensions.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.Interactive/KernelExtensions.cs b/src/Microsoft.DotNet.Interactive/KernelExtensions.cs index 59b3aadf23..4868de002f 100644 --- a/src/Microsoft.DotNet.Interactive/KernelExtensions.cs +++ b/src/Microsoft.DotNet.Interactive/KernelExtensions.cs @@ -289,14 +289,27 @@ private static void ConfigureAndAddSetMagicCommand(T kernel) where T : Kernel .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(); var x = tasks .Select(t => t.Events.OfType()) - .SelectMany(events => events.Select(e => new { e.Command.TargetKernelName, e.ValueInfos })) - .SelectMany(x => x.ValueInfos.Select(i => $"@{x.TargetKernelName}:{i.Name}")) + .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();