robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
2dd5e472df
master
1" 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. 10let args = substitute ( a: args , '\s\+$' , '' , '' ) 11if exists ( 'g:cargo_shell_command_runner' ) 12let cmd = g: cargo_shell_command_runner 13elseif has ( 'terminal' ) 14let cmd = 'terminal' 15elseif has ( 'nvim' ) 16let cmd = 'noautocmd new | terminal' 17else 18let cmd = '!' 19endif 20execute 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 29let l: is_getcwd = get ( a: , 1 , 0 ) 30if l: is_getcwd 31let l: starting_path = get ( a: , 2 , getcwd ()) 32else 33let l: starting_path = get ( a: , 2 , expand ( '%:p:h' )) 34endif 35 36return findfile ( 'Cargo.toml' , l: starting_path . ';' ) 37endfunction 38 39function ! cargo#nearestCargo ( is_getcwd ) abort 40return s: nearest_cargo ( a: is_getcwd ) 41endfunction 42 43function ! cargo#nearestWorkspaceCargo ( is_getcwd ) abort 44let l: nearest = s: nearest_cargo ( a: is_getcwd ) 45while l: nearest !=# '' 46for l: line in readfile ( l: nearest , '' , 0x100 ) 47if l: line =~# '\V[workspace]' 48return l: nearest 49endif 50endfor 51let l: next = fnamemodify ( l: nearest , ':p:h:h' ) 52let l: nearest = s: nearest_cargo ( 0 , l: next ) 53endwhile 54return '' 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 60let l: workspace_cargo = cargo#nearestWorkspaceCargo ( a: is_getcwd ) 61if l: workspace_cargo !=# '' 62return l: workspace_cargo 63endif 64return s: nearest_cargo ( a: is_getcwd ) 65endfunction 66 67 68function ! cargo#build ( args ) 69call cargo#cmd ( "build " . a: args ) 70endfunction 71 72function ! cargo#check ( args ) 73call cargo#cmd ( "check " . a: args ) 74endfunction 75 76function ! cargo#clean ( args ) 77call cargo#cmd ( "clean " . a: args ) 78endfunction 79 80function ! cargo#doc ( args ) 81call cargo#cmd ( "doc " . a: args ) 82endfunction 83 84function ! cargo#new ( args ) 85call cargo#cmd ( "new " . a: args ) 86cd `=a:args` 87endfunction 88 89function ! cargo#init ( args ) 90call cargo#cmd ( "init " . a: args ) 91endfunction 92 93function ! cargo#run ( args ) 94call cargo#cmd ( "run " . a: args ) 95endfunction 96 97function ! cargo#test ( args ) 98call cargo#cmd ( "test " . a: args ) 99endfunction 100 101function ! cargo#bench ( args ) 102call cargo#cmd ( "bench " . a: args ) 103endfunction 104 105function ! cargo#update ( args ) 106call cargo#cmd ( "update " . a: args ) 107endfunction 108 109function ! cargo#search ( args ) 110call cargo#cmd ( "search " . a: args ) 111endfunction 112 113function ! cargo#publish ( args ) 114call cargo#cmd ( "publish " . a: args ) 115endfunction 116 117function ! cargo#install ( args ) 118call cargo#cmd ( "install " . a: args ) 119endfunction 120 121function ! cargo#runtarget ( args ) 122let l: filename = expand ( '%:p' ) 123let l: read_manifest = system ( 'cargo read-manifest' ) 124let l: metadata = json_decode ( l: read_manifest ) 125let l: targets = get ( l: metadata , 'targets' , []) 126let l: did_run = 0 127for l: target in l: targets 128let l: src_path = get ( l: target , 'src_path' , '' ) 129let l: kinds = get ( l: target , 'kind' , []) 130let l: name = get ( l: target , 'name' , '' ) 131if l: src_path == l: filename 132if index ( l: kinds , 'example' ) != -1 133let l: did_run = 1 134call cargo#run ( "--example " . shellescape ( l: name ) . " " . a: args ) 135return 136elseif index ( l: kinds , 'bin' ) != -1 137let l: did_run = 1 138call cargo#run ( "--bin " . shellescape ( l: name ) . " " . a: args ) 139return 140endif 141endif 142endfor 143if l: did_run != 1 144call cargo#run ( a: args ) 145return 146endif 147endfunction 148 149" vim: set et sw=4 sts=4 ts=8: