-
Notifications
You must be signed in to change notification settings - Fork 3
/
mat.complete.ps1
64 lines (53 loc) · 3.02 KB
/
mat.complete.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$MatPythonCommand = "python .\mat.py"
$MatCommandAlias = "mat"
Function mat {
Invoke-Expression "$MatPythonCommand $args"
}
$MatArgCompleteScriptBlock = {
param($wordToComplete, $commandAst, $cursorPosition)
# In case of scripts, this object hold the current line after string conversion
$line = "$commandAst"
# The behaviour of completion should depend on the trailing spaces in the current line:
# * "command subcommand " --> TAB --> Completion items parameters/sub-subcommands of "subcommand"
# * "command subcom" --> TAB --> Completion items to extend "subcom" into matching subcommands.
# $line never contains the trailing spaces. However, $cursorPosition is the length of the original
# line (with trailing spaces) in this case. This comparision allows the expected user experience.
if ($cursorPosition -gt $line.Length) {
$line = "$line "
}
# Mock bash with environment variable settings
New-Item -Path Env: -Name _ARGCOMPLETE -Value 1 | Out-Null # Enables tab complition in argcomplete
New-Item -Path Env: -Name COMP_TYPE -Value 9 | Out-Null # Constant
New-Item -Path Env: -Name _ARGCOMPLETE_IFS -Value " " | Out-Null # Separator of the items
New-Item -Path Env: -Name _ARGCOMPLETE_SUPPRESS_SPACE -Value 1 | Out-Null # Constant
New-Item -Path Env: -Name _ARGCOMPLETE_COMP_WORDBREAKS -Value "" | Out-Null # Constant
New-Item -Path Env: -Name COMP_POINT -Value $cursorPosition | Out-Null # Refers to the last character of the current line
New-Item -Path Env: -Name COMP_LINE -Value $line | Out-Null # Current line
# Set a special environment variable to mark that the completion is executed by PowerShell
New-Item -Path Env: -Name _ARGCOMPLETE_POWERSHELL -Value 1 | Out-Null
# Just call the script without any parameter
# Since the environment variables are set, the argcomplete.autocomplete(...) function will be executed.
# The result will be printed on the standard output (see the details in the Python file).
Invoke-Expression $MatPythonCommand -OutVariable completionResult -ErrorVariable errorOut -ErrorAction SilentlyContinue | Out-Null
# Delete environment variables
Remove-Item Env:\_ARGCOMPLETE | Out-Null
Remove-Item Env:\COMP_TYPE | Out-Null
Remove-Item Env:\_ARGCOMPLETE_IFS | Out-Null
Remove-Item Env:\_ARGCOMPLETE_SUPPRESS_SPACE | Out-Null
Remove-Item Env:\_ARGCOMPLETE_COMP_WORDBREAKS | Out-Null
Remove-Item Env:\COMP_POINT | Out-Null
Remove-Item Env:\COMP_LINE | Out-Null
Remove-Item Env:\_ARGCOMPLETE_POWERSHELL | Out-Null
# If there is only one completion item, it will be immediately used. In this case
# a trailing space is important to show the user that the complition for the current
# item is ready.
$items = $completionResult.Split()
if ($items -eq $completionResult) {
"$items "
}
else {
$items
}
}
# Register tab completion for the mat command
Register-ArgumentCompleter -Native -CommandName $MatCommandAlias -ScriptBlock $MatArgCompleteScriptBlock