2015-07-04 04:29:25 +00:00
|
|
|
|
;;; packages.el --- Java functions File for Spacemacs
|
|
|
|
|
;;
|
2018-01-04 07:00:25 +00:00
|
|
|
|
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
|
2015-07-04 04:29:25 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Lukasz Klich <klich.lukasz@gmail.com>
|
2016-01-12 02:40:54 +00:00
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
2015-07-04 04:29:25 +00:00
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2017-02-09 22:22:22 +00:00
|
|
|
|
(defun spacemacs//java-define-command-prefixes ()
|
|
|
|
|
"Define command prefixes for java-mode."
|
2018-03-03 21:44:16 +00:00
|
|
|
|
(setq java/key-binding-prefixes '(("md" . "eclimd")
|
|
|
|
|
("mE" . "errors")
|
2017-02-09 22:22:22 +00:00
|
|
|
|
("mf" . "find")
|
|
|
|
|
("mg" . "goto")
|
|
|
|
|
("mr" . "refactor")
|
|
|
|
|
("mh" . "documentation")
|
|
|
|
|
("ma" . "ant")
|
|
|
|
|
("mp" . "project")
|
|
|
|
|
("mt" . "test")))
|
|
|
|
|
(mapc (lambda(x) (spacemacs/declare-prefix-for-mode
|
|
|
|
|
'java-mode (car x) (cdr x)))
|
|
|
|
|
java/key-binding-prefixes))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-backend ()
|
|
|
|
|
"Conditionally setup java backend."
|
|
|
|
|
(pcase java-backend
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`meghanada (spacemacs//java-setup-meghanada))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(`eclim (spacemacs//java-setup-eclim))
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`ensime (spacemacs//java-setup-ensime))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-company ()
|
|
|
|
|
"Conditionally setup company based on backend."
|
|
|
|
|
(pcase java-backend
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`meghanada (spacemacs//java-setup-meghanada-company))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(`eclim (spacemacs//java-setup-eclim-company))
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`ensime (spacemacs//java-setup-ensime-company))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-flycheck ()
|
|
|
|
|
"Conditionally setup flycheck based on backend."
|
|
|
|
|
(pcase java-backend
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`meghanada (spacemacs//java-setup-meghanada-flycheck))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(`eclim (spacemacs//java-setup-eclim-flycheck))
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`ensime (spacemacs//java-setup-ensime-flycheck))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-flyspell ()
|
|
|
|
|
"Conditionally setup flyspell based on backend."
|
|
|
|
|
(pcase java-backend
|
2017-03-19 17:15:33 +00:00
|
|
|
|
(`ensime (spacemacs//java-setup-ensime-flyspell))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-eldoc ()
|
|
|
|
|
"Conditionally setup eldoc based on backend."
|
|
|
|
|
(pcase java-backend
|
2017-03-19 17:15:33 +00:00
|
|
|
|
;; meghanada setup eldoc on its own
|
|
|
|
|
(`ensime (spacemacs//java-setup-ensime-eldoc))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-09 21:45:27 +00:00
|
|
|
|
|
|
|
|
|
;; ensime
|
|
|
|
|
|
|
|
|
|
(autoload 'ensime-config-find-file "ensime-config")
|
|
|
|
|
(autoload 'ensime-config-find "ensime-config")
|
|
|
|
|
(autoload 'projectile-project-p "projectile")
|
|
|
|
|
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(defun spacemacs//java-setup-ensime ()
|
|
|
|
|
"Setup ENSIME."
|
|
|
|
|
;; jump handler
|
2017-06-04 19:20:42 +00:00
|
|
|
|
(add-to-list 'spacemacs-jump-handlers-java-mode 'ensime-edit-definition)
|
2017-02-09 22:22:38 +00:00
|
|
|
|
;; ensure the file exists before starting `ensime-mode'
|
2017-02-09 21:45:27 +00:00
|
|
|
|
(cond
|
|
|
|
|
((and (buffer-file-name) (file-exists-p (buffer-file-name)))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(ensime-mode))
|
2017-02-09 21:45:27 +00:00
|
|
|
|
((buffer-file-name)
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(add-hook 'after-save-hook 'ensime-mode nil t))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-ensime-company ()
|
|
|
|
|
"Setup ENSIME auto-completion.")
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-ensime-flycheck ()
|
|
|
|
|
"Setup ENSIME syntax checking.")
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-ensime-flyspell ()
|
|
|
|
|
"Setup ENSIME spell checking."
|
|
|
|
|
(flyspell-mode)
|
|
|
|
|
(setq-local flyspell-generic-check-word-predicate
|
|
|
|
|
'spacemacs//ensime-flyspell-verify))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-ensime-eldoc ()
|
|
|
|
|
"Setup ENSIME eldoc."
|
|
|
|
|
(setq-local eldoc-documentation-function
|
|
|
|
|
(lambda ()
|
|
|
|
|
(when (ensime-connected-p)
|
2017-06-19 10:50:18 +00:00
|
|
|
|
(ensime-type-at-point))))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(eldoc-mode))
|
2017-02-09 21:45:27 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//ensime-maybe-start ()
|
|
|
|
|
(when (buffer-file-name)
|
|
|
|
|
(let ((ensime-buffer (spacemacs//ensime-buffer-for-file (buffer-file-name)))
|
|
|
|
|
(file (ensime-config-find-file (buffer-file-name)))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(is-source-file (s-matches? (rx (or "/src/" "/test/"))
|
|
|
|
|
(buffer-file-name))))
|
2017-02-09 21:45:27 +00:00
|
|
|
|
|
|
|
|
|
(when (and is-source-file (null ensime-buffer))
|
|
|
|
|
(noflet ((ensime-config-find (&rest _) file))
|
|
|
|
|
(save-window-excursion
|
|
|
|
|
(ensime)))))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ensime-buffer-for-file (file)
|
|
|
|
|
"Find the Ensime server buffer corresponding to FILE."
|
|
|
|
|
(let ((default-directory (file-name-directory file)))
|
|
|
|
|
(-when-let (project-name (projectile-project-p))
|
|
|
|
|
(--first (-when-let (bufname (buffer-name it))
|
|
|
|
|
(and (s-contains? "inferior-ensime-server" bufname)
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(s-contains? (file-name-nondirectory project-name)
|
|
|
|
|
bufname)))
|
2017-02-09 21:45:27 +00:00
|
|
|
|
(buffer-list)))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ensime-flyspell-verify ()
|
|
|
|
|
"Prevent common flyspell false positives in scala-mode."
|
|
|
|
|
(and (flyspell-generic-progmode-verify)
|
|
|
|
|
(not (s-matches? (rx bol (* space) "package") (current-line)))))
|
|
|
|
|
|
|
|
|
|
;; interactive functions
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-gen-and-restart()
|
|
|
|
|
"Regenerate `.ensime' file and restart the ensime server."
|
|
|
|
|
(interactive)
|
|
|
|
|
(progn
|
|
|
|
|
(sbt-command ";ensimeConfig;ensimeConfigProject")
|
|
|
|
|
(ensime-shutdown)
|
|
|
|
|
(ensime)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-inf-eval-buffer-switch ()
|
|
|
|
|
"Send buffer content to shell and switch to it in insert mode."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ensime-inf-eval-buffer)
|
|
|
|
|
(ensime-inf-switch)
|
|
|
|
|
(evil-insert-state))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-inf-eval-region-switch (start end)
|
|
|
|
|
"Send region content to shell and switch to it in insert mode."
|
|
|
|
|
(interactive "r")
|
|
|
|
|
(ensime-inf-switch)
|
|
|
|
|
(ensime-inf-eval-region start end)
|
|
|
|
|
(evil-insert-state))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-refactor-accept ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(funcall continue-refactor)
|
|
|
|
|
(ensime-popup-buffer-quit-function))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-refactor-cancel ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(funcall cancel-refactor)
|
|
|
|
|
(ensime-popup-buffer-quit-function))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-completing-dot ()
|
|
|
|
|
"Insert a period and show company completions."
|
|
|
|
|
(interactive "*")
|
|
|
|
|
(when (s-matches? (rx (+ (not space)))
|
|
|
|
|
(buffer-substring (line-beginning-position) (point)))
|
|
|
|
|
(delete-horizontal-space t))
|
|
|
|
|
(company-abort)
|
|
|
|
|
(insert ".")
|
|
|
|
|
(company-complete))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-yank-type-at-point ()
|
|
|
|
|
"Yank to kill ring and print short type name at point to the minibuffer."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ensime-type-at-point t nil))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ensime-yank-type-at-point-full-name ()
|
|
|
|
|
"Yank to kill ring and print full type name at point to the minibuffer."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ensime-type-at-point t t))
|
|
|
|
|
|
|
|
|
|
|
2017-02-09 22:22:38 +00:00
|
|
|
|
;; eclim
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-eclim ()
|
|
|
|
|
"Setup Eclim."
|
|
|
|
|
;; jump handler
|
|
|
|
|
(add-to-list 'spacemacs-jump-handlers '(eclim-java-find-declaration :async t))
|
|
|
|
|
;; enable eclim
|
|
|
|
|
(eclim-mode))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-eclim-company ()
|
|
|
|
|
"Setup Eclim auto-completion."
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-emacs-eclim
|
|
|
|
|
:modes eclim-mode
|
2018-05-12 06:33:52 +00:00
|
|
|
|
:variables
|
|
|
|
|
company-idle-delay 0.5
|
|
|
|
|
company-minimum-prefix-length 1
|
|
|
|
|
:append-hooks nil
|
|
|
|
|
:call-hooks t)
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(company-mode))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-eclim-flycheck ()
|
|
|
|
|
"Setup Eclim syntax checking."
|
|
|
|
|
(flycheck-eclim-setup)
|
2017-02-15 04:22:29 +00:00
|
|
|
|
;; disable auto check, use `SPC e e'
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(setq eclim-autoupdate-problems nil)
|
2017-02-15 04:22:29 +00:00
|
|
|
|
(set (make-local-variable 'flycheck-check-syntax-automatically) nil)
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(flycheck-mode))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/java-eclim-completing-dot ()
|
2015-05-20 21:06:46 +00:00
|
|
|
|
"Insert a period and show company completions."
|
|
|
|
|
(interactive "*")
|
2015-07-19 09:50:50 +00:00
|
|
|
|
(spacemacs//java-delete-horizontal-space)
|
2015-05-20 21:06:46 +00:00
|
|
|
|
(insert ".")
|
|
|
|
|
(company-emacs-eclim 'interactive))
|
|
|
|
|
|
2017-02-09 22:22:38 +00:00
|
|
|
|
(defun spacemacs/java-eclim-completing-double-colon ()
|
2015-07-19 09:50:50 +00:00
|
|
|
|
"Insert double colon and show company completions."
|
|
|
|
|
(interactive "*")
|
|
|
|
|
(spacemacs//java-delete-horizontal-space)
|
|
|
|
|
(insert ":")
|
|
|
|
|
(let ((curr (point)))
|
|
|
|
|
(when (s-matches? (buffer-substring (- curr 2) (- curr 1)) ":")
|
|
|
|
|
(company-emacs-eclim 'interactive))))
|
|
|
|
|
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
;; meghanada
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-meghanada ()
|
|
|
|
|
"Setup Meghanada."
|
|
|
|
|
(require 'meghanada)
|
|
|
|
|
;; jump handler
|
|
|
|
|
(add-to-list 'spacemacs-jump-handlers
|
|
|
|
|
'(meghanada-jump-declaration
|
|
|
|
|
:async spacemacs//java-meghanada-server-livep))
|
|
|
|
|
;; auto-install meghanada server
|
|
|
|
|
(let ((dest-jar (meghanada--locate-server-jar)))
|
|
|
|
|
(unless dest-jar
|
|
|
|
|
(meghanada-install-server)))
|
|
|
|
|
;; enable meghanada
|
|
|
|
|
(meghanada-mode))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-meghanada-company ()
|
|
|
|
|
"Setup Meghanada auto-completion."
|
|
|
|
|
(meghanada-company-enable))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-setup-meghanada-flycheck ()
|
|
|
|
|
"Setup Meghanada syntax checking."
|
2018-05-14 02:39:17 +00:00
|
|
|
|
(when (spacemacs/enable-flycheck 'java-mode)
|
|
|
|
|
(require 'flycheck-meghanada)
|
|
|
|
|
(add-to-list 'flycheck-checkers 'meghanada)
|
|
|
|
|
(flycheck-mode)))
|
2017-02-09 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-meghanada-server-livep ()
|
|
|
|
|
"Return non-nil if the Meghanada server is up."
|
|
|
|
|
(and meghanada--client-process (process-live-p meghanada--client-process)))
|
|
|
|
|
|
2017-02-09 21:45:27 +00:00
|
|
|
|
|
|
|
|
|
;; Maven
|
2015-07-19 09:50:50 +00:00
|
|
|
|
|
2017-12-17 23:39:55 +00:00
|
|
|
|
(defun spacemacs/mvn-clean-compile ()
|
|
|
|
|
"Recompile using maven."
|
2015-05-20 21:06:46 +00:00
|
|
|
|
(interactive)
|
2017-12-17 23:39:55 +00:00
|
|
|
|
(mvn-clean)
|
|
|
|
|
(mvn-compile))
|
2015-05-20 21:06:46 +00:00
|
|
|
|
|
2017-12-17 23:39:55 +00:00
|
|
|
|
|
|
|
|
|
;; Gradle
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/gradle-clean ()
|
|
|
|
|
"Execute 'gradle clean' command."
|
|
|
|
|
(interactive)
|
|
|
|
|
(gradle-execute "clean"))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/gradle-clean-build ()
|
|
|
|
|
"Execute 'gradle clean build' command."
|
2015-05-20 21:06:46 +00:00
|
|
|
|
(interactive)
|
2017-12-17 23:39:55 +00:00
|
|
|
|
(gradle-execute "clean build"))
|
2015-05-20 21:06:46 +00:00
|
|
|
|
|
2017-12-17 23:39:55 +00:00
|
|
|
|
(defun spacemacs/gradle-test-buffer ()
|
|
|
|
|
"Execute 'gradle test' command against current buffer tests."
|
2015-05-20 21:06:46 +00:00
|
|
|
|
(interactive)
|
2017-12-17 23:39:55 +00:00
|
|
|
|
(gradle-single-test (file-name-base (buffer-file-name))))
|
2017-02-09 21:45:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Misc
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//java-delete-horizontal-space ()
|
|
|
|
|
(when (s-matches? (rx (+ (not space)))
|
|
|
|
|
(buffer-substring (line-beginning-position) (point)))
|
|
|
|
|
(delete-horizontal-space t)))
|