|
| 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 |
0 commit comments