summaryrefslogtreecommitdiffstats
path: root/autoload/cargo.vim
blob: ea63bd7a810dd75b5259da238cf86af084de1a9a (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rust') == -1
function! cargo#Load() 
    " Utility call to get this script loaded, for debugging
endfunction

function! cargo#cmd(args)
    execute "! cargo" a:args
endfunction

function! s:nearest_cargo(...) abort
    " If the second argument is not specified, the first argument determines
    " whether we will start from the current directory or the directory of the
    " current buffer, otherwise, we start with the provided path on the 
    " second argument.

    let l:is_getcwd = get(a:, 1, 0)
    if l:is_getcwd 
        let l:starting_path = get(a:, 2, getcwd())
    else
        let l:starting_path = get(a:, 2, expand('%:p:h'))
    endif

    return findfile('Cargo.toml', l:starting_path . ';')
endfunction

function! cargo#nearestCargo(is_getcwd) abort
    return s:nearest_cargo(a:is_getcwd)
endfunction

function! cargo#nearestWorkspaceCargo(is_getcwd) abort
    let l:nearest = s:nearest_cargo(a:is_getcwd)
    while l:nearest !=# ''
        for l:line in readfile(l:nearest, '', 0x100)
            if l:line =~# '\V[workspace]'
                return l:nearest
            endif
        endfor
        let l:next = fnamemodify(l:nearest, ':p:h:h')
        let l:nearest = s:nearest_cargo(0, l:next)
    endwhile
    return ''
endfunction

function! cargo#nearestRootCargo(is_getcwd) abort
    " Try to find a workspace Cargo.toml, and if not found, take the nearest
    " regular Cargo.toml
    let l:workspace_cargo = cargo#nearestWorkspaceCargo(a:is_getcwd)
    if l:workspace_cargo !=# ''
        return l:workspace_cargo
    endif
    return s:nearest_cargo(a:is_getcwd)
endfunction


function! cargo#build(args)
    call cargo#cmd("build " . a:args)
endfunction

function! cargo#clean(args)
    call cargo#cmd("clean " . a:args)
endfunction

function! cargo#doc(args)
    call cargo#cmd("doc " . a:args)
endfunction

function! cargo#new(args)
    call cargo#cmd("new " . a:args)
    cd `=a:args`
endfunction

function! cargo#init(args)
    call cargo#cmd("init " . a:args)
endfunction

function! cargo#run(args)
    call cargo#cmd("run " . a:args)
endfunction

function! cargo#test(args)
    call cargo#cmd("test " . a:args)
endfunction

function! cargo#bench(args)
    call cargo#cmd("bench " . a:args)
endfunction

function! cargo#runtarget(args)
    let l:filename = expand('%:p')
    let l:read_manifest = system('cargo read-manifest')
    let l:metadata = json_decode(l:read_manifest)
    let l:targets = get(l:metadata, 'targets', [])
    let l:did_run = 0
    for l:target in l:targets
        let l:src_path = get(l:target, 'src_path', '')
        let l:kinds = get(l:target, 'kind', [])
        let l:name = get(l:target, 'name', '')
        if l:src_path == l:filename
        if index(l:kinds, 'example') != -1
            let l:did_run = 1
            call cargo#run("--example " . shellescape(l:name) . " " . a:args)
            return
        elseif index(l:kinds, 'bin') != -1
            let l:did_run = 1
            call cargo#run("--bin " . shellescape(l:name) . " " . a:args)
            return
        endif
        endif
    endfor
    if l:did_run != 1
        call cargo#run(a:args)
        return
    endif
endfunction

" vim: set et sw=4 sts=4 ts=8:
endif