Skip to content

Commit 39b90e7

Browse files
Merge pull request #7 from orausch/master
Add lsp-ivy-show-symbol-kind and lsp-ivy-filter-symbol-kind options
2 parents 78c1429 + 6d4f318 commit 39b90e7

File tree

1 file changed

+93
-8
lines changed

1 file changed

+93
-8
lines changed

lsp-ivy.el

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
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
2021
;; URL: https://github.com/emacs-lsp/lsp-ivy
2122
;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (lsp-mode "5.0") (ivy "0.13.0"))
22-
;; Version: 0.1
23+
;; Version: 0.2
2324
;;
2425

2526
;;; Commentary:
@@ -34,13 +35,93 @@
3435
(require 'dash)
3536
(require 'lsp-mode)
3637

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+
t
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+
113+
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)))
41-
(if (or (null container-name) (string-empty-p container-name))
42-
name
43-
(format "%s.%s" container-name name))))
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 (if lsp-ivy-show-symbol-kind
120+
(propertize (format "[%s] " (car type)) 'face `(:foreground ,(cdr type)))
121+
"")))
122+
(concat typestr (if (or (null container-name) (string-empty-p container-name))
123+
(format "%s" name)
124+
(format "%s.%s" container-name name)))))
44125

45126
(defun lsp-ivy--workspace-symbol-action (candidate)
46127
"Jump to selected CANDIDATE."
@@ -51,6 +132,9 @@
51132
(forward-line line)
52133
(forward-char character)))
53134

135+
(defun lsp-ivy--filter-func (candidate)
136+
(member (gethash "kind" candidate) lsp-ivy-filter-symbol-kind))
137+
54138
(defun lsp-ivy--workspace-symbol (workspaces prompt initial-input)
55139
"Search against WORKSPACES with PROMPT and INITIAL-INPUT."
56140
(let ((current-request-id nil))
@@ -68,7 +152,8 @@
68152
(plist-get request :id))
69153
(lsp-send-request-async
70154
request
71-
#'ivy-update-candidates
155+
(lambda (result)
156+
(ivy-update-candidates (-remove 'lsp-ivy--filter-func result)))
72157
:mode 'detached)))
73158
0)
74159
:dynamic-collection t

0 commit comments

Comments
 (0)