cargo.vim 4.0 KB

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