[API Proposal]: System.Diagnostics.CodeAnalysis.StringSyntaxAttribute · Issue #62505 · dotnet/runtime (original) (raw)

Background and motivation

VS provides nice colorization of regular expressions when passed to known methods on Regex, e.g.
image

It also supports putting a comment before any string to mark the language being used in that string:
image

But there's currently no way to define an arbitrary API and annotate a string parameter as to what language is expected, e.g. you don't get this colorization today for RegexGenerator, because VS hasn't been taught about this API specifically:
image

While these examples are about regex, more generally this applies to arbitrary domain-specific languages. If VS adds colorization for JSON, for example, it'd be nice if every API that accepts JSON could be annotated appropriately.

API Proposal

namespace System.Diagnostics.CodeAnalysis { [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public sealed class StringLanguageAttribute : Attribute { public StringLanguageAttribute(string language); public string Language { get; } } }

API Usage

public sealed class RegexGeneratorAttribute : Attribute { public RegexGeneratorAttribute([StringLanguage("regex")] string pattern); }

Alternative Designs

No response

Risks

No response