22 January 2011

Using CoffeeScript in RubyMine 3

The other day I was looking for a way to see the output of the CoffeeScript compiler from within RubyMine, and I saw a mention of CoffeeBrew, which is an alternative syntax highlighting plugin to coffeescript-idea.

As an aside, when I moved to RubyMine 3, coffeescript-idea wouldn't install for me, so I had to manually copy the plugin .jar file from RubyMine 2 to RubyMine 3.

CoffeeBrew has a couple of advantages over coffeescript-idea:
  • block comments!
  • matching brace highlighting
At the moment it has a problem with highlighting interpolated expressions within strings, but the author is looking into a fix. The plugin can be installed from RubyMine.

Back to showing the CoffeeScript compiler output in RubyMine:
  • Open Settings and go to External Tools
  • Add a new tool with these parameters:
    • Program: coffee
    • Parameters: -c -o public/javascripts --watch app/scripts (I store *.coffee files in app/scripts and compiled JavaScript goes into public/javascripts)
    • Working directory: $ProjectFileDir$
  • Start the tool from the Tools menu, and you will get a window that shows the output of the compiler. The compiler will run every time you save changes to a *.coffee file in the watched directory.
Next on my wishlist: support for CoffeeScript navigation (such as going to class declaration). 

9 comments:

  1. nice, but I could not get it to run under that directory using your tool, and I haven't found a way to force it to using full paths to coffee and the source targets. I know it compiles because the command works via the command line using explit directory paths...so I don't know what is up...

    ReplyDelete
  2. I'm not sure what the problem is from your description. Does RubyMine report any errors? Are you able to start the external tool?

    ReplyDelete
  3. yes externally, I can compile. When I do so, I do not need to explicitly define paths to the source, destination or "coffee" command. It works similar to how you configured it.

    However, if I use a tool within ruby mine 3, and I use your example OR I explicity tell it where source, destination and coffee command is, it gives me an error: Cannot compile -2

    Heres an explicit form that fails:

    /Applications/local/nodejs/bin/coffee -c -o /Users/FOO/Documents/source_code/myproject/public/javascripts/custom --watch /Users/FOO/Documents/source_code/myproject/app/coffeescripts
    env: node: No such file or directory

    Process finished with exit code 127


    Yet this worked from Terminal:

    /Applications/local/nodejs/bin/coffee -c -o /Users/FOO/Documents/source_code/myproject/public/javascripts/custom --watch /Users/FOO/Documents/source_code/myproject/app/coffeescripts
    15:57:55 GMT-0800 (PST) - compiled /Users/FOO/Documents/source_code/myproject/app/coffeescripts/test.coffee


    go figure...

    ReplyDelete
  4. Hmm, not sure what's going on there. What version of CoffeeScript are you running? I think it only started working with RubyMine from version 0.9.6 or something like that.

    ReplyDelete
  5. Im using RubyMine: 3.1
    Im using coffeescript: 1.0.1

    some strangenessess but I dont think its directory based issues.

    *sigh

    ReplyDelete
  6. I've got RubyMine 3.1 and CoffeeScript 1.0.0 on Ubuntu 10.04. I don't really know what to do about this problem, sorry.

    ReplyDelete
  7. Thanks/\/\/\/

    OSX

    Maybe something will turn up here from someone else over time.

    ReplyDelete
  8. Hello,

    On rubymine 3.1 and coffee 1.1.1
    Try -cwo public/javascripts (-cwo = '--compile, --watch, --output')

    This will compile and save to correspdonding .js file in public/javascripts

    This doesn't seem to run the file....only compile and save the js output to the put put file.

    Now....how to just simply run without having to open a command prompt.....

    ReplyDelete
  9. Adjusted what was done above on my rubymine/coffeescript setup:

    Go to external tools and add: "Coffee_My_Tool_Name"; program: coffee; Paramters: -co public/javascripts app/scripts/$FileName$; Working Directory: $ProjectFileDir$

    This allow me to compile and output the coffeescipt file to a corresponding js file when I want.....when I set watch on the whole directory....funny things happened in the terminal....no errors....files would compile on save.....but no output over the top of existing js files.......

    I then went to Preferences>Keymap>Other and found my tool addition.....and set the key as ctrl+F10

    Now to run:
    Repeat above.....when you get to Parameters....use $FileName$ - and in Working Directory: $FileDir$......

    Save tool and then go to Keymap and use F10....

    This allows me to stack my js file below my coffeescript file (Drag the js editor file window down and dock it half-way)......now I can edit my coffeescript.....press ctrl+F10 to compile and watch changes in the js file.....and then press F10 to watch my script run.

    Rubymine 3.1 and coffeescript 1.1.1

    ReplyDelete