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 .net!), 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
- Take any question that has multiple explicit language-related tags.
For example: Webclient.UploadFile which is tagged tagged c#, .net, vb.net and webclient. - 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) vb.net, .net c#
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.
3 Answers 3
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:
-
The iphone, mac, objective-c, cocoa, etc tags are all very active. how about tossing "objc" in as a supported language?Dave DeLong– Dave DeLong2011年03月07日 09:09:06 +00:00Commented 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-prettify2011年03月07日 09:27:05 +00:00Commented Mar 7, 2011 at 9:27
-
Wouldn't it be easier to add the
lang-
prefix automatically, especially since the comment already contains the mandatorylanguage
term?Andreas Bonini– Andreas Bonini2011年03月08日 02:07:17 +00:00Commented Mar 8, 2011 at 2:07 -
@kop conflict with tags, which are also supported, see stackoverflow.com/tags/default and superuser.com/tags/default2011年03月08日 03:28:53 +00:00Commented 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
andc#
but neither of them worked. Could you please give a detailed explanation of how to go about this?Alex Essilfie– Alex Essilfie2011年03月08日 13:00:57 +00:00Commented 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 post2011年03月09日 09:12:14 +00:00Commented 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– Arjan2011年03月09日 10:55:17 +00:00Commented 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.Alex Essilfie– Alex Essilfie2011年03月16日 18:06:44 +00:00Commented Mar 16, 2011 at 18:06
-
1Is 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– Arjan2011年03月31日 10:27:54 +00:00Commented 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– alex2011年04月29日 05:10:10 +00:00Commented Apr 29, 2011 at 5:10
-
10Any chance for Windows command line / batch file (
.bat
) syntax?Martin Scharrer– Martin Scharrer2011年07月06日 13:59:43 +00:00Commented Jul 6, 2011 at 13:59 -
1I'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– Kit2011年09月06日 04:41:44 +00:00Commented Sep 6, 2011 at 4:41
-
@kit that "drop down" is the tags chosen2011年09月06日 04:56:01 +00:00Commented 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– Kit2011年09月06日 04:59:56 +00:00Commented 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– doncherry2011年10月16日 15:34:05 +00:00Commented Oct 16, 2011 at 15:34
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!
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.
-
When I ask questions on SO, I usually tag them with
c#
andvb.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– Alex Essilfie2011年02月07日 14:58:58 +00:00Commented 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).Alex Essilfie– Alex Essilfie2011年02月07日 15:03:12 +00:00Commented Feb 7, 2011 at 15:03
Explore related questions
See similar questions with these tags.
'This is VB.Net
) does not terminate its opening quotation mark.#!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.