Skip to content

Commit 8ce419c

Browse files
authored
Merge pull request #6 from Wilfred/import_positions
Find correct import location when there's a module docstring
2 parents 50789ef + 9fe80a0 commit 8ce419c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

pyimport.el

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,18 @@ To terminate the loop early, throw 'break."
129129
;; Find the first non-comment non-blank line.
130130
(dotimes (_ 30)
131131
(forward-line 1)
132-
(when (and (not (looking-at "\n"))
133-
(not (looking-at "#"))
134-
(not (looking-at "\"")))
135-
(setq insert-pos (point))
136-
(throw 'found nil))))
132+
(let* ((ppss (syntax-ppss))
133+
;; Since point is at the start of the line, we
134+
;; are outside single line comments or
135+
;; strings. However, we might be in a multiline
136+
;; comment.
137+
(string-comment-p (nth 8 ppss)))
138+
(when (and (not (looking-at "\n"))
139+
(not (looking-at "#"))
140+
(not (looking-at "\""))
141+
(or (not string-comment-p) t))
142+
(setq insert-pos (point))
143+
(throw 'found nil)))))
137144
(insert line "\n"))))))
138145

139146
(defun pyimport--get-alias (import-as symbol)

test/pyimport-test.el

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@
160160
(equal (buffer-string)
161161
"from foo import x\n"))))
162162

163+
(ert-deftest pyimport-insert-import-module-docstring ()
164+
"Test inserting an import when the module starts with a docstring."
165+
(with-temp-buffer
166+
(insert "\"\"\"hello world.\n\n\"\"\"\n\nfrom bar import y")
167+
(pyimport--insert-import "from foo import x")
168+
(should
169+
(equal (buffer-string)
170+
"\"\"\"hello world.\n\n\"\"\"\n\nfrom foo import x\nfrom bar import y"))))
171+
163172
(ert-deftest pyimport-extract-simple-import ()
164173
(should
165174
(equal

0 commit comments

Comments
 (0)