0

JAVASCRIPT :

A = {
    x : funciton(){

    },
    y : function(){

    }
}

What do I want?

I want to write a sublime snippet which when invoked inside x or y function, should be able to get the function name and put it.

Example - The DocBlockr plugin automatically gets the name/arguments of a method before which you create the block by hittin /** and hit Enter

I know how to write basic snippets but not sure whether a snippet can get the names or tokens from a file. Not even sure if it is possible.

NOTE - I could not do much by myself as I cant figure out where to start for this one.

Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68

2 Answers2

0

This is not possible with Snippets. They cant "look" at the surrounding code and do actions based on that. You would have to write a Sublimetext-plugin do to that.

Looking at the sourcecode of DocBlockr should give you an idea how to do that; The file jsdocs.py contains methods like parseFunction() and getArgType(), which extract the functionname and parameters using Regular Expressions.

LinuCC
  • 410
  • 5
  • 9
0

NOTE: this doesn't directly answer the question, but chances are it solves your problem - if it happens that you just need it for debugging purposes. I'd submit it as a comment, but not enough reputation yet.

Try this:

arguments.callee.name

However note:

  • this is not a ST feature, it's a JS feature to get function name
  • this feature is actually discouraged, chances are it can hurt performance - so development mode only
  • it's actually forbidden in strict mode
  • it won't work with anonymous functions like in your snippet - you will need to give your functions a name x: function x() {}
  • some browsers may not support name properties for functions