completion-nvim icon indicating copy to clipboard operation
completion-nvim copied to clipboard

Support rust-analyzer.completion.enableAutoimportCompletions for auto importing code on completion

Open dialtone opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. It would be nice to be able to auto import libraries in rust if they are standard library or dependencies, upon completion, it would make the completion logic a lot more useful as well.

Describe the solution you'd like Behave like VSCode does with rust autocomplete

dialtone avatar Dec 23 '20 23:12 dialtone

I'm not very familiar with rust and rust-analyer though. It'd be great if you can provide an example that works in vscode but not here. Further more, this kind of server dependence feature is kind of a pain to maintain, probably we should have a easier way to customize completion behavior.

haorenW1025 avatar Jan 01 '21 09:01 haorenW1025

Hey, you can see the auto import completion in action here: https://github.com/rust-analyzer/rust-analyzer/pull/6553 and I think the primary way to enable it is to use textDocument/completion with additional properties like seen here: https://github.com/microsoft/vscode-languageserver-node/blob/fde994bb4e29518a1988d0070e3f160146135021/client/src/common/client.ts#L1514 and in particular I think it needs additionalTextEdits enabled and then to apply those returned.

dialtone avatar Jan 01 '21 19:01 dialtone

Hmm actually completion-nvim DOES support addtionalyTextEdits, at least it works fine on ccls on my side. I'm not sure why this doesn't work in rust-analyzer. I'll take a closer look to it.

haorenW1025 avatar Jan 02 '21 09:01 haorenW1025

Hmm I guess the behavior is quite weird comparing to the link you show, it's activate with codeActions so I tried using codeActions when my cursor is at

Plug 'tpope/vim-surround'
Plug 'tpope/vim-repeat'
fn main() {
    let map = HashMap|;
}

and it does auto import it. But when I type HashMap no completion was available so I can't triggered it with complete an items.

haorenW1025 avatar Jan 02 '21 12:01 haorenW1025

rust-analyzer only provides auto-importing completions when the textDocument.completion.resolveSupport client capability includes additionalTextEdits, indicating that the client supports lazily resolving the additional edit required to do the import

Jesse-Bakker avatar Jan 03 '21 21:01 Jesse-Bakker