Parag
Parag

Reputation: 963

Emacs print javascript object properties or object outline in a buffer

I have a javascript object, like so:

var myObj = {
    prop1 : 'abc',
    prop2: function(){
              return true;
           },
    prop3: function(){},
    ... // and so on
};

Is there a way in emacs to see a list of all the properties myObj has? And I can click on that property and it takes me to the occurrence in the javascript file. Something on the lines of M-x occur, which shows you "clickable" occurrences in a new buffer.

I could do M-x list-js-props myObj, and it opens up the aforementioned buffer.

Upvotes: 0

Views: 287

Answers (1)

Chris
Chris

Reputation: 137169

I'm not aware of anything that exactly does what you're after, but there are a few things you can try:

  1. The speedbar should show you functions¹ (though it does not show other properties).

    Start it with M-x speedbar, then expand nodes by clicking on the "+" icon. Clicking on a function name should take you to the matching occurrence in your buffer.

    Speedbar works best if you are using a GUI version of Emacs, since it starts a new frame. If you are working in a console, check out SrSpeedbar.

  2. You could use a standalone JavaScript analyzer like Tern with Auto Complete (or with Company) to quickly get suggestions, e.g.

    var myObj = {
        foo = // Hrm, what do I want?
    };
    
    otherObj. // See completions of otherObj here, find the one you want,
              // and complete.
    

    Once you've completed the other object you can use tern-find-definition (bound to M-. by default) to go to the property's definition, and tern-pop-find-definition (bound to M-, by default) to return.

    Note: I didn't have much luck installing Tern via MELPA a few weeks ago. Instead, I followed these instructions from the Tern website.

  3. Finally, js2-mode may offer some help, though I wasn't able to find anything obvious. It is likely the most easy to modify to your needs using elisp, though.

¹Speedbar shows my JavaScript functions out of the box on Emacs 24.3.50.1 on Ubuntu.

Upvotes: 1

Related Questions