1 # Copyright © 2025, Tais Hjortshøj <tbh@mjolner.dk> / Mjølner Informatics A/S
2 # SPDX-License-Identifier: Apache-2.0
3 
4 # region custom west board finder initialize
5 $s = {
6     param($wordToComplete, $commandAst, $cursorPosition)
7 
Get-MatchingBoards()8     function Get-MatchingBoards {
9         param($wordToComplete)
10         west boards | Out-String | ForEach-Object {
11              $_ -split '\r?\n' | Where-Object { $_ -CMatch "^$wordToComplete.*" } | Sort-Object
12         }
13     }
14 
15     $commandDecider = (($commandAst -split ' ') | Select-Object -First 2 -ExpandProperty $_) -join ' '
16     if ($commandDecider -eq 'west build') {
17 
18         $argDecider = (($commandAst -split ' ') | Select-Object -Last 2)
19 
20         if ($argDecider -contains '-b' -or $argDecider -contains '--board') {
21             $boardsFound = Get-MatchingBoards -wordToComplete $wordToComplete
22             $output = $boardsFound
23         } else {
24             # Fallback to default behavior of suggesting files in the current directory
25             $output = (Get-NexusRepository).Name
26         }
27     } else {
28         # Fallback to default behavior of suggesting files in the current directory
29         $output = (Get-NexusRepository).Name
30     }
31 
32     # Uncomment the following lines to log the output for debugging purposes
33     # @("wordToComplete:  $wordToComplete",
34     # "commandAst:        $commandAst",
35     # "cursorPosition:    $cursorPosition",
36     # "commandDecider:    $commandDecider",
37     # "argDecider:        $argDecider",
38     # "",
39     # "boardsFound:",
40     # ($boardsFound | ForEach-Object { $_ -split ' '})
41     # ) | Set-Content log.txt
42 
43     $output
44 }
45 Register-ArgumentCompleter -Native -CommandName west -ScriptBlock $s
46 echo "West completion tool loaded"
47 # endregion
48