What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 133 Stack Exchange communities.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by animuson Sep 16 '13 at 5:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
The exact code that was not highlighted correctly would be a useful addition to this question. –  jzd Feb 7 '11 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. –  Alex Essilfie Feb 7 '11 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 Feb 7 '11 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 Feb 7 '11 at 20:57
    

3 Answers 3

up vote 59 down vote accepted

This is now implemented. See:

http://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:

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

    code goes here

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

    code goes 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?

share|improve this answer
    
The iphone, mac, objective-c, cocoa, etc tags are all very active. how about tossing "objc" in as a supported language? –  Dave DeLong Mar 7 '11 at 9:09
1  
@dave that would have to be filed against Google Prettify not us. Please see code.google.com/p/google-code-prettify –  Jeff Atwood Mar 7 '11 at 9:27
    
Wouldn't it be easier to add the lang- prefix automatically, especially since the comment already contains the mandatory language term? –  Andreas Bonini Mar 8 '11 at 2:07
    
@kop conflict with tags, which are also supported, see stackoverflow.com/tags/default and superuser.com/tags/default –  Jeff Atwood Mar 8 '11 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? –  Alex Essilfie Mar 8 '11 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 –  Jeff Atwood Mar 9 '11 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 Mar 9 '11 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. –  Alex Essilfie Mar 16 '11 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 Mar 31 '11 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 Apr 29 '11 at 5:10
5  
Any chance for Windows command line / batch file (.bat) syntax? –  Martin Scharrer Jul 6 '11 at 13:59
    
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 Sep 6 '11 at 4:41
    
@kit that "drop down" is the tags chosen –  Jeff Atwood Sep 6 '11 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 Sep 6 '11 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 Oct 16 '11 at 15:34

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.

share|improve this answer
    
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#. –  Alex Essilfie Feb 7 '11 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). –  Alex Essilfie Feb 7 '11 at 15:03

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!

share|improve this answer

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