Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Implement methods #199

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

pfcoperez
Copy link

@pfcoperez pfcoperez commented Sep 23, 2017

This PR tries to add ImplementMethod transformation aimed to offer a similar functionality to widespread IDEs, e.g:

image

It works through Selection mechanism as follows:

  1. The user selectsa template (mixed template) extended in the definition of another (target template)
  2. The transformation finds methods, values and type tags without implementation in the mixed template...
  3. ... adding them to the target template as long as they are not implemented in its body.

@pfcoperez pfcoperez changed the title [WIP] Implement methods Implement methods Oct 17, 2017
Copy link
Member

@wpopielarski wpopielarski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfcoperez looks quite neat, but it needs part on scala-ide part I guess. Let me look at it

@pfcoperez
Copy link
Author

Thanks @wpopielarski for taking the time reviewing it! I thought that part would require a new PR, but not to this repo but to https://github.com/scala-ide/scala-ide . If that's the case, I can do it too. My plan is to bring this functionality to https://github.com/ensime/ensime-server too.

""".stripMargin
} applyRefactoring implementMethods

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, these tests are failing

  @Test
  def shouldNotModifyComments() = new FileSet() {
    """
      |package implementMethods
      |
      |trait T {
      |  def t = 42
      |  def f(x: Int): String
      |}
      |
      |object Obj extends /*(*/T/*)*/ { // don't remove this comment
      |}
    """.stripMargin becomes
    """
      |package implementMethods
      |
      |trait T {
      |  def t = 42
      |  def f(x: Int): String
      |}
      |
      |object Obj extends /*(*/T/*)*/ { // don't remove this comment
      |  def f(x: Int): String = {
      |    ???
      |  }
      |}
    """.stripMargin
  } applyRefactoring implementMethods

  @Test
  def implementUnimplementedBackticksEvilMethod() = new FileSet() {
    """
      |package implementMethods
      |
      |trait T {
      |  def `Zła metoda`: Int
      |}
      |
      |object Obj extends /*(*/T/*)*/ {
      |  val doNotCutMeOut = 42
      |}
    """.stripMargin becomes
    """
      |package implementMethods
      |
      |trait T {
      |  def `Zła metoda`: Int
      |}
      |
      |object Obj extends /*(*/T/*)*/ {
      |  val doNotCutMeOut = 42
      |
      |  def `Zła metoda`: Int = {
      |    ???
      |  }
      |}
    """.stripMargin
  } applyRefactoring implementMethods

  @Test
  def implementUnimplementedSpecialMethod() = new FileSet() {
    """
      |package implementMethods
      |
      |trait T {
      |  def :++: : Int
      |}
      |
      |object Obj extends /*(*/T/*)*/ {
      |  val doNotCutMeOut = 42
      |}
    """.stripMargin becomes
    """
      |package implementMethods
      |
      |trait T {
      |  def :++: : Int
      |}
      |
      |object Obj extends /*(*/T/*)*/ {
      |  val doNotCutMeOut = 42
      |
      |  def :++: : Int = {
      |    ???
      |  }
      |}
    """.stripMargin
  } applyRefactoring implementMethods

}

val transformation = topdown {
matchingChildren {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I think that it will be quite hard to modify trees. The way how you determine unimplemented methods/fields from tree is done OK, but it is safer to modify source code directly, not its tree. There is scala.tools.refactoring.util.Movement util to walk on source. Generally I have to look at this closer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants