-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-imports.el
281 lines (250 loc) · 9.87 KB
/
py-imports.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
(require 'etags)
(defvar py-import-interactive-select-tag nil)
(defvar py-import-show-import-location nil)
(defvar py-import-predefined-imports
'(("Interface" "zope/interface")
("verifyObject" "zope/interface/verify")
("zapi" "zope/app")
("adapts" "zope/component")
("implements" "zope/interface")
("traverse" "zope/app/traversing/api")
("NotFound" "zope/publisher/interfaces")
("TestRequest" "zope/publisher/browser")
("removeSecurityProxy" "zope.security.proxy")
("Attribute" "zope.interface")
("ztapi" "zope.app.tests")
("queryMultiAdapter" "zope.component")
("Field" "zope.schema")
("provideSubscriptionAdapter" "zope.component")
("URIInstruction" "schooltool.app.relationships")
("URISection" "schooltool.app.relationships")
("URIMembership" "schooltool.app.membership")
("URIMember" "schooltool.app.membership")
("implementer" "zope.interface")
("Iterable" "zope.schema")
("provideAdapter" "zope.component" "provideAdapter(factory, adapts=None, provides=None, name='')")
("classImplements" "zope.interface")
("directlyProvides" "zope.interface")
("OOBTree" "BTrees.OOBTree")
("directlyProvidedBy" "zope.interface")
("absoluteURL" "zope.traversing.browser")
("getUtility" "zope.component")))
(defvar py-import-path-postprocessor 'remove-src)
(defun hacky-replace (a b)
(flet ((message (&rest ignore)))
(beginning-of-buffer)
(while (search-forward a nil t)
(replace-match b nil t))))
(defun hacky-regexp-replace (a b)
(flet ((message (&rest ignore)))
(beginning-of-buffer)
(while (search-forward-regexp a nil t)
(replace-match b nil nil))))
(defun remove-syspath (path)
(with-temp-buffer
(insert path)
(hacky-replace ".py$" "")
;;(insert (substring path 1 -3)) ;; remove the .py suffix
(loop for path in py-import-path-postprocessor
do (beginning-of-line)
do (hacky-replace (expand-file-name path) ""))
(buffer-string)))
(defun get-non-module-path (path)
(message (expand-file-name path))
(let* ((fn path)
(dir (directory-file-name (file-name-directory fn))))
(while (file-exists-p (expand-file-name "__init__.py" dir))
(setf dir (directory-file-name (file-name-directory dir))))
dir))
(defun remove-src (path)
(with-temp-buffer
(insert path)
(hacky-replace ".py" "")
(beginning-of-line)
(hacky-replace (concat (get-non-module-path path) "/") "")
(beginning-of-line)
(hacky-regexp-replace ".*\.egg/" "")
(buffer-string)))
(defun process-import (import-file tag)
(with-temp-buffer
(beginning-of-line)
(if (equal (string-match tag import-file) 0)
(insert (format "%s" import-file))
(insert (format "from %s import %s"
(funcall py-import-path-postprocessor import-file)
tag)))
(beginning-of-line)
(hacky-replace "/" ".")
(beginning-of-line)
(hacky-replace ".__init__" "")
(hacky-replace "RttaxProduct" "Products.RttaxProduct")
(hacky-replace "._bootstrapfields" "") ;; workaround zope.schema._bootstrapfields
(hacky-replace "._api" "") ;; workaround zope.component._api
(buffer-string)))
(defun process-import-zope (import-file tag)
(with-temp-buffer
(beginning-of-line)
(if (equal (string-match tag import-file) 0)
(insert (format "%s" import-file))
(insert (format "import %s"
(funcall py-import-path-postprocessor import-file)
tag)))
(beginning-of-line)
(hacky-replace "/" ".")
(beginning-of-line)
(hacky-replace ".__init__" "")
(hacky-replace "._bootstrapfields" "") ;; workaround zope.schema._bootstrapfields
(hacky-replace "._api" "") ;; workaround zope.component._api
(buffer-string)))
(defvar py-import-processor 'process-import)
(defun process-import-in-place (import-file tag)
(with-temp-buffer
(beginning-of-line)
(if (equal (string-match tag import-file) 0)
(insert (format "%s" import-file))
(insert (format "%s.%s"
(funcall py-import-path-postprocessor import-file)
tag)))
(beginning-of-line)
(hacky-replace "/" ".")
(beginning-of-line)
(hacky-replace ".__init__" "")
(hacky-replace "._bootstrapfields" "") ;; workaround zope.schema._bootstrapfields
(hacky-replace "._api" "") ;; workaround zope.component._api
(buffer-string)))
(defun lookup-predefined-import (pattern)
(loop for import in py-import-predefined-imports
when (equal (car import)
pattern)
return (cdr import)))
(defun find-all-locations-of-tag-vtags (pattern)
(or (lookup-predefined-import pattern)
(let ((tfi (find-tags-table-heuristically))
(files))
(save-excursion
(let ((dir (file-name-directory (tagfileinfo-file tfi))))
(vtags-look pattern tfi
(lambda (line count)
(when (equal (tagEntryInfo-name (vtags-parse-line line))
pattern)
(push (expand-file-name (tagEntryInfo-file (vtags-parse-line line)) dir)
files))))))
files)))
(defun find-all-locations-of-tag-etags (pattern)
(or (lookup-predefined-import pattern)
(progn
(visit-tags-table-buffer)
(save-excursion
(let ((search-forward-func find-tag-search-function)
file ;name of file containing tag
tag-info ;where to find the tag in FILE
(case-fold-search (if (memq tags-case-fold-search '(nil t))
tags-case-fold-search
case-fold-search))
(pos-list nil))
(visit-tags-table-buffer)
(goto-char (point-min))
(loop as pos = (funcall search-forward-func pattern nil t)
while (and pos
(not (find pos pos-list)))
do (push pos pos-list)
;; Naive match found. Qualify the match.
when (tag-exact-match-p pattern)
;; Make sure it is not a previous qualified match.
;;tag-info (funcall snarf-tag-function)
collect (progn
(save-excursion
(beginning-of-line)
(expand-file-name
(file-of-tag))))))))))
(defvar py-import-find-all-locations-of-tag 'find-all-locations-of-tag-etags)
(defun find-all-imports (tag)
(interactive)
(save-excursion
(let ((table (remove-duplicates
(loop for import in (funcall py-import-find-all-locations-of-tag tag)
collect (funcall py-import-processor import tag))
:test 'equal)))
(if table
(if (= (length table) 1)
(car table)
(completing-read "Select-import:"
table
nil
t
"from "))
(error "Suitable import was not found!")))))
(defun find-all-imports-to-expand (tag)
(interactive)
(save-excursion
(let ((table (remove-duplicates
(loop for import in (funcall py-import-find-all-locations-of-tag tag)
collect (process-import-in-place import tag))
:test 'equal)))
(if table
(if (= (length table) 1)
(car table)
(completing-read "Select-import:"
table
nil
t
""))
(error "Suitable import was not found!")))))
(defun select-tag ()
(if py-import-interactive-select-tag
(find-tag-interactive "Import tag: ")
(list (find-tag-default))))
;;(global-set-key [f5] 'select-tag-to-import-at-top)
(defun select-tag-to-import-at-top (tag)
(interactive (select-tag))
(insert-import-at-top (find-all-imports tag)))
;;(global-set-key [S-f5] 'select-tag-to-import-in-place)
(defun select-tag-to-import-in-place (tag)
(interactive (select-tag))
(insert-import (find-all-imports tag)))
;;(global-set-key [C-f5] 'select-tag-to-expand)
(defun select-tag-to-expand (tag)
(interactive (select-tag))
(expand-import-in-place (find-all-imports-to-expand tag)))
(defun expand-import-in-place (import-string)
(flet ((message (&rest ignore)))
(forward-char)
(backward-word)
(kill-word 1)
(insert import-string)))
(defun insert-import (import-string)
(flet ((message (&rest ignore)))
(beginning-of-line)
(insert import-string "\n")
(previous-line)
(indent-for-tab-command)))
(defun insert-import-at-top (import-string)
(save-excursion
(save-restriction
(beginning-of-buffer)
(search-forward-regexp "^\"\"\"" nil t)
(search-forward-regexp "^\"\"\"" nil t)
(forward-line 1)
(set-mark (point))
(while (not (or (eobp)
(not (looking-at "^$\\|^#.*$\\|^.*import.*"))))
(forward-line 1))
(narrow-to-region (mark) (point))
(beginning-of-buffer)
(let ((max 0)
(point (point)))
(while (not (eobp))
(let ((matching-chars (compare-strings import-string nil nil
(buffer-substring (line-beginning-position)
(line-end-position)) nil nil)))
(when (equal matching-chars t)
(setf matching-chars 9999))
(when (> (abs matching-chars) max)
(setf max (abs matching-chars))
(setf point (point))))
(forward-line 1))
(unless (= max 9999)
(goto-char point)
(insert import-string)
(insert "\n"))))))
(provide 'py-imports)