]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/powershell/powershell.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Do not set EDITOR/VISUAL for shell
[etc/vim.git] / .vim / bundle / ale / ale_linters / powershell / powershell.vim
1 " Author: Jesse Harris - https://github.com/zigford
2 " Description: This file adds support for powershell scripts synatax errors
3
4 call ale#Set('powershell_powershell_executable', 'pwsh')
5
6 function! ale_linters#powershell#powershell#GetExecutable(buffer) abort
7     return ale#Var(a:buffer, 'powershell_powershell_executable')
8 endfunction
9
10 " Some powershell magic to show syntax errors without executing the script
11 " thanks to keith hill:
12 " https://rkeithhill.wordpress.com/2007/10/30/powershell-quicktip-preparsing-scripts-to-check-for-syntax-errors/
13 function! ale_linters#powershell#powershell#GetCommand(buffer) abort
14     let l:script = ['Param($Script);
15     \   $ErrorView = "Normal";
16     \   trap {$_;continue} & {
17     \   $Contents = Get-Content -Path $Script;
18     \   $Contents = [string]::Join([Environment]::NewLine, $Contents);
19     \   [void]$ExecutionContext.InvokeCommand.NewScriptBlock($Contents);
20     \   };']
21
22     return ale#powershell#RunPowerShell(
23     \   a:buffer, 'powershell_powershell', l:script)
24 endfunction
25
26 " Parse powershell error output using regex into a list of dicts
27 function! ale_linters#powershell#powershell#Handle(buffer, lines) abort
28     let l:output = []
29     " Our 3 patterns we need to scrape the data for the dicts
30     let l:patterns = [
31     \   '\v^At line:(\d+) char:(\d+)',
32     \   '\v^(At|\+| )@!.*',
33     \   '\vFullyQualifiedErrorId : (\w+)',
34     \]
35
36     let l:matchcount = 0
37
38     for l:match in ale#util#GetMatches(a:lines, l:patterns)
39         " We want to work with 3 matches per syntax error
40         let l:matchcount = l:matchcount + 1
41
42         if l:matchcount == 1 || str2nr(l:match[1])
43             " First match consists of 2 capture groups, and
44             " can capture the line and col
45             if exists('l:item')
46                 " We may be here because the last syntax
47                 " didn't emit a code, and so only had 2
48                 " matches
49                 call add(l:output, l:item)
50                 let l:matchcount = 1
51             endif
52
53             " If the match is 0, it was a failed match
54             " probably due to an unexpected token which
55             " contained a newline. Reset matchcount. to
56             " continue to the next match
57             if !empty(l:match[1])
58                 let l:item = {
59                 \   'lnum': str2nr(l:match[1]),
60                 \   'col': str2nr(l:match[2]),
61                 \   'type': 'E',
62                 \}
63             else
64                 let l:matchcount = 0
65             endif
66         elseif l:matchcount == 2
67             " Second match[0] grabs the full line in order
68             " to handles the text
69             let l:item['text'] = l:match[0]
70         else
71             " Final match handles the code, however
72             " powershell only emits 1 code for all errors
73             " so, we get the final code on the last error
74             " and loop over the previously added items to
75             " append the code we now know
76             call add(l:output, l:item)
77             unlet l:item
78
79             if len(l:match[1]) > 0
80                 for l:i in l:output
81                     let l:i['code'] = l:match[1]
82                 endfor
83             endif
84
85             " Reset the matchcount so we can begin gathering
86             " matches for the next syntax error
87             let l:matchcount = 0
88         endif
89     endfor
90
91     return l:output
92 endfunction
93
94 call ale#linter#Define('powershell', {
95 \   'name': 'powershell',
96 \   'executable': function('ale_linters#powershell#powershell#GetExecutable'),
97 \   'command': function('ale_linters#powershell#powershell#GetCommand'),
98 \   'output_stream': 'stdout',
99 \   'callback': 'ale_linters#powershell#powershell#Handle',
100 \})