spences10 / vscode-vba

VBA syntax highlighting and snippets for use in VSCode
MIT License
55 stars 30 forks source link

Syntax highlighting does not work on multi-line comments (MINOR) #7

Closed pacificweb closed 4 years ago

pacificweb commented 5 years ago

Screen capture, VsCode on the left and Excel Vba IDE on the right.

vbacomments

As you can see, the second line in VsCode is not highlighted correctly.

rediffusion commented 5 years ago

I was checked for me it's not working too! Need to fix it! 😒

retailcoder commented 4 years ago

I'm not sure this can be fixed with a regex.

Rubberduck's parser defines comments as follows:

commentOrAnnotation :
    (annotationList 
    | remComment
    | comment) 
    // all comments must end with a logical line. See VBA Language Spec 3.3.1
    (NEWLINE | EOF)
;
remComment : REM whiteSpace? commentBody;
comment : SINGLEQUOTE commentBody;
commentBody : (~NEWLINE)*;

Where NEWLINE is a lexer token defined as follows:

NEWLINE : '\r' '\n' | [\r\n\u2028\u2029];

Line continuations have been wrecking our parser for a long time, until we decided to make whitespace a parser rule:

whiteSpace : (WS | LINE_CONTINUATION)+;

So a LINE_CONTINUATION token is implicitly captured as part of the commentBody, because the comment body parser rule matches anything that isn't a NEWLINE; the EOF token in the parser rule ensures we correctly pick up a comment that would be the last thing in the module - the annotation stuff is Rubberduck-specific, and parses '@Something as a parameterizable annotation; that part of the logic could be left out.

Fixing this issue would require turning this part of the grammar into a regular expression.

Hope it helps!

retailcoder commented 4 years ago

That said, multiline / line-continuated comments in VB6/VBA are an abomination.

Consider implementing comment blocks like this instead (current syntax highlighter regex seems to work fine for this):

' comment block line 1
' comment block line 2
' comment block line 3
spences10 commented 4 years ago

@pacificweb thanks for taking the time to log this issue!

As you can tell by the date of my response this project isn't maintained.

@retailcoder I'd happily accept a PR to fix this, I don't have the time or inclination to fix this personally.

@rediffusion if you don't like it you can always have you money back :stuck_out_tongue:

sancarn commented 4 years ago

Hi all, I'm currently working on a PR for vscode-vba. This issue will be covered. I'm fairly certain comments can be matched without the need for beginning and end using the following regular expression:

(?i:('|Rem).*([^_\s]\s*|(\s*_\s*.*[^_\s]\s*)+))$

An example of its use can be foudn here

Edit: My fix was incorrect but I'm working on a potential fix. Hopefull it will make it to the PR.

spences10 commented 4 years ago

Brilliant! Thanks!

sancarn commented 4 years ago

@spences10 No problem. PR ready here https://github.com/spences10/vscode-vba/pull/10

Thanks for the useful extension!

spences10 commented 4 years ago

@sancarn let me know if this fixes the issue and I'll close this.

sancarn commented 4 years ago

@spences10 Seems like it fixes the issue without introducing new ones :)

spences10 commented 4 years ago

Brilliant!

Thanks for the contribution 🙏

Closing this issue now.