-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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]: Allow StringSyntaxAttribute to be applied to return values. #76203
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 motivationThe addition of API ProposalAdd API Usage[return: StringSyntax(syntax: StringSyntaxAttribute.Xml)]
public static string BuildTwiml(string name) => $"""
<?xml version="1.0" encoding="utf-8"?>
<Response>
<Say>Ahoy {name}!</Say>
</Response>
"""; Alternative DesignsNo response RisksNo response
|
@CyrusNajmabadi, would Roslyn be able to handle this? @Swimburger, your example uses interpolated strings. They already don't get the syntax highlighting and related features you mention. Do you have real examples that don't involve dynamically-generated content? |
Sure. We could handle this. But we still won't do anything special for interpolated strings as we have no idea how to interpret the content. We also won't do anything with xml as we lack an xml parser that gives things like positions (afaik) |
What happens if you have We could possibly attempt to support a subset of features here. But we very likely could not effectively do diagnostics. |
When I do that, the program works without errors or warnings. When I intentionally malform the XML, it also doesn't complain about it in combination with string interpolation. With the interpolation, there's still warnings for empty XML nodes, and code completion. |
Yeah, this makes it just a heuristic. We could consider that, but it's feels like it's full of caveats and can easily make people think code is fine when it's really not. |
That makes sense, although the same would apply to the current attribute targets, no? |
This is solely about interpolated strings. Normal non-interpolated strings are fine and can be correctly analyzed in all contexts. |
Attributes generally tell something about a method to something outside that method. But here, the attribute would be only used inside the method itself. Because of that, I think the |
@svick , wouldn't it be possible for the IDE itself to be able to leverage the attribute to display the text in a formatted/syntax-highlighted manner when hovering over a variable for example when in debug mode? |
Can you explain more about that Edit: It seems to be a JetBrains Rider only feature: https://www.jetbrains.com/help/rider/Language_Injections.html#use-comments |
@arakis It is a comment that allows you to tell the format for a specific
It is not Rider-specific. It is already part of VS. |
Amazing, i didn't know that. I never read about that little detail, is there any official documentation (except #62505). By the way, it seems not to work with xml: |
no xml support has been added. |
Background and motivation
The addition of
StringSyntaxAttribute
(#62505) adds syntax highlighting and more to parameters, fields, and properties which is very helpful already.A lot of methods also construct strings of a specific syntax and return them, which could benefit from the syntax highlighting and tooling of the
StringSyntaxAttribute
.API Proposal
Add
AttributeTargets.ReturnValue
to the list of targets of theStringSyntaxAttribute
.API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: