Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IntelliJ to recognize imports in Scala script?

I'm having trouble getting IntelliJ to recognize Ammonite imports in my Scala script. This is a new feature, reported here, where there are also instructions. I followed the instructions and I started a new sbt project in IntelliJ to try it out, but it's not working.

I see the Ammonite dependencies in the project's "External Libraries", which I specified in the build.sbt: libraryDependencies += "com.lihaoyi" %% "ammonite-ops" % "1.0.3" libraryDependencies += "com.lihaoyi" % "ammonite" % "1.0.3" cross CrossVersion.full

The (quite simple) project seems to successfully build. I also specified that Scala worksheet files should be treated as "Always Ammonite".

All this, yet the IDE shows the script like: enter image description here

What more do I need to do to get this to work?

Versions:

  • Java 1.8.0
  • Scala 2.12.4
  • sbt 1.0.3
  • IntelliJ CE 2017.3.2
  • IntelliJ scala plugin 2017.3.11
like image 435
einnocent Avatar asked Dec 28 '17 06:12

einnocent


1 Answers

I think IntelliJ IDEA won't find a dependency in a local cache if you're working mostly with Scala 2.13 and SBT 1.3+ with the Coursier library management. We need to help IntelliJ IDEA by downloading dependencies to the Ivy cache.

I solved this issue by:

  1. Creating the ivy.xml somewhere with required dependencies, e.g:

    <ivy-module version="1.0">
        <info organisation="test" module="download-deps"/>
        <dependencies>
            <dependency org="com.softwaremill.sttp.client" name="core_2.12" rev="2.1.1"/>
            <dependency org="com.softwaremill.sttp.client" name="okhttp-backend_2.12" rev="2.1.1"/>
            <dependency org="com.github.tototoshi" name="scala-csv_2.12" rev="1.3.6"/>
        </dependencies>
    </ivy-module>
    

    Note, I specified Scala 2.12 here.

  2. Running ivy -refresh from the directory with the created ivy.xml.

  3. Then we need is to "Create library from jar..." from the red import's context actions in IntelliJ IDEA.

That's all!

like image 95
arz.freezy Avatar answered Oct 14 '22 00:10

arz.freezy