Skip to content

Commit 74c98e4

Browse files
committed
Fix DiagnosticsCollector API to be a bit cleaner
1 parent bfae0fe commit 74c98e4

File tree

7 files changed

+40
-27
lines changed

7 files changed

+40
-27
lines changed

src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public void Write(Diagnostic diagnostic)
4141
}
4242
}
4343

44-
4544
public enum Severity { Error, Warning }
4645

4746
public readonly record struct Diagnostic
@@ -70,7 +69,6 @@ public void Write(Diagnostic diagnostic)
7069
}
7170
}
7271

73-
7472
public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollection<IDiagnosticsOutput> outputs)
7573
: IHostedService
7674
{
@@ -84,24 +82,32 @@ public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollect
8482
public long Warnings => _warnings;
8583
public long Errors => _errors;
8684

85+
private Task? _started;
86+
8787
public HashSet<string> OffendingFiles { get; } = new();
8888

89-
public async Task StartAsync(Cancel ctx)
89+
public Task StartAsync(Cancel ctx)
9090
{
91-
await Channel.WaitToWrite();
92-
while (!Channel.CancellationToken.IsCancellationRequested)
91+
if (_started is not null) return _started;
92+
_started = Task.Run(async () =>
9393
{
94-
try
95-
{
96-
while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken))
97-
Drain();
98-
}
99-
catch
94+
await Channel.WaitToWrite();
95+
while (!Channel.CancellationToken.IsCancellationRequested)
10096
{
101-
//ignore
97+
try
98+
{
99+
while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken))
100+
Drain();
101+
}
102+
catch
103+
{
104+
//ignore
105+
}
102106
}
103-
}
104-
Drain();
107+
108+
Drain();
109+
}, ctx);
110+
return _started;
105111

106112
void Drain()
107113
{
@@ -124,7 +130,12 @@ private void IncrementSeverityCount(Diagnostic item)
124130
Interlocked.Increment(ref _warnings);
125131
}
126132

127-
protected virtual void HandleItem(Diagnostic diagnostic) {}
133+
protected virtual void HandleItem(Diagnostic diagnostic) { }
128134

129-
public virtual Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
135+
public virtual async Task StopAsync(CancellationToken cancellationToken)
136+
{
137+
if (_started is not null)
138+
await _started;
139+
await Channel.Reader.Completion;
140+
}
130141
}

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public async Task GenerateAll(Cancel ctx)
9797

9898
var handledItems = 0;
9999

100-
var collectTask = Task.Run(async () => await Context.Collector.StartAsync(ctx), ctx);
100+
_ = Context.Collector.StartAsync(ctx);
101101

102102
await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
103103
{
@@ -126,11 +126,8 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
126126

127127
await GenerateDocumentationState(ctx);
128128

129-
await collectTask;
130-
await Context.Collector.Channel.Reader.Completion;
131129
await Context.Collector.StopAsync(ctx);
132130

133-
134131
IFileInfo OutputFile(string relativePath)
135132
{
136133
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath));

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
7676
var pathOnDisk = Path.Combine(includeFrom, url.TrimStart('/'));
7777
if (!context.Build.ReadFileSystem.File.Exists(pathOnDisk))
7878
processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}");
79+
else
80+
{
81+
82+
}
7983
}
8084
else
8185
link.Url = "";

tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class TipTests(ITestOutputHelper output) : AdmonitionTests(output, "tip")
4141
public void SetsTitle() => Block!.Title.Should().Be("Tip");
4242
}
4343

44+
public class AttentionTests(ITestOutputHelper output) : AdmonitionTests(output, "attention")
45+
{
46+
[Fact]
47+
public void SetsTitle() => Block!.Title.Should().Be("Attention");
48+
}
49+
4450
public class NoteTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
4551
"""
4652
```{note} This is my custom note

tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ A regular paragraph.
2626
}
2727

2828
// ReSharper disable UnusedType.Global
29-
public class AttentionTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "attention");
3029
public class DangerTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "danger");
3130
public class ErrorTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "error");
3231
public class HintTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "hint");

tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,12 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { }
8686

8787
public virtual async Task InitializeAsync()
8888
{
89-
var collectTask = Task.Run(async () => await Collector.StartAsync(default), default);
89+
_ = Collector.StartAsync(default);
9090

9191
Document = await File.ParseFullAsync(default);
9292
Html = File.CreateHtml(Document);
9393
Collector.Channel.TryComplete();
9494

95-
await collectTask;
96-
await Collector.Channel.Reader.Completion;
9795
await Collector.StopAsync(default);
9896
}
9997

tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,14 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { }
9696

9797
public virtual async Task InitializeAsync()
9898
{
99-
var collectTask = Task.Run(async () => await Collector.StartAsync(default), default);
99+
_ = Collector.StartAsync(default);
100100

101101
await Set.ResolveDirectoryTree(default);
102102

103103
Document = await File.ParseFullAsync(default);
104104
Html = File.CreateHtml(Document);
105105
Collector.Channel.TryComplete();
106106

107-
await collectTask;
108-
await Collector.Channel.Reader.Completion;
109107
await Collector.StopAsync(default);
110108
}
111109

0 commit comments

Comments
 (0)