Skip to content

Commit

Permalink
Refactoring all-the-icons functions (#4588)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho authored Nov 13, 2024
1 parent 5991c60 commit c41769e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lsp-headerline.el
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ caching purposes.")
lsp-headerline-arrow
(setq lsp-headerline-arrow (let ((all-the-icons-scale-factor 1.0)
(all-the-icons-default-adjust 0))
(lsp-icons-all-the-icons-material-icon
(lsp-icons-all-the-icons-icon
'material
"chevron_right"
'lsp-headerline-breadcrumb-separator-face
">"
Expand Down
22 changes: 14 additions & 8 deletions lsp-icons.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@
:type 'boolean
:group 'lsp-icons)

(declare-function all-the-icons-material "ext:all-the-icons" t t)
(defcustom lsp-modeline-code-action-icons-enable t
"If non-nil, icons support is enabled for modeline-code-action"
:type 'boolean
:group 'lsp-icons)

(declare-function lsp-treemacs-symbol-icon "ext:lsp-treemacs" (kind))
(declare-function lsp-treemacs-get-icon "ext:lsp-treemacs" (icon-name))

(defun lsp-icons--enabled-for-feature (feature)
"Check if icons support is enabled for FEATURE."
(cond
((eq feature 'headerline-breadcrumb) lsp-headerline-breadcrumb-icons-enable)
((eq feature 'modeline-code-action) lsp-modeline-code-action-icons-enable)
(t t)))

(defun lsp-icons--fix-image-background (image)
Expand Down Expand Up @@ -79,16 +84,17 @@ if its enabled."
(lsp-icons--fix-image-background
(lsp-treemacs-symbol-icon kind))))

(defun lsp-icons-all-the-icons-material-icon (icon-name face fallback &optional feature)
"Get a material icon from all-the-icons by ICON-NAME using FACE.
(defun lsp-icons-all-the-icons-icon (icon-set icon-name face fallback &optional feature &rest args)
"Get icon ICON-NAME from `all-the-icons' ICON-SET using FACE.
If ARGS is provided, it's a plist passed directly to the `all-the-icons' function.
Fallback to FALLBACK string if not found or not available.
FEATURE is the feature that will use the icon which we should check
if its enabled."
(if (and (functionp 'all-the-icons-material)
(lsp-icons--enabled-for-feature feature))
(all-the-icons-material icon-name
:face face)
(propertize fallback 'face face)))
(let ((icon-set-fn (intern-soft (concat "all-the-icons-" (symbol-name icon-set)))))
(if (and (fboundp icon-set-fn)
(lsp-icons--enabled-for-feature feature))
(apply icon-set-fn icon-name :face face args)
(propertize fallback 'face face))))

(lsp-consistency-check lsp-icons)

Expand Down
18 changes: 8 additions & 10 deletions lsp-modeline.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
;;; Code:

(require 'lsp-mode)
(require 'lsp-icons)

(defgroup lsp-modeline nil
"LSP support for modeline"
Expand Down Expand Up @@ -71,7 +72,6 @@
(const :tag "All Projects" :global))
:package-version '(lsp-mode . "6.3"))

(declare-function all-the-icons-octicon "ext:all-the-icons" t t)
(declare-function lsp-treemacs-errors-list "ext:lsp-treemacs" t)


Expand All @@ -86,14 +86,6 @@
'lsp-modeline-code-actions-preferred-face
'lsp-modeline-code-actions-face))

(defun lsp-modeline--code-actions-icon (face)
"Build the icon for modeline code actions using FACE."
(if (require 'all-the-icons nil t)
(all-the-icons-octicon "light-bulb"
:face face
:v-adjust -0.0575)
(propertize lsp-modeline-code-action-fallback-icon 'face face)))

(defun lsp-modeline--code-action-name (actions preferred-code-action-title)
"Return the code action name from ACTIONS and PREFERRED-CODE-ACTION-TITLE."
(or preferred-code-action-title
Expand All @@ -116,7 +108,13 @@
(mapconcat
(lambda (segment)
(pcase segment
('icon (lsp-modeline--code-actions-icon face))
('icon (lsp-icons-all-the-icons-icon
'octicon
"light-bulb"
face
lsp-modeline-code-action-fallback-icon
'modeline-code-action
:v-adjust -0.0575))
('name (propertize (lsp-modeline--code-action-name actions preferred-code-action)
'face face))
('count (propertize (number-to-string (seq-length actions))
Expand Down

0 comments on commit c41769e

Please sign in to comment.