简体   繁体   English

如何让 IntelliJ 识别 Scala 脚本中的导入?

[英]How to get IntelliJ to recognize imports in Scala script?

I'm having trouble getting IntelliJ to recognize Ammonite imports in my Scala script.我无法让 IntelliJ 在我的 Scala 脚本中识别 Ammonite 导入。 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.我按照说明在 IntelliJ 中启动了一个新的 sbt 项目来尝试一下,但它不起作用。

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我在项目的“外部库”中看到了 Ammonite 依赖项,我在build.sbt指定了build.sbtlibraryDependencies += "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".我还指定应将 Scala 工作表文件视为“Always Ammonite”。

All this, yet the IDE shows the script like:所有这些,但 IDE 显示的脚本如下: 在此处输入图片说明

What more do I need to do to get this to work?我还需要做什么才能让它发挥作用?

Versions:版本:

  • Java 1.8.0 Java 1.8.0
  • Scala 2.12.4斯卡拉 2.12.4
  • sbt 1.0.3 sbt 1.0.3
  • IntelliJ CE 2017.3.2 IntelliJ CE 2017.3.2
  • IntelliJ scala plugin 2017.3.11 IntelliJ scala 插件 2017.3.11

I had similar issues with IntelliJ, so let me share how I made it work.我在 IntelliJ 上遇到了类似的问题,所以让我分享一下我是如何使它工作的。

My versions:我的版本:

  • IntelliJ Ultimate 2017.3.2 (so basically the same version as yours) IntelliJ Ultimate 2017.3.2(与你的版本基本相同)
  • IntelliJ Scala plugin 2017.3.11 IntelliJ Scala 插件 2017.3.11
  • Kubuntu, so keyboard shortcuts below may have to be adjusted Kubuntu,所以下面的键盘快捷键可能需要调整

Now, I started from the same code as you have, with updated version of dependency 0.6.7 .现在,我从与您相同的代码开始,使用更新版本的依赖项0.6.7 凝视代码

Already now pressing the green arrow to next to 1st line runs Ammonite.现在已经按下绿色箭头到第一行旁边运行 Ammonite。 The dependencies are downloaded and link is displayed.下载依赖项并显示链接。 That is mandatory step for IntelliJ to recognize that new jar is available.这是IntelliJ 识别新 jar 可用的强制性步骤

Also at this stage Intellij shows me a small popup with information that I can import the jar.同样在这个阶段,Intellij 向我展示了一个小弹出窗口,其中包含我可以导入 jar 的信息。 I did not use it .我没有使用它 导入 $ivy 依赖项

To make IntelliJ recognize the imports, I press Alt + Enter Show Intention Actions on the broken com.lihaoyi::scalatags:0.6.7 dependency and use Create library from jar...为了让 IntelliJ 识别导入,我在损坏的com.lihaoyi::scalatags:0.6.7依赖项上按Alt + Enter Show Intention Actions并使用Create library from jar ... 从jar创建库...

This makes Intellij recognize the library, imports and it can start giving you hints.这使 Intellij 识别库、导入并开始为您提供提示。 在此处输入图片说明

The library is added to the Project's dependencies ( Ctrl + Alt + Shift + s ).该库被添加到项目的依赖项( Ctrl + Alt + Shift + s )。 You can see also dependencies coming from other files below.您还可以在下面看到来自其他文件的依赖项。 具有附加依赖项的项目结构

I had similar issues with IntelliJ and seem to be figthing bugs in IntelliJ.我在 IntelliJ 上遇到了类似的问题,并且似乎在解决 IntelliJ 中的错误。

My versions:我的版本:

  • IntelliJ Ultimate 2018.2.4 IntelliJ Ultimate 2018.2.4
  • IntelliJ Scala plugin 2018.2.11 IntelliJ Scala 插件 2018.2.11
  • OS: Windows操作系统:Windows
  • Scala: 2.12.6 (at the moment Ammonite current release 1.2.1 is not released for Scala 2.12.7, see #879 and below) Scala:2.12.6(目前 Ammonite 当前版本 1.2.1 未针对 Scala 2.12.7 发布,请参阅#879及以下)

Without IntelliJ Project没有 IntelliJ 项目

If I just open the .sc file without open project then I get prompted with如果我只是在没有打开项目的情况下打开 .sc 文件,那么我会收到提示

  • Project JDK is not defined项目 JDK 未定义
  • No Scala SDK in module模块中没有 Scala SDK
  • the notification: 'Add all Ammonite standard dependencies to the project?'通知:“将所有 Ammonite 标准依赖项添加到项目中?”

The notification quickly dissapears.通知很快消失。 If I click 'Add' button before fixing the JDK, the imports does not work, and the add button is gone.如果我在修复 JDK 之前单击“添加”按钮,导入将不起作用,并且添加按钮消失了。 fix the jdk and scala, and then find the 'Add all Ammonite...' in the eventlog and click 'add' there, then it works.修复 jdk 和 scala,然后在事件日志中找到“添加所有菊石...”并在那里单击“添加”,然后它就可以工作了。

However all changes in settings are lost next time I reopen IntelliJ.但是,下次我重新打开 IntelliJ 时,所有设置更改都会丢失。

With IntelliJ Project使用 IntelliJ 项目

  • Create new project Scala sbt project, default directory based project.创建新项目 Scala sbt 项目,基于默认目录的项目。
  • I place my sc-files in there somewhere我把我的 sc 文件放在那里的某个地方

Now IntelliJ remembers settings for sc-files.现在 IntelliJ 会记住 sc 文件的设置。 I can even open sc files directly from explorer/total commander in IntelliJ after configuring 'open with...' to intellij, though I prefer.在将“打开方式...”配置为 Intellij 后,我什至可以直接从 IntelliJ 中的资源管理器/总指挥官打开 sc 文件,尽管我更喜欢。

Do NOT allow IntelliJ to auto-import sbt project settings via notification, it seems to ruin the library imports, and I need to recreate project to recover.不允许 IntelliJ 通过通知自动导入 sbt 项目设置,它似乎破坏了库导入,我需要重新创建项目才能恢复。

Scala Version issues Scala 版本问题

The library auto-import 'add' button will only work for Scala versions for which Ammonite is released.库自动导入“添加”按钮仅适用于发布 Ammonite 的 Scala 版本。 See Maven Central Ammonite releases .请参阅Maven Central Ammonite 版本 At the time of writing Ammonite 1.2.1 is not released for scala-2.12.7, though the current unstable version of ammonite is.在撰写本文时,Ammonite 1.2.1 尚未针对 scala-2.12.7 发布,尽管目前 ammonite 的不稳定版本是。 See Ammonite bug-report #879 .参见Ammonite 错误报告 #879 The add button just fails silently, which sucks, esp.添加按钮只是默默地失败,这很糟糕,尤其是。 for a newbie like me.对于像我这样的新手。

The auto-generated sbt build file looks like this自动生成的 sbt 构建文件如下所示

name := "Ammonite"
version := "0.1"
scalaVersion := "2.12.6"

This is how it looks in project settings after libraries have successfully loaded:这是库成功加载后在项目设置中的外观:

在此处输入图片说明

and here are the dependencies that make it compile.这是使其编译的依赖项。 Note that the 'SBT : ' dependencies are those auto-generated via SBT-file (it seems), and the others are libraries added by scala plugin ammonite notifications feature请注意,“SBT :”依赖项是通过 SBT 文件自动生成的那些(似乎),而其他依赖项是由 Scala 插件 ammonite 通知功能添加的库

在此处输入图片说明

The above library list is empty when IntelliJ fails to load the ammonite libraries.当 IntelliJ 无法加载菊石库时,上述库列表为空。

IntelliJ with SBT (because IntelliJ loses ammonite imports, and for $ivy) IntelliJ 与 SBT(因为 IntelliJ 丢失了菊石进口,并且为 $ivy)

IntelliJ seems to frequently for lose its ammonite imports. IntelliJ 似乎经常丢失其菊石进口。 If I have SBT setup to give the imports, then it does not matter so much.如果我有 SBT 设置来提供导入,那么就没有那么重要了。 Also IntelliJ $ivy support for ammonite only works when files are already cached locally in ivy-cache, and running Ammonite script on my windows caches files in different location, which means IntelliJ $ivy imports effectively does not work.此外,IntelliJ $ivy 对 ammonite 的支持仅在文件已经本地缓存在 ivy-cache 中时才有效,并且在我的 Windows 上运行 Ammonite 脚本会将文件缓存在不同的位置,这意味着 IntelliJ $ivy 导入无效。 Hence I use SBT to setup the sum of the imports I use in my scripts.因此,我使用 SBT 来设置我在脚本中使用的导入的总和。

My build.sbt我的 build.sbt

scalaVersion := "2.12.7"
// https://mvnrepository.com/artifact/com.lihaoyi/ammonite
libraryDependencies += "com.lihaoyi" % ("ammonite_" + scalaVersion) % "1.2.1-16-48ce533"

// Extra example dependency
libraryDependencies += "org.jsoup" % "jsoup" % "1.11.3"

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.如果您主要使用 Scala 2.13 和 SBT 1.3+ 和 Coursier 库管理,我认为 IntelliJ IDEA 不会在本地缓存中找到依赖项。 We need to help IntelliJ IDEA by downloading dependencies to the Ivy cache.我们需要通过将依赖项下载到 Ivy 缓存来帮助 IntelliJ IDEA。

I solved this issue by:我通过以下方式解决了这个问题:

  1. Creating the ivy.xml somewhere with required dependencies, eg:在具有所需依赖项的某处创建ivy.xml ,例如:

     <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.注意,我在这里指定了 Scala 2.12。

  2. Running ivy -refresh from the directory with the created ivy.xml .使用创建的ivy.xml从目录运行ivy -refresh

  3. Then we need is to "Create library from jar..." from the red import's context actions in IntelliJ IDEA.然后我们需要从 IntelliJ IDEA 中红色导入的上下文操作中“从 jar 创建库...”。

That's all!仅此而已!

you need to associate .sc files with ammonite.您需要将 .sc 文件与菊石相关联。

in you settings, it should look like this在你的设置中,它应该是这样的在此处输入图片说明 (solution taken from here ) (从这里采取的解决方案)

I would also recommend reading the following blog post (scroll down to Ammonite scripts support ) to make sure you have all needed requirements我还建议您阅读以下博客文章(向下滚动到Ammonite 脚本支持)以确保您拥有所有需要的要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM