forked from emacs-lsp/lsp-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-haskell.el
273 lines (238 loc) · 10.2 KB
/
lsp-haskell.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
;;; lsp-haskell.el --- Haskell support for lsp-mode
;; Version: 1.0
;; Package-Requires: ((lsp-mode "3.0") (haskell-mode "1.0"))
;; Keywords: haskell
;; URL: https://github.com/emacs-lsp/lsp-haskell
;;; 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.
;;; Code:
(require 'haskell)
(require 'lsp-mode)
(require 'projectile nil 'noerror)
;; ---------------------------------------------------------------------
;; Configuration
(defgroup lsp-haskell nil
"Customization group for ‘lsp-haskell’."
:group 'lsp-mode)
;; ---------------------------------------------------------------------
;; Language server options
;; These are registered with lsp-mode below, which handles preparing them for the server.
;; Originally generated from the vscode extension's package.json using lsp-generate-bindings.
;; Should ideally stay in sync with what's offered in the vscode extension.
(defcustom lsp-haskell-hlint-on
t
"Get suggestions from hlint."
:group 'lsp-haskell
:type 'boolean)
(defcustom lsp-haskell-max-number-of-problems
100
"Controls the maximum number of problems produced by the server."
:group 'lsp-haskell
:type 'number)
(defcustom
lsp-haskell-diagnostics-on-change
t
"Compute diagnostics continuously as you type. Turn off to only generate diagnostics on file save."
:group 'lsp-haskell
:type 'boolean)
(defcustom lsp-haskell-liquid-on
nil
"Get diagnostics from liquid haskell."
:group 'lsp-haskell
:type 'boolean)
(defcustom lsp-haskell-completion-snippets-on
t
"Show snippets with type information when using code completion."
:group 'lsp-haskell
:type 'boolean)
(defcustom lsp-haskell-format-on-import-on
t
"When adding an import, use the formatter on the result."
:group 'lsp-haskell
:type 'boolean)
(defcustom lsp-haskell-formatting-provider
"ormolu"
"The formatter to use when formatting a document or range."
:group 'lsp-haskell
:type '(choice (const :tag "brittany" "brittany")
(const :tag "floskell" "floskell")
(const :tag "fourmolu" "fourmolu")
(const :tag "ormolu" "ormolu")
(const :tag "stylish-haskell" "stylish-haskell")
(const :tag "none" "none")))
;; ---------------------------------------------------------------------
;; Non-language server options
(defcustom lsp-haskell-server-path
"haskell-language-server-wrapper"
"The language server executable. Can be something on the $PATH (e.g. 'ghcide') or a path to an executable itself."
:group 'lsp-haskell
:type 'string)
(defcustom lsp-haskell-server-log-file
(expand-file-name "hls.log" temporary-file-directory)
"The log file used by the server. Note that this is passed to the server via 'lsp-haskell-server-args', so if
you override that setting then this one will have no effect."
:group 'lsp-haskell
:type 'string)
(defcustom lsp-haskell-server-args
`("-d" "-l" ,lsp-haskell-server-log-file)
"The arguments for starting the language server.
For a debug log when using haskell-language-server, use `-d -l /tmp/hls.log'."
:group 'lsp-haskell
:type '(repeat (string :tag "Argument")))
(defcustom lsp-haskell-server-wrapper-function
#'identity
"Use this to wrap the language server process started by lsp-haskell.
For example, use the following the start the process in a nix-shell:
(lambda (argv)
(append
(append (list \"nix-shell\" \"-I\" \".\" \"--command\" )
(list (mapconcat 'identity argv \" \"))
)
(list (concat (lsp-haskell--get-root) \"/shell.nix\"))
)
)"
:group 'lsp-haskell
:type '(choice
(function-item :tag "None" :value identity)
(function :tag "Custom function")))
;; ---------------------------------------------------------------------
;; HaRe functions
(defun lsp-demote ()
"Demote a function to the level it is used"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:demote"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))))))
(defun lsp-duplicate-definition (newname)
"Duplicate a definition"
(interactive "sNew definition name: ")
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:dupdef"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))
:text ,newname))))
(defun lsp-if-to-case ()
"Convert an if statement to a case statement"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:iftocase"
(vector `(:file ,(concat "file://" buffer-file-name)
:start_pos ,(lsp-get-start-position)
:end_pos ,(lsp-get-end-position)))))
(defun lsp-lift-level ()
"Lift a function to the top level"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:liftonelevel"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))))))
(defun lsp-lift-to-top ()
"Lift a function to the top level"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:lifttotoplevel"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))))))
(defun lsp-delete-definition ()
"Delete a definition"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:deletedef"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))))))
(defun lsp-generalise-applicative ()
"Generalise a monadic function to use applicative"
(interactive)
(lsp--cur-workspace-check)
(lsp--send-execute-command
"hare:genapplicative"
(vector `(:file ,(concat "file://" buffer-file-name)
:pos ,(lsp-point-to-position (point))))))
;; ---------------------------------------------------------------------
;; Miscellaneous useful functions
(defun lsp-haskell--session-cabal-dir ()
"Get the session cabal-dir."
(let* ((cabal-file (haskell-cabal-find-file))
(cabal-dir (if cabal-file
(file-name-directory cabal-file)
"." ;; no cabal file, use directory only
)))
(progn
(message "cabal-dir: %s" cabal-dir)
cabal-dir)))
(defun lsp-haskell--get-root ()
"Get project root directory.
First searches for root via projectile. Tries to find cabal file
if projectile way fails"
;; (if (and (fboundp 'projectile-project-root) (projectile-project-root))
(if nil
(projectile-project-root)
(let ((dir (lsp-haskell--session-cabal-dir)))
(if (string= dir "/")
(user-error (concat "Couldn't find cabal file, using:" dir))
dir))))
;; ---------------------------------------------------------------------
;; Starting the server and registration with lsp-mode
(defun lsp-haskell--server-command ()
"Command and arguments for launching the inferior language server process.
These are assembled from the customizable variables `lsp-haskell-server-path'
and `lsp-haskell-server-args' and `lsp-haskell-server-wrapper-function'."
(funcall lsp-haskell-server-wrapper-function (append (list lsp-haskell-server-path "--lsp") lsp-haskell-server-args) ))
;; Register all the language server settings with lsp-mode.
;; Note that customizing these will currently *not* send the updated configuration to the server,
;; users must manually restart. See https://github.com/emacs-lsp/lsp-mode/issues/1174.
(lsp-register-custom-settings '(
("haskell.formattingProvider" lsp-haskell-formatting-provider)
("haskell.formatOnImportOn" lsp-haskell-format-on-import-on t)
("haskell.completionSnippetsOn" lsp-haskell-completion-snippets-on t)
("haskell.liquidOn" lsp-haskell-liquid-on t)
("haskell.diagnosticsOnChange" lsp-haskell-diagnostics-on-change t)
("haskell.maxNumberOfProblems" lsp-haskell-max-number-of-problems)
("haskell.hlintOn" lsp-haskell-hlint-on t)))
;; This mapping is set for 'haskell-mode -> haskell' in the lsp-mode repo itself. If we move
;; it there, then delete it from here.
;; It also isn't *too* important: it only sets the language ID, see
;; https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
(add-to-list 'lsp-language-id-configuration '(haskell-literate-mode . "haskell"))
;; Register the client itself
(lsp-register-client
(make-lsp--client
:new-connection (lsp-stdio-connection (lambda () (lsp-haskell--server-command)))
;; Should run under haskell-mode and haskell-literate-mode. We need to list the
;; latter even though it's a derived mode of the former
:major-modes '(haskell-mode haskell-literate-mode)
;; This is arbitrary.
:server-id 'lsp-haskell
;; We need to manually pull out the configuration section and set it. Possibly in
;; the future lsp-mode will asssociate servers with configuration sections more directly.
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--set-configuration (lsp-configuration-section "haskell"))))
;; This is somewhat irrelevant, but it is listed in lsp-language-id-configuration, so
;; we should set something consistent here.
:language-id "haskell"
;; This is required for completions to works inside language pragma statements
:completion-in-comments? t
))
;; ---------------------------------------------------------------------
(provide 'lsp-haskell)
;;; lsp-haskell.el ends here