]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/rust/cargo.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:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / ale_linters / rust / cargo.vim
1 " Author: Daniel Schemala <istjanichtzufassen@gmail.com>,
2 " Ivan Petkov <ivanppetkov@gmail.com>
3 " Description: rustc invoked by cargo for rust files
4
5 call ale#Set('rust_cargo_use_check', 1)
6 call ale#Set('rust_cargo_check_all_targets', 0)
7 call ale#Set('rust_cargo_check_examples', 0)
8 call ale#Set('rust_cargo_check_tests', 0)
9 call ale#Set('rust_cargo_avoid_whole_workspace', 1)
10 call ale#Set('rust_cargo_default_feature_behavior', 'default')
11 call ale#Set('rust_cargo_include_features', '')
12 call ale#Set('rust_cargo_use_clippy', 0)
13 call ale#Set('rust_cargo_clippy_options', '')
14 call ale#Set('rust_cargo_target_dir', '')
15
16 function! ale_linters#rust#cargo#GetCargoExecutable(bufnr) abort
17     if ale#path#FindNearestFile(a:bufnr, 'Cargo.toml') isnot# ''
18         return 'cargo'
19     else
20         " if there is no Cargo.toml file, we don't use cargo even if it exists,
21         " so we return '', because executable('') apparently always fails
22         return ''
23     endif
24 endfunction
25
26 function! ale_linters#rust#cargo#GetCwd(buffer) abort
27     if ale#Var(a:buffer, 'rust_cargo_avoid_whole_workspace')
28         let l:nearest_cargo = ale#path#FindNearestFile(a:buffer, 'Cargo.toml')
29         let l:nearest_cargo_dir = fnamemodify(l:nearest_cargo, ':h')
30
31         if l:nearest_cargo_dir isnot# '.'
32             return l:nearest_cargo_dir
33         endif
34     endif
35
36     return ''
37 endfunction
38
39 function! ale_linters#rust#cargo#GetCommand(buffer, version) abort
40     let l:use_check = ale#Var(a:buffer, 'rust_cargo_use_check')
41     \   && ale#semver#GTE(a:version, [0, 17, 0])
42     let l:use_all_targets = ale#Var(a:buffer, 'rust_cargo_check_all_targets')
43     \   && ale#semver#GTE(a:version, [0, 22, 0])
44     let l:use_examples = ale#Var(a:buffer, 'rust_cargo_check_examples')
45     \   && ale#semver#GTE(a:version, [0, 22, 0])
46     let l:use_tests = ale#Var(a:buffer, 'rust_cargo_check_tests')
47     \   && ale#semver#GTE(a:version, [0, 22, 0])
48     let l:target_dir = ale#Var(a:buffer, 'rust_cargo_target_dir')
49     let l:use_target_dir = !empty(l:target_dir)
50     \   && ale#semver#GTE(a:version, [0, 17, 0])
51
52     let l:include_features = ale#Var(a:buffer, 'rust_cargo_include_features')
53
54     if !empty(l:include_features)
55         let l:include_features = ' --features ' . ale#Escape(l:include_features)
56     endif
57
58     let l:default_feature_behavior = ale#Var(a:buffer, 'rust_cargo_default_feature_behavior')
59
60     if l:default_feature_behavior is# 'all'
61         let l:include_features = ''
62         let l:default_feature = ' --all-features'
63     elseif l:default_feature_behavior is# 'none'
64         let l:default_feature = ' --no-default-features'
65     else
66         let l:default_feature = ''
67     endif
68
69     let l:subcommand = l:use_check ? 'check' : 'build'
70     let l:clippy_options = ''
71
72     if ale#Var(a:buffer, 'rust_cargo_use_clippy')
73         let l:subcommand = 'clippy'
74         let l:clippy_options = ale#Var(a:buffer, 'rust_cargo_clippy_options')
75
76         if l:clippy_options =~# '^-- '
77             let l:clippy_options = join(split(l:clippy_options, '-- '))
78         endif
79
80         if l:clippy_options isnot# ''
81             let l:clippy_options = ' -- ' . l:clippy_options
82         endif
83     endif
84
85     return 'cargo '
86     \   . l:subcommand
87     \   . (l:use_all_targets ? ' --all-targets' : '')
88     \   . (l:use_examples ? ' --examples' : '')
89     \   . (l:use_tests ? ' --tests' : '')
90     \   . (l:use_target_dir ? (' --target-dir ' . ale#Escape(l:target_dir)) : '')
91     \   . ' --frozen --message-format=json -q'
92     \   . l:default_feature
93     \   . l:include_features
94     \   . l:clippy_options
95 endfunction
96
97 call ale#linter#Define('rust', {
98 \   'name': 'cargo',
99 \   'executable': function('ale_linters#rust#cargo#GetCargoExecutable'),
100 \   'cwd': function('ale_linters#rust#cargo#GetCwd'),
101 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
102 \       buffer,
103 \       ale_linters#rust#cargo#GetCargoExecutable(buffer),
104 \       '%e --version',
105 \       function('ale_linters#rust#cargo#GetCommand'),
106 \   )},
107 \   'callback': 'ale#handlers#rust#HandleRustErrors',
108 \   'output_stream': 'both',
109 \   'lint_file': 1,
110 \})