dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.74k stars 3.99k forks source link

Add support for more StringSyntaxAttribute syntaxes #60593

Open stephentoub opened 2 years ago

stephentoub commented 2 years ago

.NET 7 adds the new [StringSyntax(...)] attribute, which Roslyn is now already recognizing in support of the StringSyntaxAttribute.DateTimeFormat/Regex/Json values. https://github.com/dotnet/runtime/pull/67621 is rounding out the named syntaxes on the attribute with an additional set, while also attributing all relevant parameters across the core libraries with these:

cc: @CyrusNajmabadi

CyrusNajmabadi commented 2 years ago

I would be fine with support being added for all of these. The hope of roslyn though would be that we don't own most of these though. The purpose of my efforts to expose extensibility points here is so htat others can own this.

Sergio0694 commented 1 year ago

Related (also cc. @teo-tsirpanis), would it be possible to also support syntax highlight for languages that the IDE already supports? Essentially what we get with the 3 backticks + language id on GitHub/Discord/etc., which could work whenever the language id (ie. the file extension) is one that VS already has support for.

For instance, if I had a [StringSyntax("cpp")] parameter (or "cs", or whatever), as long as that id was one which would give me syntax highlight if I was to just open a file with that extension in VS, I would expect the same to apply to the literal as well.

Example scenario: I have a [D2DPixelShaderSource] attribute in ComputeSharp you can use like this:

image

The parameter of that [D2DPixelShaderSource] has "hlsl" as [StringSyntax] argument. Given I do get syntax highlight whenever I open an .hlsl file in VS, I would expect the same to apply here as well. Would this be doable? I mean VS evidently does have logic to do syntax highlight based on the file extension, but it be doable to plug that through here as well? 🙂

NOTE: @CyrusNajmabadi if this is out of scope for this issue let me know and I can open a separate one 😄

CyrusNajmabadi commented 1 year ago

Our intent is to expose this for plugins to be written.

Sergio0694 commented 1 year ago

Right, but I guess what I'm wondering is: if the identifier is an extension that VS already has support for, couldn't this just work out of the box just like when you open a file, without the need for consumers to install any plugins or really anything at all? 😄 It would really be convenient especially when using identifiers that are widely used and with built-in support in VS.

teo-tsirpanis commented 1 year ago

@CyrusNajmabadi by "plugins" you mean Visual Studio extensions? What I would like is to allow library authors to define a custom language syntax with possible tooltips within the assembly, for example with an embedded resource file that gets read by IDEs. It would enable writing custom languages for both VS and VSCode. And of course also recognize the languages VS already knows as @Sergio0694 said.

CyrusNajmabadi commented 1 year ago

Right, but I guess what I'm wondering is: if the identifier is an extension that VS already has support for, couldn't this just work out of the box just like when you open a file

Anything is possible. A core difficulty is that these are not the same languages. For example , in a c# string you have escape sequences that the underlying language will not understand.

CyrusNajmabadi commented 1 year ago

It would really be convenient especially when using identifiers that are widely used and with built-in support in VS.

Sure. But someone has to pay for all that work. :-)

For context, json and regex was multiple weeks of work to try to get those working. Now someone has to test and validate every language embedded in c#

rauhs commented 10 months ago

Just an idea (feel free to ignore). Should something like this be possible:

[StringSyntax("lang_de")]

[StringSyntax("lang_en-us")]

so IDEs can offer context dependent spell checking?

teo-tsirpanis commented 10 months ago

I don't think it would be of much value. Localized strings should be in a resource file either way.

gdifilippo-ca commented 8 months ago

Hi, I'm working on a free Visual Studio extension that adds support for GraphQL syntax. The extension is called GraphQLTools and it is currently in preview. The source code is available here; I hope that it could be of help to people interested in extending the feature to more syntaxes. Writing a Visual Studio extension is quite challenging because the documentation is not that exhaustive and we would love to be able to write plugins for the StringSyntaxAttribute feature in a more standardized way.

Meir017 commented 2 months ago

This can we useful for playwright-dotnet and puppeteer-sharp for having literal JavaScript & HTML & CSS strings.

using puppeteer-sharp:

await using var page = await browser.NewPageAsync();
await page.SetContentAsync("<div>My Receipt</div>"); // this can be highlighted as HTML
var result = await page.GetContentAsync();
var watchDog = page.WaitForFunctionAsync("()=> window.innerWidth < 100"); // this can be highlighted as JavaScript
await page.WaitForSelectorAsync("div.main-content"); // this can be highlighted as CSS

FYI @kblok

using playwright-dotnet

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = false });
var page = await browser.NewPageAsync();
var href = await page.EvaluateAsync<string>("document.location.href"); // // this can be highlighted as JavaScript
await page.SetContentAsync("<div>My Receipt</div>"); // this can be highlighted as HTML

FYI @pavelfeldman @mxschmitt