Skip to content

Commit

Permalink
formatted values in trailing expressions (#3420)
Browse files Browse the repository at this point in the history
* formatted values in trailing expressions

* improved
  • Loading branch information
colombod authored Jan 18, 2024
1 parent fb2af1b commit 9c1817e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
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

0 comments on commit 9c1817e

Please sign in to comment.