2016-05-02 09:11:16 +00:00
|
|
|
|
;;; funcs.el --- TypeScript Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2018-01-04 07:00:25 +00:00
|
|
|
|
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
|
2016-05-02 09:11:16 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
;; backend
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-backend ()
|
|
|
|
|
"Conditionally setup typescript backend."
|
|
|
|
|
(pcase typescript-backend
|
|
|
|
|
(`tide (spacemacs//typescript-setup-tide))
|
|
|
|
|
(`lsp (spacemacs//typescript-setup-lsp))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-company ()
|
|
|
|
|
"Conditionally setup company based on backend."
|
|
|
|
|
(pcase typescript-backend
|
|
|
|
|
(`tide (spacemacs//typescript-setup-tide-company))
|
|
|
|
|
(`lsp (spacemacs//typescript-setup-lsp-company))))
|
|
|
|
|
|
2018-05-12 06:46:51 +00:00
|
|
|
|
(defun spacemacs//typescript-setup-eldoc ()
|
|
|
|
|
"Conditionally setup eldoc based on backend."
|
2018-05-13 13:20:39 +00:00
|
|
|
|
(pcase typescript-backend
|
2018-05-12 06:46:51 +00:00
|
|
|
|
(`tide (spacemacs//typescript-setup-tide-eldoc))
|
|
|
|
|
(`lsp (spacemacs//typescript-setup-lsp-eldoc))))
|
|
|
|
|
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
;; tide
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-tide ()
|
|
|
|
|
"Setup tide backend."
|
|
|
|
|
(progn
|
|
|
|
|
(evilified-state-evilify tide-references-mode tide-references-mode-map
|
|
|
|
|
(kbd "C-k") 'tide-find-previous-reference
|
|
|
|
|
(kbd "C-j") 'tide-find-next-reference
|
|
|
|
|
(kbd "C-l") 'tide-goto-reference)
|
|
|
|
|
(add-to-list 'spacemacs-jump-handlers-typescript-tsx-mode
|
|
|
|
|
'(tide-jump-to-definition :async t))
|
|
|
|
|
(add-to-list 'spacemacs-jump-handlers-typescript-mode
|
2018-05-12 06:46:51 +00:00
|
|
|
|
'(tide-jump-to-definition :async t))
|
|
|
|
|
(tide-setup)))
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-tide-company ()
|
|
|
|
|
"Setup tide auto-completion."
|
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-tide
|
2018-03-31 07:50:23 +00:00
|
|
|
|
:modes typescript-mode typescript-tsx-mode
|
|
|
|
|
:variables
|
|
|
|
|
company-minimum-prefix-length 2)
|
2018-04-27 04:51:30 +00:00
|
|
|
|
(company-mode))
|
|
|
|
|
|
2018-05-12 06:46:51 +00:00
|
|
|
|
(defun spacemacs//typescript-setup-tide-eldoc ()
|
|
|
|
|
"Setup eldoc for tide."
|
|
|
|
|
(eldoc-mode))
|
|
|
|
|
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
;; lsp
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-lsp ()
|
|
|
|
|
"Setup lsp backend."
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
|
|
|
|
(progn
|
2018-05-08 04:17:56 +00:00
|
|
|
|
(spacemacs//setup-lsp-jump-handler 'typescript-mode
|
2018-05-12 06:46:51 +00:00
|
|
|
|
'typescript-tsx-mode)
|
|
|
|
|
(lsp-javascript-typescript-enable))
|
|
|
|
|
(message (concat "`lsp' layer is not installed, "
|
|
|
|
|
"please add `lsp' layer to your dofile."))))
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-lsp-company ()
|
|
|
|
|
"Setup lsp auto-completion."
|
|
|
|
|
(if (configuration-layer/layer-used-p 'lsp)
|
|
|
|
|
(progn
|
2018-04-26 19:43:50 +00:00
|
|
|
|
(fix-lsp-company-prefix)
|
2018-04-27 04:51:30 +00:00
|
|
|
|
(spacemacs|add-company-backends
|
|
|
|
|
:backends company-lsp
|
2018-03-31 07:50:23 +00:00
|
|
|
|
:modes typescript-mode typescript-tsx-mode
|
2018-05-12 06:33:52 +00:00
|
|
|
|
:variables company-minimum-prefix-length 2
|
|
|
|
|
:append-hooks nil
|
|
|
|
|
:call-hooks t)
|
2018-04-27 04:51:30 +00:00
|
|
|
|
(company-mode))
|
2018-05-12 06:46:51 +00:00
|
|
|
|
(message (concat "`lsp' layer is not installed, "
|
|
|
|
|
"please add `lsp' layer to your dofile."))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//typescript-setup-lsp-eldoc ()
|
|
|
|
|
"Setup eldoc for LSP."
|
|
|
|
|
(eldoc-mode))
|
2018-04-27 04:51:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Others
|
|
|
|
|
|
2017-07-14 20:50:34 +00:00
|
|
|
|
(defun spacemacs/typescript-tsfmt-format-buffer ()
|
2016-05-02 09:11:16 +00:00
|
|
|
|
"Format buffer with tsfmt."
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (executable-find "tsfmt")
|
2017-02-11 09:25:04 +00:00
|
|
|
|
(let* ((extension (file-name-extension (or buffer-file-name "tmp.ts") t))
|
|
|
|
|
(tmpfile (make-temp-file "~fmt-tmp" nil extension))
|
2016-05-02 09:11:16 +00:00
|
|
|
|
(coding-system-for-read 'utf-8)
|
|
|
|
|
(coding-system-for-write 'utf-8)
|
|
|
|
|
(outputbuf (get-buffer-create "*~fmt-tmp.ts*")))
|
|
|
|
|
(unwind-protect
|
|
|
|
|
(progn
|
|
|
|
|
(with-current-buffer outputbuf (erase-buffer))
|
|
|
|
|
(write-region nil nil tmpfile)
|
|
|
|
|
(if (zerop (apply 'call-process "tsfmt" nil outputbuf nil
|
|
|
|
|
(list (format
|
|
|
|
|
"--baseDir='%s' --"
|
|
|
|
|
default-directory)
|
|
|
|
|
tmpfile)))
|
|
|
|
|
(let ((p (point)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(with-current-buffer (current-buffer)
|
|
|
|
|
(erase-buffer)
|
|
|
|
|
(insert-buffer-substring outputbuf)))
|
|
|
|
|
(goto-char p)
|
|
|
|
|
(message "formatted.")
|
|
|
|
|
(kill-buffer outputbuf))
|
|
|
|
|
(progn
|
|
|
|
|
(message "Formatting failed!")
|
|
|
|
|
(display-buffer outputbuf)))
|
|
|
|
|
(progn
|
|
|
|
|
(delete-file tmpfile)))))
|
|
|
|
|
(error "tsfmt not found. Run \"npm install -g typescript-formatter\"")))
|
|
|
|
|
|
2017-07-14 20:50:34 +00:00
|
|
|
|
(defun spacemacs/typescript-format ()
|
2016-05-02 09:11:16 +00:00
|
|
|
|
"Call formatting tool specified in `typescript-fmt-tool'."
|
|
|
|
|
(interactive)
|
|
|
|
|
(cond
|
|
|
|
|
((eq typescript-fmt-tool 'typescript-formatter)
|
2017-07-14 20:50:34 +00:00
|
|
|
|
(call-interactively 'spacemacs/typescript-tsfmt-format-buffer))
|
2016-05-02 09:11:16 +00:00
|
|
|
|
((eq typescript-fmt-tool 'tide)
|
|
|
|
|
(call-interactively 'tide-format))
|
|
|
|
|
(t (error (concat "%s isn't valid typescript-fmt-tool value."
|
|
|
|
|
" It should be 'tide or 'typescript-formatter."
|
|
|
|
|
(symbol-name typescript-fmt-tool))))))
|
|
|
|
|
|
2017-07-14 20:50:34 +00:00
|
|
|
|
(defun spacemacs/typescript-fmt-before-save-hook ()
|
|
|
|
|
(add-hook 'before-save-hook 'spacemacs/typescript-format t t))
|
2016-05-02 09:11:16 +00:00
|
|
|
|
|
2017-07-14 20:50:34 +00:00
|
|
|
|
(defun spacemacs/typescript-open-region-in-playground (start end)
|
2016-05-02 09:11:16 +00:00
|
|
|
|
"Open selected region in http://www.typescriptlang.org/Playground
|
|
|
|
|
If nothing is selected - open the whole current buffer."
|
|
|
|
|
(interactive (if (use-region-p)
|
|
|
|
|
(list (region-beginning) (region-end))
|
|
|
|
|
(list (point-min) (point-max))))
|
|
|
|
|
(browse-url (concat "http://www.typescriptlang.org/Playground#src="
|
|
|
|
|
(url-hexify-string (buffer-substring-no-properties start end)))))
|