Skip to content

Automatic syntax highlighting with .rs files does not work #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alkorang opened this issue Feb 8, 2019 · 3 comments · Fixed by #319
Closed

Automatic syntax highlighting with .rs files does not work #306

alkorang opened this issue Feb 8, 2019 · 3 comments · Fixed by #319

Comments

@alkorang
Copy link

alkorang commented Feb 8, 2019

rust.vim version:
commit 12f549f

Steps to reproduce:

On CentOS 7, install vim 7.4

# yum install vim-enhance vim-common

Then, install rust.vim using vim-plug

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

~/.vimrc file:

call plug#begin('~/.vim/plugged')
Plug 'rust-lang/rust.vim', { 'tag' : '12f549f9e4939bca53c8a87e6921a36fb143af9a' }
call plug#end()

After saved ~/.vimrc, run :PlugInstall in vim or $ vim +PlugInstall +qall on terminal.

Expected vs. actual behavior:

  • Expected:
    When I opened rust source file with .rs extension, for example main.rs, then vim automatically highlights Rust syntax.
    (And this is the actual behavior until commit 329fa78.)
    image

  • Actual:
    With commit 12f549f, vim automatically highlights syntax but it is not for Rust.
    image
    When I run :set syntax=rust manually, then Rust syntax highlight works.

I cannot get additional debug information because :RustInfo failed with E492 error.
image

@alkorang alkorang changed the title Automatic syntax highlighting with .rs files does not work from 12f549f Automatic syntax highlighting with .rs files does not work Feb 8, 2019
@chris-morgan
Copy link
Member

I can’t reproduce this on Vim 8.1.1 on Windows, probably for the reasons mentioned below.

This smells like another ftdetect rule butting in first, thereby triggering exactly the deliberate and intended difference between setfiletype rust and set filetype=rust.

Please run this to determine what the filetype actually is on a .rs file, and where it was last set:

:verbose set filetype?

Working from distant memories of such truly ancient versions of Vim worked out of the box, I expect the answer will be hercules from $VIMRUNTIME/filetype.vim. (For I observe even now that my own Vim has it set to rust from $VIMRUNTIME/filetype.vim rather than the rust.vim ftdetect.vim, suggesting that it’s currently basically only working because Vim already knows about Rust.)

I shan’t look into it now, but we may need to do some autocmd group rejiggering or something to gain higher priority than it (if that’s possible), without clobbering it altogether (because set filetype=rust is still definitely the wrong thing to do, even if this may now explain why it was that way). I’ll think about it more some other time, probably at least a few days hence. If suitable augroup fiddling isn’t possible, maybe we just clobber it iff filetype=hercules.

@chris-morgan
Copy link
Member

Oh, and about the :RustInfo: that’s from ftplugin/rust.vim, so it’s only available once you load a Rust file.

@alkorang
Copy link
Author

Please run this to determine what the filetype actually is on a .rs file, and where it was last set:

:verbose set filetype?

@chris-morgan The output of :verbose set filetype? with main.rs is:

  filetype=hercules
        Last set from /usr/share/vim/vim74/filetype.vim
Press ENTER or type command to continue

@da-x da-x closed this as completed in #319 May 19, 2019
da-x pushed a commit that referenced this issue May 19, 2019
- `: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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants