Fiddle inclusion

@@fiddle block

The @@fiddle block is used to include Scala code snippets from a separate file that are runnable in

@@fiddle [Hello.scala](/docs/src/main/scala/Hello.scala) { #hello_fiddle }

Inside of Hello.scala mark the desired section you want to extract using the #hello_fiddle label as follows:


This lets us compile and test the source before including it in the documentation. The fiddle is rendered with code formatting like this and the top right button let you run your code using and embedded view of scalafiddle:


Valid fiddle directive’s attributes are:

  • prefix
  • dependency
  • scalaversion
  • template
  • theme
  • minheight
  • layout

and their usage is described in the upstream fiddle README.

Source base directories

In order to specify your fiddle source paths off certain base directories you can define placeholders either in the page’s front matter or globally like this (for example):

paradoxProperties in Compile ++= Map(
  "" -> "/docs/src/main/../some/dir",
  "fiddle.test.base_dir" -> s"${(sourceDirectory in Test).value}/scala/org/example",
  "fiddle.project.base_dir" -> (baseDirectory in ThisBuild).value.getAbsolutePath

and it will work such as described for snippets.

The source code for this page can be found here.