forked from emacs-lsp/lsp-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-ui.el
177 lines (151 loc) · 6.23 KB
/
lsp-ui.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
;;; lsp-ui.el --- UI modules for lsp-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Tobias Pisani
;; Copyright (C) 2018 Sebastien Chapuis, Fangrui Song
;; Author: Sebastien Chapuis <[email protected]>, Fangrui Song <[email protected]>
;; Keywords: languages, tools
;; URL: https://github.com/emacs-lsp/lsp-ui
;; Package-Requires: ((emacs "26.1") (dash "2.18.0") (lsp-mode "6.0") (markdown-mode "2.3"))
;; Version: 8.0.1
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;; lsp-ui contains a series of useful UI integrations for lsp-mode, like
;; flycheck support and code lenses.
;;; Code:
(require 'dash)
(require 'lsp-protocol)
(require 'find-func)
(defconst lsp-ui-resources-dir
(--> (or load-file-name (buffer-file-name))
(file-name-directory it)
(expand-file-name "resources" it)
(file-name-as-directory it)
(and (file-directory-p it) it))
"Resource folder for package `lsp-ui'.")
(require 'lsp-ui-sideline)
(require 'lsp-ui-peek)
(require 'lsp-ui-imenu)
(require 'lsp-ui-doc)
(defgroup lsp-ui nil
"‘lsp-ui’ contains a series of useful UI integrations for ‘lsp-mode’."
:group 'tools
:group 'convenience
:link '(custom-manual "(lsp-ui) Top")
:link '(info-link "(lsp-ui) Customizing"))
(with-eval-after-load 'flycheck
(require 'lsp-ui-flycheck))
(with-eval-after-load 'winum
(when (and (boundp 'winum-ignored-buffers-regexp) lsp-ui-doc-winum-ignore)
(add-to-list 'winum-ignored-buffers-regexp lsp-ui-doc--buffer-prefix)))
(defun lsp-ui-peek--render (major string)
(with-temp-buffer
(insert string)
(delay-mode-hooks
(let ((inhibit-message t)) (funcall major))
(ignore-errors (font-lock-ensure)))
(buffer-string)))
(defun lsp-ui--workspace-path (path)
"Return the PATH relative to the workspace.
If the PATH is not in the workspace, it returns the original PATH."
(let* ((path (file-truename path))
(root (lsp-workspace-root path))
(in-workspace (and root (string-prefix-p root path))))
(if in-workspace
(substring path (length root))
path)))
(defun lsp-ui--toggle (enable)
(dolist (feature '(lsp-ui-peek lsp-ui-sideline lsp-ui-doc lsp-ui-imenu))
(let* ((sym (--> (intern-soft (concat (symbol-name feature) "-enable"))
(and (boundp it) it)))
(value (symbol-value sym))
(fn (symbol-function sym)))
(and (or value (not enable))
(functionp fn)
(funcall fn enable)))))
(defvar lsp-ui-mode-map (make-sparse-keymap))
;;;###autoload
(define-minor-mode lsp-ui-mode
"Toggle language server UI mode on or off.
‘lsp-ui-mode’ is a minor mode that contains a series of useful UI
integrations for ‘lsp-mode’. With a prefix argument ARG, enable
language server UI mode if ARG is positive, and disable it
otherwise. If called from Lisp, enable the mode if ARG is
omitted or nil, and toggle it if ARG is ‘toggle’."
:init-value nil
:group lsp-ui
:keymap lsp-ui-mode-map
(lsp-ui--toggle lsp-ui-mode))
;; The request is delegated to xref-backend-apropos defined in lsp-mode.
;; xref-find-apropos does similar job but is less appealing because it splits and
;; regex quotes the pattern. The language server likely knows more about how
;; to do fuzzy matching.
(defun lsp-ui-find-workspace-symbol (pattern)
"List project-wide symbols matching the query string PATTERN."
(interactive (list (read-string
"workspace/symbol: "
nil 'xref--read-pattern-history)))
(xref--find-xrefs pattern 'apropos pattern nil))
(defun lsp-ui--location< (x y)
"Compares two triples X and Y.
Both should have the form (FILENAME LINE COLUMN)."
(if (not (string= (car x) (car y)))
(string< (car x) (car y))
(if (not (= (cadr x) (cadr y)))
(< (cadr x) (cadr y))
(< (caddr x) (caddr y)))))
(defun lsp-ui--reference-triples (include-declaration)
"Return references as a list of (FILENAME LINE COLUMN) triples given EXTRA."
(let ((refs (lsp-request "textDocument/references"
(lsp--make-reference-params nil include-declaration))))
(sort
(mapcar
(-lambda ((&Location :uri :range (&Range :start (&Position :line :character))))
(list (lsp--uri-to-path uri) line character))
refs)
#'lsp-ui--location<)))
;; TODO Make it efficient
(defun lsp-ui-find-next-reference (&optional include-declaration)
"Find next reference of the symbol at point."
(interactive)
(let* ((cur (list buffer-file-name (1- (line-number-at-pos)) (- (point) (line-beginning-position))))
(refs (lsp-ui--reference-triples include-declaration))
(idx -1)
(res (-first (lambda (ref) (cl-incf idx) (lsp-ui--location< cur ref)) refs)))
(if res
(progn
(find-file (car res))
(goto-char 1)
(forward-line (cadr res))
(forward-char (caddr res))
(cons idx (length refs)))
(cons 0 0))))
;; TODO Make it efficient
(defun lsp-ui-find-prev-reference (&optional include-declaration)
"Find previous reference of the symbol at point."
(interactive)
(let* ((cur (list buffer-file-name (1- (line-number-at-pos)) (- (point) (line-beginning-position))))
(refs (lsp-ui--reference-triples include-declaration))
(idx -1)
(res (-last (lambda (ref) (and (lsp-ui--location< ref cur) (cl-incf idx))) refs)))
(if res
(progn
(find-file (car res))
(goto-char 1)
(forward-line (cadr res))
(forward-char (caddr res))
(cons idx (length refs)))
(cons 0 0))))
(provide 'lsp-ui)
;;; lsp-ui.el ends here