These days, I was working on a React Native project. In the meanwhile, I found it’s kind of hard to
catch up the modern syntax of Javascript, so as for imenu (a built in project structure menu bar in
Emacs).
Fortunately, there are so many people love Emacs and contribute to it. Thanks for Chen bin’s great
post. I just put down more
imenu generic expressions for modern Javascript like React, Vue. This helps me navigate and go
through projects.
Note: To get functions like componentDidMount, and exclude if, for, while, plain regexp in
Emacs seems not working. At least I couldn’t find an easy solution like [^if|for|while]. But
since imenu-generic-expression takes functions as parameters, I created another function to wrap
one generic expression. Finally it works but as you can see, it’s not beautiful.
;; this imenu generic expression aims to exclude for, while, if when aims to match functions in ;; es6 js, e.g. ComponentDidMount(), render() function in React ;; https://emacs-china.org/t/topic/4538/7 (defun js-exception-imenu-generic-expression-regexp () ;; (async)? xxx (e) { } (if (re-search-backward "^[ \t]*\\(async\\)?[ \t]*\\([A-Za-z_$][A-Za-z0-9_$]+\\)[ \t]*([a-zA-Z0-9, ]*) *\{ *$" nil t) (progn (if (member (match-string 2) '("for" "if" "while" "switch")) (js-exception-imenu-generic-expression-regexp) t)) nil))