tern icon indicating copy to clipboard operation
tern copied to clipboard

CoffeeScript

Open Tranquility opened this issue 10 years ago • 28 comments

Is there a way to use tern with CoffeeScript (in vim)?

Tranquility avatar Aug 25 '13 12:08 Tranquility

There's a hack at https://github.com/mntmn/tern-coffeescript , but that's not very mature. More work might be done on this in the future, but I can't promise anything.

marijnh avatar Aug 25 '13 12:08 marijnh

I have taken a quick look at the implementation. It seems @mntmn reimplemented source maps for coffeescript because it wasnt available at the time tern was released. Now it should be more trivial since the coffeescript compiler natively supports v3 source maps. I'm going to give it a shot using the current version of the coffeescript compiler.

I was thinking of adding some server hooks that would maintain maps of coffeescript filenames/source maps that get used transparently when queries are made with the coffeescript plugin loaded. @marijnh, is something like this currently possible? If so, can you provide a simple example?

If such extension is not currently possible then it might be worth adding it, since there quite a few compile-to-js tools out there(coffeescript, typescript, traceur...) that could make use of it.

tarruda avatar Sep 16 '13 23:09 tarruda

A rough form of CoffeeScript support will be added soon. It will not have error-tolerant parsing until someone puts in the work of implementing such a parser for CoffeeScript.

marijnh avatar Sep 17 '13 07:09 marijnh

@marijnh that is nice to know. I also tought a bit about the error-tolerant parsing issue and a few possible hacks to partially work around it. What do you think of this:

1- Try to parse the current file 2- If the error ocurrs after the line being completed, try to parse only up to the line before the completion point(since coffeescript uses indentation as block delimiter this might work) 3- If the error occurs before the line being completed, replace the line where the error happened with an empty line and try again. 4- If everything failed, use the result of the previous compilation

A big problem with this approach is that it will be very expensive for editors that use plugins like vim's youcompleteme which queries for completions without user the user explicitily asking for it, but might be worth a shot anyway.

tarruda avatar Sep 17 '13 09:09 tarruda

HI

I have try to complete this. My first principle is not to hack tern.js. So I use plugin and add a new command 'coffee'. And update tern_for_vim to use the new command.

I use mntmn's approach to do the job. The new 'coffee' command is a pre parser. It will rename the file.name and convert file.text from coffeescript to javascript. Calculate the end position. Then send to server to do a real request.

And since coffeescript compiler is not error tolerance. I do a little hack try to make coffeescript can be compile. ex: obj. will become obj.__fake__, then the __fake__ part will be removed before send to the server.

This approach have some issues can't fix.

First, It's not possible to deal with required file. Other plugin like requirejs and node will not call the new 'coffee' command. If tern.js can allow plugin to hijack any request before it do anything at request method. This issue should be possible to fix. And I don't need to add new command. Will much easier to integrate with other editor.

Second is about file name extension. And for now, requirejs and node all think file extension is 'js'. It means coffeescript will not be loaded by this two plugin. So I think it should have a list by order. tern.js should check one by one in getFile.

The last problem is the command run function. Because its sync function. I can't real use my current approach on some condition. ex: If getFile is async mode, my solution will fail.

But if the first 2 problem can be solve. Then the last one is not a problem anymore. The first one is easy. But the second one is not that easy. So I think I should see if you have any idea before did any hack.

My workable version is here:

I install tern in my tern_for_vim by clone it directly. Don't forget to npm install in tern.

othree avatar Jan 31 '14 06:01 othree

Hi @othree , could you create a branch with only these changes, in a few structured commits, to make it easier for me to look at them?

marijnh avatar Feb 13 '14 12:02 marijnh

Hi @marijnh , I have another post in tern-dev group about this. My current implementation solves problems I mentioned above. But I didn't fine tune the code. I spend lots of time to make ternjs works on Backbone projects in recent days. So I will explain what I did here instead. (I am willing to send a pull request with clean commit. But I want to fine tune it first.)

I overwrite getFile method to find possible coffee files. If a coffee script is found. It will compile it and send back. If not find any coffee script file. It will look for js file.

I also overwrite request to remember the mapping information. And the mapping information comes from @mntmn's code.

The last part I did is to modify coffee script to make it possible to compile. But this process is only on part file request. I did 2 modification:

  • If cursor is at . and no following attribute name. I will add a fake attribute and remove it before send to tern server.
  • Detect file content indent. Trim undesired indent lines. In both direction.

othree avatar Feb 13 '14 13:02 othree

is there any update on this...

cooolbasha avatar Apr 12 '14 01:04 cooolbasha

+1

joaoafrmartins avatar Apr 14 '14 10:04 joaoafrmartins

The original project (where I claimed that support would be added soon) fell through. I still haven't seen a serious implementation of this.

marijnh avatar Apr 16 '14 16:04 marijnh

this is rather an extensive project... i tried the plugin on sublime with @marijnh implementation and although it works its not really what one would call quality code completion... what would be the requirements for such an implementation?

joaoafrmartins avatar Apr 16 '14 17:04 joaoafrmartins

+1

PEM-- avatar Apr 22 '14 21:04 PEM--

what would be teh requirements for full coffeescript integration with tern? a recursive descent parser simillar to acorn?

joaoafrmartins avatar Apr 23 '14 18:04 joaoafrmartins

+1

romanzolotarev avatar Aug 06 '14 21:08 romanzolotarev

+1

calebl avatar Oct 29 '14 13:10 calebl

+1

dcsan avatar Nov 03 '14 07:11 dcsan

Stop adding '+1' posts here. If you want to be notified of further activity, subscribe to this issue. If you want something to actually happen, organize for someone to pay me to work on this.

marijnh avatar Nov 03 '14 10:11 marijnh

You could put some gittips on the project(now apparently gratipay - you can imagine searching for git tips...), or on a per request basis if possible, I would give a few bucks for coffeescript support. Great job with all your other projects and writings btw, you always seem to popup in javascript land ;)

fpaboim avatar Nov 08 '14 14:11 fpaboim

@marijnh how have people generally organized to pay you for a feature? Do you have some sort of bounty system you prefer, since there are several out there?

jeffling avatar Feb 12 '15 09:02 jeffling

I've set up crowd-funding campaigns before, but there haven't been any user-organized funding drives. Bountysource looks like a decent platform for this, but I have no strong preference.

marijnh avatar Feb 12 '15 09:02 marijnh

I started refactoring @othree's tern-coffee plugin: https://github.com/katspaugh/tern-coffee The main difference is it's using Mozilla's source-map module. The fork is unfinished and I would rather help fund someone else to solve this issue.

@marijnh, can we reopen this issue to start crowdfunding it on Bountysource (https://www.bountysource.com/issues/1607447-coffeescript)?

katspaugh avatar Apr 11 '15 21:04 katspaugh

I am currently super busy with other things. Once I have time for Tern, ES6 support is first, but after that, I can revisit compile-to-JS language support. Not sure why I closed this -- reopening.

marijnh avatar Apr 29 '15 11:04 marijnh

Great, thanks!

I've posted a small bounty, guys please join!

katspaugh avatar Apr 29 '15 13:04 katspaugh

+1

davibe avatar Jun 02 '15 07:06 davibe

Link to crowd-funding of this issue:

https://www.bountysource.com/issues/1607447-coffeescript

katspaugh avatar Jun 02 '15 08:06 katspaugh

@marijnh how much funding would you need for CS support?

danihodovic avatar Jun 21 '15 11:06 danihodovic

+1

zeekay avatar Aug 22 '15 09:08 zeekay

On the same note, I was looking at tern for ClojureScript completion as we seem not to have a good completion story for it at the moment, especially for JavaScript objects.

arichiardi avatar Mar 30 '17 05:03 arichiardi