Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rust.vim 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. " Author: Kevin Ballard
  2. " Description: Helper functions for Rust commands/mappings
  3. " Last Modified: May 27, 2014
  4. " Jump {{{1
  5. function! rust#Jump(mode, function) range
  6. let cnt = v:count1
  7. normal! m'
  8. if a:mode ==# 'v'
  9. norm! gv
  10. endif
  11. let foldenable = &foldenable
  12. set nofoldenable
  13. while cnt > 0
  14. execute "call <SID>Jump_" . a:function . "()"
  15. let cnt = cnt - 1
  16. endwhile
  17. let &foldenable = foldenable
  18. endfunction
  19. function! s:Jump_Back()
  20. call search('{', 'b')
  21. keepjumps normal! w99[{
  22. endfunction
  23. function! s:Jump_Forward()
  24. normal! j0
  25. call search('{', 'b')
  26. keepjumps normal! w99[{%
  27. call search('{')
  28. endfunction
  29. " Run {{{1
  30. function! rust#Run(bang, args)
  31. if a:bang
  32. let idx = index(a:args, '--')
  33. if idx != -1
  34. let rustc_args = idx == 0 ? [] : a:args[:idx-1]
  35. let args = a:args[idx+1:]
  36. else
  37. let rustc_args = a:args
  38. let args = []
  39. endif
  40. else
  41. let rustc_args = []
  42. let args = a:args
  43. endif
  44. let b:rust_last_rustc_args = rustc_args
  45. let b:rust_last_args = args
  46. call s:WithPath(function("s:Run"), rustc_args, args)
  47. endfunction
  48. function! s:Run(path, rustc_args, args)
  49. try
  50. let exepath = tempname()
  51. if has('win32')
  52. let exepath .= '.exe'
  53. endif
  54. let rustc_args = [a:path, '-o', exepath] + a:rustc_args
  55. let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
  56. let output = system(shellescape(rustc) . " " . join(map(rustc_args, 'shellescape(v:val)')))
  57. if output != ''
  58. echohl WarningMsg
  59. echo output
  60. echohl None
  61. endif
  62. if !v:shell_error
  63. exe '!' . shellescape(exepath) . " " . join(map(a:args, 'shellescape(v:val)'))
  64. endif
  65. finally
  66. if exists("exepath")
  67. silent! call delete(exepath)
  68. endif
  69. endtry
  70. endfunction
  71. " Expand {{{1
  72. function! rust#Expand(bang, args)
  73. if a:bang && !empty(a:args)
  74. let pretty = a:args[0]
  75. let args = a:args[1:]
  76. else
  77. let pretty = "expanded"
  78. let args = a:args
  79. endif
  80. call s:WithPath(function("s:Expand"), pretty, args)
  81. endfunction
  82. function! s:Expand(path, pretty, args)
  83. try
  84. let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
  85. let args = [a:path, '--pretty', a:pretty] + a:args
  86. let output = system(shellescape(rustc) . " " . join(map(args, "shellescape(v:val)")))
  87. if v:shell_error
  88. echohl WarningMsg
  89. echo output
  90. echohl None
  91. else
  92. new
  93. silent put =output
  94. 1
  95. d
  96. setl filetype=rust
  97. setl buftype=nofile
  98. setl bufhidden=hide
  99. setl noswapfile
  100. endif
  101. endtry
  102. endfunction
  103. function! rust#CompleteExpand(lead, line, pos)
  104. if a:line[: a:pos-1] =~ '^RustExpand!\s*\S*$'
  105. " first argument and it has a !
  106. let list = ["normal", "expanded", "typed", "expanded,identified", "flowgraph="]
  107. if !empty(a:lead)
  108. call filter(list, "v:val[:len(a:lead)-1] == a:lead")
  109. endif
  110. return list
  111. endif
  112. return glob(escape(a:lead, "*?[") . '*', 0, 1)
  113. endfunction
  114. " Emit {{{1
  115. function! rust#Emit(type, args)
  116. call s:WithPath(function("s:Emit"), a:type, a:args)
  117. endfunction
  118. function! s:Emit(path, type, args)
  119. try
  120. let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
  121. let args = [a:path, '--emit', a:type, '-o', '-'] + a:args
  122. let output = system(shellescape(rustc) . " " . join(map(args, "shellescape(v:val)")))
  123. if v:shell_error
  124. echohl WarningMsg
  125. echo output
  126. echohl None
  127. else
  128. new
  129. silent put =output
  130. 1
  131. d
  132. if a:type == "ir"
  133. setl filetype=llvm
  134. elseif a:type == "asm"
  135. setl filetype=asm
  136. endif
  137. setl buftype=nofile
  138. setl bufhidden=hide
  139. setl noswapfile
  140. endif
  141. endtry
  142. endfunction
  143. " Utility functions {{{1
  144. function! s:WithPath(func, ...)
  145. try
  146. let save_write = &write
  147. set write
  148. let path = expand('%')
  149. let pathisempty = empty(path)
  150. if pathisempty || !save_write
  151. " use a temporary file named 'unnamed.rs' inside a temporary
  152. " directory. This produces better error messages
  153. let tmpdir = tempname()
  154. call mkdir(tmpdir)
  155. let save_cwd = getcwd()
  156. silent exe 'lcd' fnameescape(tmpdir)
  157. let path = 'unnamed.rs'
  158. let save_mod = &mod
  159. set nomod
  160. silent exe 'keepalt write! ' . fnameescape(path)
  161. if pathisempty
  162. silent keepalt 0file
  163. endif
  164. else
  165. update
  166. endif
  167. call call(a:func, [path] + a:000)
  168. finally
  169. if exists("save_mod") | let &mod = save_mod | endif
  170. if exists("save_write") | let &write = save_write | endif
  171. if exists("save_cwd") | silent exe 'lcd' fnameescape(save_cwd) | endif
  172. if exists("tmpdir") | silent call s:RmDir(tmpdir) | endif
  173. endtry
  174. endfunction
  175. function! rust#AppendCmdLine(text)
  176. call setcmdpos(getcmdpos())
  177. let cmd = getcmdline() . a:text
  178. return cmd
  179. endfunction
  180. function! s:RmDir(path)
  181. " sanity check; make sure it's not empty, /, or $HOME
  182. if empty(a:path)
  183. echoerr 'Attempted to delete empty path'
  184. return 0
  185. elseif a:path == '/' || a:path == $HOME
  186. echoerr 'Attempted to delete protected path: ' . a:path
  187. return 0
  188. endif
  189. silent exe "!rm -rf " . shellescape(a:path)
  190. endfunction
  191. " }}}1
  192. " vim: set noet sw=4 ts=4: