DiagnosticDescriptor class provides a description and metadata about a specific type of diagnostic. It defines the template for creating diagnostics with a particular ID, such as what the diagnostic means, its default severity, and its message format.
Overview
While aDiagnostic represents a specific occurrence of an error or warning in code, a DiagnosticDescriptor describes the type of diagnostic. For example, there is one descriptor for “LUA1001: Identifier expected”, but there could be many individual diagnostics with that ID occurring at different locations in the code.
Key Properties
Id
The unique identifier for this type of diagnostic."LUA1001", "LUA0004", "CUSTOM001"
Title
A short, localizable title describing the diagnostic."Identifier expected", "Invalid number format"
MessageFormat
A format string used to generate the diagnostic message, which can include placeholders for arguments."Expected identifier but found '{0}'", "Numeric literal '{0}' is too large"
When a diagnostic is created, arguments are passed to fill in these placeholders using string.Format.
Category
The category of the diagnostic, used for grouping related diagnostics."Compiler"- For Loretta’s built-in compiler diagnostics- Custom categories for analyzer diagnostics
DefaultSeverity
The default severity level for diagnostics of this type.DiagnosticSeverity.ErrorDiagnosticSeverity.WarningDiagnosticSeverity.InfoDiagnosticSeverity.Hidden
IsEnabledByDefault
Whether diagnostics of this type are enabled by default.Description
An optional longer description providing more context about the diagnostic.HelpLinkUri
An optional URL providing more detailed information about the diagnostic.CustomTags
Optional custom tags for the diagnostic, used for specialized handling.How Descriptors Relate to Diagnostics
EveryDiagnostic has an associated DiagnosticDescriptor accessible through the Descriptor property:
Where Loretta’s Descriptors Are Defined
Loretta’s built-in diagnostic descriptors are defined internally and created from theErrorCode enum. The descriptors are generated using:
- ErrorFacts.GetMessageFormat() - Gets the message format for an error code
- ErrorFacts.GetTitle() - Gets the title for an error code
- ErrorFacts.GetDescription() - Gets the description for an error code
- ErrorFacts.GetCategory() - Gets the category for an error code
- ErrorFacts.GetSeverity() - Gets the default severity for an error code
MessageProvider class, which uses the code prefix "LUA" for all Loretta diagnostics.
Creating Custom Descriptors
You can create your own diagnostic descriptors for custom analyzers or tools:With Localization
For localizable strings, you can useLocalizableResourceString:
Accessing Descriptor from Diagnostic
A common pattern is to access the descriptor to get metadata about a diagnostic:Filtering Diagnostics by Descriptor
You can use descriptors to filter or categorize diagnostics:Use Case: Custom Diagnostic Suppression
Descriptors are useful for implementing custom diagnostic filtering or suppression:Equality
Diagnostic descriptors implement value equality based on all their properties:See Also
- Diagnostic - Individual diagnostic instances
- Error Handling Guide - Best practices for working with diagnostics
- LuaSyntaxTree - Syntax tree API