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

Incorrect syntax highlighting of cshtml files in GitHub #3760

Closed
infinnie opened this issue Aug 8, 2017 · 17 comments
Closed

Incorrect syntax highlighting of cshtml files in GitHub #3760

infinnie opened this issue Aug 8, 2017 · 17 comments

Comments

@infinnie
Copy link

infinnie commented Aug 8, 2017

Razor pages (i.e. those with an extension of .cshtml) are syntax highlighted using the C# syntax highlighter. However, the vast majority of a Razor page is HTML instead of C#, and the syntax highlighting of the HTML part is either absent or completely wrong. Not only that, but many of the at-directives are not correctly syntax highlighted.

(see https://github.com/infinnie/TodoServer/blob/master/TodoServer/Views/Todos/Index.cshtml )

@Alhadis
Copy link
Collaborator

Alhadis commented Aug 8, 2017

The cshtml extension is currently associated with the C# language itself, whereas it sounds like this should be categorised as a separate language altogether.

Are all cshtml files "Razor pages"? Or do some "ordinary" C# files use that file extension as well?

@infinnie
Copy link
Author

infinnie commented Aug 8, 2017

No ordinary C# files use that file extension.
Razor pages with the extension .cshtml are pages such as views in ASP.NET MVC or standalone ASP.NET Web Pages.

@Rabadash8820
Copy link

I second this. Razor, like other view engines, uses a syntax that's kind of a hybrid of HTML and an actual programming language--in this case, C#. So neither HTML nor C# is an appropriate language tag for Razor code in Github comments, issues, etc.

@worldbeater
Copy link
Contributor

worldbeater commented Feb 9, 2018

Visual Studio Code editor already has grammars that are needed to correctly highlight cshtml files. The grammars are located in Visual Studio Code syntaxes folder and are fully compatible with Atom & Github parsing engines as far as I know. So why not to take production-ready highlighters from there? atom/language-csharp repository already uses Microsoft's grammar (but packages don't seem to be uploaded to Atom and Github yet), so this can be done with cshtml too. @Alhadis hope this helps!

@ob1dev
Copy link

ob1dev commented May 6, 2018

The *.cshtml is a mix of html and c#. I would say treat it as HTML, but when you see @<expression>, then it's a C# code.

For example:

@if (User.Identity.IsAuthenticated)
{
  <ul class="nav navbar-nav navbar-right">
    <li><a asp-controller="Account" asp-action="Signout">Sign out</a></li>
  </ul>
}
else
{
  <ul class="nav navbar-nav navbar-right">
    <li><a asp-controller="Account" asp-action="Signin">Sign in</a></li>
  </ul>
}

Where @<expression> can be inside HTML tag. For instance,

<img width="20" height="20" class="avatar" alt="@User.Identity.Name" src="@User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value">

Check Microsoft Docs for more details about Razor syntax.

@stale
Copy link

stale bot commented Nov 6, 2018

This issue has been automatically marked as stale because it has not had activity in a long time. If this issue is still relevant and should remain open, please reply with a short explanation (e.g. "I have checked the code and this issue is still relevant because ___."). Thank you for your contributions.

@stale stale bot added the Stale label Nov 6, 2018
@ghost
Copy link

ghost commented Nov 6, 2018

This issue is still relevant as the linguist languages.yml file still considers the .cshtml extension to be C#. Looks like @worldbeater provided a pretty good reference above for how this Razor highlighting can be correctly implemented.

@stale stale bot removed the Stale label Nov 6, 2018
@worldbeater
Copy link
Contributor

worldbeater commented Nov 6, 2018

Worth using .cshtml grammar located at https://github.com/austincummings/razor-plus repository (see syntaxes directory). See it in action on Lightshow, looks pretty good.

@pchaigno
Copy link
Contributor

pchaigno commented Nov 6, 2018

Does one of you want to open a pull request to implement this change (move .cshtml to its own language entry with the adequate grammar)?

@lildude
Copy link
Member

lildude commented Nov 20, 2018

Closing as this is resolved by #4314.

@lildude lildude closed this as completed Nov 20, 2018
@dazbradbury
Copy link

Looks like there are still some issues that have resulted following this, example here:

https://gist.github.com/dazbradbury/2526da9ee866628ee10a0f2a4829253d

Previously, the red error highlighting wouldn't appear.

@lildude
Copy link
Member

lildude commented Jan 10, 2019

@dazbradbury That's going to be a problem with the grammar itself and easily reproducible by checking it directly within Lightshow.

You'll need to raise an issue against the grammar itself for this to be resolved. Once the grammar has resolved the issue, the change will automatically be pulled in in the next release of Linguist after it's fixed in the grammar.

@worldbeater
Copy link
Contributor

@dazbradbury the grammar used for .cshtml files is located here, just in case you wish to submit a fix.

@taori
Copy link

taori commented Sep 7, 2019

if this was fixed at some point - cshtml is now being considered as html again - and i agree with others, that it should de it's own categorized language because it isn't actually either of those languages

@alphonso77
Copy link

This is still annoying. Quotes aren't correctly being highlighted.

image

@lildude
Copy link
Member

lildude commented Oct 23, 2019

This is still annoying. Quotes aren't correctly being highlighted.

@alphonso77 This isn't a Linguist issue. This is an issue with the grammar. If you wish to have it resolved, you need to log an issue with the grammar.

@alphonso77
Copy link

@lildude, I'm sorry - I saw the comment earlier stating that, it was just a moment of frustration.

@github-linguist github-linguist locked as resolved and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants