only send top level functions

... and don't get confused by functions within functions
This commit is contained in:
Valentin Boettcher 2025-01-22 12:57:10 -05:00
parent 6b39ac33fc
commit 461b8c423b
No known key found for this signature in database
GPG key ID: E034E12B7AF56ACE

View file

@ -584,14 +584,6 @@ point, the cell is assumed to end with the buffer."
If the function has no arguments, the function call is run immediately." If the function has no arguments, the function call is run immediately."
(interactive) (interactive)
(save-mark-and-excursion (save-mark-and-excursion
;; (let ((last-mark (mark)))
;; (python-mark-defun)
;; (while (not (eq (mark) last-mark))
;; (setf last-mark (mark))
;; (python-mark-defun 1)))
;; (mark (region-beginning))
;; (next-line)
;; (cl-return)
(let ((begin (save-mark-and-excursion (let ((begin (save-mark-and-excursion
(python-nav-beginning-of-defun 1) (python-nav-beginning-of-defun 1)
(point))) (point)))
@ -599,34 +591,40 @@ If the function has no arguments, the function call is run immediately."
(python-nav-end-of-defun) (python-nav-end-of-defun)
(point)))) (point))))
(python-nav-beginning-of-defun 1) (python-nav-beginning-of-defun 1)
(next-line) (beginning-of-line)
(let ((function-name-regex "def[ \t]+\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*(\\(.*\\))\\([ \t]*->[ \t]*.*?\\)?:")) (if (looking-at "[[:blank:]]+.*")
(save-match-data (progn
(let ((name-found (re-search-backward function-name-regex nil nil))) (previous-line)
(if name-found (py-vterm-interaction-run-current-function))
(progn (progn
(let ((name (match-string 1)) (next-line)
(args (match-string 2)) (let ((function-name-regex "def[ \t]+\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*(\\(.*\\))\\([ \t]*->[ \t]*.*?\\)?:"))
(py-vterm-interaction-paste-with-return nil)) (save-match-data
(let ((name-found (re-search-backward function-name-regex nil nil)))
(if name-found
(progn
(let ((name (match-string 1))
(args (match-string 2))
(py-vterm-interaction-paste-with-return nil))
;; capture decorators too ;; capture decorators too
(save-excursion (save-excursion
(while (while
(progn (forward-line -1) (progn (forward-line -1)
(beginning-of-line) (beginning-of-line)
(if (looking-at "@") (if (looking-at "@")
(setq begin (point)) (setq begin (point))
nil)))) nil))))
(let ((func (buffer-substring-no-properties begin end))) (let ((func (buffer-substring-no-properties begin end)))
(py-vterm-interaction--send-maybe-silent func name) (py-vterm-interaction--send-maybe-silent func name)
(py-vterm-interaction-send-return-key))
(if (= 0 (length args))
(progn
(py-vterm-interaction-paste-string (format "%s() " name))
(py-vterm-interaction-send-return-key)) (py-vterm-interaction-send-return-key))
(py-vterm-interaction-paste-string (format "%s(" name))))) (if (= 0 (length args))
(message "No function found")))))))) (progn
(py-vterm-interaction-paste-string (format "%s() " name))
(py-vterm-interaction-send-return-key))
(py-vterm-interaction-paste-string (format "%s(" name)))))
(message "No function found"))))))))))
(defun py-vterm-interaction-send-buffer () (defun py-vterm-interaction-send-buffer ()
"Send the whole content of the script buffer to the Python REPL line by line." "Send the whole content of the script buffer to the Python REPL line by line."