Use which-func-mode with js2-mode

  |   Source

Two years ago in my article "Why Emacs is better editor - a case study for javascript developer" I proved that Emacs is a better javascript editor because its js2-mode can parse the tags ) in opened file more intelligently.

Since then, the js2-mode keeps improving and becomes better every day. I'm absolutely satisfied with it except one minor issue.

For performance reason, js2-mode place the imenu results into a cache which is updated by a timer.

`(which-function)` from which-func-mode takes advantage of the imenu results. So the result of `(which-function)` may be incorrect if the cache is not updated by timer.

The solution is to re-define a `my-which-function`:

(defun my-which-function ()
  ;; clean the imenu cache
  ;; @see http://stackoverflow.com/questions/13426564/how-to-force-a-rescan-in-imenu-by-a-function
  (setq imenu--index-alist nil)
  (which-function))

The new API is very useful when I use yasnippet to insert logging code in javascript file.

Here is a sample (log-which-func.yasnippet):

# -*- coding: utf-8; mode: snippet -*-
# name: console.log which function called
# key: lwf
# --
console.log('${1:`(my-which-function)`} called');
Comments powered by Disqus