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

add DisplayTable method #3098

Merged
merged 1 commit into from
Jul 21, 2023
Merged

Conversation

jonsequitur
Copy link
Contributor

@jonsequitur jonsequitur commented Jul 20, 2023

This addresses #3086.

It adds a new extension method, IEnumerable<T>.DisplayTable, which can be used to display objects in a tabular format with the property names as headers. This implementation is unlike the original tabular format replaced in #2671 in the following ways:

  • Nested objects are displayed as tree views rather than as nested tables.
  • Property headers are based on the type T rather than on the object instances, so instances of heterogeneous types in the sequence will not appear different from one another in the table.

I've also made some changes to the DisplayedValue class, removing the MimeTypes property and instead storing the complete FormattedValue instances, allowing inspection from the notebook, which was previously not possible:

image

Finally, I've removed a duplicate Diagnostic class.

@@ -226,22 +226,7 @@ void Initialize()
var formatter = GetDefaultFormatterForAnyObject(type);
return formatter.Format(value, context);
}),

// Final last resort is to convert to plain text
new HtmlFormatter<object>((value, context) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this removed because it was redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. There were two different registrations for the same type. This one had no test coverage so it wasn't important.

@@ -226,22 +226,7 @@ void Initialize()
var formatter = GetDefaultFormatterForAnyObject(type);
Copy link
Contributor

@shyamnamboodiripad shyamnamboodiripad Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like most lambdas in this collection don't guard against the value being null (although the removed last resort one below was checking for this). Is this intentional or does some other piece of code else already filter out nulls before these lambdas are invoked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching is done based on the runtime type and the formatter provides a universal early out for null.

.Which
.Location
.SourceSpan
.LinePositionSpan.Start.Character
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering creating a local for the LinePositionSpan so that the code to select the diagnostic need not be repeated across the 2 assertions.

@@ -145,7 +146,69 @@ await kernel.FindKernelByName("csharp").As<CSharpKernel>()
[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task Display_helper_can_be_called_without_specifying_class_name(Language language)
public async Task DisplayTable_produces_tabular_HTML_output_for_IEnumerable_T(Language language)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be another test to validate what happens when the collection contains instances with heterogeneous types?


_displayId = displayId;
_mimeTypes = new HashSet<string>(mimeTypes.Where(mimetype => !string.IsNullOrWhiteSpace(mimetype)));
FormattedValues = formattedValues ?? throw new ArgumentNullException(nameof(formattedValues));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also validate that there is at least one formatted value?

}

public IReadOnlyCollection<string> MimeTypes => _mimeTypes;
public string DisplayId { get; }
Copy link
Contributor

@shyamnamboodiripad shyamnamboodiripad Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be private? Just checking since this seems like an implementation detail (for the DisplayedValue itself)

@shyamnamboodiripad shyamnamboodiripad merged commit fc216b0 into dotnet:main Jul 21, 2023
@shyamnamboodiripad
Copy link
Contributor

@jonsequitur I have gone ahead and merged this since none of my comments above seemed blocking (and can be addressed in a follow up PR)

@colombod colombod added the Area-Formatting Data and object formatting as HTML and plaintext label Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Formatting Data and object formatting as HTML and plaintext
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants