-
Notifications
You must be signed in to change notification settings - Fork 13
/
company-anaconda.el
154 lines (132 loc) · 5.84 KB
/
company-anaconda.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
;;; company-anaconda.el --- Anaconda backend for company-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2013-2018 by Artem Malyshev
;; Author: Artem Malyshev <proofit404@gmail.com>
;; URL: https://github.com/proofit404/anaconda-mode
;; Version: 0.2.0
;; Package-Requires: ((emacs "25.1") (company "0.8.0") (anaconda-mode "0.1.1") (cl-lib "0.5.0") (dash "2.6.0") (s "1.9"))
;; Keywords: convenience company anaconda
;; 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:
(require 'anaconda-mode)
(require 'company)
(require 'python)
(require 'cl-lib)
(require 'rx)
(require 'dash)
(require 's)
(defgroup company-anaconda nil
"Company back-end for Python code completion."
:group 'programming)
(defcustom company-anaconda-annotation-function
'company-anaconda-annotation
"Function that returns candidate annotations."
:group 'company-anaconda
:type 'function)
(defcustom company-anaconda-case-insensitive t
"Use case insensitive candidates match."
:group 'company-anaconda
:type 'boolean)
(defun company-anaconda-at-the-end-of-identifier ()
"Check if the cursor at the end of completable identifier."
(let ((limit (line-beginning-position)))
(or
;; We can't determine at this point if we can complete on a space
(looking-back " " limit)
;; At the end of the symbol, but not the end of int number
(and (looking-at "\\_>")
(not (looking-back "\\_<\\(0[bo]\\)?[[:digit:]]+" limit))
(not (looking-back "\\_<0x[[:xdigit:]]+" limit)))
;; After the dot, but not when it's a dot after int number
;; Although identifiers like "foo1.", "foo111.", or "foo1baz2." are ok
(and (looking-back "\\." (- (point) 1))
(not (looking-back "\\_<[[:digit:]]+\\." limit)))
;; After dot in float constant like "1.1." or ".1."
(or (looking-back "\\_<[[:digit:]]+\\.[[:digit:]]+\\." limit)
(looking-back "\\.[[:digit:]]+\\." limit)))))
(defun company-anaconda-prefix ()
"Grab prefix at point."
(and anaconda-mode
(not (company-in-string-or-comment))
(company-anaconda-at-the-end-of-identifier)
(let* ((line-start (line-beginning-position))
(start
(save-excursion
(if (not (re-search-backward
(python-rx
(or whitespace open-paren close-paren string-delimiter))
line-start
t 1))
line-start
(forward-char (length (match-string-no-properties 0)))
(point))))
(symbol (buffer-substring-no-properties start (point))))
(if (or (s-ends-with-p "." symbol)
(string-match-p
(rx (* space) word-start (or "from" "import") word-end space)
(buffer-substring-no-properties line-start (point))))
(cons symbol t)
(if (s-blank-p symbol)
'stop
symbol)))))
(defun company-anaconda-candidates (callback given-prefix)
"Pass candidates list for GIVEN-PREFIX to the CALLBACK asynchronously."
(anaconda-mode-call
"company_complete"
(lambda (result)
(funcall callback
(--map
(let ((candidate (s-concat given-prefix (aref it 0))))
(put-text-property 0 1 'struct it candidate)
candidate)
result)))))
(defun company-anaconda-annotation (candidate)
"Return the description property of CANDIDATE inside chevrons."
(--when-let (aref (get-text-property 0 'struct candidate) 1)
(concat "<" it ">")))
(defun company-anaconda-doc-buffer (candidate)
"Return documentation buffer for chosen CANDIDATE."
(let ((docstring (aref (get-text-property 0 'struct candidate) 2)))
(unless (s-blank? docstring)
(anaconda-mode-documentation-view (vector (vector "" docstring))))))
(defun company-anaconda-meta (candidate)
"Return short documentation string for chosen CANDIDATE."
(let ((docstring (aref (get-text-property 0 'struct candidate) 2)))
(unless (s-blank? docstring)
(car (s-split-up-to "\n" docstring 1)))))
(defun company-anaconda-location (candidate)
"Return location (path . line) for chosen CANDIDATE."
(-when-let* ((struct (get-text-property 0 'struct candidate))
(module-path (pythonic-emacs-readable-file-name (aref struct 3)))
(line (aref struct 4)))
(cons module-path line)))
;;;###autoload
(defun company-anaconda (command &optional arg &rest _args)
"Anaconda backend for company-mode.
See `company-backends' for more info about COMMAND and ARG."
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-anaconda))
(prefix (company-anaconda-prefix))
(candidates (cons :async
(let ((given-prefix (s-chop-suffix (company-grab-symbol) arg)))
(lambda (callback)
(company-anaconda-candidates callback given-prefix)))))
(doc-buffer (company-anaconda-doc-buffer arg))
(meta (company-anaconda-meta arg))
(annotation (funcall company-anaconda-annotation-function arg))
(location (company-anaconda-location arg))
(ignore-case company-anaconda-case-insensitive)
(sorted t)))
(provide 'company-anaconda)
;;; company-anaconda.el ends here