Skip to content

Commit 63c80e4

Browse files
ido integration for lsp-mode (#2640)
* ido integration for lsp-mode * adding to Cask file * adding thing-at-point for prefix ARG call * adding read-string * typo * formatting the buffer * link ido integration into rEadme * remove tab
1 parent fb8e1e4 commit 63c80e4

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

Cask

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"lsp-diagnostics.el"
1414
"lsp-headerline.el"
1515
"lsp-iedit.el"
16+
"lsp-ido.el"
1617
"lsp-lens.el"
1718
"lsp-modeline.el"
1819
"lsp-semantic-tokens.el"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ most popular Emacs packages like `company`, `flycheck` and `projectile`.
7676
for better discovery
7777
- [iedit](https://emacs-lsp.github.io/lsp-mode/page/main-features/#iedit)
7878
- [dired](https://emacs-lsp.github.io/lsp-mode/page/main-features/#dired)
79+
- [ido](https://emacs-lsp.github.io/lsp-mode/page/main-features/#integrations)
7980

8081
## Presentations/demos
8182
- [System Crafters](https://twitter.com/SystemCrafters) channel
145 KB
Loading

docs/page/integration/ido.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Ido integration
2+
3+
`lsp-mode` provides integration with `ido` for `workspace-symbol`
4+
command. `lsp-mode` will show all workspace/project symbols using IDO.
5+
To enable this behaviour add `(require 'lsp-ido)` call to your config.
6+
7+
By installing
8+
[ido-completing-read+](https://github.com/DarwinAwardWinner/ido-completing-read-plus)
9+
`lsp-mode` will use IDO for showing candidates on other commands as
10+
well e.g. refactor, code actions, etc.
11+
12+
## Screenshot
13+
![ido-workspace-symbol](ido-workspace-symbol.png)
14+
15+
16+
## Command
17+
18+
- `lsp-ido-workspace-symbol` command will be available as an interactive call.
19+
20+
21+
## Configuration
22+
23+
You can disable symbol kind and symbol filepath from the candidate
24+
list by changing the following settings
25+
26+
```elisp
27+
(setq lsp-ido-show-symbol-kind nil
28+
lsp-ido-show-symbol-filename nil)
29+
```

lsp-ido.el

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
;;; lsp-ido.el --- `ido' integration -*- lexical-binding: t -*-
2+
;;
3+
;; Copyright (C) 2021 emacs-lsp maintainers
4+
;;
5+
;; This program is free software; you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation, either version 3 of the License, or
8+
;; (at your option) any later version.
9+
10+
;; This program is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
;;; Commentary:
19+
20+
;; This module provides an interactive ido interface to the workspace symbol
21+
;; functionality offered by lsp-mode.
22+
23+
;;; Code:
24+
25+
(require 'ido)
26+
(require 'lsp-protocol)
27+
(require 'lsp-mode)
28+
29+
(defgroup lsp-ido nil
30+
"LSP support for ido-based symbol completion"
31+
:group 'lsp-mode)
32+
33+
(defcustom lsp-ido-symbol-kind-to-string
34+
[" " ; Unknown - 0
35+
"File" ; File - 1
36+
"Modu" ; Module - 2
37+
"Nmsp" ; Namespace - 3
38+
"Pack" ; Package - 4
39+
"Clss" ; Class - 5
40+
"Meth" ; Method - 6
41+
"Prop" ; Property - 7
42+
"Fld " ; Field - 8
43+
"Cons" ; Constructor - 9
44+
"Enum" ; Enum - 10
45+
"Intf" ; Interface - 11
46+
"Func" ; Function - 12
47+
"Var " ; Variable - 13
48+
"Cnst" ; Constant - 14
49+
"Str " ; String - 15
50+
"Num " ; Number - 16
51+
"Bool " ; Boolean - 17
52+
"Arr " ; Array - 18
53+
"Obj " ; Object - 19
54+
"Key " ; Key - 20
55+
"Null" ; Null - 21
56+
"EmMm" ; EnumMember - 22
57+
"Srct" ; Struct - 23
58+
"Evnt" ; Event - 24
59+
"Op " ; Operator - 25
60+
"TPar"] ; TypeParameter - 26
61+
"A vector of 26 itens representing the SymbolKind."
62+
:group 'lsp-ido
63+
:type 'vector)
64+
65+
(defcustom lsp-ido-show-symbol-filename
66+
t
67+
"Whether to show the project-relative path to a symbol's point of definition."
68+
:group 'lsp-ido
69+
:type 'boolean)
70+
71+
(defcustom lsp-ido-show-symbol-kind
72+
t
73+
"Whether to show the symbol's kind when showing lsp symbols."
74+
:group 'lsp-ido
75+
:type 'boolean)
76+
77+
(eval-when-compile
78+
(lsp-interface
79+
(lsp-ido:FormattedSymbolInformation
80+
(:kind :name :location :textualRepresentation)
81+
(:containerName :deprecated))))
82+
83+
(lsp-defun lsp-ido--transform-candidate
84+
((symbol-information &as &SymbolInformation :kind :location (&Location :uri))
85+
lsp-ido--results project-root)
86+
(let* ((sanitized-kind (if (< kind (length lsp-ido-symbol-kind-to-string)) kind 0))
87+
(type (elt lsp-ido-symbol-kind-to-string sanitized-kind))
88+
(typestr (if lsp-ido-show-symbol-kind
89+
(format "[%s] " type)
90+
""))
91+
(pathstr (if lsp-ido-show-symbol-filename
92+
(propertize (format " . %s" (file-relative-name (lsp--uri-to-path uri) project-root))
93+
'face 'font-lock-comment-face)
94+
""))
95+
(textual-representation
96+
(lsp-render-symbol-information symbol-information "."))
97+
(entry (concat typestr textual-representation pathstr)))
98+
(puthash entry symbol-information lsp-ido--results)))
99+
100+
(lsp-defun lsp-ido--jump-selected-candidate
101+
((&SymbolInformation
102+
:location (&Location :uri :range (&Range :start (&Position :line :character)))))
103+
"Jump to selected candidate."
104+
(find-file (lsp--uri-to-path uri))
105+
(goto-char (point-min))
106+
(forward-line line)
107+
(forward-char character))
108+
109+
(defun lsp-ido--workspace-symbol (workspaces query)
110+
"Search against WORKSPACES based on QUERY."
111+
(let* ((lsp-ido--results (make-hash-table :test 'equal))
112+
(workspace-root (lsp-workspace-root))
113+
(raw-choices
114+
(with-lsp-workspaces workspaces
115+
(lsp-request
116+
"workspace/symbol"
117+
(lsp-make-workspace-symbol-params :query query)))))
118+
(mapc (lambda (it)
119+
(lsp-ido--transform-candidate it lsp-ido--results workspace-root))
120+
raw-choices)
121+
lsp-ido--results))
122+
123+
;;;###autoload
124+
(defun lsp-ido-workspace-symbol (arg)
125+
"`ido' for lsp workspace/symbol.
126+
When called with prefix ARG the default selection will be symbol at point."
127+
(interactive "P")
128+
(let* ((query (if arg "" (read-string "Workspace symbol: ")))
129+
(hash-table-candidates (lsp-ido--workspace-symbol (lsp-workspaces) query))
130+
(choice (ido-completing-read
131+
"Workspace symbol: "
132+
(hash-table-keys hash-table-candidates)
133+
nil
134+
nil
135+
(when arg (thing-at-point 'symbol)))))
136+
(lsp-ido--jump-selected-candidate (gethash choice hash-table-candidates))))
137+
138+
(provide 'lsp-ido)
139+
;;; lsp-ido.el ends here

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ nav:
1818
- Helm: https://github.com/emacs-lsp/helm-lsp
1919
- Ivy: https://github.com/emacs-lsp/lsp-ivy
2020
- Iedit: page/integration/iedit.md
21+
- Ido: page/integration/ido.md
2122
- Limitations: page/limitations.md
2223
- Remote: page/remote.md
2324
- FAQ: page/faq.md

0 commit comments

Comments
 (0)