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

formatted values in trailing expressions #3420

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ await RunAsync(
{
if (ScriptState is not null && HasReturnValue)
{
var formattedValues = FormattedValue.CreateManyFromObject(ScriptState.ReturnValue);
IReadOnlyList<FormattedValue> formattedValues = ScriptState.ReturnValue switch
{
FormattedValue formattedValue => new[] { formattedValue },
IEnumerable<FormattedValue> formattedValueEnumerable => formattedValueEnumerable.ToArray(),
_ => FormattedValue.CreateManyFromObject(ScriptState.ReturnValue)
};
context.Publish(
new ReturnValueProduced(
ScriptState.ReturnValue,
Expand Down
10 changes: 7 additions & 3 deletions src/Microsoft.DotNet.Interactive.FSharp/FSharpKernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,13 @@ type FSharpKernel () as this =
| Ok(result) when not isError ->
match result with
| Some(value) when value.ReflectionType <> typeof<unit> ->
let value = value.ReflectionValue
let formattedValues = FormattedValue.CreateManyFromObject(value)
context.Publish(ReturnValueProduced(value, codeSubmission, formattedValues))
let resultValue = value.ReflectionValue
let formattedValues : IReadOnlyList<FormattedValue> =
match resultValue with
| :? FormattedValue as formattedValue -> Seq.singleton( formattedValue ).ToImmutableList()
| :? IEnumerable<FormattedValue> as formattedValueEnumerable -> formattedValueEnumerable.ToImmutableList()
| _ -> FormattedValue.CreateManyFromObject(resultValue)
context.Publish(ReturnValueProduced(resultValue, codeSubmission, formattedValues))
| Some _
| None -> ()
| _ ->
Expand Down
37 changes: 37 additions & 0 deletions src/Microsoft.DotNet.Interactive.Tests/LanguageKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.Linq;
Expand Down Expand Up @@ -865,6 +866,42 @@ public async Task it_returns_a_result_for_a_if_expressions(Language language, st
.Be(25);
}

[Theory]
[InlineData(Language.CSharp, """
using Microsoft.DotNet.Interactive;
FormattedValue.CreateSingleFromObject(1)
""")]
[InlineData(Language.FSharp, """
open Microsoft.DotNet.Interactive
FormattedValue.CreateSingleFromObject(1)
""")]

[InlineData(Language.CSharp, """
using Microsoft.DotNet.Interactive;
FormattedValue.CreateManyFromObject(1, "text/plain","application/json")
""")]
[InlineData(Language.FSharp, """
open Microsoft.DotNet.Interactive
FormattedValue.CreateManyFromObject(1, "text/plain","application/json")
""")]

public async Task it_returns_formattedValue_without_additional_formatting(Language language, string expression)
{
var kernel = CreateKernel(language);

await SubmitCode(kernel, expression);

var returnValueProduced = KernelEvents.Should().ContainSingle<ReturnValueProduced>().Which;

var returnedValues = returnValueProduced.Value switch
{
IEnumerable<FormattedValue> formattedValues => formattedValues,
FormattedValue formattedValue => new[] { formattedValue },
_ => throw new InvalidOperationException()
};
returnValueProduced.FormattedValues.Should().BeEquivalentTo(returnedValues);
}


[Theory]
[InlineData(Language.CSharp)]
Expand Down
Loading