summaryrefslogtreecommitdiffstats
path: root/compiler/cargo.vim
blob: 89c1cff110e2108bfae061e2f533d9db5e12ffa6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
" Vim compiler file
" Compiler:         Cargo Compiler
" Maintainer:       Damien Radtke <damienradtke@gmail.com>
" Latest Revision:  2014 Sep 18

if exists("current_compiler")
  finish
endif
let current_compiler = "cargo"

if exists(":CompilerSet") != 2
    command -nargs=* CompilerSet setlocal <args>
endif

CompilerSet errorformat&
CompilerSet makeprg=cargo\ $*

" Allow a configurable global Cargo.toml name. This makes it easy to
" support variations like 'cargo.toml'.
if !exists('g:cargo_toml_name')
    let g:cargo_toml_name = 'Cargo.toml'
endif

let s:toml_dir = fnamemodify(findfile(g:cargo_toml_name, '.;'), ':p:h').'/'

if s:toml_dir != ''
    augroup cargo
        au!
        au QuickfixCmdPost make call s:FixPaths()
    augroup END

    " FixPaths() is run after Cargo, and is used to change the file paths
    " to be relative to the current directory instead of Cargo.toml.
    function! s:FixPaths()
        let qflist = getqflist()
        for qf in qflist
            if !qf['valid']
                continue
            endif
            let filename = bufname(qf['bufnr'])
            if stridx(filename, s:toml_dir) == -1
                let filename = s:toml_dir.filename
            endif
            let qf['filename'] = simplify(s:toml_dir.bufname(qf['bufnr']))
            call remove(qf, 'bufnr')
        endfor
        call setqflist(qflist, 'r')
    endfunction
endif