143

This is a follow-up to a question I answered earlier (Changes to syntax highlighting).


For most questions that have more than one language-related tag and those with fuzzy languages (for instance !), there should be an option to specify which language to use in syntax highlighting because sometimes (and quite often too!) the language inference is wrong.

Take for instance the scenario I describe below.

When an answer is given in a language other than the "primary language" (which happens to be C#), the code is still highlighted as C# even if it isn't in that language!



Scenario

  1. Take any question that has multiple explicit language-related tags.
    For example: Webclient.UploadFile which is tagged tagged , , and .
  2. Try to answer the question with code that is different from the 'major' language.
    In this case, try answering the question with VB.NET code.

Expected Results: Code should be highlighted in VB.NET.
Actual Result: Code was highlighted in C#!

Suggested Solution
There should be a way to explicitly set the language for syntax highlighting.


Edit

It also turns out that asking a question with multiple explicit language-related tags can also give you wrong syntax highlighting!
You can try creating a new question with the following and observe the preview:

    //This is C#
    var someValue = GetValue();

some text to separate the two languages

    'This is VB.Net
    Dim someValue = GetValue()

Tag the question as follows (in the specific order) ,

You'll notice without fail that the C# code gets highlighted properly and the vb.net code doesn't.

Disclaimer
All observations are based on the preview you get while entering the question or answer so my observed result might not be wholly right.

5
  • 1
    The exact code that was not highlighted correctly would be a useful addition to this question.
    – jzd
    Commented Feb 7, 2011 at 14:45
  • If you try it out you'll see what I mean. Should you want to know what wasn't done properly off the bat, it's the VB.NET code that isn't highlighted properly. Prettify assumes both lines are comments/strings because the first line ('This is VB.Net) does not terminate its opening quotation mark. Commented Feb 7, 2011 at 15:01
  • I figured Jeff's note was about fragments (in both questions and answers), but maybe not: and yes, there are plans to allow an explicit override syntax but for now we want to try to get the defaults mostly correct before worrying about the edge conditions.
    – Arjan
    Commented Feb 7, 2011 at 20:28
  • As an aside: Jeff's above comment also links to the Google Code Prettify maintainer's proposal on language specifiers. There, Mike Samuel suggests a first line like #!css or #!vb in a code block. It's just a detail, but I guess ideas of how specifying might work might better be posted there.
    – Arjan
    Commented Feb 7, 2011 at 20:57
  • meta.stackexchange.com/questions/981/…
    – nawfal
    Commented Jul 29, 2013 at 11:50

3 Answers 3

161

This is now implemented. See:

https://stackoverflow.com/editing-help#syntax-highlighting

In addition to tag inference (a recent change), you can manually specify the language as a hint to Google Code Prettify.

The spec is:

``` lang-or-tag-here
code goes here
```

<!-- language: lang-or-tag-here -->

    code goes here

<!-- language: lang-or-tag-here -->

    code goes here

You can also specify the hint to apply to all following code blocks with language-all:

<!-- language-all: lang-or-tag-here -->

You may use either a tag or a prettify language code to specify, though prettify language codes are always guaranteed to work regardless of what language the tag happens to be set to.

Available Language Hints Moved

Due to several lists of available hints existing throughout Meta, the entire list has been consolidated and moved to the following FAQ:

What is syntax highlighting and how does it work?

18
  • The iphone, mac, objective-c, cocoa, etc tags are all very active. how about tossing "objc" in as a supported language? Commented Mar 7, 2011 at 9:09
  • 2
    @dave that would have to be filed against Google Prettify not us. Please see code.google.com/p/google-code-prettify Commented Mar 7, 2011 at 9:27
  • Wouldn't it be easier to add the lang- prefix automatically, especially since the comment already contains the mandatory language term? Commented Mar 8, 2011 at 2:07
  • @kop conflict with tags, which are also supported, see stackoverflow.com/tags/default and superuser.com/tags/default Commented Mar 8, 2011 at 3:28
  • I do not know why but I cannot explicitly set the code's language. I've tried this <!-- language: lang-vb --> and <!-- language: lang-sql --> on a question tagged .net and c# but neither of them worked. Could you please give a detailed explanation of how to go about this? Commented Mar 8, 2011 at 13:00
  • @alex just follow the instructions above.. you didn't have the code syntax comment in there at all. see my revision to your post Commented Mar 9, 2011 at 9:12
  • @Jeff, thanks for trying to maintain the many places/duplicates where this feature was asked for! To get a single point of reference: would it be okay if I would change most posts to refer to a single reference?
    – Arjan
    Commented Mar 9, 2011 at 10:55
  • 3
    @Jeff: One more request... Can the question/answer preview be worked on so that the syntax highlight works there as well? The syntax highlight currently works only when a question/answer is posted. Commented Mar 16, 2011 at 18:06
  • 1
    Is it by design that each code block needs its own <!-- lang: ... --> comment? (Not an issue, but currently only the first block following the comment is using the specific language; blocks after that still use the language from the question's tags.)
    – Arjan
    Commented Mar 31, 2011 at 10:27
  • This isn't exactly obvious, is it? I knew the feature got implemented, yet I had no idea how to access it.
    – alex
    Commented Apr 29, 2011 at 5:10
  • 10
    Any chance for Windows command line / batch file (.bat) syntax? Commented Jul 6, 2011 at 13:59
  • 1
    I'm a coder for sure, but throw in a damn drop down. It's clear to me, the coder/user, and clear to SO/site what language is the intent, and doesn't require hunting down more knowledge.
    – Kit
    Commented Sep 6, 2011 at 4:41
  • @kit that "drop down" is the tags chosen Commented Sep 6, 2011 at 4:56
  • Er, yes, but I'm limited to 5 tags no? I'm using some to express intent, and some for language.
    – Kit
    Commented Sep 6, 2011 at 4:59
  • Could you add the common name for languages where the lang-abc isn't the name commonly used for the language? (e.g. lang-vb = Visual Basic)
    – doncherry
    Commented Oct 16, 2011 at 15:34
1

If there is a question about, for instance, a specific branch of a tag (like "android-emulator" for instance) that does include some code, the standard language-filter that kicks in for "android" (java i presume :) ) does not work. So a code-part that's far-from complete will not be prettified. I would say it would not be too strange to be able to hint the language!

0

In cases that more than one language tag is present, prettify will attempt to infer the language. In these cases it will not always infer the correct language, but it seems to do a nice job from what I have seen. I oppose this request as I do not think it is necessary.

2
  • When I ask questions on SO, I usually tag them with c# and vb.net because I work in both languages and I can quickly convert between both. In this regard, I'd not like code in my questions to be highlighted as C# when it is actually C#. Commented Feb 7, 2011 at 14:58
  • Also many questions are tagged with multiple languages, especially in the .net area. If the question is in VB.NET, someone may choose to answer it in C# and that throws off the syntax highlighter. (I haven't tested this out yet but questions in C# answered in VB.NET always get highlighted wrongly). Commented Feb 7, 2011 at 15:03

Not the answer you're looking for? Browse other questions tagged .