summarylogtreecommitdiffstats
path: root/isort-rpi
blob: 03c64d21a7591b5e8dfc4e2b58573905a8734690 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/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