-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycomplete.el
204 lines (158 loc) · 6.27 KB
/
pycomplete.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
;;; Complete symbols at point using Pymacs.
;;; See pycomplete.py for the Python side of things and a short description
;;; of what to expect.
(require 'pymacs)
(require 'python-mode)
(pymacs-load "pycomplete")
(defconst py-identifier
"[A-Za-z_][A-Za-z_0-9]*"
"Regular expression matching a python identifier.")
;;; regular expressions regarding import statetment
;;; based on Python Grammar
(defconst py-dotted-name-re
(concat py-identifier "\\([.]" py-identifier "\\)*")
"Regular expression matching a dotted_name production.")
(defconst py-dotted-as-name-re
(concat py-dotted-name-re "\\(\\s +as\\s +" py-identifier "\\)*")
"Regular expression matching a dotted_as_name production.")
(defconst py-dotted-as-names-re
(concat py-dotted-as-name-re
"\\(\\s *,\\s *" py-dotted-as-name-re "\\)*")
"Regular expression matching a dotted_as_names production.")
(defconst py-import-as-name-re
(concat py-identifier "\\(\\s +as\\s +" py-identifier "\\)*" )
"Regular expression matching a import_as_name production.")
(defconst py-import-as-names-re
(concat py-import-as-name-re "\\(\\s *,\\s *" py-import-as-name-re "\\)*"
"\\s *[,]?" )
"Regular expression matching a import_as_names production.")
(defconst py-import-name-re
(concat "^\\s *\\<import\\>\\s +" py-dotted-as-names-re)
"Regular expression matching a import_name production.")
(defconst py-import-from-re
(concat "^\\s *\\<from\\>\\s +" "\\([.]*" py-dotted-name-re "\\|[.]+\\)\\s +"
"\\<import\\>\\s +" "\\([*]\\|(\\s *" py-import-as-names-re "[^)]*)"
"\\|" py-import-as-names-re "\\)")
"Regular expression matching a import_from production.")
(defconst py-imports-re
(concat "\\("
(mapconcat 'identity
(list py-import-name-re
py-import-from-re)
"\\|")
"\\)")
"Regular expression matching imports.")
(defun blank-linep ()
"check if current line is empty (only whitespaces and comments)"
(save-excursion
(beginning-of-line)
(looking-at py-blank-or-comment-re)))
(defun char-before-blank ()
"check if prev character is blank-type"
(save-excursion
(forward-char -1)
(looking-at "[\n\t\r]")))
(defun py-complete ()
"show possible completions for current statement"
(interactive)
(let ((pymacs-forget-mutability t))
(if (and (eolp) (not (bolp))
(not (char-before-blank))
(not (blank-linep)))
(insert (pycomplete-pycomplete
(py-symbol-near-point)
(buffer-file-name)
(py-find-global-imports)))
(indent-for-tab-command))))
(defun py-find-global-imports ()
"find global import statements"
(save-excursion
(let ((imports nil))
(goto-char (point-min))
(while (< (point) (point-max))
(if (looking-at py-imports-re)
;; import statement found
(progn
(setq imports
(append imports (list (buffer-substring
(match-beginning 0)
(match-end 0)))))
(forward-line 1)
;; handle continuation backslashes
(while (and (py-backslash-continuation-line-p) (not (eobp)))
(goto-char (line-beginning-position))
(skip-chars-forward " \t")
(setq begin (point))
(goto-char (line-end-position))
(skip-chars-backward " \t\\")
(if (= (char-before) ?\\)
(setq end (- (point) 1))
(setq end (point)))
(setcar (last imports)
(concat (car (last imports)) " "
(buffer-substring begin end)))
(forward-line 1)))
(forward-line)))
imports)))
(defun py-complete-python-dotexpr-begin nil
(re-search-backward "[^a-zA-Z_0-9\\.]")
(forward-char))
(defun py-complete-python-dotexpr-end nil
(re-search-forward "[a-zA-Z_0-9\\.]*"))
(put 'python-dotexpr 'beginning-op 'py-complete-python-dotexpr-begin)
(put 'python-dotexpr 'end-op 'py-complete-python-dotexpr-end)
(defun py-complete-show (string)
(display-message-or-buffer string "*PythonHelp*"))
(defun py-complete-help (string)
"get help on a python expression"
(interactive "sHelp: ")
(let ((help-string
(pycomplete-pyhelp string (py-find-global-imports))))
(if (and help-string (> (length help-string) 300))
(with-output-to-temp-buffer "*Python Help*"
(print help-string))
(py-complete-show help-string))))
(defun py-complete-help-thing-at-point nil
(interactive)
(require 'thingatpt)
(let ((sym (thing-at-point 'python-dotexpr)))
(if sym
(py-complete-help sym))))
(set 'py-complete-current-signature nil)
(defun py-complete-signature (function)
"get signature of a python function or method"
(set 'py-complete-current-signature
(pycomplete-pysignature function)))
(defun py-complete-signature-show nil
(require 'thingatpt)
(let ((sym (thing-at-point 'python-dotexpr)))
(if sym
(progn
(py-complete-show (py-complete-signature sym))))))
(defun py-complete-signature-expr nil
(interactive)
(require 'thingatpt)
(let ((dotexpr (read-string "signature on: "
(thing-at-point 'python-dotexpr))))
(if dotexpr
(py-complete-show
(py-complete-signature dotexpr)))))
(defun py-complete-electric-lparen nil
"electricly insert '(', and try to get a signature for the stuff to the left"
(interactive)
(py-complete-signature-show)
(self-insert-command 1))
(defun py-complete-electric-comma nil
"electricly insert ',', and redisplay latest signature"
(interactive)
(self-insert-command 1)
(if py-complete-current-signature
(py-complete-show (format "%s" py-complete-current-signature))))
(define-key py-mode-map "\M-\C-i" 'py-complete)
(define-key py-mode-map "\t" 'py-complete)
(define-key py-mode-map [f1] 'py-complete-help-thing-at-point)
(define-key py-mode-map "(" 'py-complete-electric-lparen)
(define-key py-mode-map "," 'py-complete-electric-comma)
(define-key py-mode-map [f2] 'py-complete-signature-expr)
(define-key py-mode-map [f3] 'py-complete-help)
(provide 'pycomplete)