Skip to content

Commit 9b0b60e

Browse files
committed
update emit warning to use block processor to have sane defaults for peeking into the file for error localization
1 parent 6eea484 commit 9b0b60e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,36 @@ public static class ProcessorDiagnosticExtensions
1414
{
1515
private static string CreateExceptionMessage(string message, Exception? e) => message + (e != null ? Environment.NewLine + e : string.Empty);
1616

17+
public static void EmitError(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
18+
processor.Emit(Severity.Error, message, line, column, length);
19+
20+
public static void EmitWarning(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
21+
processor.Emit(Severity.Warning, message, line, column, length);
22+
23+
public static void EmitHint(this BlockProcessor processor, string message, int? line = null, int? column = null, int? length = null) =>
24+
processor.Emit(Severity.Hint, message, line, column, length);
25+
26+
public static void Emit(this BlockProcessor processor, Severity severity, string message, int? line = null, int? column = null, int? length = null)
27+
{
28+
var context = processor.GetContext();
29+
if (context.SkipValidation)
30+
return;
31+
var d = new Diagnostic
32+
{
33+
Severity = severity,
34+
File = processor.GetContext().MarkdownSourcePath.FullName,
35+
Column = column ?? 1,
36+
Line = line ?? processor.LineIndex + 1,
37+
Message = message,
38+
Length = length ?? processor.Line.Length + 1
39+
};
40+
context.Build.Collector.Write(d);
41+
}
42+
43+
1744
public static void EmitError(this InlineProcessor processor, int line, int column, int length, string message) =>
1845
processor.Emit(Severity.Error, line, column, length, message);
1946

20-
2147
public static void EmitWarning(this InlineProcessor processor, int line, int column, int length, string message) =>
2248
processor.Emit(Severity.Warning, line, column, length, message);
2349

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Text.RegularExpressions;
6+
using Elastic.Markdown.Diagnostics;
67
using Elastic.Markdown.Myst.Roles.Icons;
78
using Markdig;
89
using Markdig.Helpers;
@@ -29,7 +30,7 @@ public void Setup(MarkdownPipelineBuilder pipeline) =>
2930
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
3031
}
3132

32-
public partial class HeadingBlockWithSlugParser : HeadingBlockParser
33+
public class HeadingBlockWithSlugParser : HeadingBlockParser
3334
{
3435
private static readonly Regex IconSyntax = IconParser.IconRegex();
3536
private static readonly Regex AppliesToSyntax = HeadingAppliesToParser.AppliesToSyntaxRegex();
@@ -40,8 +41,9 @@ public override bool Close(BlockProcessor processor, Block block)
4041
return base.Close(processor, block);
4142

4243
var text = headingBlock.Lines.Lines[0].Slice.AsSpan();
44+
4345
if (AppliesToSyntax.IsMatch(text))
44-
processor.GetContext().Build.Collector.EmitWarning(processor.GetContext().MarkdownSourcePath, "Do not use inline 'applies_to' annotations with headings. Use a section 'applies_to' annotation instead.");
46+
processor.EmitWarning("Do not use inline 'applies_to' annotations with headings. Use a section 'applies_to' annotation instead.");
4547

4648
// Remove icon syntax from the heading text
4749
var cleanText = IconSyntax.Replace(text.ToString(), "").Trim();

0 commit comments

Comments
 (0)