forked from pythonic-emacs/anaconda-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanaconda-mode.el
690 lines (575 loc) · 25.3 KB
/
anaconda-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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
;;; anaconda-mode.el --- Code navigation, documentation lookup and completion for Python -*- lexical-binding: t; -*-
;; Copyright (C) 2013-2015 by Artem Malyshev
;; Author: Artem Malyshev <[email protected]>
;; URL: https://github.com/proofit404/anaconda-mode
;; Version: 0.1.1
;; Package-Requires: ((emacs "24") (pythonic "0.1.0") (dash "2.6.0") (s "1.9") (f "0.16.2"))
;; 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/>.
;;; Commentary:
;; See the README for more details.
;;; Code:
(eval-when-compile
(defvar url-http-end-of-headers))
(require 'tramp)
(require 'url)
(require 'json)
(require 'pythonic)
(require 'dash)
(require 's)
(require 'f)
(defgroup anaconda-mode nil
"Code navigation, documentation lookup and completion for Python."
:group 'programming)
(defcustom anaconda-mode-installation-directory
"~/.emacs.d/anaconda-mode"
"Installation directory for anaconda-mode server."
:group 'anaconda-mode
:type 'directory)
(defcustom anaconda-mode-complete-callback
'anaconda-mode-complete-callback
"Callback function used to display `anaconda-mode-complete' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-show-doc-callback
'anaconda-mode-show-doc-callback
"Callback function used to display `anaconda-mode-show-doc' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-find-definitions-callback
'anaconda-mode-find-definitions-callback
"Callback function used to display `anaconda-mode-find-definitions' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-find-assignments-callback
'anaconda-mode-find-assignments-callback
"Callback function used to display `anaconda-mode-find-assignments' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-find-references-callback
'anaconda-mode-find-references-callback
"Callback function used to display `anaconda-mode-find-references' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-eldoc-callback
'anaconda-mode-eldoc-callback
"Callback function used to display `anaconda-mode-eldoc-function' result."
:group 'anaconda-mode
:type 'function)
(defcustom anaconda-mode-eldoc-as-single-line nil
"If not nil, trim eldoc string to frame width."
:group 'anaconda-mode
:type 'boolean)
;;; Server.
(defvar anaconda-mode-server-version "0.1.1"
"Server version needed to run anaconda-mode.")
(defvar anaconda-mode-server-script "anaconda_mode.py"
"Script file with anaconda-mode server.")
(defvar anaconda-mode-process-name "anaconda-mode"
"Process name for anaconda-mode processes.")
(defvar anaconda-mode-process-buffer "*anaconda-mode*"
"Buffer name for anaconda-mode processes.")
(defvar anaconda-mode-process nil
"Currently running anaconda-mode process.")
(defvar anaconda-mode-port nil
"Port for anaconda-mode connection.")
(defvar anaconda-mode-ensure-directory-command "
import os
import sys
directory = sys.argv[-1]
if not os.path.exists(directory):
os.makedirs(directory)
" "Create `anaconda-mode-server-directory' if necessary.")
(defvar anaconda-mode-check-installation-command "
import sys, os
from pkg_resources import find_distributions
directory = sys.argv[-1]
for dist in find_distributions(directory, only=True):
if dist.project_name == 'anaconda-mode':
break
else:
# IPython patch sys.exit, so we can't use it.
os._exit(1)
" "Check if `anaconda-mode' server is installed or not.")
(defvar anaconda-mode-install-server-command "
import sys
import pip
directory = sys.argv[-2]
version = sys.argv[-1]
pip.main(['install', '-t', directory, 'anaconda_mode==' + version])
" "Install `anaconda_mode' server.")
(defun anaconda-mode-server-directory ()
"Anaconda mode installation directory."
(f-join anaconda-mode-installation-directory anaconda-mode-server-version))
(defun anaconda-mode-host ()
"Target host with anaconda-mode server."
(if (pythonic-remote-p)
(tramp-file-name-host
(tramp-dissect-file-name
(pythonic-tramp-connection)))
"127.0.0.1"))
(defun anaconda-mode-start (&optional callback)
"Start anaconda-mode server.
CALLBACK function will be called when `anaconda-mode-port' will
be bound."
(when (anaconda-mode-need-restart)
(anaconda-mode-stop))
(if (anaconda-mode-running-p)
(when callback
(funcall callback))
(anaconda-mode-ensure-directory callback)))
(defun anaconda-mode-stop ()
"Stop anaconda-mode server."
(when (anaconda-mode-running-p)
(set-process-filter anaconda-mode-process nil)
(set-process-sentinel anaconda-mode-process nil)
(kill-process anaconda-mode-process)
(setq anaconda-mode-process nil
anaconda-mode-port nil)))
(defun anaconda-mode-running-p ()
"Is `anaconda-mode' server running."
(and anaconda-mode-process
(process-live-p anaconda-mode-process)))
(defun anaconda-mode-bound-p ()
"Is `anaconda-mode' port bound."
(numberp anaconda-mode-port))
(defun anaconda-mode-need-restart ()
"Check if we need to restart `anaconda-mode-server'."
(when (and (anaconda-mode-running-p)
(anaconda-mode-bound-p))
(or (not (pythonic-proper-environment-p anaconda-mode-process))
(not (equal (process-get anaconda-mode-process 'server-directory)
(anaconda-mode-server-directory))))))
(defun anaconda-mode-ensure-directory (&optional callback)
"Ensure if `anaconda-mode-server-directory' exists.
CALLBACK function will be called when `anaconda-mode-port' will
be bound."
(setq anaconda-mode-process
(start-pythonic :process anaconda-mode-process-name
:buffer anaconda-mode-process-buffer
:sentinel (lambda (process event) (anaconda-mode-ensure-directory-sentinel process event callback))
:args (list "-c"
anaconda-mode-ensure-directory-command
(anaconda-mode-server-directory)))))
(defun anaconda-mode-ensure-directory-sentinel (process _event &optional callback)
"Run `anaconda-mode-check' if `anaconda-mode-server-directory' exists.
Raise error otherwise. PROCESS and EVENT are basic sentinel
parameters. CALLBACK function will be called when
`anaconda-mode-port' will be bound."
(if (eq 0 (process-exit-status process))
(anaconda-mode-check callback)
(pop-to-buffer anaconda-mode-process-buffer)
(error "Can't create %s directory"
(anaconda-mode-server-directory))))
(defun anaconda-mode-check (&optional callback)
"Check `anaconda-mode' server installation.
CALLBACK function will be called when `anaconda-mode-port' will
be bound."
(setq anaconda-mode-process
(start-pythonic :process anaconda-mode-process-name
:buffer anaconda-mode-process-buffer
:sentinel (lambda (process event) (anaconda-mode-check-sentinel process event callback))
:args (list "-c"
anaconda-mode-check-installation-command
(anaconda-mode-server-directory)))))
(defun anaconda-mode-check-sentinel (process _event &optional callback)
"Run `anaconda-mode-bootstrap' if server installation check passed.
Try to install `anaconda-mode' server otherwise. PROCESS and
EVENT are basic sentinel parameters. CALLBACK function will be
called when `anaconda-mode-port' will be bound."
(if (eq 0 (process-exit-status process))
(anaconda-mode-bootstrap callback)
(anaconda-mode-install callback)))
(defun anaconda-mode-install (&optional callback)
"Try to install `anaconda-mode' server.
CALLBACK function will be called when `anaconda-mode-port' will
be bound."
(setq anaconda-mode-process
(start-pythonic :process anaconda-mode-process-name
:buffer anaconda-mode-process-buffer
:sentinel (lambda (process event) (anaconda-mode-install-sentinel process event callback))
:args (list "-c"
anaconda-mode-install-server-command
(anaconda-mode-server-directory)
anaconda-mode-server-version))))
(defun anaconda-mode-install-sentinel (process _event &optional callback)
"Run `anaconda-mode-bootstrap' if server installation complete successfully.
Raise error otherwise. PROCESS and EVENT are basic sentinel
parameters. CALLBACK function will be called when
`anaconda-mode-port' will be bound."
(if (eq 0 (process-exit-status process))
(anaconda-mode-bootstrap callback)
(pop-to-buffer anaconda-mode-process-buffer)
(error "Can't install `anaconda-mode' server")))
(defun anaconda-mode-bootstrap (&optional callback)
"Run `anaconda-mode' server.
CALLBACK function will be called when `anaconda-mode-port' will
be bound."
(setq anaconda-mode-process
(start-pythonic :process anaconda-mode-process-name
:buffer anaconda-mode-process-buffer
:cwd (anaconda-mode-server-directory)
:filter (lambda (process output) (anaconda-mode-bootstrap-filter process output callback))
:sentinel 'anaconda-mode-bootstrap-sentinel
:query-on-exit nil
:args (list anaconda-mode-server-script)))
(process-put anaconda-mode-process 'server-directory (anaconda-mode-server-directory)))
(defun anaconda-mode-bootstrap-filter (process output &optional callback)
"Set `anaconda-mode-port' from PROCESS OUTPUT.
Connect to the `anaconda-mode' server. CALLBACK function will be
called when `anaconda-mode-port' will be bound."
;; Mimic default filter.
(when (buffer-live-p (process-buffer process))
(with-current-buffer (process-buffer process)
(save-excursion
(goto-char (process-mark process))
(insert output)
(set-marker (process-mark process) (point)))))
(--when-let (s-match "anaconda_mode port \\([0-9]+\\)" output)
(setq anaconda-mode-port (string-to-number (cadr it)))
(set-process-filter process nil)
(when callback
(funcall callback))))
(defun anaconda-mode-bootstrap-sentinel (process _event)
"Raise error if `anaconda-mode' server exit abnormally.
PROCESS and EVENT are basic sentinel parameters."
(unless (eq 0 (process-exit-status process))
(pop-to-buffer anaconda-mode-process-buffer)
(error "Can't start `anaconda-mode' server")))
;;; Interaction.
(defun anaconda-mode-call (command callback)
"Make remote procedure call for COMMAND.
Apply CALLBACK to it result."
(anaconda-mode-start
(lambda () (anaconda-mode-jsonrpc command callback))))
(defun anaconda-mode-jsonrpc (command callback)
"Perform JSONRPC call for COMMAND.
Apply CALLBACK to the call result when retrieve it. Remote
COMMAND must expect four arguments: python buffer content, line
number position, column number position and file path."
(let ((url-request-method "POST")
(url-request-data (anaconda-mode-jsonrpc-request command)))
(url-retrieve
(format "http://%s:%s" (anaconda-mode-host) anaconda-mode-port)
(anaconda-mode-create-response-handler command callback)
nil
t)))
(defun anaconda-mode-jsonrpc-request (command)
"Prepare JSON encoded buffer data for COMMAND call."
(json-encode (anaconda-mode-jsonrpc-request-data command)))
(defun anaconda-mode-jsonrpc-request-data (command)
"Prepare buffer data for COMMAND call."
`((jsonrpc . "2.0")
(id . 1)
(method . ,command)
(params . ((source . ,(buffer-substring-no-properties (point-min) (point-max)))
(line . ,(line-number-at-pos (point)))
(column . ,(- (point) (line-beginning-position)))
(path . ,(when (buffer-file-name)
(pythonic-file-name (buffer-file-name))))))))
(defun anaconda-mode-create-response-handler (command callback)
"Create server response handler based on COMMAND and CALLBACK function.
COMMAND argument will be used for response skip message.
Response can be skipped if point was moved sense request was
submitted."
(let ((anaconda-mode-request-point (point))
(anaconda-mode-request-buffer (current-buffer))
(anaconda-mode-request-window (selected-window))
(anaconda-mode-request-tick (buffer-chars-modified-tick)))
(lambda (_status)
(let ((http-buffer (current-buffer)))
(unwind-protect
(if (or (not (equal anaconda-mode-request-window (selected-window)))
(with-current-buffer (window-buffer anaconda-mode-request-window)
(or (not (equal anaconda-mode-request-buffer (current-buffer)))
(not (equal anaconda-mode-request-point (point)))
(not (equal anaconda-mode-request-tick (buffer-chars-modified-tick))))))
(message "Skip anaconda-mode %s response" command)
(goto-char url-http-end-of-headers)
(let* ((json-array-type 'list)
(response (condition-case nil
(json-read)
(json-readtable-error
(progn
(let ((response-string (buffer-string)))
(pop-to-buffer
(with-current-buffer (get-buffer-create "*anaconda-response*")
(insert response-string)
(goto-char (point-min))
(current-buffer))))
(error "Can't read anaconda-mode server response"))))))
(if (assoc 'error response)
(error (cdr (assoc 'error response)))
(with-current-buffer anaconda-mode-request-buffer
;; Terminate `apply' call with empty list so response
;; will be treated as single argument.
(apply callback (cdr (assoc 'result response)) nil)))))
(kill-buffer http-buffer))))))
;;; Code completion.
(defun anaconda-mode-complete ()
"Request completion candidates."
(interactive)
(unless (python-syntax-comment-or-string-p)
(anaconda-mode-call "complete" anaconda-mode-complete-callback)))
(defun anaconda-mode-complete-callback (result)
"Start interactive completion on RESULT receiving."
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(start (or (car bounds) (point)))
(stop (or (cdr bounds) (point)))
(collection (anaconda-mode-complete-extract-names result))
(completion-extra-properties '(:annotation-function anaconda-mode-complete-annotation)))
(completion-in-region start stop collection)))
(defun anaconda-mode-complete-extract-names (result)
"Extract completion names from anaconda-mode RESULT."
(--map (let* ((name (cdr (assoc 'name it)))
(type (cdr (assoc 'type it)))
(module-path (cdr (assoc 'module-path it)))
(line (cdr (assoc 'line it)))
(docstring (cdr (assoc 'docstring it)))
(description (if (equal type "statement")
"statement"
(cdr (assoc 'description it)))))
(put-text-property 0 1 'description description name)
(put-text-property 0 1 'module-path module-path name)
(put-text-property 0 1 'line line name)
(put-text-property 0 1 'docstring docstring name)
name)
result))
(defun anaconda-mode-complete-annotation (candidate)
"Get annotation for CANDIDATE."
(--when-let (get-text-property 0 'description candidate)
(concat " <" it ">")))
;;; View documentation.
(defun anaconda-mode-show-doc ()
"Show documentation for context at point."
(interactive)
(anaconda-mode-call "goto_definitions" anaconda-mode-show-doc-callback))
(defun anaconda-mode-show-doc-callback (result)
"Process view doc RESULT."
(if result
(anaconda-mode-documentation-view result)
(message "No documentation available")))
;;; Find definitions.
(defun anaconda-mode-find-definitions ()
"Find definitions for thing at point."
(interactive)
(anaconda-mode-call "goto_definitions" anaconda-mode-find-definitions-callback))
(defun anaconda-mode-find-definitions-callback (result)
"Process find definitions RESULT."
(if result
(anaconda-mode-definitions-view result)
(message "No definitions found")))
;;; Find assignments.
(defun anaconda-mode-find-assignments ()
"Find assignments for thing at point."
(interactive)
(anaconda-mode-call "goto_assignments" anaconda-mode-find-assignments-callback))
(defun anaconda-mode-find-assignments-callback (result)
"Process find assignments RESULT."
(if result
(anaconda-mode-definitions-view result)
(message "No assignments found")))
;;; Find references.
(defun anaconda-mode-find-references ()
"Find references for thing at point."
(interactive)
(anaconda-mode-call "usages" anaconda-mode-find-references-callback))
(defun anaconda-mode-find-references-callback (result)
"Process find references RESULT."
(if result
(anaconda-mode-definitions-view result)
(message "No references found")))
;;; Eldoc.
(defun anaconda-mode-eldoc-function ()
"Show eldoc for context at point."
(anaconda-mode-call "eldoc" anaconda-mode-eldoc-callback))
(defun anaconda-mode-eldoc-callback (result)
"Display eldoc from server RESULT."
(eldoc-message (anaconda-mode-eldoc-format result)))
(defun anaconda-mode-eldoc-format (result)
"Format eldoc string from RESULT."
(when result
(let* ((name (cdr (assoc 'name result)))
(index (or (cdr (assoc 'index result)) 0))
(params (cdr (assoc 'params result)))
(doc (anaconda-mode-eldoc-format-definition name index params)))
(if anaconda-mode-eldoc-as-single-line
(substring doc 0 (min (frame-width) (length doc)))
doc))))
(defun anaconda-mode-eldoc-format-definition (name index params)
"Format function definition from NAME, INDEX and PARAMS."
(concat
(propertize name 'face 'font-lock-function-name-face)
"("
(anaconda-mode-eldoc-format-params params index)
")"))
(defun anaconda-mode-eldoc-format-params (args index)
"Build colorized ARGS string with current arg pointed to INDEX."
(->>
args
(--map-indexed
(if (= index it-index)
(propertize it 'face 'eldoc-highlight-function-argument)
it))
(-interpose ", ")
(apply 'concat)))
;;; Result view.
(defmacro anaconda-mode-with-view-buffer (&rest body)
"Create view buffer and execute BODY in it."
`(let ((buf (get-buffer-create "*Anaconda*")))
(with-current-buffer buf
(setq buffer-read-only nil)
(erase-buffer)
,@body
(goto-char (point-min))
(anaconda-mode-view-mode)
buf)))
(defun anaconda-mode-definitions-view (result)
"Show definitions view for rpc RESULT."
(if (eq 1 (length result))
(anaconda-mode-find-file (car result))
(anaconda-mode-view result 'anaconda-mode-view-definitions-presenter)))
(defun anaconda-mode-documentation-view (result)
"Show documentation view for rpc RESULT."
(anaconda-mode-view result 'anaconda-mode-view-documentation-presenter))
(defun anaconda-mode-view (result presenter)
"Show RESULT to user for future selection.
RESULT must be an RESULT field from json-rpc response.
PRESENTER is the function used to format buffer content."
(pop-to-buffer
(anaconda-mode-with-view-buffer
(funcall presenter result))))
(defun anaconda-mode-view-make-bold (string)
"Make passed STRING look bold."
(propertize string 'face 'bold))
(defun anaconda-mode-view-make-source (string)
"Make passed STRING look like python source."
(with-temp-buffer
(insert string)
(let ((delay-mode-hooks t))
(python-mode))
(run-hooks 'font-lock-mode-hook)
(font-lock-fontify-buffer)
(buffer-string)))
(define-button-type 'anaconda-mode-definition-button
'action #'anaconda-mode-view-jump
'face nil)
(defun anaconda-mode-view-jump (button)
"Jump to definition file saved in BUTTON."
(let ((definition (button-get button 'definition)))
(anaconda-mode-find-file definition)))
(defun anaconda-mode-view-jump-other-window (button)
"Jump to definition file saved in BUTTON."
(let ((definition (button-get button 'definition)))
(anaconda-mode-find-file-other-window definition)))
(defun anaconda-mode-find-file (definition)
"Find DEFINITION file, go to DEFINITION point."
(anaconda-mode-find-file-generic definition 'find-file))
(defun anaconda-mode-find-file-other-window (definition)
"Find DEFINITION file other window, go to DEFINITION point."
(anaconda-mode-find-file-generic definition 'find-file-other-window))
(defvar-local anaconda-mode-go-back-definition nil
"Previous definition from which current buffer was navigated.")
(defun anaconda-mode-find-file-generic (definition find-function)
"Find DEFINITION with FIND-FUNCTION."
(let ((backward-navigation (when (buffer-file-name)
`((module-path . ,(buffer-file-name))
(line . ,(line-number-at-pos (point)))
(column . ,(- (point) (line-beginning-position)))))))
(--if-let (cdr (assoc 'module-path definition))
(progn
(funcall find-function it)
(goto-char 0)
(forward-line (1- (cdr (assoc 'line definition))))
(forward-char (cdr (assoc 'column definition)))
(setq anaconda-mode-go-back-definition backward-navigation))
(message "Can't open %s module" (cdr (assoc 'module-name definition))))))
(defun anaconda-mode-view-insert-button (name definition)
"Insert text button with NAME opening the DEFINITION."
(insert-text-button name
'type 'anaconda-mode-definition-button
'definition definition))
(defun anaconda-mode-view-definitions-presenter (result)
"Insert definitions from RESULT."
(->>
(--group-by (cdr (assoc 'module-name it)) result)
(--map (anaconda-mode-view-insert-module-definition it))))
(defun anaconda-mode-view-insert-module-definition (module)
"Insert MODULE definition into current buffer."
(insert (concat (anaconda-mode-view-make-bold (car module)) "\n"))
(--map
(progn
(insert " ")
(anaconda-mode-view-insert-button
(anaconda-mode-view-make-source (cdr (assoc 'description it)))
it)
(insert "\n"))
(cdr module)))
(defun anaconda-mode-view-documentation-presenter (result)
"Insert documentation from RESULT."
(--map
(progn
(insert (anaconda-mode-view-make-bold (cdr (assoc 'module-name it))))
(insert "\n")
(insert (s-trim-right (cdr (assoc 'docstring it))))
(insert "\n\n"))
result))
(defvar anaconda-mode-view-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "n") 'next-error-no-select)
(define-key map (kbd "p") 'previous-error-no-select)
(define-key map (kbd "q") 'quit-window)
map))
(define-derived-mode anaconda-mode-view-mode special-mode "Anaconda-View"
"Major mode for definitions view and navigation for `anaconda-mode'.
\\{anaconda-mode-view-mode-map}"
(setq next-error-function #'anaconda-mode-next-definition))
(defun anaconda-mode-next-definition (num _reset)
"Navigate tot the next definition in the view buffer.
NUM is the number of definitions to move forward. RESET mean go
to the beginning of buffer before definitions navigation."
(forward-button num)
(anaconda-mode-view-jump-other-window (button-at (point))))
(defun anaconda-mode-go-back ()
"Jump backward if buffer was navigated from `anaconda-mode' command."
(interactive)
(if anaconda-mode-go-back-definition
(anaconda-mode-find-file anaconda-mode-go-back-definition)
(error "No previous buffer")))
;;; Anaconda minor mode.
(defvar anaconda-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-M-i") 'anaconda-mode-complete)
(define-key map (kbd "M-.") 'anaconda-mode-find-definitions)
(define-key map (kbd "M-,") 'anaconda-mode-find-assignments)
(define-key map (kbd "M-r") 'anaconda-mode-find-references)
(define-key map (kbd "M-*") 'anaconda-mode-go-back)
(define-key map (kbd "M-?") 'anaconda-mode-show-doc)
map)
"Keymap for `anaconda-mode'.")
;;;###autoload
(define-minor-mode anaconda-mode
"Code navigation, documentation lookup and completion for Python.
\\{anaconda-mode-map}"
:lighter " Anaconda"
:keymap anaconda-mode-map
(if anaconda-mode
(turn-on-anaconda-mode)
(turn-off-anaconda-mode)))
(defun turn-on-anaconda-mode ()
"Turn on `anaconda-mode'."
(make-local-variable 'eldoc-documentation-function)
(setq-local eldoc-documentation-function 'anaconda-mode-eldoc-function))
(defun turn-off-anaconda-mode ()
"Turn off `anaconda-mode'."
(kill-local-variable 'eldoc-documentation-function))
(provide 'anaconda-mode)
;;; anaconda-mode.el ends here