Permalink
Browse files

Export TabExpansion from module instead of defining in profile

  • Loading branch information...
1 parent bcdef7a commit 36d61ca85cd6e241b8cd8068eb45699dc40101e7 @dahlbyk committed Jan 26, 2012
Showing with 18 additions and 20 deletions.
  1. +17 −0 GitTabExpansion.ps1
  2. +1 −1 posh-git.psm1
  3. +0 −19 profile.example.ps1
View
17 GitTabExpansion.ps1
@@ -208,3 +208,20 @@ function GitTabExpansion($lastBlock) {
}
}
}
+
+if (Test-Path Function:\TabExpansion) {
+ Rename-Item Function:\TabExpansion TabExpansionBackup
+}
+
+function TabExpansion($line, $lastWord) {
+ $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
+
+ switch -regex ($lastBlock) {
+ # Execute git tab completion for all git-related commands
+ "^$(Get-AliasPattern git) (.*)" { GitTabExpansion $lastBlock }
+ "^$(Get-AliasPattern tgit) (.*)" { GitTabExpansion $lastBlock }
+
+ # Fall back on existing tab expansion
+ default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
+ }
+}
View
2 posh-git.psm1
@@ -12,7 +12,7 @@ Export-ModuleMember -Function @(
'Get-GitStatus',
'Enable-GitColors',
'Get-GitDirectory',
- 'GitTabExpansion',
+ 'TabExpansion',
'Get-AliasPattern',
'Start-SshAgent',
'Stop-SshAgent',
View
19 profile.example.ps1
@@ -23,25 +23,6 @@ function prompt {
return "> "
}
-if(Test-Path Function:\TabExpansion) {
- $teBackup = 'posh-git_DefaultTabExpansion'
- if(!(Test-Path Function:\$teBackup)) {
- Rename-Item Function:\TabExpansion $teBackup
- }
-
- # Set up tab expansion and include git expansion
- function TabExpansion($line, $lastWord) {
- $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
- switch -regex ($lastBlock) {
- # Execute git tab completion for all git-related commands
- "^$(Get-AliasPattern git) (.*)" { GitTabExpansion $lastBlock }
- "^$(Get-AliasPattern tgit) (.*)" { GitTabExpansion $lastBlock }
- # Fall back on existing tab expansion
- default { & $teBackup $line $lastWord }
- }
- }
-}
-
Enable-GitColors
Pop-Location

5 comments on commit 36d61ca

@chaliy

In PsGet I also using similar technic to modify TabExpansion from module. However I am concerned of what situation when module is unloaded with Remove-Module? TabExpansion will remain changed. This always occurs when PsGet updates module. It forces it to reload. I am trying to revert it back in $Module.OnRemove event. But this will make things even worth if something already changed TabExpansion.

Idea that you use with Prompt seems to be much more reliable. If everybody can agree on common name of the global variable that will keep all prompts and remove them in $Module.OnRemove event, looks quite promising.

What do you think?

@shanselman

I think suggesting a prompt reload when loading and unloading major stuff is a reasonable thing to do.

@dahlbyk
Owner

I started down the path of a global tab expansion "registry" but didn't like how it was turning out. Can try again, but I tend to agree with Scott - I wouldn't expect a clean uninstall for a module that customizes several key pieces of the PowerShell environment.

@dahlbyk
Owner

One other point: this implementation essentially creates a closure around the current TabExpansion function at the time the module is imported. It doesn't matter if that's the default expansion, or a custom implementation from another module - we'll use whatever we find as a fallback.

@chaliy

Ok, clear :).

Please sign in to comment.