Thanks to LazyOne advice, I wrote this small shell script to run either black
or sqlfluff
depending on the file extension:
#!/bin/sh
file_path="$1"
file_name=$(basename -- "$file_path")
extension="${file_name##*.}"
echo "Formatting file $file_path"
echo "With extension $extension"
if [ "$extension" = "py" ]; then
/Users/alexandre/.local/bin/black "$file_path"
elif [ "$extension" = "sql" ]; then
/Users/alexandre/.local/bin/sqlfluff fix -f --dialect postgres "$file_path"
else
exit 2
fi
Make sure to change the paths to your actual installation paths (outputs of which black
and which sqlfluff
).
You can save the script in a file (e.g. code_formatter.sh
), and make it executable:
chmod +x code_formatter.sh
Then configure it as an external tool in Pycharm settings, and don't forget the "$FilePath$"
argument:

You can now define a single keyboard shortcut for the "my code formatter" external tool, that should work on both sql and python files.