‘dumb-jump-go’ is an obsolete command (as of 2020-06-26); ‘dumb-jump-go’ has been obsoleted by the xref interface.
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate)
我以前用也是和 xref 一起用的
(defun my-find-definition ()
(interactive)
(condition-case err
(call-interactively #'xref-find-definitions)
(user-error
(cond
((and (featurep 'counsel)
(counsel-etags-locate-tags-file))
(call-interactively #'counsel-etags-find-tag-at-point))
((commandp 'dumb-jump-go) (call-interactively #'dumb-jump-go))))))
意思是你现在只要把按键绑定到xref-find-definitions
,而不再需要绑定到dumb jump的函数,dumb jump现在通过设定xref的backend的方式工作,这样的好处是:
- 抽象说,更好地利用built-in的基础设施,并提供统一的api
- 具体来说,如果有dumb jump不支持的major mode,但是major mode自己提供了find definition的功能,那你只要和别的dumb jump支持的mode一样按下
xref-find-definition
就可以了,不需要考虑dumb jump是否支持/是否用dumb-jump-go
。如果有一天不想用dumb jump了,你只要不再require它,而绑定xref-find-definition
的部分不需要动,major-mode / 其它插件会继续设置合适的backend来让你愉快地跳转。
不过我现在js是用 js2的内置跳转 + xref-js2 了。优先跳到当前buffer的定义,不行再在项目里搜:
(use-package xref-js2
:custom (xref-js2-search-program 'rg)
:init
(add-hook 'js2-mode-hook
(lambda () (add-hook 'xref-backend-functions #'xref-js2-xref-backend nil t)))
(defun jester/js2-jump-or-xref-definition ()
"`js2-jump-to-definition' if we can, `xref-find-definitions' if can't or already at import statement."
(interactive)
(if (save-excursion
(beginning-of-line)
(let ((case-fold-search nil)) (search-forward "import " nil t)))
(xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))
(condition-case err
(js2-jump-to-definition)
(xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend))))))
(general-define-key
:states '(normal motion)
:keymaps '(js2-mode-map)
"g d" 'jester/js2-jump-or-xref-definition
"g r" 'jester/xref-find-references-at-point))
(defun jester/xref-find-references-at-point ()
"Find references for symbol at point."
(interactive)
(xref-find-references (xref-backend-identifier-at-point (xref-find-backend))))
搜到了xref-js2包,以前完全不知道有这玩意,感谢大佬的代码,好用~
上面帖子不能编辑了,勘误一下,上面的condition-case
的用法写错了(奇怪的是我明明记得当初刚写这个函数的时候是可以工作的,可能Emacs版本升级取消了对这种错误写法的兼容):
(defun jester/js2-jump-or-xref-definition ()
"`js2-jump-to-definition' if we can, `xref-find-definitions' if can't or already at import statement."
(interactive)
(if (save-excursion
(beginning-of-line)
(let ((case-fold-search nil)) (search-forward "import " nil t)))
(xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))
(condition-case err
(js2-jump-to-definition)
(error (xref-find-definitions (xref-backend-identifier-at-point (xref-find-backend)))))))
其实也可以这样:(setq dumb-jump-disable-obsolete-warnings t)
.