Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make intellij Idea to highlight Scala script correctly with #! (shebang)

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
like image 296
sumnulu Avatar asked Mar 16 '17 10:03

sumnulu


People also ask

How do I select Scala version in IntelliJ?

Press Ctrl+Alt+S to open Settings/Preferences dialog. From the options on the left, select Build, Execution, Deployment | Compiler | Scala | Scala Compiler Server. In JVM SDK field specify the appropriate SDK.


2 Answers

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.

like image 58
SerCe Avatar answered Oct 08 '22 01:10

SerCe


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.

like image 38
david.perez Avatar answered Oct 08 '22 00:10

david.perez