highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.31k stars 3.52k forks source link

(C#) Assigning a variable in code sample brings to tottaly different highlight #3990

Closed HedgehogNSK closed 4 months ago

HedgehogNSK commented 4 months ago

Describe the issue Hello. I'm using your extension for blogger platform. I just found out that if sample of code doesn't contain any variable assignment, then highlighter doesn't determine that the language of a sample is C#. I use "vs2015" style template

Are you using highlight or highlightAuto? I use snippet from the highlight homepage. If this question asks do I set language name in <code> tag explicitly? Then the answer is: I tried them both to determine the issue.

Sample Code to Reproduce

public partial class MainWindow : Window
{
    DisposableBag disposable; // DisposableBag is struct, no need new and don't copy

    public MainWindow()
    {
        Observable.IntervalFrame(1).Subscribe().AddTo(ref disposable);
        Observable.IntervalFrame(1).Subscribe().AddTo(ref disposable);
        Observable.IntervalFrame(1).Subscribe().AddTo(ref disposable);
    }

    void OnClick()
    {
        Observable.IntervalFrame(1).Subscribe().AddTo(ref disposable);
    }

    protected override void OnClosed(EventArgs e)
    {
        disposable.Dispose();
    }
}

When I don't set language explicitly <code>: image

When I set language explicitly <code class="cs">: image

When I don't set language explicitly, but I add assignment of some value (ex. var x = 1;): image

Expected behavior

  1. AutoDetection has to detect C# language even if there is no assignment of any variables.
  2. Names of classes, methods, types, as well as keywords as "ref", "class" etc have to by highlighted
dschach commented 4 months ago

The code you provided auto-detects to scss.

scss: relevance 21, csharp: 19

I don't know how to "fix" it, or even if it needs to be fixed. Auto-detect is difficult and should probably not be used, as it will be deprecated with v12.0 anyway, I believe. It's best to specify the language for each of your snippets.

joshgoebel commented 4 months ago

Yeah, we make no guarantees about auto-detect, it's more of a toy than a precision tool. Specify the language and you'll get much better results.