Tree Sitter Fortran in Neovim

I’m currently updating my nvim config and have encountered some indentation problems since I installed nvim-treesitter with a Fortran parser.

Does anyone have any experience with nvim and treesitter?

I only used fortrls (LSP) before, and it feels like I had fewer issues without tree-sitter.

Currently, for example, when I type do <enter> the indentation level is zero and messes everything up (no indentation at all) as long as I don’t have an end do.

    ! some code
    ! with indent level 1
do ! as soon as I type the 'o', indent is zero
! indentation is zero instead of +1
    end do ! this magically gets indented correctly
        ! but the next line is indented ???
    ! but only one line, this is fine again

The most relevant settings:

indentexpr=nvim_treesitter#indent()
indent={enable=true}

If someone managed to set tree-sitter-fortran correctly, I would be very happy to see the nvim config.

Does nvim also use GitHub - stadelmanma/tree-sitter-fortran: Fortran grammar for tree-sitter like helix? I’m seeing indentation issues when using block ... end block in helix. I suspect the fix is to update the tree-sitter files used to match the updates on github.

1 Like

I’ve tried tree-sitter on Neovim and ended up disabling it in Fortran because there were a lot of errors, mostly with the more modern constructs. I assume it only handles FORTRAN77 and maybe some parts of Fortran90 and should be updated. In their GitHub (GitHub - nvim-treesitter/nvim-treesitter: Nvim Treesitter configurations and abstraction layer) they say that they support Fortran via the one that @freevryheid said Helix uses

1 Like

Yes, exactly!

The reason you see this behaviour is because there is no indentation rule for blocks in indents.scm:

[
  (module)
  (submodule)
  (program)
  (subroutine)
  (module_procedure)
  (function)
  ; (interface)
  (if_statement)
  (do_loop_statement)
  (where_statement)
  (derived_type_definition)
  (enum)
] @indent

I guess I will do the same and hope for more updates. Maybe if I find some time I will contribute to it, too.
Did you disable it for everything or just indentation?

Yes, I’m thinking on contributing at some time too (but never made up the time to see how Tree-Sitter works). I did disable it for everything because I had other problems that I don’t remember exactly right now, but they were related to syntax highlighting.

Right now I’m just using the indentation that Neovim provides, plus I run fprettify on my files fairly often to fix indentations and spaces (watch out to anyone that runs fprettify! Sometimes it can delete your whole file so always run fprettify --diff first)

1 Like

Would these tools (tree-sitter, fprettify) not benefit from an AST, that I understand LFortran already provides?

I don’t know much about Tree Sitter, but afaik it needs a “grammar” and generates a parser from it.

lfortran fmt takes care of formatting, essentially a stand-in for fprettify
lfortran already uses a grammar file that I’m guessing is used by the compiler. Instead of re-inventing the wheel so to speak, why not just “convert” this asdl file to tree-sitter format.