ternjs / tern_for_sublime

Sublime Text package adding Tern support
MIT License
803 stars 54 forks source link

Insert arguments when calling a function #49

Closed danihodovic closed 9 years ago

danihodovic commented 9 years ago

Is it possible to automatically insert function arguments when entering the parenthesis on a function that is going to be called? Right now there is an implementation that hints of the arguments the moment you enter the parenthesis, but it would be very handy to automatically insert them and jump into the selections of the arguments to directly edit the names. The current implementation that hints of the arguments and the possible types has great functionality, but isn't very useful as the panel is barely visible.

/**
 * @param {number} arg1
 * @param {Object} arg2
 */
fun(${1:arg1}, ${2:arg2}...) {}

I've tried implementing this by hacking render_argument_hints(*args) at line 398 that intends to display the function argument hints. It worked fairly ok, until I realized it needs some sort of context. If a new function is being created, inserting the message that is being hinted is wrong. This is also breaks when you try to edit a functions parameters, it simply inserts them again.

On a side note, would it be possible to implement this auto-insertion for object literals passed to functions (sometimes as optional parameters)? I've tried, and Tern, can read the types of a object literal if annotated with a specific JSDoc format.

Example:

/**
 * This format CANT be read, although it's a little more readible in some situations
 *  @param  {Object} obj
 *  @param  {obj.hello} hello  optional argument to hello
 */
function fn1(obj) {
    // body...
}

/**
 *  This format CAN be read, but it would be great if the arguments could be auto-inserted on a function call
 *  @param  {{a: number, b: string}} obj
 */
function fn2(obj) {
    // body...
}
marijnh commented 9 years ago

It would definitely be possible to do this, but the current tern_for_sublime code does not implement it.

danihodovic commented 9 years ago

Do you think it would be easier to write a new Tern plugin or add functionality here?

marijnh commented 9 years ago

A new plugin would probably be best. Though if you need to make small changes to the main tern_for_sublime module in order to be able to reuse its code in another plugin, I'd be fine with accepting such pull requests. (The code is not currently written with external reuse in mind.)