Skip to content

Commit 5d3a476

Browse files
Apply some code comments, clean-up the YAML Parsing method
1 parent ef0b41a commit 5d3a476

File tree

7 files changed

+59
-36
lines changed

7 files changed

+59
-36
lines changed

common/CommunityToolkit.Labs.Core.SourceGenerators.Tests/CommunityToolkit.Labs.Core.SourceGenerators.Tests/ToolkitSampleMetadataTests.Documentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace CommunityToolkit.Labs.Core.SourceGenerators.Tests
1212
{
1313
public partial class ToolkitSampleMetadataTests
1414
{
15-
//// We currently need at least one sample to test the document registry, so we'll have this for the base cases to share.
15+
// We currently need at least one sample to test the document registry, so we'll have this for the base cases to share.
1616
private static readonly string SimpleSource = $@"
1717
using System.ComponentModel;
1818
using CommunityToolkit.Labs.Core.SourceGenerators;

common/CommunityToolkit.Labs.Core.SourceGenerators.Tests/CommunityToolkit.Labs.Core.SourceGenerators.Tests/ToolkitSampleMetadataTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ from assembly in AppDomain.CurrentDomain.GetAssemblies()
395395
GC.KeepAlive(sampleAttributeType);
396396
}
397397

398-
//// From: https://github.com/dotnet/roslyn/blob/main/src/Compilers/Test/Core/SourceGeneration/TestGenerators.cs
398+
// From: https://github.com/dotnet/roslyn/blob/main/src/Compilers/Test/Core/SourceGeneration/TestGenerators.cs
399399
internal class InMemoryAdditionalText : AdditionalText
400400
{
401401
private readonly SourceText _content;

common/CommunityToolkit.Labs.Core.SourceGenerators/Metadata/ToolkitSampleMetadata.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,45 @@ namespace CommunityToolkit.Labs.Core.SourceGenerators.Metadata;
1414
/// </summary>
1515
public sealed class ToolkitSampleMetadata
1616
{
17+
/// <summary>
18+
/// Gets or sets a unique identifier for the sample, across all samples.
19+
/// </summary>
1720
public string Id { get; set; }
1821

22+
/// <summary>
23+
/// Gets or sets the display name for this sample page.
24+
/// </summary>
1925
public string DisplayName { get; set; }
2026

27+
/// <summary>
28+
/// Gets or sets the description for this sample page.
29+
/// </summary>
2130
public string Description { get; set; }
2231

32+
/// <summary>
33+
/// Gets or sets a type that can be used to construct an instance of the sample control.
34+
/// </summary>
2335
public Type SampleControlType { get; set; }
2436

37+
/// <summary>
38+
/// Gets or sets a factory method that returns a new instance of the control.
39+
/// </summary>
2540
public Func<object> SampleControlFactory { get; set; }
2641

42+
/// <summary>
43+
/// Gets or sets the (optional) control type for the sample page's options pane.
44+
/// Constructor should have exactly one parameter that can be assigned to the control type (<see cref="SampleControlType"/>).
45+
/// </summary>
2746
public Type? SampleOptionsPaneType { get; set; }
2847

48+
/// <summary>
49+
/// Gets or sets a factory method that returns a new instance of the sample options control.
50+
/// </summary>
2951
public Func<object, object>? SampleOptionsPaneFactory { get; set; }
3052

53+
/// <summary>
54+
/// Gets or sets the generated sample options that were declared alongside this sample, if any.
55+
/// </summary>
3156
public IEnumerable<IGeneratedToolkitSampleOptionViewModel>? GeneratedSampleOptions { get; set; }
3257

3358
/// <summary>

common/CommunityToolkit.Labs.Core.SourceGenerators/ToolkitSampleMetadataGenerator.Documentation.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
namespace CommunityToolkit.Labs.Core.SourceGenerators;
1717

18-
/// <summary>
19-
/// This part of the partial class deals with finding Markdown files and creating
20-
/// <see cref="ToolkitFrontMatter"/> metadata from it.
21-
/// </summary>
18+
// This part of the partial class deals with finding Markdown files
19+
// and creating ToolkitFrontMatter metadata from it.
2220
public partial class ToolkitSampleMetadataGenerator
2321
{
2422
private const string FrontMatterRegexTitleExpression = @"^title:\s*(?<title>.*)$";
@@ -117,14 +115,14 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
117115
var subcategory = ParseYamlField(ref ctx, file.Path, ref frontmatter, FrontMatterRegexSubcategory, "subcategory");
118116

119117
// Check we have all the fields we expect to continue (errors will have been spit out otherwise already from the ParseYamlField method)
120-
if (!(title.Success && description.Success && keywords.Success &&
121-
category.Success && subcategory.Success))
118+
if (title == null || description == null || keywords == null ||
119+
category == null || subcategory == null)
122120
{
123121
return null;
124122
}
125123

126124
// Grab/Check Enum values
127-
if (!Enum.TryParse<ToolkitSampleCategory>(category.Text, out var categoryValue))
125+
if (!Enum.TryParse<ToolkitSampleCategory>(category, out var categoryValue))
128126
{
129127
// TODO: extract index to get proper line number?
130128
ctx.ReportDiagnostic(
@@ -136,7 +134,7 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
136134
return null;
137135
}
138136

139-
if (!Enum.TryParse<ToolkitSampleSubcategory>(subcategory.Text, out var subcategoryValue))
137+
if (!Enum.TryParse<ToolkitSampleSubcategory>(subcategory, out var subcategoryValue))
140138
{
141139
// TODO: extract index to get proper line number?
142140
ctx.ReportDiagnostic(
@@ -173,9 +171,9 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
173171
// Finally, construct the complete object.
174172
return new ToolkitFrontMatter()
175173
{
176-
Title = title.Text,
177-
Description = description.Text,
178-
Keywords = keywords.Text,
174+
Title = title!,
175+
Description = description!,
176+
Keywords = keywords!,
179177
Category = categoryValue,
180178
Subcategory = subcategoryValue,
181179
FilePath = filepath,
@@ -185,7 +183,7 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
185183
}).OfType<ToolkitFrontMatter>().ToImmutableArray();
186184
}
187185

188-
private (bool Success, string? Text) ParseYamlField(ref SourceProductionContext ctx, string filepath, ref string content, Regex pattern, string fieldname)
186+
private string? ParseYamlField(ref SourceProductionContext ctx, string filepath, ref string content, Regex pattern, string fieldname)
189187
{
190188
var match = pattern.Match(content);
191189

@@ -197,10 +195,10 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
197195
Location.Create(filepath, TextSpan.FromBounds(0, 1), new LinePositionSpan(LinePosition.Zero, LinePosition.Zero)),
198196
filepath,
199197
fieldname));
200-
return (false, null);
198+
return null;
201199
}
202200

203-
return (true, match.Groups[fieldname].Value.Trim());
201+
return match.Groups[fieldname].Value.Trim();
204202
}
205203

206204
private void AddDocuments(SourceProductionContext ctx, ImmutableArray<ToolkitFrontMatter> matter)

common/CommunityToolkit.Labs.Core.SourceGenerators/ToolkitSampleMetadataGenerator.Sample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace CommunityToolkit.Labs.Core.SourceGenerators;
1717

1818
/// <summary>
19-
/// Crawls all referenced projects for <see cref="ToolkitSampleAttribute"/>s and generates a static method that returns metadata for each one found.
19+
/// Crawls all referenced projects for <see cref="ToolkitSampleAttribute"/>s and generates a static method that returns metadata for each one found. Uses markdown files to generate a listing of <see cref="ToolkitFrontMatter"/> data and an index of references samples for them.
2020
/// </summary>
2121
[Generator]
2222
public partial class ToolkitSampleMetadataGenerator : IIncrementalGenerator
@@ -72,7 +72,7 @@ void Execute(IncrementalValuesProvider<ISymbol> types)
7272
.Collect();
7373

7474
var markdownFiles = context.AdditionalTextsProvider
75-
.Where(static file => file.Path.EndsWith(".md")) // TODO: file.Path.Contains("samples") - this seems to break things?
75+
.Where(static file => file.Path.EndsWith(".md"))
7676
.Collect();
7777

7878
var all = optionsPaneAttributes
@@ -104,7 +104,7 @@ void Execute(IncrementalValuesProvider<ISymbol> types)
104104
)
105105
);
106106

107-
//// TODO: NOTE: BUGBUG: This is currently guarding us generating duplicate document registeries based on how the SG are setup to run twice to gather samples depending on how they're loaded.
107+
//// TODO: NOTE: BUGBUG: This is currently guarding us generating duplicate document registeries based on how the SG are setup to run this Execute method twice to gather samples depending on how they're loaded.
108108
//// However, that also means that if a sample only contains documentation without samples we won't load it. That shouldn't be the case currently, though makes testing more difficult.
109109
if (!sampleMetadata.Any())
110110
return;

common/CommunityToolkit.Labs.Shared/Renderers/ToolkitDocumentationRenderer.xaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ private async Task LoadData()
106106
return;
107107
}
108108

109-
// TODO: Show list of samples in a side-panel
110109
List<ToolkitSampleMetadata> samples = new();
111110
if (Metadata.SampleIdReferences != null && Metadata.SampleIdReferences.Length > 0 &&
112111
!string.IsNullOrWhiteSpace(Metadata.SampleIdReferences[0]))
@@ -154,7 +153,6 @@ private void SampleListHyperlink_Click(object sender, RoutedEventArgs e)
154153

155154
private static async Task<string> GetDocumentationFileContents(ToolkitFrontMatter metadata)
156155
{
157-
// TODO: Path will be different if single vs. multi-sample?
158156
var fileUri = new Uri($"ms-appx:///SourceAssets/{metadata.FilePath}");
159157

160158
try

common/CommunityToolkit.Labs.Shared/TabbedPage.xaml.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,31 @@ public TabbedPage()
4040

4141
protected override void OnNavigatedTo(NavigationEventArgs e)
4242
{
43+
// Note: Need to use as for tuple, think this is the tracking issue here: https://github.com/dotnet/csharplang/issues/3197
4344
var info = e.Parameter as (IEnumerable<ToolkitSampleMetadata> Samples, IEnumerable<ToolkitFrontMatter> Docs, bool AreDocsFirst)?;
4445

45-
if (info is not null)
46+
if (info is null)
4647
{
47-
if (info.Value.AreDocsFirst)
48-
{
49-
foreach (var item in info.Value.Docs)
50-
{
51-
Items.Add(item);
52-
}
53-
}
54-
55-
foreach (var item in info.Value.Samples)
48+
return;
49+
}
50+
else if (info.Value.AreDocsFirst)
51+
{
52+
foreach (var item in info.Value.Docs)
5653
{
5754
Items.Add(item);
5855
}
56+
}
5957

60-
if (!info.Value.AreDocsFirst)
58+
foreach (var item in info.Value.Samples)
59+
{
60+
Items.Add(item);
61+
}
62+
63+
if (!info.Value.AreDocsFirst)
64+
{
65+
foreach (var item in info.Value.Docs)
6166
{
62-
foreach (var item in info.Value.Docs)
63-
{
64-
Items.Add(item);
65-
}
67+
Items.Add(item);
6668
}
6769
}
6870

0 commit comments

Comments
 (0)