Skip to content

Commit

Permalink
Add powershell support, closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Dec 11, 2014
1 parent 23af3f7 commit 676e9dd
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Optionally download one of the [releases](https://github.com/sheerun/vim-polyglo
- [perl](https://github.com/vim-perl/vim-perl) (syntax, indent, ftplugin, ftdetect)
- [php](https://github.com/StanAngeloff/php.vim) (syntax)
- [puppet](https://github.com/rodjek/vim-puppet) (syntax, indent, ftplugin, ftdetect)
- [powershell](https://github.com/Persistent13/vim-ps1) (syntax, indent, ftplugin, ftdetect)
- [protobuf](https://github.com/uarun/vim-protobuf) (syntax, ftdetect)
- [python](https://github.com/mitsuhiko/vim-python-combined) (syntax, indent)
- [r-lang](https://github.com/vim-scripts/R.vim) (syntax, ftplugin)
Expand Down
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ PACKS="
perl:vim-perl/vim-perl
php:StanAngeloff/php.vim
puppet:rodjek/vim-puppet
powershell:Persistent13/vim-ps1
protobuf:uarun/vim-protobuf
python:mitsuhiko/vim-python-combined
r-lang:vim-scripts/R.vim
Expand Down
4 changes: 4 additions & 0 deletions ftdetect/polyglot.vim
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ endfunction
autocmd BufReadPost *.pl,*.pm,*.t call s:DetectPerl6()
autocmd BufNew,BufRead *.nqp setf perl6
autocmd BufNewFile,BufRead *.proto setfiletype proto
au BufNewFile,BufRead *.ps1 set ft=ps1
au BufNewFile,BufRead *.psd1 set ft=ps1
au BufNewFile,BufRead *.psm1 set ft=ps1
au BufNewFile,BufRead *.ps1xml set ft=ps1xml
au! BufRead,BufNewFile *.pp setfiletype puppet
au! BufRead,BufNewFile Puppetfile setfiletype ruby
function! s:setf(filetype) abort
Expand Down
34 changes: 34 additions & 0 deletions ftplugin/ps1.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
" Vim filetype plugin file
" Language: Windows PowerShell
" Maintainer: Peter Provost <[email protected]>
" Version: 2.10
" Project Repository: https://github.com/PProvost/vim-ps1
" Vim Script Page: http://www.vim.org/scripts/script.php?script_id=1327

" Only do this when not done yet for this buffer
if exists("b:did_ftplugin") | finish | endif

" Don't load another plug-in for this buffer
let b:did_ftplugin = 1

setlocal tw=0
setlocal commentstring=#%s
setlocal formatoptions=tcqro
" Enable autocompletion of hyphenated PowerShell commands,
" e.g. Get-Content or Get-ADUser
setlocal iskeyword+=-

" Change the browse dialog on Win32 to show mainly PowerShell-related files
if has("gui_win32")
let b:browsefilter =
\ "All PowerShell Files (*.ps1, *.psd1, *.psm1, *.ps1xml)\t*.ps1;*.psd1;*.psm1;*.ps1xml\n" .
\ "PowerShell Script Files (*.ps1)\t*.ps1\n" .
\ "PowerShell Module Files (*.psd1, *.psm1)\t*.psd1;*.psm1\n" .
\ "PowerShell XML Files (*.ps1xml)\t*.ps1xml\n" .
\ "All Files (*.*)\t*.*\n"
endif

" Undo the stuff we changed
let b:undo_ftplugin = "setlocal tw< cms< fo<" .
\ " | unlet! b:browsefilter"

31 changes: 31 additions & 0 deletions ftplugin/ps1xml.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
" Vim filetype plugin file
" Language: Windows PowerShell
" Maintainer: Peter Provost <[email protected]>
" Version: 2.10
" Project Repository: https://github.com/PProvost/vim-ps1
" Vim Script Page: http://www.vim.org/scripts/script.php?script_id=1327

" Only do this when not done yet for this buffer
if exists("b:did_ftplugin") | finish | endif

" Don't load another plug-in for this buffer
let b:did_ftplugin = 1

setlocal tw=0
setlocal commentstring=#%s
setlocal formatoptions=tcqro

" Change the browse dialog on Win32 to show mainly PowerShell-related files
if has("gui_win32")
let b:browsefilter =
\ "All PowerShell Files (*.ps1, *.psd1, *.psm1, *.ps1xml)\t*.ps1;*.psd1;*.psm1;*.ps1xml\n" .
\ "PowerShell Script Files (*.ps1)\t*.ps1\n" .
\ "PowerShell Module Files (*.psd1, *.psm1)\t*.psd1;*.psm1\n" .
\ "PowerShell XML Files (*.ps1xml)\t*.ps1xml\n" .
\ "All Files (*.*)\t*.*\n"
endif

" Undo the stuff we changed
let b:undo_ftplugin = "setlocal tw< cms< fo<" .
\ " | unlet! b:browsefilter"

20 changes: 20 additions & 0 deletions indent/ps1.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
" Vim indent file
" Language: Windows PowerShell
" Maintainer: Peter Provost <[email protected]>
" Version: 2.10
" Project Repository: https://github.com/PProvost/vim-ps1
" Vim Script Page: http://www.vim.org/scripts/script.php?script_id=1327"

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1

" smartindent is good enough for powershell
setlocal smartindent
" disable the indent removal for # marks
inoremap # X#
let b:undo_indent = "setl si<"

162 changes: 162 additions & 0 deletions syntax/ps1.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
" Vim syntax file
" Language: Windows PowerShell
" Maintainer: Peter Provost <[email protected]>
" Version: 2.10
" Project Repository: https://github.com/PProvost/vim-ps1
" Vim Script Page: http://www.vim.org/scripts/script.php?script_id=1327"
"
" The following settings are available for tuning syntax highlighting:
" let ps1_nofold_blocks = 1
" let ps1_nofold_sig = 1

" Compatible VIM syntax file start
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif

" PowerShell doesn't care about case
syn case ignore

" Sync-ing method
syn sync minlines=100

" Certain tokens can't appear at the top level of the document
syn cluster ps1NotTop contains=@ps1Comment,ps1CDocParam,ps1Function

" Comments and special comment words
syn keyword ps1CommentTodo TODO FIXME XXX TBD HACK NOTE contained
syn match ps1CDocParam /.*/ contained
syn match ps1CommentDoc /^\s*\zs\.\w\+\>/ nextgroup=ps1CDocParam contained
syn match ps1CommentDoc /#\s*\zs\.\w\+\>/ nextgroup=ps1CDocParam contained
syn match ps1Comment /#.*/ contains=ps1CommentTodo,ps1CommentDoc,@Spell
syn region ps1Comment start="<#" end="#>" contains=ps1CommentTodo,ps1CommentDoc,@Spell

" Language keywords and elements
syn keyword ps1Conditional if else elseif switch default
syn keyword ps1Repeat while for do until break continue foreach in
syn match ps1Repeat /\<foreach\>/ nextgroup=ps1Block skipwhite
syn match ps1Keyword /\<while\>/ nextgroup=ps1Block skipwhite
syn match ps1Keyword /\<where\>/ nextgroup=ps1Block skipwhite

syn keyword ps1Exception begin process end exit
syn keyword ps1Keyword try catch finally throw
syn keyword ps1Keyword return filter in trap param data dynamicparam
syn match ps1Keyword /&/
syn keyword ps1Constant $true $false $null
syn match ps1Constant +\$?+
syn match ps1Constant +\$_+
syn match ps1Constant +\$\$+
syn match ps1Constant +\$^+

" Keywords reserved for future use
syn keyword ps1Keyword class define from using var

" Functions and Cmdlets
syn match ps1Cmdlet /\w\+-\w\+/
syn keyword ps1Keyword function nextgroup=ps1Function skipwhite
syn keyword ps1Keyword filter nextgroup=ps1Function skipwhite
syn match ps1Function /\w\+-*\w*/ contained

" Type declarations
syn match ps1Type /\[[a-z0-9_:.]\+\(\[\]\)\?\]/
syn match ps1StandaloneType /[a-z0-9_.]\+/ contained
syn keyword ps1Scope global local private script contained

" Variables and other user defined items
syn match ps1Variable /\$\w\+/
syn match ps1Variable /\${\w\+:\\\w\+}/
syn match ps1ScopedVariable /\$\w\+:\w\+/ contains=ps1Scope
syn match ps1VariableName /\w\+/ contained

" Operators all start w/ dash
syn match ps1OperatorStart /-c\?/ nextgroup=ps1Operator
syn keyword ps1Operator eq ne ge gt lt le like notlike match notmatch replace split /contains/ notcontains contained
syn keyword ps1Operator ieq ine ige igt ile ilt ilike inotlike imatch inotmatch ireplace isplit icontains inotcontains contained
syn keyword ps1Operator ceq cne cge cgt clt cle clike cnotlike cmatch cnotmatch creplace csplit ccontains cnotcontains contained
syn keyword ps1Operator is isnot as join contained
syn keyword ps1Operator and or not xor band bor bnot bxor contained
syn keyword ps1Operator f contained

" Regular Strings
" These aren't precisely correct and could use some work
syn region ps1String start=/"/ skip=/`"/ end=/"/ contains=@ps1StringSpecial
syn region ps1String start=/'/ skip=/''/ end=/'/

" Here-Strings
syn region ps1String start=/@"$/ end=/^"@/ contains=@ps1StringSpecial
syn region ps1String start=/@'$/ end=/^'@/

" Interpolation
syn match ps1Escape /`./ contained
syn region ps1Interpolation matchgroup=ps1InterpolationDelimiter start="$(" end=")" contained contains=ALLBUT,@ps1NotTop
syn region ps1NestedParentheses start="(" skip="\\\\\|\\)" matchgroup=ps1Interpolation end=")" transparent contained
syn cluster ps1StringSpecial contains=ps1Escape,ps1Interpolation,ps1Variable,ps1Boolean,ps1Constant,ps1BuiltIn

" Numbers
syn match ps1Number "\<\(0[xX]\x\+\|\d\+\)\([KMGTP][B]\)\=\>"
syn match ps1Number "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[dD]\="
syn match ps1Number "\<\d\+[eE][-+]\=\d\+[dD]\=\>"
syn match ps1Number "\<\d\+\([eE][-+]\=\d\+\)\=[dD]\>"

" Constants
syn match ps1Boolean "$\%(true\|false\)\>"
syn match ps1Constant /\$null\>/
syn match ps1BuiltIn "$^\|$?\|$_\|$\$"
syn match ps1BuiltIn "$\%(args\|error\|foreach\|home\|input\)\>"
syn match ps1BuiltIn "$\%(match\(es\)\?\|myinvocation\|host\|lastexitcode\)\>"
syn match ps1BuiltIn "$\%(ofs\|shellid\|stacktrace\)\>"

" Folding blocks
if !exists('g:ps1_nofold_blocks')
syn region ps1Block start=/{/ end=/}/ transparent fold
endif

if !exists('g:ps1_nofold_sig')
syn region ps1Signature start=/# SIG # Begin signature block/ end=/# SIG # End signature block/ transparent fold
endif

" Setup default color highlighting
if version >= 508 || !exists("did_ps1_syn_inits")
if version < 508
let did_ps1_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif

HiLink ps1Number Number
HiLink ps1Block Block
HiLink ps1Exception Exception
HiLink ps1Constant Constant
HiLink ps1String String
HiLink ps1Escape SpecialChar
HiLink ps1InterpolationDelimiter Delimiter
HiLink ps1Conditional Conditional
HiLink ps1Function Function
HiLink ps1Variable Identifier
HiLink ps1ScopedVariable Identifier
HiLink ps1VariableName Identifier
HiLink ps1Boolean Boolean
HiLink ps1Constant Constant
HiLink ps1BuiltIn StorageClass
HiLink ps1Type Type
HiLink ps1Scope Type
HiLink ps1StandaloneType Type
HiLink ps1Number Number
HiLink ps1Comment Comment
HiLink ps1CommentTodo Todo
HiLink ps1CommentDoc Tag
HiLink ps1CDocParam Todo
HiLink ps1Operator Operator
HiLink ps1Repeat Repeat
HiLink ps1RepeatAndCmdlet Repeat
HiLink ps1Keyword Keyword
HiLink ps1KeywordAndCmdlet Keyword
HiLink ps1Cmdlet Statement
delcommand HiLink
endif

let b:current_syntax = "ps1"

56 changes: 56 additions & 0 deletions syntax/ps1xml.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
" Vim syntax file
" Language: Windows PowerShell XML
" Maintainer: Peter Provost <[email protected]>
" Version: 2.10
" Project Repository: https://github.com/PProvost/vim-ps1
" Vim Script Page: http://www.vim.org/scripts/script.php?script_id=1327"

" Compatible VIM syntax file start
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif

let s:ps1xml_cpo_save = &cpo
set cpo&vim

doau syntax xml
unlet b:current_syntax

syn case ignore
syn include @ps1xmlScriptBlock <sfile>:p:h/ps1.vim
unlet b:current_syntax

syn region ps1xmlScriptBlock
\ matchgroup=xmlTag start="<Script>"
\ matchgroup=xmlEndTag end="</Script>"
\ fold
\ contains=@ps1xmlScriptBlock
\ keepend
syn region ps1xmlScriptBlock
\ matchgroup=xmlTag start="<ScriptBlock>"
\ matchgroup=xmlEndTag end="</ScriptBlock>"
\ fold
\ contains=@ps1xmlScriptBlock
\ keepend
syn region ps1xmlScriptBlock
\ matchgroup=xmlTag start="<GetScriptBlock>"
\ matchgroup=xmlEndTag end="</GetScriptBlock>"
\ fold
\ contains=@ps1xmlScriptBlock
\ keepend
syn region ps1xmlScriptBlock
\ matchgroup=xmlTag start="<SetScriptBlock>"
\ matchgroup=xmlEndTag end="</SetScriptBlock>"
\ fold
\ contains=@ps1xmlScriptBlock
\ keepend

syn cluster xmlRegionHook add=ps1xmlScriptBlock

let b:current_syntax = "ps1xml"

let &cpo = s:ps1xml_cpo_save
unlet s:ps1xml_cpo_save

0 comments on commit 676e9dd

Please sign in to comment.