github-linguist / linguist

Language Savant. If your repository's language is being reported incorrectly, send us a pull request!
MIT License
12.27k stars 4.25k forks source link

Incorrect syntax highlighting of cshtml files in GitHub #3760

Closed infinnie closed 5 years ago

infinnie commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

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[bot] commented 5 years ago

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.

ghost commented 5 years ago

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.

worldbeater commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Closing as this is resolved by https://github.com/github/linguist/pull/4314.

dazbradbury commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

taori commented 5 years ago

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 commented 5 years ago

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

image

lildude commented 5 years ago

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 commented 5 years ago

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