Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code formatting with black or ruff #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

nilaykumar
Copy link
Contributor

@nilaykumar nilaykumar commented Jan 1, 2025

Use M-x ob-python-extras-format to format code blocks (supports ruff and black).

  • DONE format src-edit buffers
  • DONE format src-blocks outside of src-edit buffers
  • DONE format using black or ruff, optionally via
(use-package! ob-python-extras
  :config
  (setq ob-python-extras-formatter "ruff"))
  • DONE do nothing if code block is not lang python
  • DONE learn some emacs-lisp to consolidate the ruff and black functions (ruff takes "format" as the first argument whereas black takes no arguments)
  • DONE formatter can be set to run on code block execution by modifying the appropriate hook (add-hook 'org-babel-after-execute-hook 'ob-python-extras-format)
  • DONE ensure that formatters are using their configs (e.g. checking pyproject.toml/ruff.toml/etc. if necessary)
  • DONE remove "Wrote /tmp/babel-RRecW4/ob-python-extras-formatOBqumF" messages every time format is run
  • DONE add documentation to the README

@nilaykumar nilaykumar force-pushed the feat/org-code-formatting branch from 7e177be to 38752e1 Compare January 1, 2025 20:29
@nilaykumar nilaykumar marked this pull request as ready for review January 11, 2025 05:16
@nilaykumar nilaykumar force-pushed the feat/org-code-formatting branch from 37a45f9 to dd5dca1 Compare January 11, 2025 05:23
@ElleNajt
Copy link
Owner

Thanks for the pull request and the work on this project! :)

Here are my thoughts:

  1. My main concern right now is that some of these functions are replicating functionality of existing emacs packages, and I think it would probably be better to rely on them.

For instance, to format a buffer with black, there's already the (python-black-buffer)
function from the 'python-black package, which gets installed in doom with :editor format and python enabled in init.el.

We could use it like this:

(require 'python-black)

(defun ob-python-extras--format-src-block ()
  "Format the current org babel Python source block using python-black.
Creates a temporary buffer, sets python-mode, applies formatting, and copies back."
  (interactive)
  (let* ((element (org-element-at-point))
         (language (org-element-property :language element))
         (orig-code (org-element-property :value element)))
    (when (string= language "python")
      (let ((formatted-code
             (with-temp-buffer
               (insert orig-code)
               (python-mode)
               (python-black-buffer)
               (buffer-string))))
        (save-excursion
          (goto-char (org-element-property :begin element))
          (org-babel-update-block-body formatted-code))))))

(add-hook 'org-babel-after-execute-hook 'ob-python-extras--format-src-block)

For ruff -- I haven't tried this package, but perhaps this: https://github.com/JoshHayes/emacs-ruff-format/blob/main/ruff-format.el ?

Both python-black and ruff-format rely on https://github.com/purcell/emacs-reformatter/blob/master/reformatter.el

  1. In terms of configuration, my inclination would be to default to the chosen formatter for python buffers.

What do you think about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants