File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 6
6
unsaved or not visiting a file.
7
7
* ` pyimport-insert-missing ` now steps over shebangs when choosing
8
8
where to insert new imports.
9
+ * Fixed an issue with ` pyimport-insert-missing ` using the contents of
10
+ multiline strings that looked like imports.
9
11
10
12
## v1.0
11
13
Original file line number Diff line number Diff line change 46
46
" Return non-nil if the current line is the last in the buffer."
47
47
(looking-at (rx (0+ not-newline) buffer-end)))
48
48
49
+ (defun pyimport--in-string-p ()
50
+ " Return non-nil if point is inside a string."
51
+ (nth 3 (syntax-ppss )))
52
+
49
53
(defun pyimport--buffer-lines (buffer )
50
- (with-current-buffer buffer
51
- (s-split " \n " (buffer-string ))))
54
+ " Return all the lines in BUFFER, ignoring lines that are within a string."
55
+ (let (lines)
56
+ (with-current-buffer buffer
57
+ (save-excursion
58
+ (goto-char (point-min ))
59
+ (unless (pyimport--in-string-p)
60
+ (push (pyimport--current-line) lines))
61
+ (while (zerop (forward-line 1 ))
62
+ (unless (pyimport--in-string-p)
63
+ (push (pyimport--current-line) lines)))))
64
+ (nreverse lines)))
52
65
53
66
(defun pyimport--import-lines (buffer )
54
67
" Return all the lines in this Python BUFFER that look like imports."
Original file line number Diff line number Diff line change 185
185
(equal
186
186
(pyimport--extract-simple-import " import foo as bar" " bar" )
187
187
" import foo as bar" )))
188
+
189
+ (ert-deftest pyimport-buffer-lines ()
190
+ " We should ignore multiline strings, as they may not contain valid imports."
191
+ (let (lines)
192
+ (with-temp-buffer
193
+ (insert " x = \"\"\"\n from foo\n\"\"\"\n " )
194
+ (setq lines (pyimport--buffer-lines (current-buffer ))))
195
+ (should (not
196
+ (-contains-p lines " from foo" )))))
You can’t perform that action at this time.
0 commit comments