我想通过方法跳转我的 java 文件,例如当我走到任何地方时,只需使用一个键盘快捷键即可跳转到方法的下一个结尾或方法的开头。
Emacs 使用 C-M-a 和 C-M-e 的“通过 defuns 移动”对于 C 非常有用并且完全符合我的要求。但显然在 Java 中,defun 是一个完整的类。
按 defuns 移动: http://www.gnu.org/software/emacs/manual/html_node/emacs/Moving-by-Defuns.html
我发现我可以强制 C-M-f 和 C-M-b 做我想做的事情。它们在任何括号平衡的表达式上向前和向后移动。问题是,当从方法定义的左括号或右括号外调用时,它们只有我正在寻找的功能,这是非常有限的。
带平衡括号的表达式: http://www.delorie.com/gnu/docs/emacs/emacs_282.html
欢迎任何想法!
最佳答案
否则你可以自己定义。 你可以从这样的事情开始:
(defvar java-function-regexp
(concat
"^[ \t]*" ; leading white space
"\\(public\\|private\\|protected\\|" ; some of these 8 keywords
"abstract\\|final\\|static\\|"
"synchronized\\|native"
"\\|[ \t\n\r]\\)*" ; or whitespace
"[a-zA-Z0-9_$]+" ; return type
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]+" ; whitespace
"\\([a-zA-Z0-9_$]+\\)" ; the name we want!
"[ \t\n\r]*" ; optional whitespace
"(" ; open the param list
"\\([ \t\n\r]*" ; optional whitespace
"\\<[a-zA-Z0-9_$]+\\>" ; typename
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]+" ; whitespace
"\\<[a-zA-Z0-9_$]+\\>" ; variable name
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]*,?\\)*" ; opt whitespace and comma
"[ \t\n\r]*" ; optional whitespace
")" ; end the param list
))
(defun my:next-java-method()
(interactive)
(re-search-forward java-function-regexp nil t)
)
(defun my:prev-java-method()
(interactive)
(re-search-backward java-function-regexp nil t)
)
然后将 my:next-java-method
和 my:prev-java-method
绑定(bind)到您想要转到的任何键上
关于java - 在 emacs 中跳转 java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127959/