scalameta / metals-feature-requests

Issue tracker for Metals feature requests
37 stars 4 forks source link

"Extract interface" code action to convert classes to abstract traits #355

Open keynmol opened 10 months ago

keynmol commented 10 months ago

Is your feature request related to a problem? Please describe.

Not sure who else wants that, but I often find myself writing a class implementation and then realise that I'd rather have it as a trait + implementation class.

This code action can be especially useful on an already existing class with a non trivial amount of public methods implemented.

Describe the solution you'd like

A code action that

  1. Extracts all public methods into a trait named the same as the class
  2. Explicitly annotates the methods with inferred return types
  3. Renames class to NameImpl
  4. Converts all methods into overrides
object BEFORE:
  class Client(bla: String):
    def test() = 1
    def hello(x: String, b: Int) = bla + x

// Apply code action "Extract interface"

object AFTER:
  trait Client:
    def test(): Int
    def hello(x: String, b: Int): String

  class ClientImpl(bla: String) extends Client:
    override def test() = 1
    override def hello(x: String, b: Int) = bla + x

There's complexity around type parameters of course, but it doesn't have to be part of MVP.

Describe alternatives you've considered

For my personal use an alternative would be to plan ahead and always create traits, but I don't have that level of control over my life.

Additional context

No response

Search terms

code action, extract, trait, implementation, interface