Fix for google-code-prettify w/ c#

前端 未结 4 1183
忘掉有多难
忘掉有多难 2021-02-07 09:19

Prettify gives types and methods the same class when tokenizing c# so they are colored the same. This is because methods are pascal case in c# instead of camel case like in java

相关标签:
4条回答
  • 2021-02-07 09:53

    I actually wrote my own syntax highlighting library to solve problems like this. It's pretty similar to prettify but you have to specify the language explicitly.

    • Website
    • Demo
    0 讨论(0)
  • 2021-02-07 10:00

    It is possible for identical syntax to have different meanings. There just isn't enough information to properly syntax highlight everything.

    Take a look at this example:

    static class Program
    {
        class Foo { public class Bar { public static void Func() { } } }
        class Foo2 { public static Baz Bar2 { get; set; } }
        class Baz { public void Func2() { } }
    
        static void Main()
        {
            Foo.Bar.Func();
            Foo2.Bar2.Func2();
        }
    }
    

    In the first line, Bar is an inner class, and should be highlighted green. In the second line, Bar2 is a property of type Foo2, and should be highlighted black. Both Func and Func2 are functions, and should be highlighted black.

    Here's how Visual Studio highlights that code.

    alt text

    0 讨论(0)
  • 2021-02-07 10:03

    TextMate (OS X) or E-TextEditor (Windows)

    TextMate/E-TextEditor will generate HTML & CSS from syntax highlighting for many, many languages.

    Here is what you do:

    1. Open the file in TextMate/E-TextEditor
    2. Select the language from the menu at the bottom of the screen if it doesn't choose it automatically
    3. Go to Bundles->TextMate->Create HTML From Document
      • This will create all the HTMl/CSS in a new document.
      • Note: Windows users also choose the 'TextMate' bundle (not 'E-TextEditor')
    4. Profit!

    Note: You will have to install the C# bundle for C# syntax (every other common language is included). To do this, install the "Get Bundles" bundle, and use that to install the C# bundle.

    EDIT: Reading the comments I realized TextMate is only a solution for Mac users. Sometimes I forget about Windows.

    You can also use E-TextEditor for Windows. The steps are the same.

    0 讨论(0)
  • 2021-02-07 10:08

    The problem is that without context, it's impossible to find out whether it's a method or a type.

    Take the following example:

    var value = new Test();
    
    Test();
    

    This example instantiates a new Test and then calls the method Test. The only way to find out which is a class and which is a type is by having 1. the entire code base and 2. a compiler.

    And then I haven't even touched invalid code.

    That being said, I think that the current prettifier as used by SO does a terrific job of highlighting the code samples without any context whatsoever.

    0 讨论(0)
提交回复
热议问题