6

I mainly answer PHP questions where syntax highlighting has always worked for me with just indentation. I've just started using PowerShell and have asked a couple of questions and syntax highlighting seems to be wonky:

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)

Wouldn't even some default syntax highlighting get one of the most common comment # styles right?

mklement0 states in Get original script name included script name and possibly others that using ```sh is a hack to get it to work:

    . "$PSScriptRoot\third.ps1"
    #get $Current  = second.ps1
    #get $Caller   = first.ps1 
    #get $Original = first.ps1

    $wc = new-object System.Net.WebClient
    $wc.Headers.Add("user-agent", "PowerShell Script")
    $wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    
    $action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)
5

1 Answer 1

6

Can't shed any light on the why, but the highlighter isn't even defaulting to anything here, it's just doing nothing. Setting the block manually to lang-default has seemingly better results:

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)

So to answer your question

Wouldn't even some default syntax highlighting get one of the most common comment # styles right?

The answer is actually yes, the highlighter just isn't defaulting to anything here for some reason.


Also, calling the triple backticks method (```sh) a "hack" is a little weird... It's an officially supported way of specifying the highlighting language. In this case, you're specifying the tag , which tells the highlighter to use the language code specified by that tag.

In this case, is set to lang-sh, so it gives you the same results as if you set the block to use lang-sh manually (note that this language id happens to be synonymous with lang-bashlang-zsh):

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)
10
  • 2
    Thanks. I call it a "hack" because PowerShell syntax is not shell, bash or zsh. I will test with lang-c and see what it does. And would it not be a bug to not default to anything? Jun 11, 2021 at 16:40
  • 2
    That makes sense; it's disappointing that some languages just aren't supported. At least this is a case where what's available is seemingly close-ish.
    – zcoop98
    Jun 11, 2021 at 16:42
  • It looks like it does have some colouring based on the fact that some of the text is syntax highlighted here
    – Thom A
    Jun 11, 2021 at 16:50
  • @Larnu It does look like it worked in that case, even if it's still using the bash highlighter. If I had to guess (since they OP didn't share a link), the no-highlight that OP saw probably is due to tags on the question, since that's been known to affect the applied highlighter. I've forgotten the specifics of that though.
    – zcoop98
    Jun 11, 2021 at 16:55
  • Seems like it's hit an miss, @zcoop98 . For the OP's code, it doesn't work at all: Sandbox attempt
    – Thom A
    Jun 11, 2021 at 17:03
  • 3
    I think you know this but just in case: meta.stackexchange.com/questions/355370
    – rene
    Jun 11, 2021 at 17:05
  • @Larnu Let me clarify (though you might be spot on that I'm wrong about this case), I dug up the post I was thinking of. This post by animuson explains how tags interact with the highlighter. "[W]e revert to "default" if there are two tags on a question that have different highlighters set."
    – zcoop98
    Jun 11, 2021 at 17:24
  • @Larnu In your SO answer that you shared, that question has a 4 tags, and exactly one of them has a language set: [powershell], set to "default". If OP's answer, on the other hand, was on a question with >1 highlighted tag, it may have failed to highlight altogether (which I've definitely seen in the wild before).
    – zcoop98
    Jun 11, 2021 at 17:25
  • 1
    In all cases I use ```powershell though @zcoop98 , so that statement shouldn't apply.
    – Thom A
    Jun 11, 2021 at 18:35
  • @Larnu Gotcha! I definitely missed that detail on your's.
    – zcoop98
    Jun 11, 2021 at 20:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .