How to execute Kotlin code in IntelliJ plugin?

I want to create an IntelliJ Idea plugin that

  • detects that a specific Kotlin class appears in the source code file (e.g. SomeGraph) and
  • if such a class executes code and displays the result in the sidebar.

Kotlin code as a minor DSL.

Layout

For example, let's say I have the following code snippet:

val vertex1 = GraphElement()
val vertex2 = GraphElement()

fun main(args : Array<String>) {
  val myGraph = SomeGraph()
  // Initialize graph, add data to it
  myGraph.addEdge(vertex1, vertex2)

  // Now myGraph.toDot() will return a Graphviz dot diagram in text form
}

myGraph.toDot()will return the Graphviz Dot graph ( digraph G {...) code .

My plugin should take this text, let Dot visualize the graph and display the graph.

Is it technically possible? If so, how can I execute part of the Kotlin code inside the IntelliJ plugin?

Notes:

  • A DSL file does not require a function main. If this simplifies the situation, I can change the place of creation SomeGraph.

. . DSL- :

class SpecialCaseGraph : SomeGraph() {
  fun init() {
    val vertex1 = GraphElement()
    val vertex2 = GraphElement()
    myGraph.addEdge(vertex1, vertex2)
  }
  fun toDot():String {
    // Returns the Dot representation of the graph
  }
}
  1. PlantUML - . , , , ( PlantUML, Dot-).

Image showing the difference with PlantUML

1 (02.09.2017 19:53 MSK):

ScriptEngine Kotlin, , Kotlin.

ScriptEngineManager mgr = new ScriptEngineManager();

  List<ScriptEngineFactory> factories = mgr.getEngineFactories();

  for (ScriptEngineFactory factory : factories) {

      System.out.println("ScriptEngineFactory Info");

      String engName = factory.getEngineName();
      String engVersion = factory.getEngineVersion();
      String langName = factory.getLanguageName();
      String langVersion = factory.getLanguageVersion();

      System.out.printf("\tScript Engine: %s (%s)%n", engName, engVersion);

      List<String> engNames = factory.getNames();
      for(String name : engNames) {
          System.out.printf("\tEngine Alias: %s%n", name);
      }

      System.out.printf("\tLanguage: %s (%s)%n", langName, langVersion);

  }

, Kotlin, .

Screenshot of script files available in my plugin

+4

Source: https://habr.com/ru/post/1684950/


All Articles