3

I want to be able to quickly go through all invocations of a function inside a file or outside. Currently i use search in all files method. But is there a way to see where this method was used.

Optional: Also i'd want to go back in other direction as well. Say there is a method call like this:

makeBread();

Now i want to see what the function do. so somehow jump to its declaration.

hata
  • 11,633
  • 6
  • 46
  • 69
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

2 Answers2

1

Find invocation

Trying to use text search to find invocations may easily betray you. Consider this:

function myFunction() {
    console.log("Hello :)");
}

document.getElementById("page-title").addEventListener("click", myFunction);

I think you understand where this is going - if you want to get a list of invocations, best bet is to use console.trace at runtime:

function myFunction() {
    console.trace();
    console.log("Hello :)");
}

Find what the function does

The function can be overriden at runtime. Dynamic languages cannot be analysed like static ones (C++, Java). You wanna know what the function does? Print it in the console at runtime:

console.log(makeBread.toString());

Find declaration

Again, console.trace will tell you the line for every function it came through. You can also get the stack trace as array - but beware that this slows execution a lot, so don't do it in production.

Conclusion

As I said, you cannot inspect dynamic languages where anything can be anything using any IDE reliably. Most IDEs give good hints though, just conbine them with runtime debuging. Consider debuging running application more fun than looking ad the dead code.

More reading

If you want to make some reguler expressions, this is gonna be handy: http://www.bryanbraun.com/2014/11/27/every-possible-way-to-define-a-javascript-function

The console object: https://developer.mozilla.org/en-US/docs/Web/API/Console Using stack trace: https://stackoverflow.com/a/635852/607407

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
1

Not natively, but with plugins, yes

A popular plugin that has this functionality is SublimeCodeIntel:

SublimeCodeIntel will also allow you to jump around symbol definitions even across files with just a click ..and back.

For Mac OS X:

  • Jump to definition = Control+Click
  • Jump to definition = Control+Command+Alt+Up
  • Go back = Control+Command+Alt+Left
  • Manual Code Intelligence = Control+Shift+space

For Linux:

  • Jump to definition = Super+Click
  • Jump to definition = Control+Super+Alt+Up
  • Go back = Control+Super+Alt+Left
  • Manual Code Intelligence = Control+Shift+space

For Windows:

  • Jump to definition = Alt+Click
  • Jump to definition = Control+Windows+Alt+Up
  • Go back = Control+Windows+Alt+Left
  • Manual Code Intelligence = Control+Shift+space
gfullam
  • 11,531
  • 5
  • 50
  • 64
  • `[ { "button": "button1", "modifiers": ["ctrl"], "command": "goto_python_definition", "press_command": "drag_select" } ] ` – Muhammad Umer Oct 13 '15 at 14:09
  • I can't troubleshoot plugin-specific issues. You may have conflicting key bindings; if so, this may be solved by [customizing the User key bindings](http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/customization/key_bindings.html). – gfullam Oct 13 '15 at 14:19
  • also question is how to find where function has been invoked not where it's defined – Muhammad Umer Oct 14 '15 at 14:42