neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

cargo.vim (4059B)


      1 " Last Modified: 2023-09-11
      2 
      3 function! cargo#Load()
      4    " Utility call to get this script loaded, for debugging
      5 endfunction
      6 
      7 function! 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
     21 endfunction
     22 
     23 function! 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 . ';')
     37 endfunction
     38 
     39 function! cargo#nearestCargo(is_getcwd) abort
     40    return s:nearest_cargo(a:is_getcwd)
     41 endfunction
     42 
     43 function! 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 ''
     55 endfunction
     56 
     57 function! 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)
     65 endfunction
     66 
     67 
     68 function! cargo#build(args)
     69    call cargo#cmd("build " . a:args)
     70 endfunction
     71 
     72 function! cargo#check(args)
     73    call cargo#cmd("check " . a:args)
     74 endfunction
     75 
     76 function! cargo#clean(args)
     77    call cargo#cmd("clean " . a:args)
     78 endfunction
     79 
     80 function! cargo#doc(args)
     81    call cargo#cmd("doc " . a:args)
     82 endfunction
     83 
     84 function! cargo#new(args)
     85    call cargo#cmd("new " . a:args)
     86    cd `=a:args`
     87 endfunction
     88 
     89 function! cargo#init(args)
     90    call cargo#cmd("init " . a:args)
     91 endfunction
     92 
     93 function! cargo#run(args)
     94    call cargo#cmd("run " . a:args)
     95 endfunction
     96 
     97 function! cargo#test(args)
     98    call cargo#cmd("test " . a:args)
     99 endfunction
    100 
    101 function! cargo#bench(args)
    102    call cargo#cmd("bench " . a:args)
    103 endfunction
    104 
    105 function! cargo#update(args)
    106    call cargo#cmd("update " . a:args)
    107 endfunction
    108 
    109 function! cargo#search(args)
    110    call cargo#cmd("search " . a:args)
    111 endfunction
    112 
    113 function! cargo#publish(args)
    114    call cargo#cmd("publish " . a:args)
    115 endfunction
    116 
    117 function! cargo#install(args)
    118    call cargo#cmd("install " . a:args)
    119 endfunction
    120 
    121 function! 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
    147 endfunction
    148 
    149 " vim: set et sw=4 sts=4 ts=8: