-
Notifications
You must be signed in to change notification settings - Fork 3
/
nerd-icons-corfu.el
136 lines (120 loc) · 6.81 KB
/
nerd-icons-corfu.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;; nerd-icons-corfu.el --- Icons for Corfu via nerd-icons -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Luigi Sartor Piucco
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Author: Luigi Sartor Piucco <luigipiucco@gmail.com>
;; Maintainer: Luigi Sartor Piucco <luigipiucco@gmail.com>
;; Created: September 21, 2023
;; Version: 0.2.0
;; Keywords: convenience, files, icons
;; Homepage: https://github.com/LuigiPiucco/nerd-icons-corfu
;; Package-Requires: ((emacs "27.1") (nerd-icons "0.1.0"))
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Introduces a margin formatter for Corfu which adds icons. The icons are
;; configurable, but should be text icons provided by the icons fonts in
;; `nerd-icons'.
;;
;; To use, install the package and add the following to your init:
;;
;; (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter)
;;; Code:
(require 'nerd-icons)
(defgroup nerd-icons-corfu
nil
"Icons for Corfu via Nerd Icons."
:group 'nerd-icons)
(define-widget 'nerd-icons-corfu-icon-type 'plist
"The type of an icon mapping."
:tag "Icon parameters"
:options '((:style (choice (const :tag "wicon" "w")
(const :tag "faicon" "fa")
(const :tag "flicon" "fl")
(const :tag "mdicon" "md")
(const :tag "codicon" "cod")
(const :tag "devicon" "dev")
(const :tag "ipsicon" "ips")
(const :tag "octicon" "oct")
(const :tag "pomicon" "pom")
(const :tag "sucicon" "suc")))
(:icon string)
(:face face)))
(defcustom nerd-icons-corfu-mapping
'((array :style "cod" :icon "symbol_array" :face font-lock-type-face)
(boolean :style "cod" :icon "symbol_boolean" :face font-lock-builtin-face)
(class :style "cod" :icon "symbol_class" :face font-lock-type-face)
(color :style "cod" :icon "symbol_color" :face success)
(command :style "cod" :icon "terminal" :face default)
(constant :style "cod" :icon "symbol_constant" :face font-lock-constant-face)
(constructor :style "cod" :icon "triangle_right" :face font-lock-function-name-face)
(enummember :style "cod" :icon "symbol_enum_member" :face font-lock-builtin-face)
(enum-member :style "cod" :icon "symbol_enum_member" :face font-lock-builtin-face)
(enum :style "cod" :icon "symbol_enum" :face font-lock-builtin-face)
(event :style "cod" :icon "symbol_event" :face font-lock-warning-face)
(field :style "cod" :icon "symbol_field" :face font-lock-variable-name-face)
(file :style "cod" :icon "symbol_file" :face font-lock-string-face)
(folder :style "cod" :icon "folder" :face font-lock-doc-face)
(interface :style "cod" :icon "symbol_interface" :face font-lock-type-face)
(keyword :style "cod" :icon "symbol_keyword" :face font-lock-keyword-face)
(macro :style "cod" :icon "symbol_misc" :face font-lock-keyword-face)
(magic :style "cod" :icon "wand" :face font-lock-builtin-face)
(method :style "cod" :icon "symbol_method" :face font-lock-function-name-face)
(function :style "cod" :icon "symbol_method" :face font-lock-function-name-face)
(module :style "cod" :icon "file_submodule" :face font-lock-preprocessor-face)
(numeric :style "cod" :icon "symbol_numeric" :face font-lock-builtin-face)
(operator :style "cod" :icon "symbol_operator" :face font-lock-comment-delimiter-face)
(param :style "cod" :icon "symbol_parameter" :face default)
(property :style "cod" :icon "symbol_property" :face font-lock-variable-name-face)
(reference :style "cod" :icon "references" :face font-lock-variable-name-face)
(snippet :style "cod" :icon "symbol_snippet" :face font-lock-string-face)
(string :style "cod" :icon "symbol_string" :face font-lock-string-face)
(struct :style "cod" :icon "symbol_structure" :face font-lock-variable-name-face)
(text :style "cod" :icon "text_size" :face font-lock-doc-face)
(typeparameter :style "cod" :icon "list_unordered" :face font-lock-type-face)
(type-parameter :style "cod" :icon "list_unordered" :face font-lock-type-face)
(unit :style "cod" :icon "symbol_ruler" :face font-lock-constant-face)
(value :style "cod" :icon "symbol_field" :face font-lock-builtin-face)
(variable :style "cod" :icon "symbol_variable" :face font-lock-variable-name-face)
(t :style "cod" :icon "code" :face font-lock-warning-face))
"Mapping of completion kinds to icons.
It should be a list of elements with the form (KIND :style ICON-STY :icon
ICON-NAME [:face FACE]). KIND is a symbol determining what the completion is,
and comes from calling the `:company-kind' property of the completion. ICON-STY
is a string with the icon style to use, from those available in Nerd Fonts.
ICON-NAME is a string with the name of the icon. FACE, if present, is applied
to the icon, mainly for its color. The special t symbol should be used for KIND
to represent the default icon, and must be present."
:type '(alist :key-type symbol :value-type nerd-icons-corfu-icon-type)
:group 'nerd-icons-corfu)
;;;###autoload
(defun nerd-icons-corfu-formatter (_)
"A margin formatter for Corfu, adding icons.
It receives METADATA, ignores it, and outputs a function that takes a candidate
and returns the icon."
(when-let ((kindfunc (plist-get completion-extra-properties :company-kind)))
(lambda (cand)
(when-let* ((kind (or (funcall kindfunc cand) t))
(icon-entry (cdr (assq kind nerd-icons-corfu-mapping)))
(style (plist-get icon-entry :style))
(icon (plist-get icon-entry :icon))
(icon-fun (intern (concat "nerd-icons-" style "icon")))
(icon-name (concat "nf-" style "-" icon))
(str (condition-case-unless-debug nil (funcall icon-fun icon-name)
(error nil)))
(space (propertize " " 'display '(space :width 1)))
(str (concat " " str space)))
(when-let ((extra-face (plist-get icon-entry :face)))
(put-text-property 0 3 'face extra-face str))
str))))
(provide 'nerd-icons-corfu)
;;; nerd-icons-corfu.el ends here