forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request scala#5513 from tuvior/vscodedotty-tasty
Decompile .tasty files with VS Code Extension
- Loading branch information
Showing
17 changed files
with
540 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
compiler/src/dotty/tools/dotc/core/tasty/TastyHTMLPrinter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dotty.tools.dotc | ||
package core | ||
package tasty | ||
|
||
import Contexts._, Decorators._ | ||
import Names.Name | ||
import TastyUnpickler._ | ||
import TastyBuffer.NameRef | ||
import util.Positions.offsetToInt | ||
import printing.Highlighting._ | ||
|
||
class TastyHTMLPrinter(bytes: Array[Byte])(implicit ctx: Context) extends TastyPrinter(bytes) { | ||
override protected def nameColor(str: String): String = s"<span class='name'>$str</span>" | ||
override protected def treeColor(str: String): String = s"<span class='tree'>$str</span>" | ||
override protected def lengthColor(str: String): String = s"<span class='length'>$str</span>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package dotty.tools | ||
package dotc | ||
package decompiler | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core._ | ||
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter | ||
import dotty.tools.dotc.reporting._ | ||
import dotty.tools.dotc.tastyreflect.ReflectionImpl | ||
|
||
/** | ||
* Decompiler to be used with IDEs | ||
*/ | ||
class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver { | ||
|
||
private val myInitCtx: Context = { | ||
val rootCtx = initCtx.fresh.addMode(Mode.Interactive).addMode(Mode.ReadPositions).addMode(Mode.ReadComments) | ||
rootCtx.setSetting(rootCtx.settings.YretainTrees, true) | ||
rootCtx.setSetting(rootCtx.settings.fromTasty, true) | ||
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx)._2 | ||
ctx.initialize()(ctx) | ||
ctx | ||
} | ||
|
||
private val decompiler = new PartialTASTYDecompiler | ||
|
||
def run(className: String): (String, String) = { | ||
val reporter = new StoreReporter(null) with HideNonSensicalMessages | ||
|
||
val run = decompiler.newRun(myInitCtx.fresh.setReporter(reporter)) | ||
|
||
implicit val ctx = run.runContext | ||
|
||
run.compile(List(className)) | ||
run.printSummary() | ||
val unit = ctx.run.units.head | ||
|
||
val decompiled = new ReflectionImpl(ctx).showSourceCode.showTree(unit.tpdTree) | ||
val tree = new TastyHTMLPrinter(unit.pickled.head._2).printContents() | ||
|
||
reporter.removeBufferedMessages.foreach(message => System.err.println(message)) | ||
(tree, decompiled) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
compiler/src/dotty/tools/dotc/decompiler/PartialTASTYDecompiler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dotty.tools.dotc.decompiler | ||
|
||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Partial TASTYDecompiler that doesn't execute the backendPhases | ||
* allowing to control decompiler output by manually running it | ||
* on the CompilationUnits | ||
*/ | ||
class PartialTASTYDecompiler extends TASTYDecompiler { | ||
override protected def backendPhases: List[List[Phase]] = Nil | ||
} |
6 changes: 6 additions & 0 deletions
6
language-server/src/dotty/tools/languageserver/DottyClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dotty.tools.languageserver | ||
|
||
/** | ||
* A `LanguageClient` that regroups all language server features | ||
*/ | ||
trait DottyClient extends worksheet.WorksheetClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
language-server/src/dotty/tools/languageserver/decompiler/TastyDecompilerMessages.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dotty.tools.languageserver.decompiler | ||
|
||
import org.eclipse.lsp4j.TextDocumentIdentifier | ||
|
||
// All case classes in this file should have zero-parameters secondary | ||
// constructors to allow Gson to reflectively create instances on | ||
// deserialization without relying on sun.misc.Unsafe. | ||
|
||
/** The parameter for the `tasty/decompile` request. */ | ||
case class TastyDecompileParams(textDocument: TextDocumentIdentifier) { | ||
def this() = this(null) | ||
} | ||
|
||
/** The response to a `tasty/decompile` request. */ | ||
case class TastyDecompileResult(tastyTree: String = null, scala: String = null, error: Int = 0) { | ||
def this() = this(null, null, 0) | ||
} | ||
|
||
object TastyDecompileResult { | ||
val ErrorTastyVersion = 1 | ||
val ErrorClassNotFound = 2 | ||
val ErrorOther = -1 | ||
} |
Oops, something went wrong.