spacemacs/layers/+tools/tide/funcs.el
thanhvg e3b6464649
[core][tide][lsp] improve spacemacs/set-leader-keys-for-minor-mode and apply it to tide and lsp layers (#14141)
* [core][keybinng] improve minor mode binding

This commit added add a new function defun spacemacs/declare-prefix-for-minor-mode
and improved spacemacs/set-leader-keys-for-minor-mode.

`which-key` package recently introduced a new api
which-key-add-keymap-based-replacements which improves perfomance and allows
prefix and namings to be stored directly in keymap. This is a great improvement.

With this new api we now make change to spacemacs/declare-prefix-for-minor-mode
to manage prefix also. For example:

  (spacemacs/set-leader-keys-for-minor-mode 'lsp-mode
    "=" "format"
    "=b" #'lsp-format-buffer)

Before we had to use another api to bind prefix
spacemacs/declare-prefix-for-mode which only works on major-mode. As lsp-mode is
a minor mode this api causes a lot of problems to which-key performance. An
example is https://github.com/syl20bnr/spacemacs/issues/12455 which led to my
hack in https://github.com/syl20bnr/spacemacs/pull/12474.

The improved spacemacs/set-leader-keys-for-minor-mode will take care of both
prefix and key naming for the minor mode. This will allows us to have a better
set up for dynamic minor modes such as lsp-mode, tide-mode etc.

Also another api is created to make prefix for minor mode:
spacemacs/declare-prefix-for-minor-mode.

Usage:
(spacemacs/declare-prefix-for-minor-mode 'tide-mode "E" "errors")"

* [tide] improve prefix

* [lsp] improve prefix
2020-11-21 07:34:55 +01:00

83 lines
2.6 KiB
EmacsLisp

;;; funcs.el --- Tide Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Thanh Vuong <thanhvg@gmail.com>
;; URL: https://github.com/thanhvg
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//tide-setup-bindings ()
"Define keys bindings for `tide-mode'"
(spacemacs/set-leader-keys-for-minor-mode 'tide-mode
"E" "errors"
"Ee" #'tide-fix
"Ed" #'tide-add-tslint-disable-next-line
"Ep" #'tide-project-errors
"g" "goto"
"ge" #'tide-project-errors
"gb" #'tide-jump-back
"gg" #'tide-jump-to-definition
"gt" #'spacemacs/typescript-jump-to-type-def
"gr" #'tide-references
"h" "help"
"hh" #'tide-documentation-at-point
"r" "refactor"
"ri" #'tide-organize-imports
"rr" #'tide-rename-symbol
"rf" #'tide-rename-file
"S" "server"
"Sr" #'tide-restart-server
"Sj" #'spacemacs//tide-create-jsconfig-file))
(defun spacemacs//tide-setup ()
"Setup tide backend.
Must be called by a layer using tide."
(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)
(tide-hl-identifier-mode +1)
(tide-setup))
(defun spacemacs//tide--list-to-string (list)
"Convert LIST to string."
(cl-reduce (lambda (x y) (concat x " " (symbol-name y)))
(cdr list)
:initial-value (format "%s" (car list) )))
(defun spacemacs//tide-setup-company (&rest modes)
"Setup tide company for MODES.
Must be called by a layer using tide."
(eval `(spacemacs|add-company-backends
:backends company-tide
:modes ,@modes
:append-hooks nil
:call-hooks t))
(company-mode))
(defun spacemacs//tide-setup-eldoc ()
"Setup eldoc for tide."
(eldoc-mode))
(defun spacemacs//tide-setup-jump-handle ()
"Loop through `tide-managed-modes' and set jump handlers for these modes."
(dolist (mode tide-managed-modes)
(add-to-list
(intern (format "spacemacs-jump-handlers-%S" mode))
'(tide-jump-to-definition :async t))))
(defun spacemacs//tide-create-jsconfig-file ()
"Create a jsconfig file at project root."
(interactive)
(let ((jsconfig (cdr (project-current))))
(if jsconfig
(let ((jsconfig-file (concat jsconfig "jsconfig.json")))
(if (file-exists-p jsconfig-file)
(message "File exists")
(with-temp-file jsconfig-file
(insert tide-jsconfig-content))))
(message "Project not found"))))