Neovim, fypp syntax highlighting and fortls


After having been an evangelist of the Church of Emacs for a decade, I recently moved to neovim (because why not) and I have to admit I quite like it so far. I am using kickstart.nvim as my base config file and tweaked it very little. Among the different LSP’s I use, I obviously chose fortls for my fortran work. Until now, I have been very happy with it and the whole thing works great directly out of the box.

Following this discussion, I’ve just started to play around with fyyp. And this where I would need some help neovim gurus. I can’t understand how to have syntax highlighting whenever I open a .fypp file, nor how I can use fortls as the underlying LSP. Any help is more than welcomed ! :slight_smile:

1 Like

If I’m not mistaken (I don’t have a way to double check now, I will later), you should create a ftdetect in your local nvim folder, within it you create a file named fypp.vim where you put the line

set ft=fortran

Hopefully this works.

Yes, :set ft=fortran works. I have been using it a per-file basis every time I open one but I couldn’t understand how to make it permanent. I’ll give it a try right away. Thx

EDIT : I does work. I now get consistent syntax highlighting. I just need to figure out how to make fortls works with .fypp files.

EDIT BIS : It actually works if I do nvim whatever-filename.fypp. If instead I first do nvim . in my project folder and then move through the tree to open the same file, syntax highlighting does not turn on.

What does it give if you query the filetype?

au BufRead,BufNewFile *.fypp set filetype=fortran

maybe for lua

  au BufRead,BufNewFile *.fypp set filetype=fortran
1 Like

@loiseaujc excellent, I use nvim as well. I switched to it mainly due to being able to autosave when the terminal loses focus, and reload when it regains focus (very helpful with using git in another terminal, or just recompilation).

How did you setup fortls to work with it? Do other LSP like C++ work as well? I was wondering if LSP works with nvim.

External Plugin is needed. Like
nvim-cmp(pure lua)
coc.nvim (require nodejs)

1 Like

I found that the kickstart.nvim config file is a pretty good starting point. It made my transition from emacs to neovim mostly painless. There, the various LSPs are handled using a combination of mason.nvim and nvim-lspconfig to easily install/manage a large set of different LSPs.

Regarding having different terminals : if I need just a quick one, I use the terminal emulator included in neovim. If I need one (or more than one) for more serious business, I’ve actually been able to make neovim and tmux work together fairly easily.

edit : As I’ve said earlier, the only limitation I have at the moment is to have syntax highlighting and fortls work correctly when editing .fypp files. Apart from that, it’s been a really smooth transition.

1 Like

Since fypp syntax is a not exactly standard Fortran syntax, the parsing of your source files will not always be correct. That being said to include .fypp files as Fortran sources you should set the following options:

  • --incl_suffixes=".fypp" (not sure if you need to . or not)
  • potentially --pp_suffixes=".fypp"

You can either use the CLI options or a JSON config file, see:

Here is how you can pass the options to neovim via CLI

Hope this helps.
Let me know how you get on.

P.S. it would be cool if we extended the internal parsee of fortls to understand fypp. I might have a go at it during the summer.

Got a chance to double check.
It’s a mix between my initial suggestion and @Euler-37 's one.
I’ve done this for a .fi file extension.
I’ve set a ftdetect folder, and in a fi.vim file put:

augroup filetypedetect
   au! BufRead,BufNewFile *.fi  setfiletype fortran
augroup END

For me, LSP starts right away, cause normally the way you set it up running in Neovim is by filetype, so once you’ve set it to fortran, the LSP should then attach to the buffer automatically.
Personally, I did not have to add any other flag to fortls to include .fi extension to the default ones.

@mEm’s solution works perfectly. And as soon as nvim identify .fypp files as having to follow the Fortran syntax highlighting, fortls is automatically attached to them. Syntax highlighting has a few bugs, but my guess is that it is an issue with treesitter (if my understanding is correct). All in all, it works reasonably well for what I want to do for now. Thanks everyone.

I ended up not using treesitter for Fortran.
I use the native syntax highlighting instead. I worked on that to improve few things, and if I’m not mistaken the maintainer (Ajit Thakkar) included some of those in the 106th version of the file.
However, I think this mostly affects the Vim repo, I don’t know if such changes are automatically reflected in Neovim (I doubt…).
Also, it would be easy to integrate support for fypp syntax as well, which for Treesitter would be a bit more complicated FWIU, since it involves two sides (in two different languages: Javascript and C): lexer and parser.

EDIT: if I don’t remember badly, @gnikit is coordinating a project for GSoC-24 regarding the ability of fortls to provide syntax highlithing. To me, this is the way to do it, LSPs directly providing it.

You are quite correct in this! As far as I saw in our Org’s GSoC applications, we didn’t receive a proposal for this, but hopefully I will get some dev time and work on this towards the end of the year.