开发者

How to compile and run scala code quickly in vim?

I'm using vim to learn scala. I write some scala code in vim, and then:

:!scalac xxx.scala开发者_StackOverflow
:!scala Xxx

Is there any way to run the code easier?


I suggest you try SBT - http://code.google.com/p/simple-build-tool/

You'll probably want a few shells open and you'll have to bounce between vim and your SBT session, but in general SBT streamlines Scala development to the point where it feels like an interpreted language. A few handy SBT tips:

Run sbt at the shell in your project directory to automatically create your project structure. To get started quickly, edit a file in that project named src/main/scala/MyFile.scala

In one sbt shell, run ~compile to continuously detect and compile changes as you make them.

In another sbt shell, run 'console'. This gives you a normal Scala REPL with your project and its dependencies already on the CLASSPATH.

From that scala shell you can run :load to load your Scala file and run it. Or you can simply import and instantiate classes and call functions defined in your Scala code to test functionality. When you make changes to the file, :replay in the REPL will load those changes and run all entered commands again.

This setup works great for rapid web development in conjunction with Scalatra. From sbt you can directly deploy and run your webapp with 'jetty-run' and then detect, recompile and deploy changes automatically with ~prepare-webapp. Then you just keep a browser open and see changes as you make them. A good getting started guide for this is here: http://blog.everythings-beta.com/?p=430 You can have a web app running in 5 minutes or so.


I would suggest that you use sbt to run things in one shell & modify the scala file in vim. You need to specify a mainClass method in your project file. Details here.

$ sbt
> ~ run
1. Waiting for source changes... (press enter to interrupt)

When you save the scala file in the vim session, sbt will try to invoke your mainClass.


You are probably better of using a build manager. However, there are cases where this is useful, particularly if you just want to run a single file with the scala interpreter. In vim, % matches the current file. If you want to run you file using the current file in the Scala interpreter, you could run:

:!scala %

If you were going to do this a lot, you could consider mapping a command to this.

map <F5> :!scala %<CR>

If you wanted this binding to be available only in scala files, try the following. This might require you to have the scala vim syntax files.

au FileType scala map <F5> :!scala %

Similarly, to compile files you can run the following.

:!scalac %

If you want to run your compiled class, it's more difficult because you need to know the classpath. %:r might be a start--it's the current file minus the extension.

A more advanced solution than above would be to use the scala interpreter/compiler as the build system for vim using :make. This way you can navigate through the errors within vim using quickfix. You will find a couple of compiler files I wrote helpful. You can copy these to ~/.vim/compiler/ and then set the compiler withing vim (:compiler scalai).

You may find a blog post of mine useful as it covers developing in scala using vim. It covers a good way of combining scala with continuous compilation in a build manager.


Maps are helpful here. I know nothing about scala, but e.g. the command

:nmap <F6> :!echo test<CR>

Will call the shell command echo test whenever you hit F6 in normal mode

I'm sure you can adapt it to your needs. Put in .vimrc if you want it permanent (which you probably do). More info can be found here, if you're into the whole editor scripting thing: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html

P.S. Forgot to mention that the % character expands to the current file in a shell command. You probably want something like :! scala %<CR>


EDIT:

This is a more complicated problem than I thought at first glance (I'm used to working with interpreted languages, where compile-run is a single command). Anyway, if you really still want single key compile and run functionality, try this quick script (stick it somewhere in your .vimrc file and restart vim):

function! CompileAndRunScala()
    w "save current file
    let l:compiler_output = system('scalac ' . expand('%:p'))
    " Runs scalac on current filename (absolute path)
    if v:shell_error "Gets last shell error code (0 means okay, anything else is error)
        echo 'Compile errors!'
        echo l:compiler_output
    else
        echo system('scala ' . expand('%:r'))
        " Runs scala on current file name with extension stripped (also absolute path)
    endif
endfunction
nmap <F6> :call CompileAndRunScala()<CR>


I myself kind of never moved away from make so I can compile the program with :make °target° Clean and simple.


You could shorten it down to one line by writing a script?

so in the same directory, you could do something like

scalac xxx.scala scala xxx

and them from vim you do

:!./scriptName

Make sure to make it executable first with chmod


Having installed SBT as already suggested, consider this from within a Vim editor session,

:!sbt run

In addition, for compiling or packaging,

:!sbt compile
:!sbt package
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜