You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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.
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.
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).
Rename the existing 'powershell.tmLanguage.json' to .old.
Copy in the downloaded json file.
If VS Code is already running use command palette -> reload window.
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.
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.
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. :/
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.
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.
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!
Activity
daviwil commentedon Oct 12, 2016
Thanks for filing the issue here Marco! We'll take a look.
ThubLives commentedon Aug 1, 2017
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 commentedon Aug 18, 2017
My workaround is to put a single-line comment at the end of the line, which seems to reset things:
kedia990 commentedon Aug 20, 2017
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 commentedon Aug 21, 2017
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:
I'm not heartened by the fact this seems to be getting worse though.
gravejester commentedon Aug 31, 2017
This is a known bug in how strings are scoped inside param blocks. We are working on a fix :)
ThubLives commentedon May 8, 2018
Update: I'm still getting the same parsing behaviour in vscode 1.23.0 and ms-vscode.powershell 1.7.0.
omniomi commentedon May 8, 2018
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:
ThubLives commentedon May 8, 2018
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 commentedon May 8, 2018
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 commentedon May 8, 2018
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 commentedon May 8, 2018
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 commentedon May 8, 2018
Did some fiddling and it's easy enough to fix, I'll add it to my repo tonight along with a few other changes.
I'll let you know when there's an artifact to test with.
ThubLives commentedon May 8, 2018
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 commentedon May 9, 2018
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 commentedon May 9, 2018
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 commentedon May 21, 2018
Fixed by #94