如何让SBT REPL自动重新加载已更改的Scala类

16
我是Scala的新手,使用emacs + ensime + sbt配置进行Scala开发。
这个配置非常好用且轻便,但有一件事情让我很烦恼 - 无法自动编译/重新加载在sbt启动的Scala控制台中的更改内容。
我经常使用REPL,并希望能够使用console命令从sbt启动REPL,并在REPL中测试Scala类的更改,而不必每次修改都需要关闭并重新加载。
我来自Erlang环境,这种开发方式在Erlang中很容易实现,但似乎在SBT中有些困难。我已安装JRebel插件,但似乎无法解决我所描述的情况。
是否有人能够使类似的功能正常工作,并愿意分享配置步骤?
非常感谢。
1个回答

5
sbt中有两件事是可能的:
  1. Causing automatic recompilation of the project sources triggered by a file change by prefixing a command with ~ (tilde). The console, or console-quick, or console-project commands can be prefixed, too, but you have to exit REPL to make the recompilation happen (just hit Ctrl+D and wait.)

  2. Causing automatic execution of REPL commands just after firing the console. They can be defined as properties (e.g. in build.sbt):

    initialCommands in console := """
    import some.library._
    def someFun = println("Hello")
    """
    

    It's not necessary to define the property separately in consoleQuick because it defaults to the one defined in console, but if you would like to use the console-project command you have to define it separately.

最后提醒:在*.sbt文件中每个属性之间都要留空行,这是为了正确解析属性。在上面的例子中,没有空行,这意味着所有内容都会进入initialCommands属性(这正是我们想要的)。


谢谢Rajish回复-这不完全是我要找的内容。如果每次更改后需要退出/打开REPL以重新加载更改,那么交互式Scala开发(至少在与LISP或Erlang开发人员理解相同的意义上)就不可能实现。 - Roman Shestakov
很抱歉,目前没有其他的解决方案。在ensime的github存储库https://github.com/aemoncannon/ensime/issues/149中存在问题,但是三年以来什么也没有发生,所以“欢迎拉取请求”;) - Rajish
1
如果你需要一个良好的实时更新的REPL体验,也许你应该尝试一下Scala-IDE工作表 https://github.com/scala-ide/scala-worksheet/wiki/Getting-Started - Rajish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接