config

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

lsp-icons.el (3677B)


      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 (declare-function all-the-icons-material "ext:all-the-icons" t t)
     36 (declare-function lsp-treemacs-symbol-icon "ext:lsp-treemacs" (kind))
     37 (declare-function lsp-treemacs-get-icon "ext:lsp-treemacs" (icon-name))
     38 
     39 (defun lsp-icons--enabled-for-feature (feature)
     40   "Check if icons support is enabled for FEATURE."
     41   (cond
     42    ((eq feature 'headerline-breadcrumb) lsp-headerline-breadcrumb-icons-enable)
     43    (t t)))
     44 
     45 (defun lsp-icons--fix-image-background (image)
     46   "Fix IMAGE background if it is a file otherwise return as an icon."
     47   (if image
     48       (let ((display-image (get-text-property 0 'display image)))
     49         (if (and (listp display-image)
     50                  (plist-member (cl-copy-list (cl-rest display-image)) :type))
     51             (propertize " " 'display
     52                         (cl-list* 'image
     53                                   (plist-put
     54                                    (cl-copy-list
     55                                     (cl-rest display-image))
     56                                    :background (face-attribute 'header-line :background nil t))))
     57           (if (stringp display-image)
     58               (replace-regexp-in-string "\s\\|\t" "" display-image)
     59             (replace-regexp-in-string "\s\\|\t" "" image))))
     60     ""))
     61 
     62 (defun lsp-icons-get-by-file-ext (file-ext &optional feature)
     63   "Get an icon by file FILE-EXT.
     64 FEATURE is the feature that will use the icon which we should check
     65 if its enabled."
     66   (when (and file-ext
     67              (lsp-icons--enabled-for-feature feature)
     68              (functionp 'lsp-treemacs-get-icon))
     69     (lsp-icons--fix-image-background
     70      (lsp-treemacs-get-icon file-ext))))
     71 
     72 (defun lsp-icons-get-by-symbol-kind (kind &optional feature)
     73   "Get an icon by symbol KIND.
     74 FEATURE is the feature that will use the icon which we should check
     75 if its enabled."
     76   (when (and kind
     77              (lsp-icons--enabled-for-feature feature)
     78              (functionp 'lsp-treemacs-symbol-icon))
     79     (lsp-icons--fix-image-background
     80      (lsp-treemacs-symbol-icon kind))))
     81 
     82 (defun lsp-icons-all-the-icons-material-icon (icon-name face fallback &optional feature)
     83   "Get a material icon from all-the-icons by ICON-NAME using FACE.
     84 Fallback to FALLBACK string if not found or not available.
     85 FEATURE is the feature that will use the icon which we should check
     86 if its enabled."
     87   (if (and (functionp 'all-the-icons-material)
     88            (lsp-icons--enabled-for-feature feature))
     89       (all-the-icons-material icon-name
     90                               :face face)
     91     (propertize fallback 'face face)))
     92 
     93 (lsp-consistency-check lsp-icons)
     94 
     95 (provide 'lsp-icons)
     96 ;;; lsp-icons.el ends here