-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.ps1
More file actions
80 lines (67 loc) · 3.39 KB
/
profile.ps1
File metadata and controls
80 lines (67 loc) · 3.39 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# --- PSReadLine Configuration ---
# This enhances the command-line editing experience.
if (Get-Module -ListAvailable -Name PSReadLine) {
if (-not (Get-Module -Name PSReadLine)) {
Import-Module PSReadLine -ErrorAction SilentlyContinue
}
Set-PSReadLineOption -BellStyle None
function Protect-HashBranchTokenForCompletion {
[string]$line = ''
[int]$cursor = 0
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if (-not $line -or $cursor -le 0) { return $false }
if ($cursor -gt $line.Length) { $cursor = $line.Length }
$prefix = $line.Substring(0, $cursor)
if ($prefix -notmatch '^\s*(?:git\s+switch|gsw)\b') { return $false }
$tokenStart = $prefix.Length
while ($tokenStart -gt 0 -and -not [char]::IsWhiteSpace($prefix[$tokenStart - 1])) {
$tokenStart--
}
if ($tokenStart -ge $prefix.Length) { return $false }
if ($prefix[$tokenStart] -ne '#') { return $false }
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($tokenStart, 1, '`#')
return $true
}
# Use Tab for menu completion and Shift+Tab to go backward.
# Workaround: in PowerShell, unescaped '#' starts a comment, so we
# auto-escape it for git switch/gsw branch completion.
Set-PSReadLineKeyHandler -Key Tab -ScriptBlock {
try { Protect-HashBranchTokenForCompletion | Out-Null } catch { Write-Verbose $_ }
[Microsoft.PowerShell.PSConsoleReadLine]::MenuComplete()
try { Protect-HashBranchTokenForCompletion | Out-Null } catch { Write-Verbose $_ }
}
Set-PSReadLineKeyHandler -Key "Shift+Tab" -Function TabCompletePrevious
}
# --- Add your custom modules directory to the PSModulePath ---
# This ensures PowerShell can find your 'git-aliases-extra' module.
$dotFilesRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$dotModules = Join-Path $dotFilesRoot 'modules'
$sep = [IO.Path]::PathSeparator
if ($env:PSModulePath -notlike "*$dotModules*") {
$env:PSModulePath = "$dotModules$sep$env:PSModulePath"
}
# --- Load Git Modules in the Correct Order ---
# 1. posh-git: Provides the core Git prompt and tab completion engine.
# It MUST be loaded first.
Import-Module posh-git -ErrorAction Stop
# 2. git-aliases: Provides the standard set of 'g' aliases (gco, gsw, etc.).
Import-Module git-aliases -ErrorAction Stop -DisableNameChecking
# Set the base 'g' alias to 'git' after modules that might define it as a function.
# This ensures 'g<tab>' works correctly.
if (Get-Command g -CommandType Function -ErrorAction SilentlyContinue) {
Remove-Item Function:\g -Force -ErrorAction SilentlyContinue
}
Set-Alias -Name g -Value git -Force
# 3. git-aliases-extra: Your custom module that DEPENDS on posh-git.
# It finds all aliases and registers the proxy completer.
$extrasManifest = Join-Path $dotFilesRoot 'modules\git-aliases-extra\git-aliases-extra.psd1'
if (-not (Get-Module -Name git-aliases-extra -ErrorAction SilentlyContinue)) {
if (Test-Path -LiteralPath $extrasManifest) {
Import-Module $extrasManifest -DisableNameChecking -ErrorAction Stop
} elseif (Test-Path -LiteralPath (Join-Path $dotFilesRoot '.gitmodules')) {
Write-Warning "git-aliases-extra is missing. Initialize submodules: git -C '$dotFilesRoot' submodule update --init --recursive"
} else {
Write-Warning "git-aliases-extra module not found at '$extrasManifest'."
}
}
# Write-Host "PowerShell profile loaded. Posh-git and custom alias completion are active." -ForegroundColor Green