PowerShell / EditorSyntax

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

Highlight parameter names after function invocation #76

Open omniomi opened 6 years ago

omniomi commented 6 years ago

Given Get-ChildItem -Path . -Recurse I cannot find a way to highlight "Path" and "Recurse". Currently I've only managed to highlight the -. The same goes for say "regex" in switch -Regex () {}.

It's gross but surly something like \\s\\-(?!(?i:[ic]?(?:eq|ne|gt|lt|ge|le|not|isnot|is|f|as|like|notmatch|match|notcontains|contains|in|and|or|xor|join|split|band|bor|bnot|bxor|replace))\\s)\\w+ would cover it? Provided it didn't step on constant.numeric and override negative integer highlighting.

Playing around on my own computer and adding this to powershell.tmLanguage.json:

{
    "match": "\\s\\-(?!(?i:[ic]?(?:eq|ne|gt|lt|ge|le|not|isnot|is|f|as|like|notmatch|match|notcontains|contains|in|and|or|xor|join|split|band|bor|bnot|bxor|replace))\\s)\\w+",
    "name": "variable.parameter.powershell"
},

I get the desired result although there may be edge cases I am not considering...

operators commands

omniomi commented 6 years ago

On further experimentation the regex above does indeed catch negative integers. A quick n' dirty workaround is to include \\d+ in the negative lookahead as a purely numerical parameter name is unlikely although parameter names that include some numbers do exist such as -UseUTF16 on New-PSSessionOption making the \\w+ necessary.

anthonyjvoss commented 5 years ago

Glad I found an existing open issue on this. It would be great to see the same syntax highlighting that exists in the Powershell console: image

msftrncs commented 5 years ago
        {
            "match": "(?:(?<!\\w|!)|\\G)-[\\p{L}][\\w]*(?!\\p{L})",
            "name": "entity.other.attribute-name.powershell"
        },

I added this to my JSON file after all the other operators, seems to do the trick. I think the look ahead is redundant, but it was left over from the other operators. The scope was just what I experimented with.

~I couldn't find anything definitive, can a parameter name begin with a digit? Or is that impossible since variable names cannot start with a digit?~ EDIT, forgot, in PowerShell, they can be entirely numeric! That raises the level of complexity.