rust-lang / rls

Repository for the Rust Language Server (aka RLS)
Other
3.51k stars 257 forks source link

Include Snippets/Templates to completion #1097

Open mickaelistria opened 6 years ago

mickaelistria commented 6 years ago

Some IDEs (VSCode, Eclipse IDE) do add support for templates as completion items. See for instance https://github.com/rust-lang-nursery/rls-vscode/blob/50a4e37bc88310ae1dda88c0a399cebe4408a8c0/snippets/rust.json which contains the snippets provided by VSCode. It seems like this feature shouldn't be specific to an IDE or the other and that it could be unified/shared in common place, which could be the language server itself. I suggest RLS should try to incorporate such a snippet/template mechanism for completion items.

Boereck commented 6 years ago

When the basic infrastructure is in place, further possibilities could be considered:

Ideally snippets should also be able to add external crate dependencies.

kevincox commented 5 years ago

I think a great option would be to use the type information to auto-generate snippets such as:

For example some IDEs provide an option to fill in struct fields. It would be very cool if this was supported. I think most of the value of these Language Server snippets comes from the possible dynanacism.

struct Foo {
  a: String,
  b: u32,
}

fn new() -> Foo {
  Foo {
    a: "".to_string(),
    b: 0,
  }
}

You can imagine that I selected the completion after typing (part of) Foo and selecting the completion the fields were auto-filled. Then you should be able to jump to the "default" values to easily modify them. There would need to be some logic for what the "default" value of an arbitrary type should be but even handling common ones would be amazing. The biggest win here is that you don't have to remember the field names or types.

kevincox commented 5 years ago

For example a lot of the templates in this file are just expanding function or macro arguments.

https://github.com/rust-lang/rls-vscode/blob/master/snippets/rust.json

lijinpei commented 5 years ago

And snippet is part of LSP' Completetion Request feature https://microsoft.github.io/language-server-protocol/specification#textDocument_completion

lijinpei commented 5 years ago

When trying to complete the struct:

struct Foo {
    ba1: i32,
    ba2: i32,
}

RLS generates the following response,

{"label":"Foo","kind":7,"detail":"struct Foo","insertText":"Foo","insertTextFormat":2}

Looks like we can pr racer to provide more sophisticated snippets https://github.com/rust-lang/rls/blob/0d6f53e1a4adbaf7d83cdc0cb54720203fcb522e/rls/src/actions/requests.rs#L284 https://github.com/racer-rust/racer/blob/eebd594bcc0382499c830130d384773d3edd686e/src/racer/snippets.rs#L33

Xanewok commented 5 years ago

cc #894