Skip to content

Commit 53f40ec

Browse files
mhinzda-x
authored andcommitted
Improve filetype setting
- `:set filetype` forces a filetype. - `:setfiletype` only sets a filetype if it didn't already happen earlier due to the same event. Current versions of Vim and Neovim have a `$VIMRUNTIME/filetype.vim` that sets the `rust` filetype for all files with an `.rs` extension. This plugin did that with `set filetype=rust` as well. But now, with the filetype getting set twice, `ftplugin/rust/*` was sourced twice as well. This got fixed in #301 by changing `set filetype=rust` to `setfiletype rust`. But that fix introduced a regression for all older Vim versions. There is still a lot of Vim 7.4 around and Debian stable still provides Nvim 0.1.7, both being very old versions. The `$VIMRUNTIME/filetype.vim` of these old versions set the filetype to `hercules` for all `.rs` files via `setfiletype hercules`. Now, usually (it also depends on how plugins gets sourced) `filetype.vim` gets sourced _before_ any `ftdetect/*` files from plugins. And since autocmds are processed in order, Vim would first do `setfiletype hercules` and so any subsequent `setfiletype rust` fails. The obvious solution: Force the `rust` filetype for all `.rs` extensions unless the filetype is already set to `rust`. So, we have the filetype setting working again for older versions and also avoid setting the filetype twice. Fixes #306 Fixes #318
1 parent 9984b97 commit 53f40ec

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

ftdetect/rust.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
" vint: -ProhibitAutocmdWithNoGroup
22

3-
autocmd BufRead,BufNewFile *.rs setf rust
3+
autocmd BufRead,BufNewFile *.rs call s:set_rust_filetype()
44
autocmd BufRead,BufNewFile Cargo.toml setf FALLBACK cfg
55

6+
function! s:set_rust_filetype() abort
7+
if &filetype !=# 'rust'
8+
set filetype=rust
9+
endif
10+
endfunction
11+
612
" vim: set et sw=4 sts=4 ts=8:

0 commit comments

Comments
 (0)