-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: System.Diagnostics.CodeAnalysis.StringSyntaxAttribute #62505
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @tommcdon Issue DetailsBackground and motivationVS provides nice colorization of regular expressions when passed to known methods on Regex, e.g. It also supports putting a comment before any string to mark the language being used in that string: 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: 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 Proposalnamespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class StringLanguageAttribute : Attribute
{
public StringLanguageAttribute(string language);
public string Language { get; }
}
} API Usagepublic sealed class RegexGeneratorAttribute : Attribute
{
public RegexGeneratorAttribute([StringLanguage("regex")] string pattern);
} Alternative DesignsNo response RisksNo response
|
cc: @CyrusNajmabadi, @jasonmalinowski, @terrajobst Related to dotnet/roslyn#34640 |
cc: @joperezr |
FWIW, i would love this. Note: we're also leaving design space open with Raw String Literals to allow for a direct way in the language to do it. Specifically: var x = """regex
\G(\w+\s?\w*),?
"""; Embedded languages being a first class concept would be great. We would also like to do dotnet/roslyn#42634 so that 3rd parties could then implement the LS support for a particular embedded language (instead of Roslyn being responsible for it). |
Though I can't think of any concrete use cases off-hand, my gut reaction is that |
Yeah, I had that initially, but it seemed like there would be various places where it'd be challenging to reflect that in a tool, e.g. passing such a field by ref would lose that information, and potentially ambiguities as to whether it should be applied, e.g. on a property, is that meant to impact assigning to the property and/or the code in the property returning a string. Combined with my not having scenarios in mind for it, I left it off. But, I'd leave this up to @CyrusNajmabadi and @jasonmalinowski and friends, if they expect it would be useful and feasible initially. It could always be added later. |
Cool. We have APIs in runtime that accept regexes, XML, and JSON; I expect we'd want to annotate many of those with this, e.g. Line 117 in 12a8819
We'll just need to standardize on a set of names for at least the languages we use, although maybe we would just snap to something like what GitHub uses for markdown language names. Thankfully, in general, the naming choice is obvious. |
Allowing it to be set on a property/field so the IDE can directly use that for the assignment/initializer. I'm guessing there's an API out there somewhere like:
And the IDE could process that too. I don't see a huge risk in allowing the attribute against field/property even if we don't immediately support it, in the IDE; there's absolutely ecosystem risk of not allowing it against field/property until "later" and the forcing libraries to have to wait until later .NET versions before they can target that, or they have to start #ifdefing more stuff to deal with downlevel. |
Should we have a place where we document known languages as const fields? |
You mean like adding: public sealed class StringLanguageAttribute
{
...
public const string Regex = "regex";
public const string Xml = "xml";
public const string Json = "json";
} ? |
Yes. Not sure I'd want them on this particular class (because the class name is quite lengthy and AFAIK the field wouldn't be in scope when applying the attribute, so users would have to qualify the field references). |
How about |
I don't have a strong opinion here. But for the number of places this will be used, I personally would be fine with e.g. |
I agree with @jasonmalinowski here. Let the attribute appear in these places. Ide can then add support as necessary. |
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter |
AttributeTargets.Field |
AttributeTargets.Property,
AllowMultiple = false,
Inherited = false)]
public sealed class StringSyntaxAttribute : Attribute
{
public StringSyntaxAttribute(string syntax);
public string Syntax { get; }
public const string Regex = "regex";
// As we're adding more support we can add new languages like:
// public const string Xml = "xml";
// public const string Json = "json";
}
} |
@stephentoub Would like to get involved with this. Feel free to assign it to me. |
In my opinion, we should add a code analyzer for it, which gives a warning if you pass an unsupported language. |
How do you define "unsupported"? VS isn't the only possible consumer, nor are we the arbiters for all possible languages. Someone should be able to write a library that accepts strings for any language they like and author tooling components to recognize those names. |
You are right :). I assumed that only mentioned languages should be supported. |
@bartonjs, opinions on the additional members? |
No major opinions. Public arrays are always a fun "is it shared, or is it yours?", but it's an attribute, so it's probably not going to be passed around. The lack of static typing will mean that anything touching an arguments-based syntax highlighter needs to be prepared for garbage... but they're generally dealing with probably-not-compilable code as input, so hopefully they're nice and defensive. (Documentation will, of course, be a pain) |
Will/can we add a constant for |
@stephentoub Thanks for the demo about this today. You said it is available in VS17.2, and I'm trying with preview 1, and not seeing it working. Is it supposed to work and I'm doing something wrong, or not in the first preview? (I included the attribute code as you mentioned would still work so I get it in versions prior to .NET7) |
D'oh! I just made the argument for why the "Json" text should be a constant, because I spelled it all lower-case. Works once properly cased |
We have no plan on this currently. Primarily because there's no real way to estimate the cost of this work currently. For one thing, we have no tech available to us to even suitable lex/parse even one dialect of sql, let alone the myriad dialects actually used in practice. We'd need a strong proposal on how to actually achieve this. Json has hte benefit of being a staggeringly simple language to add support for. And for regex we have hte canonical impl in teh runtime that we were able to ape in order to add this support. |
@CyrusNajmabadi is it a requirement that there be full lexing/parsing capabilities just to annotate this? Even very trivial support in an IDE (think highlighting common keywords like SELECT, WHERE, and JOIN plus operators) would make blocks of SQL text a lot easier on the eyes. |
@stephentoub @bartonjs did we add any other constants for well-known languages? The approved shape only had one for Regex. Here is my understanding of the final shape: namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter |
AttributeTargets.Field |
AttributeTargets.Property,
AllowMultiple = false,
Inherited = false)]
public sealed class StringSyntaxAttribute : Attribute
{
public StringSyntaxAttribute(string syntax);
public StringSyntaxAttribute(string syntax, params object[] arguments);
public string Syntax { get; }
public object[] Arguments { get; }
public const string Regex = "regex";
// As we're adding more support we can add new languages like:
// public const string Xml = "xml";
// public const string Json = "json";
}
} |
The string is Lines 40 to 47 in 785f818
|
@madelson I'm not opposed to adding annotations for languages that Roslyn has no plans to support; this would allow other tools such as Rider to add support. However, I think we'll want at least one party that does something useful with it to ensure the annotations are sensible. For example, if you only ever want syntax highlighting, then sure, |
We added Json, which is in the shown final shape as being fine to add when there's support, and we added DateTimeFormat, as you proposed and was discussed here #62505 (comment). |
@stephentoub in the Community Standup you mentioned keeping For a multi-targeting public API (think nuget package) the best workaround I can think of without risking assembly conflicts is public class Class2
{
public void MyMethod([StringSyntax("Regex")] string pattern) { }
}
#if !NET7_0_OR_GREATER
internal sealed class StringSyntaxAttribute : Attribute { ... }
#endif Is there a way to expose |
What's wrong with that approach? I don't think that's so much a workaround as it is the recommended course of action if you need to use the attribute downlevel. |
Making it internal works beautifully for me in my multi targeted project |
Sorry, thought I had tested this and thought the attribute had to be public to take effect. |
sorry for dumb question: I ask about SQL. If I know which dialect we are using in our project, is there any way to colorize SQL keywords? I mean parse SQL not by VS itself, but I already have a piece of code that can grab SQL from the code (via Roslyn) + can parse it to the tokens, so I have an information about tokens and theoretically can provide this info to VS visualization layer somehow. is it feasible now? |
@terrajobst I hear what you're saying about there being different dialects, but in many if not most cases SQL strings are being passed to a more generic SQL library ( Waiting to get buy-in from an implementer makes sense; just offering that as a consumer this would be valuable; definitely more so for my use-cases than JSON or date formats. I also suppose that there doesn't need to be an official constant here so long as the library authors and IDEs agree on a value to use for this. |
For SQL, see this comment: #65634 (comment)
I'm not sure what "lowest common denominator" means in SQL. Sure, there's a certain subset of statements which work everywhere, but it's really quite restricted. The moment you e.g. want to get only X rows, SQL Server has TOP, PG/SQLite have LIMIT/OFFSET, etc. IMHO for anything to be useful around SQL, there needs to be some setting somewhere that says what the dialect is, which the analyzer or IDE would pick up. |
We are making this extensible at the Roslyn layer. But it will be up to a different party entirely to provide any level of SQL support. |
I imagine that if VS/VSCode/Rider were to implement this then it would start with just the base keywords (SELECT, JOIN, WHERE, etc) and maybe some operators. I've worked with SQL in editors with this behavior and a little syntax highlighting goes a long way towards making the code easier to read. IDEs can of course go beyond this by offering configurable dialect highlighting like JetBrains does. |
We could indeed support only ANSI/ISO SQL (ISO/IEC 9075-1) by means of the identifier |
As mentioned previously, likely most applications of this would be SQL dialect agnostic members like |
Background and motivation
VS provides nice colorization of regular expressions when passed to known methods on Regex, e.g.
It also supports putting a comment before any string to mark the language being used in that string:
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:
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
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: