-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ZLayer visualization #473
Draft
hmemcpy
wants to merge
1
commit into
idea241.x
Choose a base branch
from
zlayer-mermaid
base: idea241.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
ZLayer visualization #473
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
54 changes: 54 additions & 0 deletions
54
src/main/scala/zio/intellij/gutter/ZLayerLineMarkerProvider.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,54 @@ | ||
package zio.intellij.gutter | ||
|
||
import com.intellij.codeInsight.daemon.{LineMarkerInfo, LineMarkerProvider} | ||
import com.intellij.openapi.editor.markup.GutterIconRenderer.Alignment | ||
import com.intellij.openapi.fileEditor.FileEditor | ||
import com.intellij.openapi.fileEditor.impl.HTMLEditorProvider | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.ui.jcef.JBCefApp | ||
import com.intellij.util.Urls | ||
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScGenericCall, ScMethodCall} | ||
import zio.intellij.icons.LayersIcon | ||
import zio.intellij.inspections._ | ||
import zio.intellij.inspections.macros.MacroGraphUtils | ||
import zio.intellij.inspections.zioMethods._ | ||
|
||
import java.awt.event.MouseEvent | ||
|
||
final class ZLayerLineMarkerProvider extends LineMarkerProvider { | ||
import zio.intellij.gutter.ZLayerLineMarkerProvider.createLineMarkerInfo | ||
|
||
override def getLineMarkerInfo(element: PsiElement): LineMarkerInfo[_ <: PsiElement] = element match { | ||
case `.inject`(base, layers @ _*) => | ||
createLineMarkerInfo(base, layers) | ||
case ScMethodCall(partiallyApplied @ ScGenericCall(`ZLayer.makeLike`(_, _), _), layers) => | ||
createLineMarkerInfo(partiallyApplied, layers) | ||
case _ => null | ||
} | ||
|
||
} | ||
object ZLayerLineMarkerProvider { | ||
def createLineMarkerInfo(element: ScExpression, layers: Seq[ScExpression]): LineMarkerInfo[PsiElement] = | ||
new LineMarkerInfo( | ||
element, | ||
element.getTextRange, | ||
LayersIcon, | ||
(_: PsiElement) => "Show ZLayer build graph (Mermaid)", | ||
(_: MouseEvent, _: PsiElement) => | ||
MacroGraphUtils.renderMermaid(element) match { | ||
case Some(mermaid) => | ||
browse(element.getProject, mermaid): Unit | ||
case None => | ||
}, | ||
Alignment.LEFT, | ||
() => "Show ZLayer build graph" | ||
) | ||
|
||
def browse(project: Project, url: String): FileEditor = { | ||
if (!JBCefApp.isSupported) throw new IllegalStateException("JCEF is not supported on this system") | ||
val request: HTMLEditorProvider.Request = HTMLEditorProvider.Request.url(Urls.newFromEncoded(url).toExternalForm) | ||
request.withQueryHandler(null) | ||
HTMLEditorProvider.openEditor(project, "ZLayer Mermaid Diagram", request) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
package zio.intellij | ||
|
||
import com.intellij.ui.IconManager | ||
import javax.swing.Icon | ||
|
||
package object icons { | ||
private def load(path: String): Icon = IconManager.getInstance.getIcon(path, getClass.getClassLoader) | ||
|
||
val SortByIdIcon: Icon = load("/icons/sortById.svg") | ||
val FiberIcon: Icon = com.intellij.icons.AllIcons.Debugger.Threads | ||
val LayersIcon: Icon = com.intellij.icons.ExpUiIcons.Toolwindow.Dependencies | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/scala/zio/intellij/inspections/macros/MacroGraphUtils.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,62 @@ | ||
package zio.intellij.inspections.macros | ||
|
||
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression | ||
import zio.intellij.inspections.macros.LayerBuilder.{MermaidGraph, ZExpr} | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.util.Base64 | ||
import scala.collection.mutable | ||
|
||
object MacroGraphUtils { | ||
|
||
def renderMermaid(element: ScExpression): Option[String] = { | ||
Option(element.getUserData(GraphDataKey)).map(renderMermaidLink) | ||
} | ||
|
||
private def renderMermaidLink(tree: LayerTree[ZExpr]) = { | ||
|
||
def escapeString(string: String): String = | ||
"\\\"" + string.replace("\"", """) + "\\\"" | ||
|
||
val map = tree | ||
.map(expr => escapeString(expr.value.getText)) | ||
.fold[MermaidGraph]( | ||
z = MermaidGraph.empty, | ||
value = MermaidGraph.make, | ||
composeH = _ ++ _, | ||
composeV = _ >>> _ | ||
) | ||
.deps | ||
|
||
val aliases = mutable.Map.empty[String, String] | ||
|
||
def getAlias(name: String): String = | ||
aliases.getOrElse( | ||
name, { | ||
val alias = s"L${aliases.size}" | ||
aliases += name -> alias | ||
s"$alias($name)" | ||
} | ||
) | ||
|
||
val mermaidCode: String = | ||
map.flatMap { | ||
case (key, children) if children.isEmpty => | ||
List(getAlias(key)) | ||
case (key, children) => | ||
children.map { child => | ||
s"${getAlias(child)} --> ${getAlias(key)}" | ||
} | ||
} | ||
.mkString("\\n") | ||
|
||
val mermaidGraph = | ||
s"""{"code":"graph BT\\n$mermaidCode","mermaid": "{\\"theme\\": \\"default\\"}"}""" | ||
|
||
val encodedMermaidGraph: String = | ||
new String(Base64.getEncoder.encode(mermaidGraph.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8) | ||
|
||
val mermaidLink = s"https://mermaid.live/view/#$encodedMermaidGraph" | ||
mermaidLink | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgive me @myazinn for I have sinned :D
(I need to recover the
LayerTree
somehow... any ideas are most welcome!)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feature look too good for me to grumble on side-effects 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This desperately needs a State, but screw it :)
My initial approach was to duplicate the inspection logic inside the gutter, but I decided against it. Passing data in the element's cache is good enough for now.
Just need to tweak the gutter menu.
Thanks! :)