From 160c0b4ac0a0c588788167162e504ed8e048dbaf Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Tue, 27 Jan 2015 18:31:17 -0800 Subject: [PATCH] Replace vendor powershell.tmBundle by SublimeText powershell. Improve samples for PS. --- .gitmodules | 6 +- grammars.yml | 2 +- samples/PowerShell/ZLocation.psd1 | 116 ++++++++++++++++++++++++++++ samples/PowerShell/ZLocation.psm1 | 91 ++++++++++++++++++++++ samples/PowerShell/hello.ps1 | 2 - samples/PowerShell/hello.psm1 | 5 -- samples/PowerShell/history.ps1 | 65 ++++++++++++++++ vendor/grammars/powershell | 1 + vendor/grammars/powershell.tmbundle | 1 - 9 files changed, 277 insertions(+), 12 deletions(-) create mode 100644 samples/PowerShell/ZLocation.psd1 create mode 100644 samples/PowerShell/ZLocation.psm1 delete mode 100644 samples/PowerShell/hello.ps1 delete mode 100644 samples/PowerShell/hello.psm1 create mode 100644 samples/PowerShell/history.ps1 create mode 160000 vendor/grammars/powershell delete mode 160000 vendor/grammars/powershell.tmbundle diff --git a/.gitmodules b/.gitmodules index 77feab1252..19fb9472d4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -121,9 +121,9 @@ [submodule "vendor/grammars/Handlebars"] path = vendor/grammars/Handlebars url = https://github.com/daaain/Handlebars -[submodule "vendor/grammars/powershell.tmbundle"] - path = vendor/grammars/powershell.tmbundle - url = https://github.com/davidpeckham/powershell.tmbundle +[submodule "vendor/grammars/powershell"] + path = vendor/grammars/powershell + url = https://github.com/SublimeText/PowerShell [submodule "vendor/grammars/jade-tmbundle"] path = vendor/grammars/jade-tmbundle url = https://github.com/davidrios/jade-tmbundle diff --git a/grammars.yml b/grammars.yml index 6dbcb64718..e446a8faad 100644 --- a/grammars.yml +++ b/grammars.yml @@ -350,7 +350,7 @@ vendor/grammars/pike-textmate: - source.pike vendor/grammars/postscript.tmbundle: - source.postscript -vendor/grammars/powershell.tmbundle: +vendor/grammars/powershell: - source.powershell vendor/grammars/processing.tmbundle: - source.processing diff --git a/samples/PowerShell/ZLocation.psd1 b/samples/PowerShell/ZLocation.psd1 new file mode 100644 index 0000000000..fcf90ef900 --- /dev/null +++ b/samples/PowerShell/ZLocation.psd1 @@ -0,0 +1,116 @@ +# +# Module manifest for module 'ZLocation' +# +# Generated by: sevoroby +# +# Generated on: 12/10/2014 +# + +@{ + +# Script module or binary module file associated with this manifest. +RootModule = 'ZLocation.psm1' + +# Version number of this module. +ModuleVersion = '0.1' + +# ID used to uniquely identify this module +GUID = '18e8ca17-7f67-4f1c-85ff-159373bf66f5' + +# Author of this module +Author = 'Sergei Vorobev' + +# Company or vendor of this module +CompanyName = 'Microsoft' + +# Copyright statement for this module +Copyright = '(c) 2014 Sergei Vorobev. All rights reserved.' + +# Description of the functionality provided by this module +# Description = '' + +# Minimum version of the Windows PowerShell engine required by this module +# PowerShellVersion = '' + +# Name of the Windows PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the Windows PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module +# CLRVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +# RequiredModules = @() + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +# FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +NestedModules = @("ZLocation.Storage.psm1", "ZLocation.Search.psm1") + +# Functions to export from this module +FunctionsToExport = '*' + +# Cmdlets to export from this module +CmdletsToExport = '*' + +# Variables to export from this module +VariablesToExport = '*' + +# Aliases to export from this module +AliasesToExport = '*' + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +# HelpInfoURI = '' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} diff --git a/samples/PowerShell/ZLocation.psm1 b/samples/PowerShell/ZLocation.psm1 new file mode 100644 index 0000000000..5ab91146d6 --- /dev/null +++ b/samples/PowerShell/ZLocation.psm1 @@ -0,0 +1,91 @@ +# +# Weight function. +# +function Update-ZLocation([string]$path) +{ + $now = [datetime]::Now + if (Test-Path variable:global:__zlocation_current) + { + $prev = $global:__zlocation_current + $weight = $now.Subtract($prev.Time).TotalSeconds + Add-ZWeight ($prev.Location) $weight + } + + $global:__zlocation_current = @{ + Location = $path + Time = [datetime]::Now + } + + # populate folder immidiatly after the first cd + Add-ZWeight $path 0 +} + +# this approach hurts `cd` performance (0.0008 sec vs 0.025 sec). +# Consider replace it with OnIdle Event. +(Get-Variable pwd).attributes.Add((new-object ValidateScript { Update-ZLocation $_.Path; return $true })) +# +# End of weight function. +# + + +# +# Tab complention. +# +if (Test-Path Function:\TabExpansion) { + Rename-Item Function:\TabExpansion PreZTabExpansion +} + +function Get-EscapedPath +{ + param( + [Parameter( + Position=0, + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true) + ] + [string]$path + ) + + process { + if ($path.Contains(' ')) + { + return '"' + $path + '"' + } + return $path + } +} + +function global:TabExpansion($line, $lastWord) { + switch -regex ($line) { + "^(Set-ZLocation|z) .*" { + $arguments = $line -split ' ' | Where { $_.length -gt 0 } | select -Skip 1 + Find-Matches (Get-ZLocation) $arguments | Get-EscapedPath + } + default { + if (Test-Path Function:\PreZTabExpansion) { + PreZTabExpansion $line $lastWord + } + } + } +} +# +# End of tab completion. +# + +function Set-ZLocation() +{ + if (-not $args) { + $args = @() + } + $matches = Find-Matches (Get-ZLocation) $args + if ($matches) { + Push-Location ($matches | Select-Object -First 1) + } else { + Write-Warning "Cannot find matching location" + } +} + + +Set-Alias -Name z -Value Set-ZLocation +Export-ModuleMember -Function Set-ZLocation, Get-ZLocation -Alias z \ No newline at end of file diff --git a/samples/PowerShell/hello.ps1 b/samples/PowerShell/hello.ps1 deleted file mode 100644 index eca1e76cc2..0000000000 --- a/samples/PowerShell/hello.ps1 +++ /dev/null @@ -1,2 +0,0 @@ -# Hello world in powershell -Write-Host 'Hello World' \ No newline at end of file diff --git a/samples/PowerShell/hello.psm1 b/samples/PowerShell/hello.psm1 deleted file mode 100644 index 3db82f0187..0000000000 --- a/samples/PowerShell/hello.psm1 +++ /dev/null @@ -1,5 +0,0 @@ -# Hello World powershell module - -function hello() { - Write-Host 'Hello World' -} \ No newline at end of file diff --git a/samples/PowerShell/history.ps1 b/samples/PowerShell/history.ps1 new file mode 100644 index 0000000000..3161a3cbdd --- /dev/null +++ b/samples/PowerShell/history.ps1 @@ -0,0 +1,65 @@ +function Save-HistoryAll() { + $history = Get-History -Count $MaximumHistoryCount + [array]::Reverse($history) + $history = $history | Group CommandLine | Foreach {$_.Group[0]} + [array]::Reverse($history) + $history | Export-Csv $historyPath +} + +function Save-HistoryIncremental() { +# Get-History -Count $MaximumHistoryCount | Group CommandLine | Foreach {$_.Group[0]} | Export-Csv $historyPath + Get-History -Count 1 | Export-Csv -Append $historyPath +} + +# hook powershell's exiting event & hide the registration with -supportevent. +#Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { Save-History } + +$oldPrompt = Get-Content function:\prompt + +if( $oldPrompt -notlike '*Save-HistoryIncremental*' ) +{ + $newPrompt = @' +Save-HistoryIncremental + +'@ + $newPrompt += $oldPrompt + $function:prompt = [ScriptBlock]::Create($newPrompt) +} + +# load previous history, if it exists +if ((Test-Path $historyPath)) { + $loadTime = + ( + Measure-Command { + Import-Csv $historyPath | Add-History + Save-HistoryAll + Clear-History + Import-Csv $historyPath | ? {$count++;$true} | Add-History + } + ).totalseconds + Write-Host -Fore Green "`nLoaded $count history item(s) in $loadTime seconds.`n" +} + + +function Search-History() +{ + <# + .SYNOPSIS + Retrive and filter history based on query + .DESCRIPTION + .PARAMETER Name + .EXAMPLE + .LINK + #> + + param( + [string[]] $query + ) + + $history = Get-History -Count $MaximumHistoryCount + foreach ($item in $query){ + $item = $item.ToLower() + $history = $history | where {$_.CommandLine.ToLower().Contains($item)} + } + $history +} \ No newline at end of file diff --git a/vendor/grammars/powershell b/vendor/grammars/powershell new file mode 160000 index 0000000000..84fd97265c --- /dev/null +++ b/vendor/grammars/powershell @@ -0,0 +1 @@ +Subproject commit 84fd97265c93abcd52de5915b4cf1179cc508373 diff --git a/vendor/grammars/powershell.tmbundle b/vendor/grammars/powershell.tmbundle deleted file mode 160000 index f8716b432e..0000000000 --- a/vendor/grammars/powershell.tmbundle +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f8716b432eb0a1a6cbc93ee42451a443db844c0f