github-linguist / linguist

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

C# `.cs` heuristic doesn't match file scoped namespaces #5870

Closed lildude closed 2 years ago

lildude commented 2 years ago

C# 10 introduced support for file scoped namespaces which are namespace declarations without a body, for example:

namespace Paseto.Cryptography;

As there is no body, and thus no { char, this won't match the heuristic: ^(\s*namespace\s*[\w\.]+\s*{|\s*\/\/)

The heuristic should be updated to cater for these new declarations.


Discussed in https://github.com/github/linguist/discussions/5869

Originally posted by **daviddesmet** April 18, 2022 Some C# files are incorrectly detected as Smalltalk. ## Describe the bug Some C# files are incorrectly detected as Smalltalk as seen here https://github.com/daviddesmet/paseto-dotnet/search?l=smalltalk image image ### Expected behaviour It should be detected as C#
kasperk81 commented 2 years ago

seeing https://github.com/github/linguist/blob/cddf7476af4c95d1572956ffc5c0cb84f7e431c5/script/list-grammars#L8-L21

can c# also switch to tree-sitter https://github.com/tree-sitter/tree-sitter-c-sharp?

given github c# code navigation is using tree-sitter for a while https://github.blog/changelog/2020-10-19-code-navigation-for-c-repositories/

We use the tree-sitter library to find definitions and call sites in your code.

lildude commented 2 years ago

can c# also switch to tree-sitter https://github.com/tree-sitter/tree-sitter-c-sharp?

Unfortunately that's not within Linguist's control (and also not relevant to this issue). Tree-sitter grammars are pulled in directly by the new syntax highlighting engine regardless of what Linguist states. We only keep the list up-to-date to make it easier to refer people to the correct source in the event there is a syntax highlighting issue.

Nixinova commented 2 years ago

Is the change as simple as {[{;]?

lildude commented 2 years ago

Possibly. That's what I would do but I don't know anything about C# to know if I'm missing something else. I only worked out the cause from searching DDG for C# namespaces 😁

TheBlackPlague commented 2 years ago

Hello @lildude and @Nixinova - being a C# developer, I can assure you the change is just as simple as that. Namespaces can now be declared either using a { with the relevant closing }. Or if you want the entire file within the namespace, the recommended syntax now is to use ;. That's literally it.

I'm not familiar with the linguist project to know what exactly you need to change (hence, hesitant regarding making a PR myself), but I hope I could provide some context.