config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

lsp-icons.el (3987B)


      1 ;;; lsp-icons.el --- LSP icons management -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2020 emacs-lsp maintainers
      4 ;;
      5 ;; This program is free software; you can redistribute it and/or modify
      6 ;; it under the terms of the GNU General Public License as published by
      7 ;; the Free Software Foundation, either version 3 of the License, or
      8 ;; (at your option) any later version.
      9 
     10 ;; This program is distributed in the hope that it will be useful,
     11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 ;; GNU General Public License for more details.
     14 
     15 ;; You should have received a copy of the GNU General Public License
     16 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 ;;
     18 ;;; Commentary:
     19 ;;
     20 ;;  LSP icons management
     21 ;;
     22 ;;; Code:
     23 (require 'lsp-mode)
     24 
     25 (defgroup lsp-icons nil
     26   "LSP icons"
     27   :group 'lsp-mode
     28   :tag "LSP Icons")
     29 
     30 (defcustom lsp-headerline-breadcrumb-icons-enable t
     31   "If non-nil, icons support is enabled for headerline-breadcrumb."
     32   :type 'boolean
     33   :group 'lsp-icons)
     34 
     35 (defcustom lsp-modeline-code-action-icons-enable t
     36   "If non-nil, icons support is enabled for modeline-code-action"
     37   :type 'boolean
     38   :group 'lsp-icons)
     39 
     40 (declare-function lsp-treemacs-symbol-icon "ext:lsp-treemacs" (kind))
     41 (declare-function lsp-treemacs-get-icon "ext:lsp-treemacs" (icon-name))
     42 
     43 (defun lsp-icons--enabled-for-feature (feature)
     44   "Check if icons support is enabled for FEATURE."
     45   (cond
     46    ((eq feature 'headerline-breadcrumb) lsp-headerline-breadcrumb-icons-enable)
     47    ((eq feature 'modeline-code-action) lsp-modeline-code-action-icons-enable)
     48    (t t)))
     49 
     50 (defun lsp-icons--fix-image-background (image)
     51   "Fix IMAGE background if it is a file otherwise return as an icon."
     52   (if image
     53       (let ((display-image (get-text-property 0 'display image)))
     54         (if (and (listp display-image)
     55                  (plist-member (cl-copy-list (cl-rest display-image)) :type))
     56             (propertize " " 'display
     57                         (cl-list* 'image
     58                                   (plist-put
     59                                    (cl-copy-list
     60                                     (cl-rest display-image))
     61                                    :background (face-attribute 'header-line :background nil t))))
     62           (if (stringp display-image)
     63               (replace-regexp-in-string "\s\\|\t" "" display-image)
     64             (replace-regexp-in-string "\s\\|\t" "" image))))
     65     ""))
     66 
     67 (defun lsp-icons-get-by-file-ext (file-ext &optional feature)
     68   "Get an icon by file FILE-EXT.
     69 FEATURE is the feature that will use the icon which we should check
     70 if its enabled."
     71   (when (and file-ext
     72              (lsp-icons--enabled-for-feature feature)
     73              (functionp 'lsp-treemacs-get-icon))
     74     (lsp-icons--fix-image-background
     75      (lsp-treemacs-get-icon file-ext))))
     76 
     77 (defun lsp-icons-get-by-symbol-kind (kind &optional feature)
     78   "Get an icon by symbol KIND.
     79 FEATURE is the feature that will use the icon which we should check
     80 if its enabled."
     81   (when (and kind
     82              (lsp-icons--enabled-for-feature feature)
     83              (functionp 'lsp-treemacs-symbol-icon))
     84     (lsp-icons--fix-image-background
     85      (lsp-treemacs-symbol-icon kind))))
     86 
     87 (defun lsp-icons-all-the-icons-icon (icon-set icon-name face fallback &optional feature &rest args)
     88   "Get icon ICON-NAME from `all-the-icons' ICON-SET using FACE.
     89 If ARGS is provided, it's a plist passed directly to the `all-the-icons' function.
     90 Fallback to FALLBACK string if not found or not available.
     91 FEATURE is the feature that will use the icon which we should check
     92 if its enabled."
     93   (let ((icon-set-fn (intern-soft (concat "all-the-icons-" (symbol-name icon-set)))))
     94     (if (and (fboundp icon-set-fn)
     95              (lsp-icons--enabled-for-feature feature))
     96         (apply icon-set-fn icon-name :face face args)
     97       (propertize fallback 'face face))))
     98 
     99 (lsp-consistency-check lsp-icons)
    100 
    101 (provide 'lsp-icons)
    102 ;;; lsp-icons.el ends here