]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/ruby.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:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / ruby.vim
1 " Author: Eddie Lebow https://github.com/elebow
2 " Description: Functions for integrating with Ruby tools
3
4 " Find the nearest dir containing "app", "db", and "config", and assume it is
5 " the root of a Rails app.
6 function! ale#ruby#FindRailsRoot(buffer) abort
7     for l:name in ['app', 'config', 'db']
8         let l:dir = fnamemodify(
9         \   ale#path#FindNearestDirectory(a:buffer, l:name),
10         \   ':h:h'
11         \)
12
13         if l:dir isnot# '.'
14         \&& isdirectory(l:dir . '/app')
15         \&& isdirectory(l:dir . '/config')
16         \&& isdirectory(l:dir . '/db')
17             return l:dir
18         endif
19     endfor
20
21     return ''
22 endfunction
23
24 " Find the nearest dir containing a potential ruby project.
25 function! ale#ruby#FindProjectRoot(buffer) abort
26     let l:dir = ale#ruby#FindRailsRoot(a:buffer)
27
28     if isdirectory(l:dir)
29         return l:dir
30     endif
31
32     for l:name in ['.solargraph.yml', 'Rakefile', 'Gemfile']
33         let l:dir = fnamemodify(
34         \   ale#path#FindNearestFile(a:buffer, l:name),
35         \   ':h'
36         \)
37
38         if l:dir isnot# '.' && isdirectory(l:dir)
39             return l:dir
40         endif
41     endfor
42
43     return ''
44 endfunction
45
46 " Handle output from rubocop and linters that depend on it (e.b. standardrb)
47 function! ale#ruby#HandleRubocopOutput(buffer, lines) abort
48     try
49         let l:errors = json_decode(a:lines[0])
50     catch
51         return []
52     endtry
53
54     if !has_key(l:errors, 'summary')
55     \|| l:errors['summary']['offense_count'] == 0
56     \|| empty(l:errors['files'])
57         return []
58     endif
59
60     let l:output = []
61
62     for l:error in l:errors['files'][0]['offenses']
63         let l:start_col = l:error['location']['column'] + 0
64         call add(l:output, {
65         \   'lnum': l:error['location']['line'] + 0,
66         \   'col': l:start_col,
67         \   'end_col': l:start_col + l:error['location']['length'] - 1,
68         \   'code': l:error['cop_name'],
69         \   'text': l:error['message'],
70         \   'type': ale_linters#ruby#rubocop#GetType(l:error['severity']),
71         \})
72     endfor
73
74     return l:output
75 endfunction
76
77 function! ale#ruby#EscapeExecutable(executable, bundle_exec) abort
78     let l:exec_args = a:executable =~? 'bundle'
79     \   ? ' exec ' . a:bundle_exec
80     \   : ''
81
82     return ale#Escape(a:executable) . l:exec_args
83 endfunction