Skip to content

Commit 7e177be

Browse files
committed
Adding option for formatting, black or ruff
Set the variable ob-python-extras-formatter to either "ruff" or "black".
1 parent 5b1a652 commit 7e177be

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

ob-python-extras.el

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,9 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
517517
nil
518518
(buffer-string))))
519519
(async-start-process "ob-python-extras-format-process"
520-
"uvx"
520+
"ruff"
521521
(apply-partially 'ob-python-extras-format-callback created-temp-file (current-buffer))
522-
"ruff" "format" created-temp-file))))
522+
"format" created-temp-file))))
523523

524524
(defun ob-python-extras-format-buffer-callback (temp-file-name src-edit-buffer process-obj)
525525
(with-current-buffer src-edit-buffer
@@ -533,6 +533,15 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
533533
(insert-file-contents temp-file-name nil nil nil t))
534534
(message "Formatted org-src-block"))
535535

536+
(defun ob-python-extras-format ()
537+
(interactive)
538+
;; check first that the var is set
539+
(if (boundp 'ob-python-extras-formatter)
540+
(cond ((equal ob-python-extras-formatter "black") (ob-python-extras-format-black))
541+
((equal ob-python-extras-formatter "ruff") (ob-python-extras-format-ruff))
542+
(t (message "No formatter %s found" ob-python-extras-formatter)))
543+
(message "No formatter set in ob-python-extras-formatter")))
544+
536545
;; TODO check to make sure that it's a python source block
537546
(defun ob-python-extras-format-black ()
538547
(interactive)
@@ -565,17 +574,36 @@ In regular org-mode, tries to view image or executes normal C-c C-c."
565574
created-temp-file))
566575
(message "Nothing to format"))))
567576

568-
;; start by using (org-src--contents-area (org-element-at-point))
569-
;; here's the trick for replacing the contents.
570-
;; (if (version< emacs-version "27.1")
571-
;; (progn (delete-region beg end)
572-
;; (insert (with-current-buffer write-back-buf
573-
;; (buffer-string))))
574-
;; (save-restriction
575-
;; (narrow-to-region beg end)
576-
;; (org-replace-buffer-contents write-back-buf 0.1 nil)
577-
;; (goto-char (point-max))))
578-
;; (when (and expecting-bol (not (bolp))) (insert "\n")))))
577+
(defun ob-python-extras-format-ruff ()
578+
(interactive)
579+
(if (org-src-edit-buffer-p)
580+
(let ((created-temp-file
581+
(make-temp-file
582+
(concat (file-name-as-directory (org-babel-temp-directory)) "ob-python-extras-format-ruff-")
583+
nil
584+
nil
585+
(buffer-string))))
586+
(async-start-process "ob-python-extras-format-process"
587+
"ruff"
588+
(apply-partially 'ob-python-extras-format-buffer-callback created-temp-file (current-buffer))
589+
"format" created-temp-file))
590+
;; note that we place this in the else clause
591+
;; because org-in-src-block-p does not like running in org-src-edit-buffers
592+
(if (org-in-src-block-p t)
593+
(let* ((created-temp-file
594+
(make-temp-file
595+
(concat (file-name-as-directory (org-babel-temp-directory)) "ob-python-extras-format-ruff-")
596+
nil
597+
nil
598+
(org-element-property :value (org-element-at-point))))
599+
(content-area (org-src--contents-area (org-element-at-point)))
600+
(beg (car content-area))
601+
(end (cadr content-area)))
602+
(async-start-process "ob-python-extras-format-process"
603+
"ruff"
604+
(apply-partially 'ob-python-extras-format-src-block-callback created-temp-file (current-buffer) beg end)
605+
"format" created-temp-file))
606+
(message "Nothing to format"))))
579607

580608
;;; Load other packages
581609

0 commit comments

Comments
 (0)