8

How do I make intellij Idea to highlight Scala script correctly.

Attempt 1 change filename to 'test.sc' . Intellij does not like the first line i.e it is not valid scala comment syntax

Attempt 2 change filename to 'test.sh' . Intellij thinks all of the syntax is bash script.

filenName = ./test.sh

#!/usr/bin/env amm
import ammonite.ops._ , ImplicitWd._

println("Stop script")
val x = 1 + 1
sumnulu
  • 3,412
  • 1
  • 19
  • 31

2 Answers2

20

If you need to preserve .sc extension here is a little trick

  • change shebang to #! /usr/bin/env amm which is still a valid shebang

  • put a file named #!.scala next to your script with the content:

class CLZ {
  def /(that: CLZ): CLZ = this
  def amm: Unit = {}
}
object #! extends CLZ
object usr extends CLZ
object bin extends CLZ
object env extends CLZ

Now, your script is properly highlighted

enter image description here

Update

JetBrains published a blog article about support for Ammonite.

SerCe
  • 5,826
  • 2
  • 32
  • 53
4

If you have a .scala extension, IDEA recognizes as a valid Scala script file. Even a multiline shebang line is supported:

#!/bin/sh
  DIR=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
  exec scala -classpath $DIR/scalaj-http_2.11-2.3.0.jar -savecompiled "$0" "$@"
!#
/* Scala code*/

Make sure:

  • you are using a relatively recent version of IDEA.
  • Scala plugin is installed.
  • The script lies in a Scala module.

It works even if the script is outside of a declared source folder. I can even edit scripts outside of my project.

david.perez
  • 6,090
  • 4
  • 34
  • 57