mirror of
				https://github.com/vim/vim.git
				synced 2025-10-31 09:57:14 -04:00 
			
		
		
		
	runtime(rust): set formatprg to rustfmt
closes: #16967 Signed-off-by: Konfekt <Konfekt@users.noreply.github.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
		
				
					committed by
					
						 Christian Brabandt
						Christian Brabandt
					
				
			
			
				
	
			
			
			
						parent
						
							b1d6db0be5
						
					
				
				
					commit
					4ac995bf93
				
			| @@ -1,5 +1,6 @@ | |||||||
| " Author: Stephen Sugden <stephen@stephensugden.com> | " Author: Stephen Sugden <stephen@stephensugden.com> | ||||||
| " Last Modified: 2023-09-11 | " Last Modified: 2023-09-11 | ||||||
|  | " Last Change:   2025 Mar 31 by Vim project (rename s:RustfmtConfigOptions()) | ||||||
| " | " | ||||||
| " Adapted from https://github.com/fatih/vim-go | " Adapted from https://github.com/fatih/vim-go | ||||||
| " For bugs, patches and license go to https://github.com/rust-lang/rust.vim | " For bugs, patches and license go to https://github.com/rust-lang/rust.vim | ||||||
| @@ -61,7 +62,7 @@ function! s:RustfmtWriteMode() | |||||||
|     endif |     endif | ||||||
| endfunction | endfunction | ||||||
|  |  | ||||||
| function! s:RustfmtConfigOptions() | function! rustfmt#RustfmtConfigOptions() | ||||||
|     let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';') |     let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';') | ||||||
|     if l:rustfmt_toml !=# '' |     if l:rustfmt_toml !=# '' | ||||||
|         return '--config-path '.shellescape(fnamemodify(l:rustfmt_toml, ":p")) |         return '--config-path '.shellescape(fnamemodify(l:rustfmt_toml, ":p")) | ||||||
| @@ -84,7 +85,7 @@ function! s:RustfmtCommandRange(filename, line1, line2) | |||||||
|  |  | ||||||
|     let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]} |     let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]} | ||||||
|     let l:write_mode = s:RustfmtWriteMode() |     let l:write_mode = s:RustfmtWriteMode() | ||||||
|     let l:rustfmt_config = s:RustfmtConfigOptions() |     let l:rustfmt_config = rustfmt#RustfmtConfigOptions() | ||||||
|  |  | ||||||
|     " FIXME: When --file-lines gets to be stable, add version range checking |     " FIXME: When --file-lines gets to be stable, add version range checking | ||||||
|     " accordingly. |     " accordingly. | ||||||
| @@ -99,7 +100,7 @@ endfunction | |||||||
|  |  | ||||||
| function! s:RustfmtCommand() | function! s:RustfmtCommand() | ||||||
|     let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display' |     let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display' | ||||||
|     let config = s:RustfmtConfigOptions() |     let config = rustfmt#RustfmtConfigOptions() | ||||||
|     return join([g:rustfmt_command, write_mode, config, g:rustfmt_options]) |     return join([g:rustfmt_command, write_mode, config, g:rustfmt_options]) | ||||||
| endfunction | endfunction | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| " Maintainer:	Chris Morgan <me@chrismorgan.info> | " Maintainer:	Chris Morgan <me@chrismorgan.info> | ||||||
| " Last Change:	2024 Mar 17 | " Last Change:	2024 Mar 17 | ||||||
| "		2024 May 23 by Riley Bruins <ribru17@gmail.com ('commentstring') | "		2024 May 23 by Riley Bruins <ribru17@gmail.com ('commentstring') | ||||||
|  | "		2025 Mar 31 by Vim project (set 'formatprg' option) | ||||||
| " For bugs, patches and license go to https://github.com/rust-lang/rust.vim | " For bugs, patches and license go to https://github.com/rust-lang/rust.vim | ||||||
|  |  | ||||||
| if exists("b:did_ftplugin") | if exists("b:did_ftplugin") | ||||||
| @@ -57,6 +58,19 @@ setlocal includeexpr=rust#IncludeExpr(v:fname) | |||||||
|  |  | ||||||
| setlocal suffixesadd=.rs | setlocal suffixesadd=.rs | ||||||
|  |  | ||||||
|  | if executable(get(g:, 'rustfmt_command', 'rustfmt')) | ||||||
|  |     if get(g:, "rustfmt_fail_silently", 0) | ||||||
|  |         augroup rust.vim.FailSilently | ||||||
|  |             autocmd! * <buffer> | ||||||
|  |             autocmd ShellFilterPost <buffer> if v:shell_error | execute 'echom "shell filter returned error " . v:shell_error . ", undoing changes"' | undo | endif | ||||||
|  |         augroup END | ||||||
|  |     endif | ||||||
|  |  | ||||||
|  |     let &l:formatprg = get(g:, 'rustfmt_command', 'rustfmt') . ' ' . | ||||||
|  |                 \ get(g:, 'rustfmt_options', '') . ' ' . | ||||||
|  |                 \ rustfmt#RustfmtConfigOptions() | ||||||
|  | endif | ||||||
|  |  | ||||||
| if exists("g:ftplugin_rust_source_path") | if exists("g:ftplugin_rust_source_path") | ||||||
|     let &l:path=g:ftplugin_rust_source_path . ',' . &l:path |     let &l:path=g:ftplugin_rust_source_path . ',' . &l:path | ||||||
| endif | endif | ||||||
| @@ -149,7 +163,7 @@ endif | |||||||
|  |  | ||||||
| let b:undo_ftplugin = " | let b:undo_ftplugin = " | ||||||
|             \ compiler make | |             \ compiler make | | ||||||
|             \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd< |             \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd< formatprg< | ||||||
|             \|if exists('b:rust_set_style') |             \|if exists('b:rust_set_style') | ||||||
|                 \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth< |                 \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth< | ||||||
|                 \|endif |                 \|endif | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user