Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Created June 29, 2024 07:23
Show Gist options
  • Save tam17aki/25a5d16a866a19e03a5b82409c45ad50 to your computer and use it in GitHub Desktop.
Save tam17aki/25a5d16a866a19e03a5b82409c45ad50 to your computer and use it in GitHub Desktop.
Sort the order of imports in Python source code by using Ruff.
;;; ruff-sort-imports.el --- Sort import libraries of Python source via Ruff -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Akira Tamamori
;; Copyright (C) 2023 Ville Skyttä
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Sort the order of imports in Python source code by using Ruff.
;;; Code:
(require 'reformatter)
(defcustom ruff-sort-command "ruff"
"Ruff command to use for sorting"
:type 'string
:group 'ruff-sort-imports)
;;;###autoload (autoload 'ruff-sort-imports-buffer "ruff-sort-imports" nil t)
;;;###autoload (autoload 'ruff-sort-imports-region "ruff-sort-imports" nil t)
;;;###autoload (autoload 'ruff-sort-imports-on-save-mode "ruff-sort-imports" nil t)
(reformatter-define ruff-sort-imports
:program ruff-sort-command
:args '("--fix" "--select" "I001" "-")
:group 'ruff-sort-imports)
(provide 'ruff-sort-imports)
;;; ruff-sort-imports.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment