rustfmt.vim 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. " Author: Stephen Sugden <stephen@stephensugden.com>
  2. " Last Modified: 2023-09-11
  3. "
  4. " Adapted from https://github.com/fatih/vim-go
  5. " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
  6. if !exists("g:rustfmt_autosave")
  7. let g:rustfmt_autosave = 0
  8. endif
  9. if !exists("g:rustfmt_command")
  10. let g:rustfmt_command = "rustfmt"
  11. endif
  12. if !exists("g:rustfmt_options")
  13. let g:rustfmt_options = ""
  14. endif
  15. if !exists("g:rustfmt_fail_silently")
  16. let g:rustfmt_fail_silently = 0
  17. endif
  18. function! rustfmt#DetectVersion()
  19. " Save rustfmt '--help' for feature inspection
  20. silent let s:rustfmt_help = system(g:rustfmt_command . " --help")
  21. let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features"
  22. " Build a comparable rustfmt version variable out of its `--version` output:
  23. silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version")
  24. let l:rustfmt_version_list = matchlist(l:rustfmt_version_full,
  25. \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)')
  26. if len(l:rustfmt_version_list) < 3
  27. let s:rustfmt_version = "0"
  28. else
  29. let s:rustfmt_version = l:rustfmt_version_list[1]
  30. endif
  31. return s:rustfmt_version
  32. endfunction
  33. call rustfmt#DetectVersion()
  34. if !exists("g:rustfmt_emit_files")
  35. let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2"
  36. endif
  37. if !exists("g:rustfmt_file_lines")
  38. let g:rustfmt_file_lines = s:rustfmt_help =~# "--file-lines JSON"
  39. endif
  40. let s:got_fmt_error = 0
  41. function! rustfmt#Load()
  42. " Utility call to get this script loaded, for debugging
  43. endfunction
  44. function! s:RustfmtWriteMode()
  45. if g:rustfmt_emit_files
  46. return "--emit=files"
  47. else
  48. return "--write-mode=overwrite"
  49. endif
  50. endfunction
  51. function! s:RustfmtConfigOptions()
  52. let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';')
  53. if l:rustfmt_toml !=# ''
  54. return '--config-path '.shellescape(fnamemodify(l:rustfmt_toml, ":p"))
  55. endif
  56. let l:_rustfmt_toml = findfile('.rustfmt.toml', expand('%:p:h') . ';')
  57. if l:_rustfmt_toml !=# ''
  58. return '--config-path '.shellescape(fnamemodify(l:_rustfmt_toml, ":p"))
  59. endif
  60. " Default to edition 2018 in case no rustfmt.toml was found.
  61. return '--edition 2018'
  62. endfunction
  63. function! s:RustfmtCommandRange(filename, line1, line2)
  64. if g:rustfmt_file_lines == 0
  65. echo "--file-lines is not supported in the installed `rustfmt` executable"
  66. return
  67. endif
  68. let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
  69. let l:write_mode = s:RustfmtWriteMode()
  70. let l:rustfmt_config = s:RustfmtConfigOptions()
  71. " FIXME: When --file-lines gets to be stable, add version range checking
  72. " accordingly.
  73. let l:unstable_features = s:rustfmt_unstable_features ? '--unstable-features' : ''
  74. let l:cmd = printf("%s %s %s %s %s --file-lines '[%s]' %s", g:rustfmt_command,
  75. \ l:write_mode, g:rustfmt_options,
  76. \ l:unstable_features, l:rustfmt_config,
  77. \ json_encode(l:arg), shellescape(a:filename))
  78. return l:cmd
  79. endfunction
  80. function! s:RustfmtCommand()
  81. let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display'
  82. let config = s:RustfmtConfigOptions()
  83. return join([g:rustfmt_command, write_mode, config, g:rustfmt_options])
  84. endfunction
  85. function! s:DeleteLines(start, end) abort
  86. silent! execute a:start . ',' . a:end . 'delete _'
  87. endfunction
  88. function! s:RunRustfmt(command, tmpname, from_writepre)
  89. let l:view = winsaveview()
  90. let l:stderr_tmpname = tempname()
  91. call writefile([], l:stderr_tmpname)
  92. let l:command = a:command . ' 2> ' . l:stderr_tmpname
  93. if a:tmpname ==# ''
  94. " Rustfmt in stdin/stdout mode
  95. " chdir to the directory of the file
  96. let l:has_lcd = haslocaldir()
  97. let l:prev_cd = getcwd()
  98. execute 'lchdir! '.expand('%:h')
  99. let l:buffer = getline(1, '$')
  100. if exists("*systemlist")
  101. silent let out = systemlist(l:command, l:buffer)
  102. else
  103. silent let out = split(system(l:command,
  104. \ join(l:buffer, "\n")), '\r\?\n')
  105. endif
  106. else
  107. if exists("*systemlist")
  108. silent let out = systemlist(l:command)
  109. else
  110. silent let out = split(system(l:command), '\r\?\n')
  111. endif
  112. endif
  113. let l:stderr = readfile(l:stderr_tmpname)
  114. call delete(l:stderr_tmpname)
  115. let l:open_lwindow = 0
  116. if v:shell_error == 0
  117. if a:from_writepre
  118. " remove undo point caused via BufWritePre
  119. try | silent undojoin | catch | endtry
  120. endif
  121. if a:tmpname ==# ''
  122. let l:content = l:out
  123. else
  124. " take the tmpfile's content, this is better than rename
  125. " because it preserves file modes.
  126. let l:content = readfile(a:tmpname)
  127. endif
  128. call s:DeleteLines(len(l:content), line('$'))
  129. call setline(1, l:content)
  130. " only clear location list if it was previously filled to prevent
  131. " clobbering other additions
  132. if s:got_fmt_error
  133. let s:got_fmt_error = 0
  134. call setloclist(0, [])
  135. let l:open_lwindow = 1
  136. endif
  137. elseif g:rustfmt_fail_silently == 0 && !a:from_writepre
  138. " otherwise get the errors and put them in the location list
  139. let l:errors = []
  140. let l:prev_line = ""
  141. for l:line in l:stderr
  142. " error: expected one of `;` or `as`, found `extern`
  143. " --> src/main.rs:2:1
  144. let tokens = matchlist(l:line, '^\s\+-->\s\(.\{-}\):\(\d\+\):\(\d\+\)$')
  145. if !empty(tokens)
  146. call add(l:errors, {"filename": @%,
  147. \"lnum": tokens[2],
  148. \"col": tokens[3],
  149. \"text": l:prev_line})
  150. endif
  151. let l:prev_line = l:line
  152. endfor
  153. if !empty(l:errors)
  154. call setloclist(0, l:errors, 'r')
  155. echohl Error | echomsg "rustfmt returned error" | echohl None
  156. else
  157. echo "rust.vim: was not able to parse rustfmt messages. Here is the raw output:"
  158. echo "\n"
  159. for l:line in l:stderr
  160. echo l:line
  161. endfor
  162. endif
  163. let s:got_fmt_error = 1
  164. let l:open_lwindow = 1
  165. endif
  166. " Restore the current directory if needed
  167. if a:tmpname ==# ''
  168. if l:has_lcd
  169. execute 'lchdir! '.l:prev_cd
  170. else
  171. execute 'chdir! '.l:prev_cd
  172. endif
  173. endif
  174. " Open lwindow after we have changed back to the previous directory
  175. if l:open_lwindow == 1
  176. lwindow
  177. endif
  178. call winrestview(l:view)
  179. endfunction
  180. function! rustfmt#FormatRange(line1, line2)
  181. let l:tmpname = tempname()
  182. call writefile(getline(1, '$'), l:tmpname)
  183. let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
  184. call s:RunRustfmt(command, l:tmpname, v:false)
  185. call delete(l:tmpname)
  186. endfunction
  187. function! rustfmt#Format()
  188. call s:RunRustfmt(s:RustfmtCommand(), '', v:false)
  189. endfunction
  190. function! rustfmt#Cmd()
  191. " Mainly for debugging
  192. return s:RustfmtCommand()
  193. endfunction
  194. function! rustfmt#PreWrite()
  195. if !filereadable(expand("%@"))
  196. return
  197. endif
  198. if rust#GetConfigVar('rustfmt_autosave_if_config_present', 0)
  199. if findfile('rustfmt.toml', '.;') !=# '' || findfile('.rustfmt.toml', '.;') !=# ''
  200. let b:rustfmt_autosave = 1
  201. let b:_rustfmt_autosave_because_of_config = 1
  202. endif
  203. else
  204. if has_key(b:, '_rustfmt_autosave_because_of_config')
  205. unlet b:_rustfmt_autosave_because_of_config
  206. unlet b:rustfmt_autosave
  207. endif
  208. endif
  209. if !rust#GetConfigVar("rustfmt_autosave", 0)
  210. return
  211. endif
  212. call s:RunRustfmt(s:RustfmtCommand(), '', v:true)
  213. endfunction
  214. " vim: set et sw=4 sts=4 ts=8: