autozimu / LanguageClient-neovim

Language Server Protocol (LSP) support for vim and neovim.
MIT License
3.55k stars 273 forks source link

Inline type inference #934

Open jestarray opened 4 years ago

jestarray commented 4 years ago

image

Is there a way to get the type inference to be displayed inlined like vscode? Or is this not supported?

jestarray commented 4 years ago

Someone said this could be implemented with "conceal text".

theHamsta commented 4 years ago

The inlay hints are requested by a custom request (rust-analyzer/inlayHints) https://github.com/rust-analyzer/rust-analyzer/blob/17dda0972a68dd88a766c223390317dc2cb3ea00/editors/code/src/inlay_hints.ts#L135-L148

I think conceal can only hide text. I think we need to wait until virtual text gets extended in Neovim to be displayed not only in at the end of line but also in the middle. I think this was planned when merging the virtual text commit.

akhilman commented 3 years ago

Is it possible to have at least an annotation at the end of the line like CoC has? image

Also can someone point to the neovim issues about virtual text?

martskins commented 3 years ago

@akhilman I think that would be fairly simple to implement. It's very specific to rust-analyzer, but we handle extensions already so I suppose we could add this one as well.

martskins commented 3 years ago

@akhilman see #1108 😄

If you could check out my branch, build from source and verify that it works as you'd expect that'd be great. I'll merge after I giving it a more in-depth try but I think it should be enough.

ibraheemdev commented 3 years ago

Was support added in #1106? I can't get it to work with the following config:

let g:LanguageClient_serverCommands = {
     \ 'rust': {
     \   'name': 'rust-analyzer',
     \   'command': ['rust-analyzer'],
     \   'initializationOptions' : {
     \       'inlayHints': {
     \           'enable': v:true,
     \           'chainingHints': v:true
     \       }
     \   },
     \ },
     \}
martskins commented 3 years ago

@ibraheemdev it works for me with that exact configuration. Which version of rust-analyzer and language-client-neovim are you using?

ibraheemdev commented 3 years ago

@martskins I built rust-analyzer from master and languageclient-neovim from next:

Plug 'autozimu/LanguageClient-neovim', {
   \ 'branch': 'next',
   \ 'do': 'bash install.sh',
   \ }

rust-analyzer.inlayHints are working for me with coc.nvim.

martskins commented 3 years ago

I see the same behaviour if I compile rust-analyzer from master. Latest release worked for me though, so maybe worth asking in rust-analyzer if something changed in master regarding chaining hints or if maybe you need to do something else to get those when compiling yourself.

Logarithmus commented 3 years ago

@akhilman @ibraheemdev @martskins not sure it's still relevant, but I also had troubles with getting chainingHints working. Sadly, there's no much information on the Internet about LanguageClient-neovim. Everyone on this planet seems to succumb either to VSCode or to coc.nvim. I refuse to use coc.nvim because I don't want to pollute my computer with JavaScript/node_modules/webdev crap just to get Rust autocompletion working.

Finally, it works. As you can see, I use next branch of LanguageClient-neovim: This is my ~/.config/nvim/settings.json:

{
    "rust-analyzer": {
        "checkOnSave": {
            "extraArgs": ["--target-dir", "/home/artur/.cache/rust-analyzer"]
        },
        "rustfmt": {
            "overrideCommand": ["rustfmt"]
        },
        "diagnostics": {
            "enable": true
        },
        "inlayHints": {
            "enable": true,
            "chainingHints": true,
            "typeHints": true,
            "parameterHints": true
        }
    }
}

And this is the part of ~/.config/nvim/init.vim which is reponsible for LanguageClient-neovim:

call plug#begin()
    Plug 'autozimu/LanguageClient-neovim', {
        \ 'branch': 'next',
        \ 'do': 'sh install.sh',
        \ }
    let g:LanguageClient_serverCommands = {
        \ 'rust': ['rust-analyzer'],
        \ 'c': ['clangd'],
        \ 'cpp': ['clangd'],
        \}
    let g:LanguageClient_settingsPath = expand('~/.config/nvim/settings.json')
    let g:LanguageClient_loggingFile = expand('~/.config/nvim/LanguageClient.log')
    let g:LanguageClient_loggingLevel = 'DEBUG'

P. S. Sorry if my comment offends JavaScript/webdev fans

Logarithmus commented 3 years ago

Are there any plans to implement something like this: https://github.com/fannheyward/coc-rust-analyzer/issues/394#issuecomment-742273063 Looks like a reasonable trade-off until https://github.com/neovim/neovim/pull/9496 is finished.

martskins commented 3 years ago

@Logarithmus that is actually what #1108 implements, unless I misunderstood what you meant. I think the issue you are having though is due to using an incorrect settings.json. Try changing rust-analyzer in the root of your settings.json to initializationOptions, if that doesn't work please open an issue and I can help you solve that.

Logarithmus commented 3 years ago

@Logarithmus that is actually what #1108 implements, unless I misunderstood what you meant. I think the issue you are having though is due to using an incorrect settings.json. Try changing rust-analyzer in the root of your settings.json to initializationOptions, if that doesn't work please open an issue and I can help you solve that.

I meant TypeHints & ParameterHints. Currently LanguageClient-neovim only supports displaying ChainingHints. In comparison, coc-rust-analyzer implements both ChainingHints & TypeHints: https://github.com/fannheyward/coc-rust-analyzer/issues/394#issuecomment-742273063. TypeHints which are located on the same line are joined into single hint and then displayed at the end of line.

martskins commented 3 years ago

Oh I see, I guess it would be a decent addition. I for one find it too distracting, but I guess it's a nice middleground for people that want that.