This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+lang/scala/funcs.el
Diego Alvarez 9b460f60e4 Add function to insert types and add yank type at point
- `, i y` Add function to yank type at point but just yank type name without full
package

- `, r a` Add function to insert type for declaration,
see `https://github.com/ensime/ensime-emacs/pull/390/files`
2016-04-01 00:26:20 -04:00

111 lines
3.6 KiB
EmacsLisp

;;; funcs.el --- Scala Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;;; Ensime
(autoload 'ensime-config-find-file "ensime-config")
(autoload 'ensime-config-find "ensime-config")
(autoload 'projectile-project-p "projectile")
(defun scala/configure-ensime ()
"Ensure the file exists before starting `ensime-mode'."
(cond
((and (buffer-file-name) (file-exists-p (buffer-file-name)))
(ensime-mode +1))
((buffer-file-name)
(add-hook 'after-save-hook (lambda () (ensime-mode +1)) nil t))))
(defun scala/maybe-start-ensime ()
(when (buffer-file-name)
(let ((ensime-buffer (scala/ensime-buffer-for-file (buffer-file-name)))
(file (ensime-config-find-file (buffer-file-name)))
(is-source-file (s-matches? (rx (or "/src/" "/test/")) (buffer-file-name))))
(when (and is-source-file (null ensime-buffer))
(noflet ((ensime-config-find (&rest _) file))
(save-window-excursion
(ensime)))))))
(defun scala/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)
(s-contains? (file-name-nondirectory project-name) bufname)))
(buffer-list)))))
(defun scala/enable-eldoc ()
(setq-local eldoc-documentation-function
(lambda ()
(when (ensime-connected-p)
(ensime-print-type-at-point))))
(eldoc-mode +1))
(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))
;;; Interactive commands
(defun spacemacs/scala-join-line ()
"Adapt `scala-indent:join-line' to behave more like evil's line join.
`scala-indent:join-line' acts like the vanilla `join-line',
joining the current line with the previous one. The vimmy way is
to join the current line with the next.
Try to move to the subsequent line and then join. Then manually move
point to the position of the join."
(interactive)
(let (join-pos)
(save-excursion
(goto-char (line-end-position))
(unless (eobp)
(forward-line)
(call-interactively 'scala-indent:join-line)
(setq join-pos (point))))
(when join-pos
(goto-char join-pos))))
(defun scala/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))
(insert ".")
(company-complete))
;;; Flyspell
(defun scala/flyspell-verify ()
"Prevent common flyspell false positives in scala-mode."
(and (flyspell-generic-progmode-verify)
(not (s-matches? (rx bol (* space) "package") (current-line)))))
(defun scala/configure-flyspell ()
(setq-local flyspell-generic-check-word-predicate 'scala/flyspell-verify))
(defun scala/yank-type-at-point ()
"Yank to kill ring and print type at point to the minibuffer without package name."
(interactive)
(let* ((type (ensime-rpc-get-type-at-point))
(shortname (ensime-type-short-name-with-args type)))
(kill-new shortname)
(message shortname)))