scala-ide / scala-refactoring

A library providing automated refactoring support for Scala.
http://scala-refactoring.org/
Other
69 stars 47 forks source link

Explicit typing feature #161

Open JohnReedLOL opened 8 years ago

JohnReedLOL commented 8 years ago

I'm currently working on implementing an explicit typing feature.

https://www.reddit.com/r/scala/comments/4mwp0o/scalatype_need_help/

Basically the idea is that it adds explicit types to variable declarations to make code easier to maintain after it has been written.

I was wondering if there was any way we could integrate functionality. Right now I'm getting my type information by telling my command line utility to compile with -print. This generates desugared source code with type information that can be extracted (using regular expression).

kiritsuku commented 8 years ago

I worked on this years ago, there is still an open PR for it: https://github.com/scala-ide/scala-refactoring/pull/66

Unfortunately it isn't a reliable feature, therefore it was never merged. scalac removes too much syntactic information from the trees, which made it really difficult for scala-refactoring to just add types to the source code at the correct position.

scala-ide however still supports that feature: https://github.com/scala-ide/scala-ide/blob/2a16bde67fc120836180bf92f0ae2ab94501a728/org.scala-ide.sdt.core/src/org/scalaide/core/internal/quickassist/explicit/ExplicitReturnType.scala The code is quite easy to understand. We basically just ask the presentation compiler for the type (no need to print it back to text as you are doing it) and then add it to the source code directly. The current implementation doesn't work in all possible use cases but it is still good enough that I use it regularly.