confi-emacs-actual/configs/base-lsp.el

217 lines
9.8 KiB
EmacsLisp

;;; base-lsp.el --- Languaje server protocol. -*- lexical-binding: t -*-
;; Author: kj <webmaster@outcontrol.net>
;; URL: https://git.kj2.me/kj/confi-emacs-actual
;;; Commentary:
;;; Code:
;; Performace tuning
;; @see https://emacs-lsp.github.io/lsp-mode/page/performance/
(setq read-process-output-max (* 1024 (* 3 1024))) ;; 3MB
(setenv "LSP_USE_PLISTS" "true")
(use-package lsp-mode
:defer t
:straight t
:diminish
:commands (lsp-enable-which-key-integration
lsp-format-buffer
lsp-organize-imports
lsp-install-server)
:hook ((prog-mode . (lambda ()
(unless (derived-mode-p 'emacs-lisp-mode 'lisp-mode 'makefile-mode)
(lsp-deferred))))
(markdown-mode . lsp-deferred)
(lsp-mode . lsp-enable-which-key-integration))
:custom
(lsp-headerline-breadcrumb-enable nil)
:init (setq lsp-keymap-prefix "C-c l"
lsp-keep-workspace-alive nil
lsp-signature-auto-activate nil
lsp-modeline-code-actions-enable nil
lsp-modeline-diagnostics-enable nil
lsp-modeline-workspace-status-enable nil
lsp-headerline-breadcrumb-enable nil
lsp-enable-file-watchers nil
lsp-enable-folding nil
lsp-enable-symbol-highlighting nil
lsp-enable-text-document-color nil
lsp-enable-indentation nil
lsp-enable-on-type-formatting nil)
:config
(with-no-warnings
;; Disable `lsp-mode' in `git-timemachine-mode'
(defun my-lsp--init-if-visible (fn &rest args)
(unless (bound-and-true-p git-timemachine-mode)
(apply fn args)))
(advice-add #'lsp--init-if-visible :around #'my-lsp--init-if-visible)
;; Enable `lsp-mode' in sh/bash/zsh
(defun my-lsp-bash-check-sh-shell (&rest _)
(and (eq major-mode 'sh-mode)
(memq sh-shell '(sh bash zsh))))
(advice-add #'lsp-bash-check-sh-shell :override #'my-lsp-bash-check-sh-shell)
;; Only display icons in GUI
(defun my-lsp-icons-get-symbol-kind (fn &rest args)
(and (icon-displayable-p) (apply fn args)))
(advice-add #'lsp-icons-get-by-symbol-kind :around #'my-lsp-icons-get-symbol-kind)
(defun my-lsp-icons-get-by-file-ext (fn &rest args)
(and (icon-displayable-p) (apply fn args)))
(advice-add #'lsp-icons-get-by-file-ext :around #'my-lsp-icons-get-by-file-ext)
(defun my-lsp-icons-all-the-icons-material-icon (icon-name face fallback &optional feature)
(if (and (icon-displayable-p)
(lsp-icons--enabled-for-feature feature))
(all-the-icons-material icon-name
:face face)
(propertize fallback 'face face)))
(advice-add #'lsp-icons-all-the-icons-material-icon
:override #'my-lsp-icons-all-the-icons-material-icon))
)
;; Interface para lsp
(use-package lsp-ui
:defer t
:straight t
:bind (:map lsp-mode-map
("<f1>" . lsp-ui-doc-glance))
:hook (lsp-mode . lsp-ui-mode)
:init
(setq lsp-ui-sideline-show-diagnostics nil
lsp-ui-sideline-ignore-duplicate t
lsp-ui-doc-show-with-cursor nil
lsp-ui-doc-show-with-mouse nil
lsp-ui-doc-position 'at-point
lsp-ui-doc-delay 0.1
lsp-ui-imenu-colors `(,(face-foreground 'font-lock-keyword-face)
,(face-foreground 'font-lock-string-face)
,(face-foreground 'font-lock-constant-face)
,(face-foreground 'font-lock-variable-name-face)))
;; Set correct color to borders
(defun my-lsp-ui-doc-set-border ()
"Set the border color of lsp doc."
(setq lsp-ui-doc-border
(if (facep 'posframe-border)
(face-background 'posframe-border nil t)
(face-foreground 'shadow nil t))))
(my-lsp-ui-doc-set-border)
(add-hook 'after-load-theme-hook #'my-lsp-ui-doc-set-border t)
)
;; Integración con ivy
(use-package lsp-ivy
:defer t
:straight t
:after lsp-mode
:bind (:map lsp-mode-map
([remap xref-find-apropos] . lsp-ivy-workspace-symbol)
("C-s-." . lsp-ivy-global-workspace-symbol))
:config
(with-no-warnings
(when (icon-displayable-p)
(defvar lsp-ivy-symbol-kind-icons
`(,(all-the-icons-material "find_in_page" :height 0.9 :v-adjust -0.15) ; Unknown - 0
,(all-the-icons-faicon "file-o" :height 0.9 :v-adjust -0.02) ; File - 1
,(all-the-icons-material "view_module" :height 0.9 :v-adjust -0.15 :face 'all-the-icons-lblue) ; Module - 2
,(all-the-icons-material "view_module" :height 0.95 :v-adjust -0.15 :face 'all-the-icons-lblue) ; Namespace - 3
,(all-the-icons-octicon "package" :height 0.9 :v-adjust -0.15) ; Package - 4
,(all-the-icons-material "settings_input_component" :height 0.9 :v-adjust -0.15 :face 'all-the-icons-orange) ; Class - 5
,(all-the-icons-faicon "cube" :height 0.9 :v-adjust -0.02 :face 'all-the-icons-purple) ; Method - 6
,(all-the-icons-faicon "wrench" :height 0.8 :v-adjust -0.02) ; Property - 7
,(all-the-icons-octicon "tag" :height 0.95 :v-adjust 0 :face 'all-the-icons-lblue) ; Field - 8
,(all-the-icons-faicon "cube" :height 0.9 :v-adjust -0.02 :face 'all-the-icons-lpurple) ; Constructor - 9
,(all-the-icons-material "storage" :height 0.9 :v-adjust -0.15 :face 'all-the-icons-orange) ; Enum - 10
,(all-the-icons-material "share" :height 0.9 :v-adjust -0.15 :face 'all-the-icons-lblue) ; Interface - 11
,(all-the-icons-faicon "cube" :height 0.9 :v-adjust -0.02 :face 'all-the-icons-purple) ; Function - 12
,(all-the-icons-octicon "tag" :height 0.95 :v-adjust 0 :face 'all-the-icons-lblue) ; Variable - 13
,(all-the-icons-faicon "cube" :height 0.9 :v-adjust -0.02 :face 'all-the-icons-purple) ; Constant - 14
,(all-the-icons-faicon "text-width" :height 0.9 :v-adjust -0.02) ; String - 15
,(all-the-icons-material "format_list_numbered" :height 0.95 :v-adjust -0.15) ; Number - 16
,(all-the-icons-octicon "tag" :height 0.9 :v-adjust 0.0 :face 'all-the-icons-lblue) ; Boolean - 17
,(all-the-icons-material "view_array" :height 0.95 :v-adjust -0.15) ; Array - 18
,(all-the-icons-octicon "tag" :height 0.9 :v-adjust 0.0 :face 'all-the-icons-blue) ; Object - 19
,(all-the-icons-faicon "key" :height 0.9 :v-adjust -0.02) ; Key - 20
,(all-the-icons-octicon "tag" :height 0.9 :v-adjust 0.0) ; Null - 21
,(all-the-icons-material "format_align_right" :height 0.95 :v-adjust -0.15 :face 'all-the-icons-lblue) ; EnumMember - 22
,(all-the-icons-material "settings_input_component" :height 0.9 :v-adjust -0.15 :face 'all-the-icons-orange) ; Struct - 23
,(all-the-icons-octicon "zap" :height 0.9 :v-adjust 0 :face 'all-the-icons-orange) ; Event - 24
,(all-the-icons-material "control_point" :height 0.9 :v-adjust -0.15) ; Operator - 25
,(all-the-icons-faicon "arrows" :height 0.9 :v-adjust -0.02) ; TypeParameter - 26
))
(lsp-defun my-lsp-ivy--format-symbol-match
((sym &as &SymbolInformation :kind :location (&Location :uri))
project-root)
"Convert the match returned by `lsp-mode` into a candidate string."
(let* ((sanitized-kind (if (< kind (length lsp-ivy-symbol-kind-icons)) kind 0))
(type (elt lsp-ivy-symbol-kind-icons sanitized-kind))
(typestr (if lsp-ivy-show-symbol-kind (format "%s " type) ""))
(pathstr (if lsp-ivy-show-symbol-filename
(propertize (format " · %s" (file-relative-name (lsp--uri-to-path uri) project-root))
'face font-lock-comment-face)
"")))
(concat typestr (lsp-render-symbol-information sym ".") pathstr)))
(advice-add #'lsp-ivy--format-symbol-match :override #'my-lsp-ivy--format-symbol-match))))
;; Debug
(use-package dap-mode
:defer t
:straight t
:defines dap-python-executable
:functions dap-hydra/nil
:diminish
:bind (:map lsp-mode-map
("<f5>" . dap-debug)
("M-<f5>" . dap-hydra))
:hook ((after-init . dap-auto-configure-mode)
(dap-stopped . (lambda (_args) (dap-hydra)))
(dap-terminated . (lambda (_args) (dap-hydra/nil)))
(python-mode . (lambda () (require 'dap-python)))
(ruby-mode . (lambda () (require 'dap-ruby)))
(go-mode . (lambda () (require 'dap-go)))
(java-mode . (lambda () (require 'dap-java)))
((c-mode c++-mode objc-mode swift-mode) . (lambda () (require 'dap-lldb)))
(php-mode . (lambda () (require 'dap-php)))
(elixir-mode . (lambda () (require 'dap-elixir)))
((js-mode js2-mode) . (lambda () (require 'dap-chrome)))
(powershell-mode . (lambda () (require 'dap-pwsh))))
:init
(setq dap-auto-configure-features '(sessions locals breakpoints expressions controls))
(when (executable-find "python3")
(setq dap-python-executable "python3")))
;; Python debug
(use-package lsp-pyright
:defer t
:straight t
:preface
;; Use yapf to format
(defun lsp-pyright-format-buffer ()
(interactive)
(when (and (executable-find "yapf") buffer-file-name)
(call-process "yapf" nil nil nil "-i" buffer-file-name)))
:hook (python-mode . (lambda ()
(require 'lsp-pyright)
(add-hook 'after-save-hook #'lsp-pyright-format-buffer t t)))
:init (when (executable-find "python3")
(setq lsp-pyright-python-executable-cmd "python3")))
;; Java LSP
(use-package lsp-java
:defer t
:straight t
:hook (java-mode . (lambda () (require 'lsp-java))))
(provide 'base-lsp)
;;; base-lsp.el ends here