简体   繁体   English

建议导入不适用于Scala IDE(和sbt)

[英]Suggest imports does not work with Scala IDE (and sbt)

I had several issues setting up Scala and Eclipse with auto import suggestions. 在设置具有自动导入建议的Scala和Eclipse时遇到了几个问题。 I can run (compile and run) simple Scala code. 我可以运行(编译并运行)简单的Scala代码。 But I need the auto/suggest import feature. 但是我需要自动/建议导入功能。

My first attempt was to use an existing Eclipse (Version: Neon.2 Release (4.6.2)) and install Scala via update site. 我的第一次尝试是使用现有的Eclipse(版本:Neon.2版本(4.6.2))并通过更新站点安装Scala。 Installation was done, but my JRE could not be detected. 安装已完成,但是无法检测到我的JRE。 Pointing to my directory where javac is located and also their parents didn't work, Eclipse didn't recognize it as a valid JRE root. 指向我的javac所在的目录,以及他们的父母也没有工作,Eclipse没有将其识别为有效的JRE根目录。

/usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac

So I decided to download a Scala IDE from http://scala-ide.org/ (Build id: 4.5.0-vfinal-2016-12-13T10:59:29Z-Typesafe) 因此,我决定从http://scala-ide.org/下载一个Scala IDE(内部版本号:4.5.0-vfinal-2016-12-13T10:59:29Z-Typesafe)

I've already installed scala (2.12.2) via homebrew but I read that Scala IDE comes with their own Scala version (2.10 and 2.11), so this should not be an issue. 我已经通过自制软件安装了scala(2.12.2),但我读到Scala IDE带有自己的Scala版本(2.10和2.11),因此这不应该成为问题。

I've tried to follow this tutorial to setup a simple Akka app, even if it's a bit outdated I gave it a try: http://doc.akka.io/docs/akka/1.3.1/intro/getting-started-first-scala-eclipse.html 我尝试按照本教程设置了一个简单的Akka应用程序,即使它有点过时,我也尝试了一下: http : //doc.akka.io/docs/akka/1.3.1/intro/getting-started负一阶,eclipse.html

Of course I installed newer version of Akka (akka_2.11-2.4.18.zip) and tried to add the akka-actor.jar as external jar. 当然,我安装了较新版本的Akka(akka_2.11-2.4.18.zip),并尝试将akka-actor.jar添加为外部jar。 When trying to use the Worker class inside of a Scala class file, it shows me errors that the class cannot be found. 尝试在Scala类文件中使用Worker类时,它向我显示找不到该类的错误。 Clicking on the red x doesn't show me a solution, I need to write the import line by hand. 单击红色的x并没有显示解决方案,我需要手工编写导入行。 My question is exactly about how to configure my setup that Eclipse show me the solution to import the proper class automatically. 我的问题恰恰是关于如何配置我的设置,以使Eclipse向我展示自动导入正确的类的解决方案。

I've tried to setup the project with sbt (0.13) and sbteclipse. 我试图用sbt(0.13)和sbteclipse设置项目。 I did the setup how it's explained here: How to add a scala library to eclipse and added Akka as dependency. 我做了安装程序,其解释方式如下: 如何将scala库添加到eclipse并添加Akka作为依赖项。 It installed lot of stuff (saw lot of output in the terminal) and I used Import existing project in Eclipse, but still the auto import doesn't work. 它安装了很多东西(在终端中看到了很多输出),并且我在Eclipse中使用了Import existing project ,但是自动导入仍然无法正常工作。 If I open the properties and navigate to Java Build Path, it looks fine, I mean no errors or warnings. 如果我打开属性并导航到Java Build Path,它看起来很好,我的意思是没有错误或警告。

This is what it shows me: 这就是给我看的: 在此处输入图片说明

Actually the line is hidden be the popup, it's just this line: 实际上,该行在弹出窗口中是隐藏的,仅此行:

class Worker extends Actor {

My .project file: 我的.project文件:

<projectDescription>
  <name>My Project</name>
  <buildSpec>
    <buildCommand>
      <name>org.scala-ide.sdt.core.scalabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.scala-ide.sdt.core.scalanature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>
  <linkedResources> </linkedResources>
</projectDescription>

My build.sbt file: 我的build.sbt文件:

name := "My Project"

version := "1.0"

scalaVersion := "2.11.11"

libraryDependencies +=
  "com.typesafe.akka" %% "akka-actor" % "2.5.1"

My .classpath file: 我的.classpath文件:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="foo.bar"/>
    <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
    <classpathentry kind="lib" path="/Users/timaschew/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.5.1.jar" sourcepath="/Users/timaschew/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/srcs/akka-actor_2.11-2.5.1-sources.jar">
        <attributes>
            <attribute name="javadoc_location" value="jar:file:/Users/timaschew/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/docs/akka-actor_2.11-2.5.1-javadoc.jar!/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="/Users/timaschew/.ivy2/cache/com.typesafe/config/bundles/config-1.3.1.jar" sourcepath="/Users/timaschew/.ivy2/cache/com.typesafe/config/srcs/config-1.3.1-sources.jar">
        <attributes>
            <attribute name="javadoc_location" value="jar:file:/Users/timaschew/.ivy2/cache/com.typesafe/config/docs/config-1.3.1-javadoc.jar!/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="/Users/timaschew/.ivy2/cache/org.scala-lang.modules/scala-java8-compat_2.11/bundles/scala-java8-compat_2.11-0.7.0.jar" sourcepath="/Users/timaschew/.ivy2/cache/org.scala-lang.modules/scala-java8-compat_2.11/srcs/scala-java8-compat_2.11-0.7.0-sources.jar">
        <attributes>
            <attribute name="javadoc_location" value="jar:file:/Users/timaschew/.ivy2/cache/org.scala-lang.modules/scala-java8-compat_2.11/docs/scala-java8-compat_2.11-0.7.0-javadoc.jar!/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

It seems that Eclipse is not a really good choice for Scala developing. 对于Scala开发而言,Eclipse似乎并不是一个很好的选择。

I switched to IntelliJ. 我切换到IntelliJ。 Scala can be installed at the very first usage of IntelliJ and it supports sbt as well. Scala可以在IntelliJ的首次使用时安装,它也支持sbt。 Change in the build.sbt file are recognized by IntelliJ to trigger sbt (to install new dependencies for instance). IntelliJ可以识别build.sbt文件中的更改以触发sbt(例如,安装新的依赖项)。

And of course suggesting to import some packages for usage of unkown classes are also working fine. 当然,建议导入一些软件包以使用未知类也可以正常工作。

It also allows you to install different versions of Scala very easy. 它还允许您非常轻松地安装不同版本的Scala。

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

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