Skip to content

Conversation

@kdelorey
Copy link
Contributor

@kdelorey kdelorey commented Feb 11, 2020

Summary

This PR adds a set of magic completions to auto complete associated trait items (functions/consts/types).

Associated Trait Impl

Notes

Since the assist and completion share the same logic when figuring out the associated items that are missing, a shared utility was created in the ra_assists::utils module.

Resolves #1046

As this is my first PR to the rust-analyzer project, I'm new to the codebase, feedback welcomed!

@kdelorey kdelorey changed the title Magic Completion for impl Trait for Magic Completion for impl Trait for Associated Items Feb 11, 2020
@kiljacken
Copy link
Contributor

This an amazing addition, thanks so much for working on it!

@matklad
Copy link
Contributor

matklad commented Feb 11, 2020

This looks good to me!

I think the test fail primaraly due to rustfmt? You can run cargo xtask format to format things, or cargo xtask install pre-commit-hook to install the hook.

I think there's a bunch of things we can further tweak here, but it's also good to land as is.

@kdelorey
Copy link
Contributor Author

I think the test fail primaraly due to rustfmt? You can run cargo xtask format to format things, or cargo xtask install pre-commit-hook to install the hook.

Ah, there was formatting issues. I was sure I had the pre-commit-hook on my environment; I'll have to check that when I get home.

I think there's a bunch of things we can further tweak here, but it's also good to land as is.

I'm happy to further refine this if you had thoughts. I can also refine it in a separate PR if your intention was to accept this as is.

@kdelorey
Copy link
Contributor Author

Whoops, spoke too soon, test still failed. Let me try removing the doc comments as it seems to be bringing up docs::no_docs_comments.

@kdelorey
Copy link
Contributor Author

kdelorey commented Feb 11, 2020

Oh, @matklad, maybe the CI is complaining about needing module level documentation? That would make sense 🤔

Edit: Yup that was it 🤦‍♂

@matklad
Copy link
Contributor

matklad commented Feb 11, 2020 via email

@kdelorey
Copy link
Contributor Author

I agree, when I get home from my day job I'll work on that change. Thanks for taking a look.

@kdelorey
Copy link
Contributor Author

kdelorey commented Feb 13, 2020

@matklad one thing I've discovered while working on the suggested change was the CONST_DEF syntax node is made a little differently than its counterparts (fn/type). Consder the following:

impl SomeTrait for () {
   fn<|>
}

will produce something like the following syntax tree:

ITEM_LIST@[85; 95)
  L_CURLY@[85; 86) "{"
  WHITESPACE@[86; 91) "\n    "
  FN_DEF@[91; 93)  <-- incomplete `fn` syntrax makes `FN_DEF`
    FN_KW@[91; 93) "fn"
  WHITESPACE@[93; 94) "\n"
  R_CURLY@[94; 95) "}"

But with a const it does something a little different:

impl SomeTrait for () {
   const<|>
}

will produce something like the following

ITEM_LIST@[85; 98)
  L_CURLY@[85; 86) "{"
  WHITESPACE@[86; 91) "\n    "
  ERROR@[91; 96)   <-- Expected `CONST_DEF`
    CONST_KW@[91; 96) "const"
  WHITESPACE@[96; 97) "\n"
  R_CURLY@[97; 98) "}"

It won't generate a CONST_DEF node until a user starts typing a IDENT. Anyways, with my limited experince, I wasn't sure if I stumbled upon a bug or something intentional? It currently makes the handling of the const case a little different than fn and type associated items.

Edit: I pushed my WIP changes so you can see how I started going about your suggestion.

@matklad
Copy link
Contributor

matklad commented Feb 13, 2020

Anyways, with my limited experince, I wasn't sure if I stumbled upon a bug or something intentional

That's semi-intentional, in that const is not necessary a const FOO, it could be a const fn as well, so we can't relibely decide, by just seening a const, what's the end result would be.

@kdelorey kdelorey requested a review from matklad February 14, 2020 01:36
@kdelorey kdelorey requested a review from matklad February 15, 2020 16:30
@matklad
Copy link
Contributor

matklad commented Feb 17, 2020

bors r+

Thanks!

bors bot added a commit that referenced this pull request Feb 17, 2020
3108: Magic Completion for `impl Trait for` Associated Items r=matklad a=kdelorey

# Summary
This PR adds a set of magic completions to auto complete associated trait items (functions/consts/types). 

![Associated Trait Impl](https://user-images.githubusercontent.com/2295721/74493144-d8f1af00-4e96-11ea-93a4-82725bf89646.gif)

## Notes
Since the assist and completion share the same logic when figuring out the associated items that are missing, a shared utility was created in the `ra_assists::utils` module.

Resolves #1046 

As this is my first PR to the rust-analyzer project, I'm new to the codebase, feedback welcomed!

Co-authored-by: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com>
@bors
Copy link
Contributor

bors bot commented Feb 17, 2020

Build succeeded

  • Rust (macos-latest)
  • Rust (ubuntu-latest)
  • Rust (windows-latest)
  • TypeScript

@bors bors bot merged commit 057d0be into rust-lang:master Feb 17, 2020
@matklad
Copy link
Contributor

matklad commented Feb 17, 2020

Opened a sequel in #3183

@kdelorey
Copy link
Contributor Author

@matklad, thanks for working with me on this. It was pretty rewarding to open reddit this morning and see a gif of my contribution 🎉

@kdelorey kdelorey deleted the kdelorey/complete-trait-impl branch February 18, 2020 04:22
slyngbaek added a commit to slyngbaek/rust-analyzer that referenced this pull request Mar 7, 2020
Allow trait autocompletions for unimplemented associated fn's, types,
and consts without using explicit keywords before hand (fn, type,
const).

The sequel to rust-lang#3108.
JoshMcguigan pushed a commit to JoshMcguigan/rust-analyzer that referenced this pull request Apr 1, 2020
Allow trait autocompletions for unimplemented associated fn's, types,
and consts without using explicit keywords before hand (fn, type,
const).

The sequel to rust-lang#3108.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inside impls, complete unimplemented functions

3 participants