fortran-lang / fortls

fortls - Fortran Language Server
https://fortls.fortran-lang.org/
MIT License
257 stars 41 forks source link

Exact instructions how to setup fortls with neovim #426

Open certik opened 3 months ago

certik commented 3 months ago

I tried several times and always failed to get (any) LSP working with neovim, and I just figured it out specifically for fortls. I am documenting here the exact steps before I forget. We can later move this into fortls documentation.

The setup below does not destroy your current neovim setup. It makes it completely optional. You can use the NVIM_APPNAME environment variable to choose between setups.

  1. First install lazyvim:

    # check that ~/.config/lazyvim does not exist. If it does, archive it, or change the directory below
    git clone https://github.com/LazyVim/starter ~/.config/lazyvim
    export NVIM_APPNAME=lazyvim
    nvim some_file.f90

    The first time you start neovim with this setup, it will load all plugins and install itself. The subsequent time will be quite fast (although not as fast as vanilla neovim unfortunately and there are other issues, such as flicker at startup, sometimes the cursor movement is lagging; so I do not have lazyvim on by default due to these issues).

  2. Install conda: Use https://github.com/conda-forge/miniforge and follow the instructions there.

  3. Install fortls:

    mamba create -n fortls python
    conda activate fortls
    pip install fortls
    echo 'require("lspconfig").fortls.setup({})' >> ~/.config/lazyvim/init.lua
    nvim some_file.f90

Check that fortls is loaded using :checkhealth lsp. Now you can use the commands listed at https://dev.to/vonheikemen/getting-started-with-neovims-native-lsp-client-in-the-year-of-2022-the-easy-way-bp3. I found that the following three commands work: K, gd, gr. Here is an example of K:

image

Also TAB completion works in insert mode:

image

certik commented 3 months ago

I think I figured out how to make it work without lazyvim.

git clone https://github.com/neovim/nvim-lspconfig ~/.config/nvim/pack/nvim/start/nvim-lspconfig

Add this into init.vim:

lua << EOF
require'lspconfig'.fortls.setup{}
EOF

(or just the middle line into init.lua)

Only K and gd works for me, here is K:

image

Yes, colors are not the best, but I am sure that can be fixed.

gd only finds the symbol in the current file, it doesn't seem to go to another file for some reason.

Tab completion does not work (maybe I don't know how to use it).

gnikit commented 2 months ago

Just saw this @certik (sorry...). I use the native LSP implementation of neovim, to configure fortls and don't pass it through lazyvim since the language server will not activate unless a Fortran file is opened. I add only a little stub to my init.lua

require'lspconfig'.fortls.setup{
    cmd = {
        'fortls',
        '--lowercase_intrisics',
        '--hover_signature',
        '--hover_language=fortran',
        '--use_signature_help'
    }
}

I added it to the docs when I switched from vim to neovim.

I will try and setup it up on my new mac and update the instructions if they are out of date.

brucechan1123 commented 3 weeks ago

I don't understand why I can't get fortls attached with my f90 files when editing with neovim, the LspInfo shows that there's no clients attached to the buffer, can you help me figure it out? thanks. Image

certik commented 2 weeks ago

@brucechan1123 you are on Windows directly. Here is one way how to find the problem:

I haven't tried this in Windows directly yet.

brucechan1123 commented 2 weeks ago

@brucechan1123 you are on Windows directly. Here is one way how to find the problem:

  • Try it in WSL first, using my instructions above. Verify that it works.
  • Then do it using your setup, but in WSL, ensure things still work
  • Then redo it in Windows directly

I haven't tried this in Windows directly yet.

Thanks a lot for offering advice!

I'm not so good at Operation systems, that I'm not sure the 'WSL' denotes windows subsystem linux or something.

However, accidently I tried to call Mason to uninstall forlts and re-install it, and the problem was just solved.

And Thank you anyway for offering advice!