Metals: Refactor: insert missing abstract members

Created on 4 Jan 2020  路  6Comments  路  Source: scalameta/metals

Is your feature request related to a problem? Please describe.
When implementing an abstract class or trait, it's useful to complete all the abstract members with stubs.

Describe the solution you'd like
Given something like

trait A {
  def someMethod(a: String): Int
  def someOtherMethod(b: String): String 
}

class B extends A {
// ^^^^^^^^^^^^^^^^^
// Error: class B needs to be abstract, blabla...
}

I would like a code action on B that allows to quickly implement all abstract members with stubs, resulting in:

class B extends A {
  override def someMethod(a: String): Int = ???
  override def someOtherMethod(b: String): String = ???
}

Describe alternatives you've considered
Mostly two alternatives exist:

  • copy paste the stubs from the error message generated by the compiler
  • use the "Implement all members" completion (which is basically the same as the code action I'm proposing, except it's triggered differently)

Additional context
I think this should be a "quickfix" code action in response to the specific compiler error.

Also, I believe it makes sense to have both the completion and the code action:

  • when implementing the trait for the first time the completion comes in handy, since I'm already typing
  • when I'm dealing with a compiler error because I forgot a member or a new abstract member was added to the trait, then the code action is more intuitive than triggering a completion.

Search terms:
abstract members, traits, interface

feature

Most helpful comment

I'm working on this, and it's roughly working.馃檶 Though there're still some to-dos before finishing up this, I think I can submit a PR maybe next weekend.

  • Robust regex to capture a diagnostic message for "missing implementation.."
  • indent
  • testing

Quick questions

  • Any thoughts on "title" (which is "Implement All Bar" for now in the screen-record below)
  • Is it ok to return WorkspaceEdit instead of "Command"?

    • code action can return Command instead of edits, and it might be better to return command concerning the performance(because we can calculate the edit lazily).

2020-01-26-180716mov

Thank you so much for working on these PRs https://github.com/scalameta/metals/pull/1031 and https://github.com/scalameta/metals/pull/1065 @ckipp01 @gabro ! 馃憤
I could follow those implementations and re-use a lot of logic!

All 6 comments

I'm working on this, and it's roughly working.馃檶 Though there're still some to-dos before finishing up this, I think I can submit a PR maybe next weekend.

  • Robust regex to capture a diagnostic message for "missing implementation.."
  • indent
  • testing

Quick questions

  • Any thoughts on "title" (which is "Implement All Bar" for now in the screen-record below)
  • Is it ok to return WorkspaceEdit instead of "Command"?

    • code action can return Command instead of edits, and it might be better to return command concerning the performance(because we can calculate the edit lazily).

2020-01-26-180716mov

Thank you so much for working on these PRs https://github.com/scalameta/metals/pull/1031 and https://github.com/scalameta/metals/pull/1065 @ckipp01 @gabro ! 馃憤
I could follow those implementations and re-use a lot of logic!

Looks amazing @tanishiking !

Any thoughts on "title" (which is "Implement All Bar" for now in the screen-record below)

I would try to use similar language as IntelliJ

Screenshot 2020-01-26 at 10 36 17

Is it ok to return WorkspaceEdit instead of "Command"?

I think it's better to return a command if it's not too much work. iirc, some editor clients don't support support yet code actions that return edits 馃

I'm really looking forward to use this feature! 馃榿 Since we added code actions to insert imports I've found myself trying to fix this error by muscle memory only to be sad realizing it's not implemented yet 馃槄

Wow, that looks great! 馃槏

In general I think it鈥檚 fine to return a workspace edit:

  • I haven鈥檛 yet found a client that doesn鈥檛 support workspace edits (I may be wrong I鈥檒l check again)
  • performance wise this is the same as the analogous completion so we鈥檙e ok, I think

Quick question: are you also handling the case in which some methods are implemented and some others are not?

Thank you for quick feedback @olafurpg @gabro 馃槃
I'm also looking for this feature! (because I usually write TypeScript at work and I got used to implementing all missing methods in this way (code action)).


I would try to use similar language as IntelliJ

Sounds good!

I think it's better to return a command if it's not too much work. iirc, some editor clients don't support support yet code actions that return edits 馃
I haven鈥檛 yet found a client that doesn鈥檛 support workspace edits (I may be wrong I鈥檒l check again)
performance wise this is the same as the analogous completion so we鈥檙e ok, I think

OK, I'll go with the edit for the first implementation! (I'll also check some LSP clients).

are you also handling the case in which some methods are implemented and some others are not?

Yes:+1: But there could be some problems in the more complex cases (especially around a range of TextEdit), I'm gonna test in various situations.

screen 2020-01-26 21 15 44 mov

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Michaelizm picture Michaelizm  路  4Comments

laughedelic picture laughedelic  路  4Comments

olafurpg picture olafurpg  路  4Comments

olafurpg picture olafurpg  路  4Comments

tgodzik picture tgodzik  路  4Comments