PowerShell / EditorSyntax

PowerShell syntax highlighting for editors (VS Code, Atom, SublimeText, TextMate, etc.) and GitHub!
MIT License
133 stars 45 forks source link

[ValidatePattern()] regex breaks SyntaxHighlighting #26

Closed OCram85 closed 6 years ago

OCram85 commented 8 years ago

System Details

Name                           Value                                                                                                   
----                           -----                                                                                                   
PSVersion                      5.0.10586.117                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                 
BuildVersion                   10.0.10586.117                                                                                          
CLRVersion                     4.0.30319.42000                                                                                         
WSManStackVersion              3.0                                                                                                     
PSRemotingProtocolVersion      2.3                                                                                                     
SerializationVersion           1.1.0.1 

Issue Description

I noticed a syntax highlight issue while using regex in [ValidatePattern()].

Example1:

2016-10-11 08_24_43-invoke-2fausersync ps1 - visual studio code

Example2:

2016-10-11 08_15_56-invoke-2fausersync ps1 - visual studio code

daviwil commented 8 years ago

Thanks for filing the issue here Marco! We'll take a look.

ThubLives commented 7 years ago

Syntax highlighting is weird inside the quotes for regex in general, but I have found that the weirdness can escape the quotes and trash the rest of the code any time a closing parentheses is followed -- even distantly -- inside the quotes with a closing square bracket. It's as though it's not getting treated as a string at all inside the parameter attribute construct.

Take this code for example: function Test { Param( [ValidatePattern('(abcd)[a-d]')] [string]$TestString ) }

Strictly speaking, all you need as a string to cause breakage is ')]', but I don't think it's really valid regex. :)

ThubLives commented 7 years ago

My workaround is to put a single-line comment at the end of the line, which seems to reset things:

function Test {
  Param(
    [ValidatePattern('(abcd)[a-d]')] # This comment fixes VSCode's syntax highlighting strangeness.
    [string]$TestString
  )
}
kedia990 commented 7 years ago

@ThubLives I stumbled into the issue just today, so your suggested workaround couldn't have come at a better time for me!

That said, a clarification - it's not just a single-line comment, but a single line comment with an apostrophe ( ' ) in it which fixes the weirdness. Remove the "VSCode's" word in your comment to see what I mean.

ThubLives commented 7 years ago

Yes you're right, I see the same with VSCode 1.15.1 and vscode-powershell 1.4.1. I'm pretty sure this is different behaviour than in 1.14.2 (with either vscode-powershell 1.4.1 or 1.4.0), which I was using when I stumbled on this workaround; in that version it was all properly coloured like a comment, starting with the # character.

I guess a slightly nicer version of the workaround above could use #'# to reset things with VSCode 1.15.1:

function Test {
  Param(
    [ValidatePattern('(abcd)[a-d]')] #'# This comment fixes VSCode's syntax highlighting strangeness.
    [string]$TestString
  )
}

I'm not heartened by the fact this seems to be getting worse though.

gravejester commented 7 years ago

This is a known bug in how strings are scoped inside param blocks. We are working on a fix :)

ThubLives commented 6 years ago

Update: I'm still getting the same parsing behaviour in vscode 1.23.0 and ms-vscode.powershell 1.7.0.

omniomi commented 6 years ago

@ThubLives, this is currently fixed on my fork but I have not submitted the pull request yet. I am soliciting opinions on reddit: https://www.reddit.com/r/PowerShell/comments/8hrcwe/attribute_highlighting_in_vs_codeatomsublime_your/ as it changes the way attributes are displayed in general.

In one or two days I will submit the PR. In the mean time you can try the current definitions if you like:

  1. Download 'powershell.tmLanguage.json' from https://gist.github.com/omniomi/dea8f3a80a8187792c74c1250c602264
  2. Open your VS Code install directory followed by resources\app\extensions\powershell\syntaxes (C:\Program Files\Microsoft VS Code\resources\app\extensions\powershell\syntaxes on Windows).
  3. Rename the existing 'powershell.tmLanguage.json' to .old.
  4. Copy in the downloaded json file.
  5. If VS Code is already running use command palette -> reload window.
ThubLives commented 6 years ago

I've tried the powershell.tmLanguage.json file you provided, but it doesn't seem to deal with this problem, at least based on my example code above. What's more, my comment-based workaround doesn't work anymore, though it was probably likely to break given the kinds of changes that happened.

omniomi commented 6 years ago

I could have sworn I tested your case but it's been through a few iterations before I added it to git. I will take a look tonight and make sure it's covered before I submit the PR.

ThubLives commented 6 years ago

Okay, so it looks like the [ValidatePattern('(abcd)[a-d]')] line is just fine as long as it's not followed by anything. That is to say, the comment (and separating whitespace) is now what is causing the problem. Even just adding a space at the end of the line breaks things, though not as badly as before. The same is true for other attributes like [alias('spanakopita')] followed by anything at all.

Now I have to fiddle with my favourite theme, "Dark (Visual Studio)" to get it to colour types again. :/

omniomi commented 6 years ago

Gotcha. Currently it's expecting an end of line at the end of the match. I'll change it to a word boundary and that should resolve the issue.

omniomi commented 6 years ago

Did some fiddling and it's easy enough to fix, I'll add it to my repo tonight along with a few other changes.

comments

I'll let you know when there's an artifact to test with.

ThubLives commented 6 years ago

That's great! It's nice to see some good work on this.

I do see a regression with indexes though. Numbers, strings, and variable names are all getting coloured the same as types. I don't know what other basic types I'm missing, but I'm guessing they would behave the same.

$Array[$Variable] = $OtherArray[$Variable - 1] + $HashTable['spanakopita']

omniomi commented 6 years ago

@ThubLives can you give this guy a try: https://gist.github.com/omniomi/dea8f3a80a8187792c74c1250c602264 should address comments/whitespace on the same line as attributes and also fix indices following variables.

I also re-scoped a couple things to be more cosmetically pleasing in the majority of themes. Unfortunately the "right" scopes aren't always covered by theme makers if they cover things not common to most languages.

ThubLives commented 6 years ago

I dropped in the updated syntax file and visually scanned a few of my more complicated scripts and try as I might, can't find anything to complain about. Great work!

omniomi commented 6 years ago

Fixed by #94