forked from Kaptch/coq-lsp-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoq-lsp.el
433 lines (368 loc) · 14.7 KB
/
coq-lsp.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
;;; coq-lsp.el --- Coq Client settings -*- lexical-binding: t; -*-
;;; Commentary:
;; lsp-coq client
;;; Code:
(unless (locate-library "coq-lsp")
(add-to-list 'load-path
(expand-file-name
(file-name-directory (or load-file-name buffer-file-name)))))
(eval-when-compile
(require 'rx)
(require 'eglot)
(require 'cl-lib)
(require 'coq-mode)
)
;; (load-file "../coq-mode.el")
;; Mode description
(defgroup coq-lsp nil
"Coq"
:group 'elgot)
(defconst goals-buffer-name "*Goals*")
(defconst info-buffer-name "*Info*")
;; (defvar coq-mode-abbrev-table nil
;; "Abbreviation table used in `coq-mode' buffers.")
;; (define-abbrev-table 'coq-mode-abbrev-table
;; '())
;; (defvar coq-mode-map
;; (let ((map (make-sparse-keymap)))
;; (define-key map (kbd "C-c v") #'coq-lsp-refresh-window-layout)
;; (define-key map (kbd "C-c C-k") #'eglot-shutdown)
;; (define-key map (kbd "C-c c") #'coq-lsp-prove-till-cursor)
;; (define-key map (kbd "C-c s") #'coq-lsp-get-doc)
;; (define-key map (kbd "C-c d") #'coq-lsp-save-vo)
;; map))
(defvar proof-mode-abbrev-table nil
"Abbreviation table used in `proof-mode' buffers.")
(define-abbrev-table 'proof-mode-abbrev-table
'())
(defvar proof-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c v") #'coq-lsp-refresh-window-layout)
(define-key map (kbd "C-c C-k") #'eglot-shutdown)
(define-key map (kbd "C-c c") #'coq-lsp-prove-till-cursor)
(define-key map (kbd "C-c s") #'coq-lsp-get-doc)
(define-key map (kbd "C-c d") #'coq-lsp-save-vo)
map))
(add-hook 'eglot-managed-mode-hook
(lambda ()
;; Show flymake diagnostics first.
(setq eldoc-documentation-functions
(cons #'flymake-eldoc-function
(remove #'flymake-eldoc-function eldoc-documentation-functions)))
;; Show all eldoc feedback.
(setq eldoc-documentation-strategy #'eldoc-documentation-compose)))
(define-derived-mode proof-mode prog-mode "Proof"
"Major mode for coq files."
:group 'coq-lsp
(set 'compilation-mode-font-lock-keywords '())
(set (make-local-variable 'lisp-indent-function)
'common-lisp-indent-function)
:abbrev-table coq-mode-abbrev-table)
;; (define-derived-mode coq-mode prog-mode "coq"
;; "Major mode for coq files."
;; :group 'coq-lsp
;; (set 'compilation-mode-font-lock-keywords '())
;; (set (make-local-variable 'lisp-indent-function)
;; 'common-lisp-indent-function)
;; :abbrev-table coq-mode-abbrev-table)
(defvar proof-mode-status nil)
(defvar proof-mode-electric-terminator-mode nil)
;; Additional requests
(defun eglot--signal-proof/goals (position)
"Send proof/goals to server, requesting the list of goals at POSITION."
(let ((server (eglot-current-server))
(params `(:textDocument ,(eglot--TextDocumentIdentifier)
:position ,position)))
(if server
(let ((response (jsonrpc-request server :proof/goals params)))
(if response
(let ((textDocumet (plist-get response :textDocument))
(position (plist-get response :position))
(goals (plist-get response :goals))
(messages (plist-get response :messages))
(err (plist-get response :error)))
(progn
(coq-lsp--process-goal-info goals)
(coq-lsp--process-info-buffer messages err)
;; Uncomment the line here to print the full debug information
;; in the info buffer
;; (coq-lsp--debug textDocumet position goals messages err)
)
))))))
(defun eglot--signal-coq/document ()
(let ((server (eglot-current-server))
(params `(:textDocument ,(eglot--TextDocumentIdentifier))))
(if server
(let ((response (jsonrpc-request server :coq/getDocument params)))
(if response
(let ((spans (plist-get response :spans))
(completed (plist-get response :completed)))
(coq-lsp--process-doc-info spans completed)))))))
(defun eglot--signal-coq/savevo ()
(let ((server (eglot-current-server))
(params `(:textDocument ,(eglot--TextDocumentIdentifier))))
(if server
(let ((response (jsonrpc-request server :coq/saveVo params)))
(if response
(coq-lsp--update-buffer-with-text info-buffer-name "Couldn't compile the file!\n\n"))))))
;; Additional notifications
(cl-defmethod eglot-handle-notification
(_server (_method (eql $/coq/fileProgress))
&key textDocument processing)
(let* ((uri (plist-get textDocument :uri))
(lst (mapcar (lambda (elem) (list (plist-get elem :range) (plist-get elem :kind))) processing))q)
(if-let ((buffer (find-buffer-visiting (eglot--uri-to-path uri))))
(with-current-buffer buffer
(dolist (elem lst)
(let ((start (plist-get (plist-get (car elem) :start) :line))
(end (plist-get (plist-get (car elem) :end) :line))
(status (cdr elem)))
;; (coq-lsp--process-notification start end status)
() ;; do nothing ?
))))))
;; Implementation
(defvar-local proof-line-position (list :pos 0 :buffer nil))
(defun coq-lsp--prove-till (pos)
"Evaluate till POS and move the cursor to the end of evaluated region."
(setq proof-line-position
(list :pos pos :buffer (current-buffer)))
(goto-char pos)
(eglot--signal-proof/goals (eglot--pos-to-lsp-position)))
;; (defun coq-lsp--highlight-text (start end color)
;; "Highlight text in COLOR from START to END in the current buffer."
;; (let ((overlay (make-overlay start end)))
;; (overlay-put overlay 'face '(:background color))))
;; ;; taken from lambdapi-proof.el
;; ;; taken from cus-edit.el
;; (defun coq-lsp--draw-horizontal-line ()
;; "Draw a horizontal line at point.
;; This works for both graphical and text displays."
;; (let ((p (point)))
;; (insert "\n")
;; (put-text-property p (1+ p) 'face '(:underline t))
;; (overlay-put (make-overlay p (1+ p))
;; 'before-string
;; (propertize "\n" 'face '(:underline t)
;; 'display
;; (list 'space :align-to
;; `(+ (0 . right)
;; ,(min (window-hscroll)
;; (- (line-end-position)
;; (line-beginning-position)))))))))
(defvar last-post-command-position 0
"Holds the cursor position from the last run of post-command-hooks.")
(make-variable-buffer-local 'last-post-command-position)
(defun coq-lsp-dots-between-points (point1 point2)
"Check if there's at least one dot between POINT1 and POINT2."
(save-excursion
(goto-char (min point1 point2))
(while (and (< (point) (max point1 point2))
(progn
(forward-char)
(not (eobp)))
(not (search-forward "." (max point1 point2) t)))
nil)
(if (<= (point) (max point1 point2))
t
nil)))
(defun coq-lsp-prove-till-cursor-if-moved-post-command ()
"Check if the cursor has moved and there's a dot in between the old and new position."
(let ((current-pos (point)))
(when (and (/= current-pos last-post-command-position)
(coq-lsp-dots-between-points last-post-command-position current-pos))
(coq-lsp-prove-till-cursor)
(setq last-post-command-position current-pos))))
(defun coq-lsp--pp-name-list (namelist)
(substring (mapconcat (lambda (h) (format "%s, " h)) namelist "") 0 -2))
(defun coq-lsp--pp-hyp (hyp)
(let ((hyp-names (plist-get hyp :names)))
(let ((hyp-name (coq-lsp--pp-name-list hyp-names))
(hyp-def (plist-get hyp :def))
(hyp-ty (plist-get hyp :ty)))
(cond (hyp-def (format "%s = %s : %s\n" hyp-name hyp-def hyp-ty))
(t (format "%s : %s\n" hyp-name hyp-ty))
))))
;; We know that (len goals) > 0, because otherwise the function is not called
(defun coq-lsp--pp-hyps (goals)
(let ((hyps (plist-get (seq-elt goals 0) :hyps)))
(let ((hyps (seq-map 'coq-lsp--pp-hyp hyps)))
(mapconcat (lambda (h) (format "%s" h)) hyps "")
)
)
)
;; We know that (len goals) > 0, because otherwise the function is not called
(defun coq-lsp--pp-main-goal (goals)
(plist-get (seq-elt goals 0) :ty))
(defun coq-lsp--pp-sub-goal (goal ngoal)
(format "Goal %d:\n%s\n\n" ngoal goal)
)
(defun coq-lsp--pp-stack-subgoals (goals)
(if (> (seq-length goals) 1)
(car
(cl-reduce
(lambda (acc goal)
(cons (concat (car acc) (coq-lsp--pp-sub-goal goal (cdr acc)))
(+ 1 (cdr acc))))
(mapcar (lambda (g) (plist-get g :ty)) (substring goals 1))
:initial-value '("" . 2)
))
""
))
(defun coq-lsp--pp-goals-focus (goals)
(if (> (seq-length goals) 0)
(format
"Goal:\n
%s
===========================\n
%s\n\n%s\n\n"
(coq-lsp--pp-hyps goals)
(coq-lsp--pp-main-goal goals)
(coq-lsp--pp-stack-subgoals goals)
)
""
))
(defun coq-lsp--pp-stack (goal-focus goal-stack)
(if (and (> (seq-length goal-stack) 0) (= (seq-length goal-focus) 0))
(mapcar
'coq-lsp--pp-stack-subgoals
(seq-elt goal-stack 0))
;; (format "%s\n" goal-stack)
""
)
)
(defun coq-lsp--length-admitted (goals)
(length (plist-get goals :given_up)))
(defun coq-lsp--length-shelved (goals)
(length (plist-get goals :shelf)))
;; TODO find a way to compute the size of remaining goal, through the
;; goals/stack, goals/goals, goals/shelf and goals/given-up
(defun coq-lsp--length-remaining (goals)
0)
(defun coq-lsp--pp-remaining-goals (goals)
(format "%d remaining goals (%s shelved) (%d admitted)\n"
(coq-lsp--length-remaining goals)
(coq-lsp--length-shelved goals)
(coq-lsp--length-admitted goals)))
(defun coq-lsp--process-goal-info (goals)
;; update only if the goal is not nil ?
(if goals
(coq-lsp--update-buffer-with-text
goals-buffer-name
(format "%s\n\n%s%s"
(coq-lsp--pp-remaining-goals goals)
(coq-lsp--pp-goals-focus (plist-get goals :goals))
(coq-lsp--pp-stack (plist-get goals :goals) (plist-get goals :stack)))
)
)
)
;; TODO better pretty-print of messages
(defun coq-lsp--pp-message (messages)
(if (> (seq-length messages) 0)
(mapconcat
(lambda (n) (format "%s\n" (plist-get (seq-elt messages n) :text)))
(number-sequence 0 (- (seq-length messages) 1))
"")
""
)
)
(defun coq-lsp--process-info-buffer (messages err)
(coq-lsp--update-buffer-with-text
info-buffer-name
(if err
(format "%s\n" err)
(coq-lsp--pp-message messages)
)
)
)
(defun coq-lsp--debug (textDocument position goals messages err)
(coq-lsp--append-buffer-with-text
info-buffer-name
(format "\ndocument: %s\nposition: %s\ngoals: %s\nmessages: %s\nerror: %s\n\n"
textDocument position goals messages err
)))
(defun coq-lsp--process-doc-info (spans completed)
(coq-lsp--append-buffer-with-text info-buffer-name (format "spans: %s\ncompleted: %s\n\n" spans completed)))
(defun coq-lsp--process-notification (start end status)
(coq-lsp--append-buffer-with-text info-buffer-name (format "start: %s\nend: %s\nstatus: %s\n\n" start end status)))
(defun coq-lsp--update-buffer-with-text (buffer-name text)
"Create a new buffer or update an existing buffer named BUFFER-NAME with formatted TEXT."
(with-current-buffer (get-buffer-create buffer-name)
(read-only-mode -1)
(erase-buffer)
(insert text)
(font-lock-fontify-buffer)
(read-only-mode 1)
(set-buffer-modified-p nil)))
(defun coq-lsp--append-buffer-with-text (buffer-name text)
"Create a new buffer or append an existing buffer named BUFFER-NAME with formatted TEXT."
(with-current-buffer (get-buffer-create buffer-name)
(read-only-mode -1)
(insert text)
(font-lock-fontify-buffer)
(read-only-mode 1)
(set-buffer-modified-p nil)))
(defun get-current-window ()
"Return the current window."
(let ((all-windows (window-list)))
(seq-find (lambda (w) (eq w (selected-window))) all-windows)))
;; Interface
(defun coq-lsp-prove-till-cursor ()
"Proves till the command/tactic at cursor"
(interactive)
(coq-lsp--prove-till (point)))
(defun coq-lsp-get-doc ()
"Return document"
(interactive)
(eglot--signal-coq/document))
(defun coq-lsp-save-vo ()
"Save vo"
(interactive)
(eglot--signal-coq/savevo))
(defun coq-lsp--safe-split-window-vertically ()
(if (<= (window-height) (* 2 window-min-height))
(enlarge-window (+ 3 (* 2 window-min-height))))
(split-window-vertically))
(defun coq-lsp--split-window-horizontally-1/3 ()
"Split the current window horizontally, with the top part taking up 1/3 of the space."
(let ((split-height-threshold nil))
(split-window-below (/ (window-height) 3)))
(other-window 1))
(defun coq-lsp--split-window-vertically-1/3 ()
"Split the current window vertically, with the left part taking up 1/3 of the space."
(interactive)
(let ((split-width-threshold nil))
(split-window-right (/ (window-width) 3)))
(other-window 1))
(defun coq-lsp--split-window-vertically-2/3 ()
"Split the current window vertically, with the left part taking up 2/3 of the space."
(interactive)
(let ((split-width-threshold nil))
(split-window-right (* (/ (window-width) 3) 2)))
(other-window 1))
(defun coq-lsp-refresh-window-layout ()
"Create a 3-window layout with left window occupying half the screen, and two right windows stacked one on top of the other, with the two right windows read-only."
(interactive)
(let ((goals-buffer (get-buffer-create goals-buffer-name))
(info-buffer (get-buffer-create info-buffer-name)))
(with-current-buffer goals-buffer
(read-only-mode 1))
(with-current-buffer info-buffer
(read-only-mode 1))
(delete-other-windows)
;; (split-window-horizontally (round (* 0.5 (frame-width))))
;; (other-window 1)
(coq-lsp--split-window-vertically-2/3)
(set-window-buffer (get-current-window) goals-buffer)
(with-selected-window (get-current-window)
(set-window-dedicated-p (selected-window) t))
;; (split-window-vertically)
(coq-lsp--safe-split-window-vertically)
(other-window 1)
(set-window-buffer (get-current-window) info-buffer)
(with-selected-window (get-current-window)
(set-window-dedicated-p (selected-window) t))
(other-window 1)))
(add-hook 'post-command-hook #'coq-lsp-prove-till-cursor-if-moved-post-command)
(add-to-list 'auto-mode-alist '("\\.v" . coq-mode))
(provide 'coq-lsp)
;;; coq-lsp.el ends here