EditorSyntax icon indicating copy to clipboard operation
EditorSyntax copied to clipboard

Handle special characters in variable names.

Open daviwil opened this issue 8 years ago • 7 comments
trafficstars

From @SteveL-MSFT on May 17, 2017 22:58

$foo?bar = 1

"$foo?bar"

only $foo is highlighted as a variable, this causes confusion to the customer as PowerShell treats $foo?bar as the variable (which is allowed)

PSReadline highlights correctly

Copied from original issue: PowerShell/vscode-powershell#759

daviwil avatar May 17 '17 22:05 daviwil

Is there a list of special characters that don't require ${ } anywhere? Ie, $Foo*bar is allowed but you need to use ${Foo*bar}. I assume it's every special character that isn't an operator or keyword but I could be wrong.

omniomi avatar May 21 '18 20:05 omniomi

Hmm I think @JamesWTruher, @daxian-dbw, or @SteveL-MSFT might know the answer to that.

TylerLeonhardt avatar May 21 '18 22:05 TylerLeonhardt

one more instance I know of is $a:b. There are a few well-known prefixes like $private:, $script:, $global:, but this syntax applies to more scenarios beyond, such as

$function:SayHello = {"Hello"}
SayHello
> Hello

$f:myfile = "content for my file"
$f:myfile
> content for my file
cat F:\myfile
> content for my file

$f:myfile:alternateDataStream = "hidden content"
$f:myfile:alternateDataStream
> hidden content
cat F:\myfile:alternateDataStream
> hidden content

daxian-dbw avatar May 21 '18 22:05 daxian-dbw

You can look at the tokenizer code to see what characters are valid. Basically almost anything and if you use a brace, pretty much anything (including whitespace) until a closing brace.

SteveL-MSFT avatar May 22 '18 05:05 SteveL-MSFT

Thanks @SteveL-MSFT.

Will need to give some thought to the best way to tackle variables. With so many ways to PowerShell a cat I want to cover all of the variable variants in as concise a manner as possible. The current definitions are not great.

@daxian-dbw, part of me hates that those are real... part of me thinks that's super cool lol.

omniomi avatar May 22 '18 13:05 omniomi

@omniomi It seems that the main set of characters is [A-z0-9_?:]. However, if it's a colon followed by another colon, it's a static member and not part of the variable name like $a::b. And if a brace follows the $, then anything until the closing brace is part of the variable name.

SteveL-MSFT avatar May 22 '18 14:05 SteveL-MSFT

@SteveL-MSFT that works and should probably cover most cases.

I just need to figure out the best way to split everything in the repository so that it can be logically included in other places. How things are highlighted on their own, in strings, in sub-expressions, etc adds to the complexity. Can't just have one variable definition and use it everywhere. Splatting adds to complexity as well since Get-Something @Splat is a variable but $ByLine = "Author: @omniomi" is not.

omniomi avatar May 22 '18 14:05 omniomi