-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathlsp-haskell.el
293 lines (237 loc) · 9.42 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;;; 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
;;;###autoload
(defgroup lsp-haskell nil
"Customization group for ‘lsp-haskell’."
:group 'lsp-mode)
;;;###autoload
(defcustom lsp-haskell-process-path-hie
;; "hie"
"hie-wrapper"
"The path for starting the haskell-ide-engine
server. hie-wrapper exists on HIE master from 2018-06-10"
:group 'lsp-haskell
:type '(choice string))
;;;###autoload
(defcustom lsp-haskell-process-args-hie
'("-d" "-l" "/tmp/hie.log")
"The arguments for starting the haskell-ide-engine server.
For a debug log, use `-d -l /tmp/hie.log'."
:group 'lsp-haskell
:type '(repeat (string :tag "Argument")))
;;;###autoload
(defcustom lsp-haskell-process-wrapper-function
#'identity
"Use this to wrap the haskell-ide-engine process started by lsp-haskell.
For example, use the following the start the hie 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))))))
;; ---------------------------------------------------------------------
(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))))
;; ---------------------------------------------------------------------
(defun lsp--haskell-hie-command ()
"Comamnd and arguments for launching the inferior hie process.
These are assembled from the customizable variables
`lsp-haskell-process-path-hie' and
`lsp-haskell-process-args-hie'. If the hie executable is
installed via its Makefile, there will be compiler-specific
versions with names like 'hie-8.0.2' or 'hie-8.2.2'."
(append (list lsp-haskell-process-path-hie "--lsp") lsp-haskell-process-args-hie) )
;; ---------------------------------------------------------------------
;; Supporting the new lsp.el operation, as per
;; https://github.com/emacs-lsp/lsp-mode/blob/master/README-NEXT.md
(eval-after-load 'lsp '(lsp-register-client
(make-lsp--client
:new-connection (lsp-stdio-connection (lambda () (lsp-haskell--hie-command)))
:major-modes '(haskell-mode)
:server-id 'hie
;; :multi-root t
:initialization-options 'lsp-haskell--make-init-options)))
(defun lsp-haskell--hie-command ()
(funcall lsp-haskell-process-wrapper-function (lsp--haskell-hie-command)))
(cl-defmethod lsp-initialization-options ((_server (eql hie)))
"Initialization options for haskell."
`(:languageServerHaskell ,lsp-haskell--config-options))
;; ---------------------------------------------------------------------
(defvar lsp-haskell--config-options (make-hash-table))
;; ---------------------------------------------------------------------
(defun lsp-haskell--set-configuration ()
(lsp--set-configuration `(:languageServerHaskell ,lsp-haskell--config-options)))
(add-hook 'lsp-after-initialize-hook 'lsp-haskell--set-configuration)
(defun lsp-haskell-set-config (name option)
"Set config option NAME to value OPTION in the haskell lsp server."
(puthash name option lsp-haskell--config-options))
;; parseJSON = withObject "Config" $ \v -> do
;; s <- v .: "languageServerHaskell"
;; flip (withObject "Config.settings") s $ \o -> Config
;; <$> o .:? "hlintOn" .!= True
;; <*> o .:? "maxNumberOfProblems" .!= 100
;; <*> o .:? "liquidOn" .!= False
;; <*> o .:? "completionSnippetsOn" .!= True
;; -------------------------------------
(defun lsp-haskell-set-hlint (val)
"Enable(t)/Disable(nil) running hlint."
(lsp-haskell-set-config "hlintOn" val))
(defun lsp-haskell-set-hlint-on ()
"Enable running hlint haskell."
(interactive)
(lsp-haskell-set-hlint t)
(lsp-haskell--set-configuration))
(defun lsp-haskell-set-hlint-off ()
"Disable running hlint."
(interactive)
(lsp-haskell-set-hlint :json-false)
(lsp-haskell--set-configuration))
;; -------------------------------------
(defun lsp-haskell-set-max-problems (val)
"Set maximum number of problems reported to VAL."
(lsp-haskell-set-config "maxNumberOfProblems" val))
(defun lsp-haskell-set-max-number-of-problems (val)
"Set maximum number of problems reported to VAL."
(interactive "nMax number of problems to report: ")
(lsp-haskell-set-max-problems val)
(lsp-haskell--set-configuration))
;; -------------------------------------
(defun lsp-haskell-set-liquid (val)
"Enable(t)/Disable(nil) running liquid haskell on save."
(lsp-haskell-set-config "liquidOn" val))
(defun lsp-haskell-set-liquid-on ()
"Enable running liquid haskell on save."
(interactive)
(lsp-haskell-set-liquid t)
(lsp-haskell--set-configuration))
(defun lsp-haskell-set-liquid-off ()
"Disable running liquid haskell on save."
(interactive)
(lsp-haskell-set-liquid :json-false)
(lsp-haskell--set-configuration))
;; -------------------------------------
(defun lsp-haskell-set-completion-snippets (val)
"Enable(t)/Disable(nil) providing completion snippets."
(lsp-haskell-set-config "completionSnippetsOn" val))
(defun lsp-haskell-set-completion-snippets-on ()
"Enable providing completion snippets."
(interactive)
(lsp-haskell-set-completion-snippets t)
(lsp-haskell--set-configuration))
(defun lsp-haskell-set-completion-snippets-off ()
"Disable providing completion snippets."
(interactive)
(lsp-haskell-set-completion-snippets :json-false)
(lsp-haskell--set-configuration))
;; ---------------------------------------------------------------------
(provide 'lsp-haskell)
;;; lsp-haskell.el ends here