Skip to content

[ValidatePattern()] regex breaks SyntaxHighlighting #26

Closed

Description

System Details

  • Operating system name and version: Win 7 Prof 64Bit
  • VS Code version: 1.5.3
  • PowerShell extension version: 0.7.2
  • Output from $PSVersionTable:
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

Activity

  1. daviwil commented on Oct 12, 2016

    @daviwil
    Contributor

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

  2. ThubLives commented on Aug 1, 2017

    @ThubLives

    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. :)

  3. ThubLives commented on Aug 18, 2017

    @ThubLives

    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
      )
    }
    
  4. kedia990 commented on Aug 20, 2017

    @kedia990

    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.

  5. ThubLives commented on Aug 21, 2017

    @ThubLives

    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.

  6. gravejester commented on Aug 31, 2017

    @gravejester
    Contributor

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

  7. ThubLives commented on May 8, 2018

    @ThubLives

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

  8. omniomi commented on May 8, 2018

    @omniomi
    Contributor

    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.
  9. ThubLives commented on May 8, 2018

    @ThubLives

    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.

  10. omniomi commented on May 8, 2018

    @omniomi
    Contributor

    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.

  11. ThubLives commented on May 8, 2018

    @ThubLives

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

  12. omniomi commented on May 8, 2018

    @omniomi
    Contributor

    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.

  13. omniomi commented on May 8, 2018

    @omniomi
    Contributor

    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.

  14. ThubLives commented on May 8, 2018

    @ThubLives

    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']

  15. omniomi commented on May 9, 2018

    @omniomi
    Contributor

    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.

  16. ThubLives commented on May 9, 2018

    @ThubLives

    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!

  17. omniomi commented on May 21, 2018

    @omniomi
    Contributor

    Fixed by #94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions