2
\$\begingroup\$

the below pertains to the flag language: lang-vba, which I have since been informed is not actually a flag, and for my use I should have been using language: lang-vb

VBA Code that uses conditional formatting, comments, date literals or open-ended strings is not highlighted correctly. For example,

Prettify

is not Highlighted like it is in the Microsoft Office Visual Basic Editor

Original

What is happening?

It would seem that instead of properly identifying ' as the comment starting literal, it was incorrectly assigned to being a char literal (note that VBA does not support char literals in any fashion). It would seem that the # character was then assigned to being the comment starting literal, instead of the Conditional Compilation literal.

Of significant not is that # also serves as the wrapping literal for declaring date type objects inline as seen in the example below

Prettify_1

Note that the #12:00:01 AM# is highlighted to indicate that it is commented out, rather than being represented like any other string or number literal

Original_1

Beyond this, VBA does not support multiline string literals of any form, and in certain cases supports string literals without a terminating ". However, the current Prettify.js setup for VBA supports multiline strings, meaning that you an end up with code blocks that look like the one below on our site.

Prettify_2

Note that line 3 is included in the string highlighting, despite not being a string

What needs to be changed?

In the Prettify.js config,

  1. The comment literal needs to be changed from # to ' (which can be escaped by wrapping string)

  2. The ' char(?) literal needs to be removed, as VBA does not support chars

  3. Removal of highlighting for multiline strings should be considered (if there are no characters, such as , or ), expected after the end of a VBA string, VBA will autoformat to add a trailing " at the end of the line, and they are otherwise illegal strings)

  4. It should be considered highlighting any, non-string, section of code which is wrapped in [...] (where ... is some expression) in the same light red color as the string and number literals are, as this equivalent to a string which is wrapped in an Application.Evaluate("...") call

\$\endgroup\$
5
  • \$\begingroup\$ If anyone knows how who to talk to or what needs to be done, to get this correct, please leave a comment \$\endgroup\$ Aug 10, 2017 at 18:57
  • 4
    \$\begingroup\$ This may be helpful, especially the part about reporting bugs - Therefore, any bugs and feature requests regarding syntax highlighting cannot be handled by Stack Exchange and should be directed to the team behind Google Code Prettify. \$\endgroup\$
    – Stephen
    Aug 10, 2017 at 19:17
  • 1
    \$\begingroup\$ @StepHen, thanks for the link, I have submitted a issue ticket. ( github.com/google/code-prettify/issues/502 ) \$\endgroup\$ Aug 10, 2017 at 19:34
  • \$\begingroup\$ Can you provide a link to the code in question? My guess is that Prettify is inferring the code is in Python. If I put <!-- Language: vba --> before the code block, it highlights correctly. \$\endgroup\$ Aug 10, 2017 at 20:45
  • 1
    \$\begingroup\$ Voting to close as off-topic for the reason in the comment above. \$\endgroup\$
    – mbomb007
    Aug 10, 2017 at 22:04

1 Answer 1

2
\$\begingroup\$

Compare (below snippets are live syntax highlighting, not screenshots):

Default (meant for C-like languages)

Sub a()
#If n = 0 Then
Debug.Print "N=0"
#Else
Return 'This throws an error
#End If
End Sub

Visual Basic

Sub a()
#If n = 0 Then
Debug.Print "N=0"
#Else
Return 'This throws an error
#End If
End Sub

In this case, you have to override the language hint, see here for details.

(Note: I used a non-existent language hint to get the default language handler in the first case because this meta forum defaults to "none")

\$\endgroup\$

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