From 60e02475e4d6d3be1f4407a21c652594a360a4c4 Mon Sep 17 00:00:00 2001 From: jaeminbird Date: Thu, 29 Oct 2026 05:19:02 -0400 Subject: [PATCH] lsh: Fix PowerShell commands and operators matching keyword rules Commands like ForEach-Object, Format-Table or Invoke-WebRequest were matched by the keyword rules before the command rule, so they were highlighted partially as a keyword or not at all. The same happened to the "in" of the -in operator. Match commands and -operators before keywords. --- assets/highlighting-tests/powershell.ps1 | 5 +++++ crates/lsh/definitions/powershell.lsh | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/assets/highlighting-tests/powershell.ps1 b/assets/highlighting-tests/powershell.ps1 index e2bb01f39c6..b95bbc47245 100644 --- a/assets/highlighting-tests/powershell.ps1 +++ b/assets/highlighting-tests/powershell.ps1 @@ -76,3 +76,8 @@ Write-Host "2 + 2 = $($array[0] + $array[1])" # Command substitution $pwdPath = $(Get-Location).Path Write-Host "Current directory: $pwdPath" + +# Commands and operators that begin with a keyword +Get-ChildItem | ForEach-Object { $_.Name } | Format-Table +Invoke-WebRequest -Uri "https://example.com" +if (42 -in $array) { Exit-PSSession } diff --git a/crates/lsh/definitions/powershell.lsh b/crates/lsh/definitions/powershell.lsh index cdcfb7e41a3..98081dc1fc3 100644 --- a/crates/lsh/definitions/powershell.lsh +++ b/crates/lsh/definitions/powershell.lsh @@ -49,6 +49,11 @@ pub fn powershell() { else if /"/ { yield string; break; } await input; } + } else if /[\w\d?:]+-[\w\d?:-]*/ { + // Commands like ForEach-Object or Invoke-WebRequest, which may begin with a keyword + yield method; + } else if /-[a-zA-Z]\w*/ { + // Operators and parameters like -in or -Path, which may contain a keyword } else if /(?i:begin|class|clean|data|define|dynamicparam|end|enum|filter|from|function|hidden|in|param|process|static|using|var)/ { if /\w+/ { yield other; @@ -75,8 +80,6 @@ pub fn powershell() { } } else if /[$@][\w\d?:]+/ { yield variable; - } else if /[\w\d?:]+-[\w\d?:-]*/ { - yield method; } else if /[\w\d?:]+/ { // Gobble any other tokens that should not be highlighted }