Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Created January 4, 2019 15:00
Show Gist options
  • Save olafurpg/a48243dcacbd9702efedd7e0f31982d2 to your computer and use it in GitHub Desktop.
Save olafurpg/a48243dcacbd9702efedd7e0f31982d2 to your computer and use it in GitHub Desktop.
mdoc:js playground for scalafmt
package jsdocs
import mhtml._
import org.scalafmt._
import org.scalajs.dom.raw.Element
import scala.scalajs.js
object ScalafmtPlayground {
def mount(node: Element): Unit = {
val start = """
value match {
case StringGCValue(string) => ps.setString(index, string)
case BooleanGCValue(boolean) => ps.setBoolean(index, boolean)
case IntGCValue(int) => ps.setInt(index, int)
case FloatGCValue(float) => ps.setDouble(index, float)
case StringIdGCValue(id) => ps.setString(index, id)
case UuidGCValue(uuid) => ps.setObject(index, uuid)
}
""".trim
val code = Var(start)
val maxColumn = Var(60)
val formatted = code.zip(maxColumn).map {
case (text, column) =>
val c = config.ScalafmtConfig.default.forSbt.copy(maxColumn = column)
Scalafmt.format(text, c).toEither.fold(_.toString, _.toString)
}
def handler(event: js.Dynamic): Unit =
code := event.target.value.asInstanceOf[String]
def sliderHandler(event: js.Dynamic): Unit =
try maxColumn := event.target.value.asInstanceOf[String].toInt
catch { case _: Throwable => }
mhtml.mount(
node,
<div>
<textarea oninput={handler _} rows="10" cols="80">{start}</textarea>
<p>
<code>maxColumn</code>:
<input type="range" min="1" max="80" value="60" oninput={sliderHandler _}/>
{maxColumn}
</p>
<pre><code>{formatted}</code></pre>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment