#!/usr/bin/env bash # -*- coding: utf-8 -*- # This script is a wrapper for reorder-python-imports that emulates # `isort --diff`, because that command is used by: # # - pyright : https://github.com/microsoft/pyright # - ms-python.python vscode extension # - coc-pyright neovim extension # # For all of these extensions, just add the following setting: # "python.sortImports.path": "isort-rpi", # # The script detects if the first argument is --diff, in which case it # generates a diff output with the import order changes. if ! command -v reorder-python-imports &>/dev/null; then echo "reorder-python-imports can't be found." 1>&2 exit 1 fi if [ "$1" = "--diff" ]; then shift filename=$1 shift printf '%s %s\n+++ %s\n' "---" "$filename" "$filename" diff -u "$filename" \ <(reorder-python-imports \ "$@" \ - <"$filename" || true) \ | tail +3 else reorder-python-imports "$@" fi