Closed OCram85 closed 6 years ago
Thanks for filing the issue here Marco! We'll take a look.
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. :)
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
)
}
@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.
This is a known bug in how strings are scoped inside param blocks. We are working on a fix :)
Update: I'm still getting the same parsing behaviour in vscode 1.23.0 and ms-vscode.powershell 1.7.0.
@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:
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. :/
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.
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.
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']
@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.
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!
Fixed by #94
System Details
Win 7 Prof 64Bit
1.5.3
0.7.2
$PSVersionTable
:Issue Description
I noticed a syntax highlight issue while using regex in
[ValidatePattern()]
.Example1:
Example2: