forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp-mode.el
179 lines (155 loc) · 6.86 KB
/
lsp-mode.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
178
179
;;; lsp-mode.el --- Minor mode for interacting with Language Servers -*- lexical-binding: t -*-
;; Copyright (C) 2016 Vibhav Pant <[email protected]>
;; 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;; Author: Vibhav Pant <[email protected]>
;; URL: https://github.com/vibhavp/emacs-lsp
;; Package-Requires: ((emacs "25.1") (flycheck "30"))
;; Version: 2.0
;;; Commentary:
;;; Code:
(require 'lsp-methods)
(require 'lsp-receive)
(require 'lsp-send)
(require 'cl-lib)
(require 'lsp-jls)
(defun lsp--make-stdio-connection (name command)
(lambda (filter)
(let ((final-command (if (consp command) command (list command))))
(unless (executable-find (nth 0 final-command))
(error (format "Couldn't find executable %s" (nth 0 final-command))))
(make-process
:name name
:connection-type 'pipe
:command final-command
:filter filter
:stderr (generate-new-buffer-name (concat name " stderr"))))))
(defun lsp--verify-regexp-list (l)
(cl-assert (cl-typep l 'list) nil
"lsp-define-client: :ignore-regexps is not a list")
(dolist (e l l)
(cl-assert (cl-typep e 'string)
nil
(format
"lsp-define-client: :ignore-regexps element %s is not a string"
e))))
(defun lsp-define-client (mode language-id type get-root &rest args)
"Define a LSP client.
MODE is the major mode for which this client will be invoked.
LANGUAGE-ID is the language id to be used when communication with the Language Server.
Optional arguments:
`:name' is the process name for the language server.
`:command' is the command to run if `TYPE' is 'stdio.
`:on-initialize' is the function to call when a new project/workspace is initialized.
`:ignore-regexps' is a list of regexps which when matched will be ignored by the output parser."
(lsp--assert-type mode #'symbolp)
(let* ((client
(cl-case type
('stdio (make-lsp--client
:language-id (lsp--assert-type language-id #'stringp)
:send-sync 'lsp--stdio-send-sync
:send-async 'lsp--stdio-send-async
:type (lsp--assert-type type #'symbolp)
:new-connection (lsp--make-stdio-connection
(plist-get args (or :name
(format
"%s language server"
mode)))
(plist-get args :command))
:get-root (lsp--assert-type get-root #'functionp)
:on-initialize (plist-get args :on-initialize)
:ignore-regexps (lsp--verify-regexp-list (plist-get
args
:ignore-regexps))))
(t (error "Invalid TYPE for LSP client")))))
(puthash mode client lsp--defined-clients)))
(defun lsp--rust-rls-command ()
(let ((rls-root (getenv "RLS_ROOT")))
(if rls-root
`("cargo" "+nightly" "run" "--quiet" ,(concat
"--manifest-path="
(concat
(file-name-as-directory
(expand-file-name rls-root))
"Cargo.toml"))
"--release")
"rls")))
(lsp-define-client 'rust-mode "rust" 'stdio #'(lambda () default-directory)
:command (lsp--rust-rls-command)
:name "Rust Language Server")
(lsp-define-client 'go-mode "go" 'stdio #'(lambda () default-directory)
:command '("go-langserver" "-mode=stdio")
:name "Go Language Server"
:ignore-regexps '("^langserver-go: reading on stdin, writing on stdout$"))
(lsp-define-client 'python-mode "python" 'stdio #'(lambda () default-directory)
:command '("pyls")
:name "Python Language Server")
(lsp-define-client 'haskell-mode "haskell" 'stdio #'(lambda () default-directory)
;; :command '("hie" "--lsp" "-d" "-l" (make-temp-file "hie" nil ".log"))
:command '("hie" "--lsp" "-d" "-l" "/tmp/hie.log")
:name "Haskell Language Server")
(lsp-define-client 'java-mode "java" 'stdio #'lsp--java-get-root
:command (lsp--java-ls-command)
:name "Java Language Server")
;;;###autoload
(define-minor-mode global-lsp-mode ""
nil nil nil
:global t
(add-hook 'find-file-hook #'lsp-on-open)
(add-hook 'after-save-hook #'lsp-on-save)
(add-hook 'kill-buffer-hook #'lsp-on-close))
(defconst lsp--sync-type
`((0 . "None")
(1 . "Full Document")
(2 . "Incremental Changes")))
(defconst lsp--capabilities
`(("textDocumentSync" . ("Document sync method" .
((1 . "None")
(2 . "Send full contents")
(3 . "Send incremental changes."))))
("hoverProvider" . ("The server provides hover support" . boolean))
("completionProvider" . ("The server provides completion support" . boolean))
("definitionProvider" . ("The server provides goto definition support" . boolean))
("referencesProvider" . ("The server provides references support" . boolean))
(("documentHighlightProvider" . ("The server provides document highlight support." . boolean)))
("documentSymbolProvider" . ("The server provides file symbol support" . boolean))
("workspaceSymbolProvider" . ("The server provides project symbol support" . boolean))
("codeActionProvider" . ("The server provides code actions" . boolean))
("codeLensProvider" . ("The server provides code lens" . boolean))
("documentFormattingProvider" . ("The server provides file formatting" . boolean))
(("documentRangeFormattingProvider" . ("The server provides region formatting" . boolean)))
(("renameProvider" . ("The server provides rename support" . boolean)))))
(defun lsp--cap-str (cap)
(let* ((elem (assoc cap lsp--capabilities))
(desc (cadr elem))
(type (cddr elem))
(value (gethash cap (lsp--server-capabilities))))
(when (and elem desc type value)
(concat desc (cond
((listp type) (concat ": " (cdr (assoc value type))))) "\n"))))
(defun lsp-capabilities ()
"View all capabilities for the language server associated with this buffer."
(interactive)
(unless lsp--cur-workspace
(user-error "No language server is associated with this buffer"))
(let ((str (mapconcat #'lsp--cap-str (reverse (hash-table-keys
(lsp--server-capabilities))) ""))
(buffer-name (generate-new-buffer-name "lsp-capabilities"))
)
(get-buffer-create buffer-name)
(with-current-buffer buffer-name
(view-mode -1)
(erase-buffer)
(insert str)
(view-mode 1))
(switch-to-buffer buffer-name)))
(provide 'lsp-mode)
;;; lsp-mode.el ends here