Skip to content

Commit 25d4421

Browse files
committed
Add lsp-ivy-show-symbol-kind and lsp-ivy-filter-symbol-kind options
1 parent 78c1429 commit 25d4421

File tree

1 file changed

+89
-6
lines changed

1 file changed

+89
-6
lines changed

lsp-ivy.el

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
;; You should have received a copy of the GNU General Public License
1616
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
;; Author: Sebastian Sturm
18+
;; Authors: Sebastian Sturm
19+
;; Oliver Rausch
1920
;; Keywords: languages, debug
21+
;; Package-Version: 20191028.902
2022
;; URL: https://github.com/emacs-lsp/lsp-ivy
2123
;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (lsp-mode "5.0") (ivy "0.13.0"))
2224
;; Version: 0.1
@@ -33,14 +35,91 @@
3335
(require 'ivy)
3436
(require 'dash)
3537
(require 'lsp-mode)
38+
(defgroup lsp-ivy nil
39+
"LSP support for ivy-based symbol completion"
40+
:group 'lsp-mode)
41+
42+
(defcustom lsp-ivy-show-symbol-kind
43+
nil
44+
"Whether to show the symbol's kind when showing lsp symbols"
45+
:group 'lsp-ivy
46+
:type 'boolean)
47+
48+
(defcustom lsp-ivy-filter-symbol-kind
49+
nil
50+
"A list of LSP SymbolKind's to filter out"
51+
:group 'lsp-ivy
52+
:type '(repeat integer))
53+
54+
(defcustom lsp-ivy-symbol-kind-to-string
55+
[(" " . "red") ;; Unknown - 0
56+
("File" . "red") ;; File - 1
57+
("Modu" . "red") ;; Module - 2
58+
("Nmsp" . "red") ;; Namespace - 3
59+
("Pack" . "red") ;; Package - 4
60+
("Clss" . "red") ;; Class - 5
61+
("Meth" . "violet") ;; Method - 6
62+
("Prop" . "violet") ;; Property - 7
63+
("Fld " . "violet") ;; Field - 8
64+
("Cons" . "red") ;; Constructor - 9
65+
("Enum" . "red") ;; Enum - 10
66+
("Intf" . "red") ;; Interface - 11
67+
("Func" . "darkgreen") ;; Function - 12
68+
("Var " . "blue") ;; Variable - 13
69+
("Cnst" . "blue") ;; Constant - 14
70+
("Str " . "blue") ;; String - 15
71+
("Num " . "blue") ;; Number - 16
72+
("Bool " . "blue") ;; Boolean - 17
73+
("Arr " . "blue") ;; Array - 18
74+
("Obj " . "blue") ;; Object - 19
75+
("Key " . "blue") ;; Key - 20
76+
("Null" . "red") ;; Null - 21
77+
("EmMm" . "violet") ;; EnumMember - 22
78+
("Srct" . "red") ;; Struct - 23
79+
("Evnt" . "red") ;; Event - 24
80+
("Op " . "red") ;; Operator - 25
81+
("TPar" . "red")] ;; TypeParameter - 26
82+
"A vector of 26 cons cells, where the ith cons cell contains the string representation and foreground color to use for the i+1th SymbolKind (defined in the LSP)"
83+
:group 'lsp-ivy
84+
:type '(vector
85+
(cons string color)
86+
(cons string color)
87+
(cons string color)
88+
(cons string color)
89+
(cons string color)
90+
(cons string color)
91+
(cons string color)
92+
(cons string color)
93+
(cons string color)
94+
(cons string color)
95+
(cons string color)
96+
(cons string color)
97+
(cons string color)
98+
(cons string color)
99+
(cons string color)
100+
(cons string color)
101+
(cons string color)
102+
(cons string color)
103+
(cons string color)
104+
(cons string color)
105+
(cons string color)
106+
(cons string color)
107+
(cons string color)
108+
(cons string color)
109+
(cons string color)
110+
(cons string color)
111+
(cons string color)))
112+
36113

37114
(defun lsp-ivy--format-symbol-match (match)
38115
"Convert the (hash-valued) MATCH returned by `lsp-mode` into a candidate string."
39-
(let ((container-name (gethash "containerName" match))
40-
(name (gethash "name" match)))
116+
(let* ((container-name (gethash "containerName" match))
117+
(name (gethash "name" match))
118+
(type (elt lsp-ivy-symbol-kind-to-string (gethash "kind" match) ))
119+
(typestr (propertize (format "[%s]" (car type)) 'face `(:foreground ,(cdr type)))))
41120
(if (or (null container-name) (string-empty-p container-name))
42-
name
43-
(format "%s.%s" container-name name))))
121+
(format "%s %s" typestr name)
122+
(format "%s %s.%s" typestr container-name name))))
44123

45124
(defun lsp-ivy--workspace-symbol-action (candidate)
46125
"Jump to selected CANDIDATE."
@@ -51,6 +130,9 @@
51130
(forward-line line)
52131
(forward-char character)))
53132

133+
(defun lsp-ivy--filter-func (candidate)
134+
(member (gethash "kind" candidate) lsp-ivy-filter-symbol-kind))
135+
54136
(defun lsp-ivy--workspace-symbol (workspaces prompt initial-input)
55137
"Search against WORKSPACES with PROMPT and INITIAL-INPUT."
56138
(let ((current-request-id nil))
@@ -68,7 +150,8 @@
68150
(plist-get request :id))
69151
(lsp-send-request-async
70152
request
71-
#'ivy-update-candidates
153+
(lambda (result)
154+
(ivy-update-candidates (-remove 'lsp-ivy--filter-func result)))
72155
:mode 'detached)))
73156
0)
74157
:dynamic-collection t

0 commit comments

Comments
 (0)