Skip to content
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

Format razor files #35

Open
belav opened this issue Mar 14, 2021 · 15 comments
Open

Format razor files #35

belav opened this issue Mar 14, 2021 · 15 comments
Labels
type:enhancement New feature or request

Comments

@belav
Copy link
Owner

belav commented Mar 14, 2021

Ideally csharpier would be able to format razor files. It appears that razor files are parsed and converted to c#. See
https://github.com/davidebbo/RoslynRazorViewEngine/blob/master/RoslynRazorViewEngine/RoslynRazorViewEngine.cs#L127

How does prettier deal with html files, react files, etc?
Should the razor formatter be a prettier plugin instead? How would it parse/format the c# code in a razor file?

@belav belav added the type:enhancement New feature or request label Mar 14, 2021
@jkoplo
Copy link

jkoplo commented Apr 22, 2022

I've been doing a lot of Blazor recently both at work and in my personal projects and would love to see csharpier support Razor files.

I think it would be perfectly acceptable to just format the long @code{...} blocks in a Razor file and ignore the html and intermixed c#. Or at least that would be a great starting point.

@belav
Copy link
Owner Author

belav commented Apr 22, 2022

I think full support for razor files would be a ton of work. The RazorTemplateEngine turns a view into a csharp file with all the HTML in strings. That doesn't help at all with deciding how to format the HTML. I haven't found any way to parse a razor file in a way that lends itself to being formatted. Although I just found an class named HtmlMarkupParser that may be what I was missing. Although all of its documentation warns about how it isn't meant to be used directly.

But your idea of formatting the @code blocks may be possible without a ton of effort. Assuming I can pull out just the code block and parse it with Roslyn then csharpier would be able to format it. I'll see how feasible that is.

Thanks for the suggestion!

@belav belav added this to the 0.17.0 milestone Apr 22, 2022
@jkoplo
Copy link

jkoplo commented Apr 22, 2022

Yeah I don't think parsing the HTML or the c# code in the HTML would be super necessary - it might even be undesirable since you're mixing two formatting paradigms.

The @code should be doable. If this is lower on the priority list for you I might take a run at it and PR it back...

@belav
Copy link
Owner Author

belav commented Apr 25, 2022

The idea of supporting a basic version of blazor/razor is pretty motivating to me, but I haven't had a ton of extra time to devote to csharpier lately. Mondays are my usual day to work on it, but I am feeling sick today and having a hard time focusing. Contributions are always welcome, so feel free to take a crack at it.

Where I ended up - regex is one option to try to find the code blocks, but is probably pretty error prone. I think we'd still want to use a parser, we can always leave the html portions as is and just use it to find the code blocks.

I did find https://stackoverflow.com/questions/50781429/how-to-create-visual-studio-extension-for-cshtml-files which is an example of parsing razor to a syntax tree, but then realized it is for the .net framework.
https://sudonull.com/post/6670-Compiling-and-running-C-and-Blazor-inside-a-browser is an example of compiling blazor, and some of the same packages are probably used to parse blazor, but they don't appear to be on nuget.
There is also this package https://www.nuget.org/packages/Microsoft.CodeAnalysis.Razor/ I dug through it a little bit but didn't see anything obviously useful.

@belav
Copy link
Owner Author

belav commented May 2, 2022

I did manage to parse a razor file, but getting to the c# code blocks requires reflection. A lot of the types needed are internal.

This branch has a hacked together POC - https://github.com/belav/csharpier/compare/basic-razor

@belav belav modified the milestones: 0.17.0, 0.18.0 May 23, 2022
@belav belav removed this from the 0.18.0 milestone Jun 13, 2022
@adamhathcock
Copy link

The POC almost looks too simple...I hope it's good. Can't wait for this for razor files.

@belav
Copy link
Owner Author

belav commented Oct 25, 2022

@Snailedlt
Copy link

Any ETA on this? Our team's productivity would greatly benefit from having .razor files autoformatted.

@Snailedlt
Copy link

@belav You've probably seen it already, but there's a prettier plugin for .razor files here -> https://github.com/KristinaPlusPlus/prettier-plugin-razor . Worth checking out if you haven't already. It's very basic, and looks to be abandoned since March 2021, but maybe there's still something you can learn or use from it.

@belav
Copy link
Owner Author

belav commented Jan 14, 2023

@Snailedlt unfortunately I don't know that I'll be getting to this any time soon, other issues keep taking priority and I haven't had as much time to dedicate to CSharpier as of late. Every time I end up in a razor file at work I am reminded how much I'd love for CSharpier to support this.

@twofingerrightclick
Copy link

twofingerrightclick commented Oct 5, 2023

As Microsoft doesn't even provided a robust razor formatting solution with VS, CSharpier would be paving a new road. So respect for even starting to look into it.

@lukedays
Copy link

Hi @belav, texting here since I saw no other means of contacting, would a sponsorship help develop this feature? Thanks in advance!

@belav
Copy link
Owner Author

belav commented May 1, 2024

Hey @lukedays I appreciate the offer, but at this point the biggest problem is my lack of time to dedicate to the project. I also worry that formatting razor may be a significant effort and don't even have a good starting place for it so would hate to promise anything.

@Eptagone
Copy link

Eptagone commented Nov 6, 2024

I also need something like this because the official MS C# extension for VS Code formats my razor files so ugly. :'(

@lulunac27a
Copy link

It's not working when I put

"files.associations": {
  "*.cshtml": "csharp"
},

in settings.json user settings of VSCode for .cshtml files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants