spacemacs/layers/+tools/lsp/funcs.el

148 lines
5.9 KiB
EmacsLisp
Raw Normal View History

lsp-layer configuration and building blocks for derived layers. See README.org for details <<amendment 1>> Updated some keybindings based on CONVENTIONS doc Corrected file headers Incorporated some immediate feedback from MaskRay <<amendment 2>> Corrected keybindings in README.org <<amendment 3>> Eliminated stray org-mode tag at table foot in README.org Eliminated new 'l' prefix and moved bindings under 'g' <<amendment 4>> Updated defaults in config.el based on feedback from sebastiencs (lsp/lsp-ui dev) - lsp-ui-sideline enabled by default - lsp-ui-peek-expand-by-default disabled <<amendment 5 09/04/18>> Removed 'spacemacs/' prefix from lsp-format-buffer binding <<amendment 6 09/04/18>> Moved lsp-ui-peek bindings under j (jump) Added goto bindings for new lsp-mode functions goto type definition and goto implementation <<amendment 7 31/05/18>> Corrected layer title in file headers Rebased on dev tip (390462e) <<amendment 8 03/07/18>> Added keybindings for lsp-describe-thing-at-point, lsp-workspace-restart, lsp-execute-code-action suggested by Yyoncho (LSP Java) Added avy keyboard navigation function provided by MaskRay Reverted lsp-ui-peek to expand by default after an upstream change that restricts expansion to current document, addressing the previous performance issue. <<amendment 9 04/07/18>> Corrected keybinding for lsp-describe-thing-at-point <<amendment 10 19/07/18>> Rebound lsp-restart-workspace under mlq Declared 'lsp' prefix (myrgy) Added evil-set-command-property fix suggested by Yyoncho Moved lsp-c-c++ layer from private branch to this PR after spending too many hours of my life rebasing after circle CI picks up a formatting error :) <<amendment 11 25/07/18>> Rebased Bound cquery-freshen-index under lf Bound cquery-preprocess-file under lp <<amendment 12 01/08/18>> Rebased (c-c++ layer) moved semantic refactor refactor-at-point binding from mr to mrp to prevent key binding error when semantic layer enabled <<amendment 13 17/08/18>> Added option to select ccls or cquery backend based on work by myrgy Rebased on current upstream develop <<amendment 14 20/08/18>> Incorporated feedback from myrgy and maskray. Corrected some duplication/inconsistencies. Rebased. <<amendment 15 21/08/18>> Reduced duplication in backend config <<amendment 16 22/08/18>> Removed lsp-c-c++ layer example -- to be merged with c-c++ layer once this PR is sorted <<amendment 17 23/08/18>> Added CHANGELOG.develop entry as per updated contribution guidelines. <<amendment 18 24/08/18>> Moved some keybindings as per feedback from sdwolfz
2018-03-16 00:38:24 +00:00
;;; funcs.el --- Language Server Protocol Layer functions file for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Fangrui Song <i@maskray.me>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//lsp-sync-peek-face ()
"Synchronize the face used in `lsp-ui' peek window according to the theme."
(set-face-attribute 'lsp-ui-peek-list nil
:background (face-attribute 'hl-line :background nil t))
(set-face-attribute 'lsp-ui-peek-peek nil
:background (face-attribute 'hl-line :background nil t))
(set-face-attribute 'lsp-ui-peek-selection nil
:background (face-attribute 'highlight :background nil t)
:foreground (face-attribute 'default :foreground nil t))
(set-face-attribute 'lsp-ui-peek-filename nil
:foreground (face-attribute 'font-lock-constant-face
:foreground nil t))
(set-face-attribute 'lsp-ui-peek-highlight nil
:background (face-attribute 'highlight :background nil t)
:foreground (face-attribute 'highlight :foreground nil t)
:distant-foreground (face-attribute 'highlight
:foreground nil t))
(set-face-attribute 'lsp-ui-peek-header nil
:background (face-attribute 'highlight :background nil t)
:foreground (face-attribute 'default :foreground nil t))
)
(defun spacemacs//setup-lsp-jump-handler (&rest modes)
"Set jump handler for LSP with the given MODE."
(dolist (m modes)
(add-to-list (intern (format "spacemacs-jump-handlers-%S" m))
#'lsp-ui-peek-find-definitions)))
(defun fix-lsp-company-prefix ()
"fix lsp-javascript company prefix
https://github.com/emacs-lsp/lsp-javascript/issues/9#issuecomment-379515379"
(interactive)
(defun lsp-prefix-company-transformer (candidates)
(let ((completion-ignore-case t))
(if (and (car candidates)
(get-text-property 0 'lsp-completion-prefix (car candidates)))
(all-completions (company-grab-symbol) candidates)
candidates)))
(make-local-variable 'company-transformers)
(add-to-list 'company-transformers 'lsp-prefix-company-transformer))
lsp-layer configuration and building blocks for derived layers. See README.org for details <<amendment 1>> Updated some keybindings based on CONVENTIONS doc Corrected file headers Incorporated some immediate feedback from MaskRay <<amendment 2>> Corrected keybindings in README.org <<amendment 3>> Eliminated stray org-mode tag at table foot in README.org Eliminated new 'l' prefix and moved bindings under 'g' <<amendment 4>> Updated defaults in config.el based on feedback from sebastiencs (lsp/lsp-ui dev) - lsp-ui-sideline enabled by default - lsp-ui-peek-expand-by-default disabled <<amendment 5 09/04/18>> Removed 'spacemacs/' prefix from lsp-format-buffer binding <<amendment 6 09/04/18>> Moved lsp-ui-peek bindings under j (jump) Added goto bindings for new lsp-mode functions goto type definition and goto implementation <<amendment 7 31/05/18>> Corrected layer title in file headers Rebased on dev tip (390462e) <<amendment 8 03/07/18>> Added keybindings for lsp-describe-thing-at-point, lsp-workspace-restart, lsp-execute-code-action suggested by Yyoncho (LSP Java) Added avy keyboard navigation function provided by MaskRay Reverted lsp-ui-peek to expand by default after an upstream change that restricts expansion to current document, addressing the previous performance issue. <<amendment 9 04/07/18>> Corrected keybinding for lsp-describe-thing-at-point <<amendment 10 19/07/18>> Rebound lsp-restart-workspace under mlq Declared 'lsp' prefix (myrgy) Added evil-set-command-property fix suggested by Yyoncho Moved lsp-c-c++ layer from private branch to this PR after spending too many hours of my life rebasing after circle CI picks up a formatting error :) <<amendment 11 25/07/18>> Rebased Bound cquery-freshen-index under lf Bound cquery-preprocess-file under lp <<amendment 12 01/08/18>> Rebased (c-c++ layer) moved semantic refactor refactor-at-point binding from mr to mrp to prevent key binding error when semantic layer enabled <<amendment 13 17/08/18>> Added option to select ccls or cquery backend based on work by myrgy Rebased on current upstream develop <<amendment 14 20/08/18>> Incorporated feedback from myrgy and maskray. Corrected some duplication/inconsistencies. Rebased. <<amendment 15 21/08/18>> Reduced duplication in backend config <<amendment 16 22/08/18>> Removed lsp-c-c++ layer example -- to be merged with c-c++ layer once this PR is sorted <<amendment 17 23/08/18>> Added CHANGELOG.develop entry as per updated contribution guidelines. <<amendment 18 24/08/18>> Moved some keybindings as per feedback from sdwolfz
2018-03-16 00:38:24 +00:00
(defun spacemacs/lsp-bind-keys-for-mode (mode)
"Define key bindings for the specific MODE."
(spacemacs/declare-prefix-for-mode mode "m=" "format")
(spacemacs/declare-prefix-for-mode mode "mg" "goto")
(spacemacs/declare-prefix-for-mode mode "mh" "help/hierarchy")
lsp-layer configuration and building blocks for derived layers. See README.org for details <<amendment 1>> Updated some keybindings based on CONVENTIONS doc Corrected file headers Incorporated some immediate feedback from MaskRay <<amendment 2>> Corrected keybindings in README.org <<amendment 3>> Eliminated stray org-mode tag at table foot in README.org Eliminated new 'l' prefix and moved bindings under 'g' <<amendment 4>> Updated defaults in config.el based on feedback from sebastiencs (lsp/lsp-ui dev) - lsp-ui-sideline enabled by default - lsp-ui-peek-expand-by-default disabled <<amendment 5 09/04/18>> Removed 'spacemacs/' prefix from lsp-format-buffer binding <<amendment 6 09/04/18>> Moved lsp-ui-peek bindings under j (jump) Added goto bindings for new lsp-mode functions goto type definition and goto implementation <<amendment 7 31/05/18>> Corrected layer title in file headers Rebased on dev tip (390462e) <<amendment 8 03/07/18>> Added keybindings for lsp-describe-thing-at-point, lsp-workspace-restart, lsp-execute-code-action suggested by Yyoncho (LSP Java) Added avy keyboard navigation function provided by MaskRay Reverted lsp-ui-peek to expand by default after an upstream change that restricts expansion to current document, addressing the previous performance issue. <<amendment 9 04/07/18>> Corrected keybinding for lsp-describe-thing-at-point <<amendment 10 19/07/18>> Rebound lsp-restart-workspace under mlq Declared 'lsp' prefix (myrgy) Added evil-set-command-property fix suggested by Yyoncho Moved lsp-c-c++ layer from private branch to this PR after spending too many hours of my life rebasing after circle CI picks up a formatting error :) <<amendment 11 25/07/18>> Rebased Bound cquery-freshen-index under lf Bound cquery-preprocess-file under lp <<amendment 12 01/08/18>> Rebased (c-c++ layer) moved semantic refactor refactor-at-point binding from mr to mrp to prevent key binding error when semantic layer enabled <<amendment 13 17/08/18>> Added option to select ccls or cquery backend based on work by myrgy Rebased on current upstream develop <<amendment 14 20/08/18>> Incorporated feedback from myrgy and maskray. Corrected some duplication/inconsistencies. Rebased. <<amendment 15 21/08/18>> Reduced duplication in backend config <<amendment 16 22/08/18>> Removed lsp-c-c++ layer example -- to be merged with c-c++ layer once this PR is sorted <<amendment 17 23/08/18>> Added CHANGELOG.develop entry as per updated contribution guidelines. <<amendment 18 24/08/18>> Moved some keybindings as per feedback from sdwolfz
2018-03-16 00:38:24 +00:00
(spacemacs/declare-prefix-for-mode mode "ml" "lsp/backend")
(spacemacs/declare-prefix-for-mode mode "mr" "refactor")
(spacemacs/declare-prefix-for-mode mode "mT" "toggle")
(spacemacs/set-leader-keys-for-major-mode mode
;;format
"=b" #'lsp-format-buffer
;;goto
"gi" #'lsp-goto-implementation
"gt" #'lsp-goto-type-definition
"gk" #'spacemacs/lsp-avy-document-symbol
lsp-layer configuration and building blocks for derived layers. See README.org for details <<amendment 1>> Updated some keybindings based on CONVENTIONS doc Corrected file headers Incorporated some immediate feedback from MaskRay <<amendment 2>> Corrected keybindings in README.org <<amendment 3>> Eliminated stray org-mode tag at table foot in README.org Eliminated new 'l' prefix and moved bindings under 'g' <<amendment 4>> Updated defaults in config.el based on feedback from sebastiencs (lsp/lsp-ui dev) - lsp-ui-sideline enabled by default - lsp-ui-peek-expand-by-default disabled <<amendment 5 09/04/18>> Removed 'spacemacs/' prefix from lsp-format-buffer binding <<amendment 6 09/04/18>> Moved lsp-ui-peek bindings under j (jump) Added goto bindings for new lsp-mode functions goto type definition and goto implementation <<amendment 7 31/05/18>> Corrected layer title in file headers Rebased on dev tip (390462e) <<amendment 8 03/07/18>> Added keybindings for lsp-describe-thing-at-point, lsp-workspace-restart, lsp-execute-code-action suggested by Yyoncho (LSP Java) Added avy keyboard navigation function provided by MaskRay Reverted lsp-ui-peek to expand by default after an upstream change that restricts expansion to current document, addressing the previous performance issue. <<amendment 9 04/07/18>> Corrected keybinding for lsp-describe-thing-at-point <<amendment 10 19/07/18>> Rebound lsp-restart-workspace under mlq Declared 'lsp' prefix (myrgy) Added evil-set-command-property fix suggested by Yyoncho Moved lsp-c-c++ layer from private branch to this PR after spending too many hours of my life rebasing after circle CI picks up a formatting error :) <<amendment 11 25/07/18>> Rebased Bound cquery-freshen-index under lf Bound cquery-preprocess-file under lp <<amendment 12 01/08/18>> Rebased (c-c++ layer) moved semantic refactor refactor-at-point binding from mr to mrp to prevent key binding error when semantic layer enabled <<amendment 13 17/08/18>> Added option to select ccls or cquery backend based on work by myrgy Rebased on current upstream develop <<amendment 14 20/08/18>> Incorporated feedback from myrgy and maskray. Corrected some duplication/inconsistencies. Rebased. <<amendment 15 21/08/18>> Reduced duplication in backend config <<amendment 16 22/08/18>> Removed lsp-c-c++ layer example -- to be merged with c-c++ layer once this PR is sorted <<amendment 17 23/08/18>> Added CHANGELOG.develop entry as per updated contribution guidelines. <<amendment 18 24/08/18>> Moved some keybindings as per feedback from sdwolfz
2018-03-16 00:38:24 +00:00
"gm" #'lsp-ui-imenu
"gd" #'lsp-ui-peek-find-definitions
"gi" #'lsp-ui-peek-find-implementation
lsp-layer configuration and building blocks for derived layers. See README.org for details <<amendment 1>> Updated some keybindings based on CONVENTIONS doc Corrected file headers Incorporated some immediate feedback from MaskRay <<amendment 2>> Corrected keybindings in README.org <<amendment 3>> Eliminated stray org-mode tag at table foot in README.org Eliminated new 'l' prefix and moved bindings under 'g' <<amendment 4>> Updated defaults in config.el based on feedback from sebastiencs (lsp/lsp-ui dev) - lsp-ui-sideline enabled by default - lsp-ui-peek-expand-by-default disabled <<amendment 5 09/04/18>> Removed 'spacemacs/' prefix from lsp-format-buffer binding <<amendment 6 09/04/18>> Moved lsp-ui-peek bindings under j (jump) Added goto bindings for new lsp-mode functions goto type definition and goto implementation <<amendment 7 31/05/18>> Corrected layer title in file headers Rebased on dev tip (390462e) <<amendment 8 03/07/18>> Added keybindings for lsp-describe-thing-at-point, lsp-workspace-restart, lsp-execute-code-action suggested by Yyoncho (LSP Java) Added avy keyboard navigation function provided by MaskRay Reverted lsp-ui-peek to expand by default after an upstream change that restricts expansion to current document, addressing the previous performance issue. <<amendment 9 04/07/18>> Corrected keybinding for lsp-describe-thing-at-point <<amendment 10 19/07/18>> Rebound lsp-restart-workspace under mlq Declared 'lsp' prefix (myrgy) Added evil-set-command-property fix suggested by Yyoncho Moved lsp-c-c++ layer from private branch to this PR after spending too many hours of my life rebasing after circle CI picks up a formatting error :) <<amendment 11 25/07/18>> Rebased Bound cquery-freshen-index under lf Bound cquery-preprocess-file under lp <<amendment 12 01/08/18>> Rebased (c-c++ layer) moved semantic refactor refactor-at-point binding from mr to mrp to prevent key binding error when semantic layer enabled <<amendment 13 17/08/18>> Added option to select ccls or cquery backend based on work by myrgy Rebased on current upstream develop <<amendment 14 20/08/18>> Incorporated feedback from myrgy and maskray. Corrected some duplication/inconsistencies. Rebased. <<amendment 15 21/08/18>> Reduced duplication in backend config <<amendment 16 22/08/18>> Removed lsp-c-c++ layer example -- to be merged with c-c++ layer once this PR is sorted <<amendment 17 23/08/18>> Added CHANGELOG.develop entry as per updated contribution guidelines. <<amendment 18 24/08/18>> Moved some keybindings as per feedback from sdwolfz
2018-03-16 00:38:24 +00:00
"gr" #'lsp-ui-peek-find-references
"gs" #'lsp-ui-peek-find-workspace-symbol
"gp" #'lsp-ui-peek-jump-backward
"gn" #'lsp-ui-peek-jump-forward
"gf" #'lsp-ui-flycheck-list
;;hierarchy
"hh" #'lsp-describe-thing-at-point
;;jump
;;lsp/backend
"la" #'lsp-execute-code-action
"lr" #'lsp-restart-workspace
;;refactor
"rr" #'lsp-rename
;;toggles
"Td" #'lsp-ui-doc-mode
"Ts" #'lsp-ui-sideline-mode
"TF" #'spacemacs/lsp-ui-doc-func
"TS" #'spacemacs/lsp-ui-sideline-symb
"TI" #'spacemacs/lsp-ui-sideline-ignore-duplicate
)
)
(defun spacemacs/lsp-ui-doc-func ()
"Toggle the function signature in the lsp-ui-doc overlay"
(interactive)
(setq lsp-ui-doc-include-signature (not lsp-ui-doc-include-signature)))
(defun spacemacs/lsp-ui-sideline-symb ()
"Toggle the symbol in the lsp-ui-sideline overlay.
(generally redundant in C modes)"
(interactive)
(setq lsp-ui-sideline-show-symbol (not lsp-ui-sideline-show-symbol)))
(defun spacemacs/lsp-ui-sideline-ignore-duplicate ()
"Toggle ignore duplicates for lsp-ui-sideline overlay"
(interactive)
(setq lsp-ui-sideline-ignore-duplicate (not lsp-ui-sideline-ignore-duplicate)))
;; Used for lsp-ui-peek-mode, but may be able to use some spacemacs fn. instead?
(defun spacemacs/lsp-define-key (keymap key def &rest bindings)
"Define multiple key bindings with KEYMAP KEY DEF BINDINGS."
(interactive)
(while key
(define-key keymap (kbd key) def)
(setq key (pop bindings)
def (pop bindings))))
;; From https://github.com/MaskRay/Config/blob/master/home/.config/doom/autoload/misc.el#L118
;;;###autoload
(defun spacemacs/lsp-avy-document-symbol ()
(interactive)
(let (ranges point0 point1 (line 0) (col 0) (w (selected-window)) candidates)
(save-excursion
(goto-char 1)
(dolist (loc
(lsp--send-request (lsp--make-request
"textDocument/documentSymbol"
;;;;;; I added :all t in ccls to return all symbols in the document
`(:textDocument ,(lsp--text-document-identifier) :all t))))
(let ((range (->> loc (gethash "location") (gethash "range"))))
(-let* [((&hash "line" l0 "character" c0) (gethash "start" range))
((&hash "line" l1 "character" c1) (gethash "end" range))]
(when (or (< line l0) (and (= line l0) (<= col c0)))
(forward-line (- l0 line))
(forward-char c0)
(setq point0 (point))
(forward-line (- l1 l0))
(forward-char c1)
(setq point1 (point))
(setq line l1 col c1)
(push `((,point0 . ,point1) . ,w) candidates))))))
(avy-with avy-document-symbol
(avy--process candidates
(avy--style-fn avy-style)))))