blob: 6696b3105f14a237c7247326bbb259e15fb50251 [file] [log] [blame]
Gregory Andersfc935942023-09-12 13:23:38 -05001" Last Modified: 2023-09-11
2
3function! cargo#Load()
4 " Utility call to get this script loaded, for debugging
5endfunction
6
7function! cargo#cmd(args) abort
8 " Trim trailing spaces. This is necessary since :terminal command parses
9 " trailing spaces as an empty argument.
10 let args = substitute(a:args, '\s\+$', '', '')
11 if exists('g:cargo_shell_command_runner')
12 let cmd = g:cargo_shell_command_runner
13 elseif has('terminal')
14 let cmd = 'terminal'
15 elseif has('nvim')
16 let cmd = 'noautocmd new | terminal'
17 else
18 let cmd = '!'
19 endif
20 execute cmd 'cargo' args
21endfunction
22
23function! s:nearest_cargo(...) abort
24 " If the second argument is not specified, the first argument determines
25 " whether we will start from the current directory or the directory of the
26 " current buffer, otherwise, we start with the provided path on the
27 " second argument.
28
29 let l:is_getcwd = get(a:, 1, 0)
30 if l:is_getcwd
31 let l:starting_path = get(a:, 2, getcwd())
32 else
33 let l:starting_path = get(a:, 2, expand('%:p:h'))
34 endif
35
36 return findfile('Cargo.toml', l:starting_path . ';')
37endfunction
38
39function! cargo#nearestCargo(is_getcwd) abort
40 return s:nearest_cargo(a:is_getcwd)
41endfunction
42
43function! cargo#nearestWorkspaceCargo(is_getcwd) abort
44 let l:nearest = s:nearest_cargo(a:is_getcwd)
45 while l:nearest !=# ''
46 for l:line in readfile(l:nearest, '', 0x100)
47 if l:line =~# '\V[workspace]'
48 return l:nearest
49 endif
50 endfor
51 let l:next = fnamemodify(l:nearest, ':p:h:h')
52 let l:nearest = s:nearest_cargo(0, l:next)
53 endwhile
54 return ''
55endfunction
56
57function! cargo#nearestRootCargo(is_getcwd) abort
58 " Try to find a workspace Cargo.toml, and if not found, take the nearest
59 " regular Cargo.toml
60 let l:workspace_cargo = cargo#nearestWorkspaceCargo(a:is_getcwd)
61 if l:workspace_cargo !=# ''
62 return l:workspace_cargo
63 endif
64 return s:nearest_cargo(a:is_getcwd)
65endfunction
66
67
68function! cargo#build(args)
69 call cargo#cmd("build " . a:args)
70endfunction
71
72function! cargo#check(args)
73 call cargo#cmd("check " . a:args)
74endfunction
75
76function! cargo#clean(args)
77 call cargo#cmd("clean " . a:args)
78endfunction
79
80function! cargo#doc(args)
81 call cargo#cmd("doc " . a:args)
82endfunction
83
84function! cargo#new(args)
85 call cargo#cmd("new " . a:args)
86 cd `=a:args`
87endfunction
88
89function! cargo#init(args)
90 call cargo#cmd("init " . a:args)
91endfunction
92
93function! cargo#run(args)
94 call cargo#cmd("run " . a:args)
95endfunction
96
97function! cargo#test(args)
98 call cargo#cmd("test " . a:args)
99endfunction
100
101function! cargo#bench(args)
102 call cargo#cmd("bench " . a:args)
103endfunction
104
105function! cargo#update(args)
106 call cargo#cmd("update " . a:args)
107endfunction
108
109function! cargo#search(args)
110 call cargo#cmd("search " . a:args)
111endfunction
112
113function! cargo#publish(args)
114 call cargo#cmd("publish " . a:args)
115endfunction
116
117function! cargo#install(args)
118 call cargo#cmd("install " . a:args)
119endfunction
120
121function! cargo#runtarget(args)
122 let l:filename = expand('%:p')
123 let l:read_manifest = system('cargo read-manifest')
124 let l:metadata = json_decode(l:read_manifest)
125 let l:targets = get(l:metadata, 'targets', [])
126 let l:did_run = 0
127 for l:target in l:targets
128 let l:src_path = get(l:target, 'src_path', '')
129 let l:kinds = get(l:target, 'kind', [])
130 let l:name = get(l:target, 'name', '')
131 if l:src_path == l:filename
132 if index(l:kinds, 'example') != -1
133 let l:did_run = 1
134 call cargo#run("--example " . shellescape(l:name) . " " . a:args)
135 return
136 elseif index(l:kinds, 'bin') != -1
137 let l:did_run = 1
138 call cargo#run("--bin " . shellescape(l:name) . " " . a:args)
139 return
140 endif
141 endif
142 endfor
143 if l:did_run != 1
144 call cargo#run(a:args)
145 return
146 endif
147endfunction
148
149" vim: set et sw=4 sts=4 ts=8: