Created
November 22, 2022 14:26
-
-
Save toudi/888a2c2112a31d6da689399a44b8e66d to your computer and use it in GitHub Desktop.
run black and isort inside sublime project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I've installed LSP and LSP-pyright but I could not make it to format code and sort imports | |
# despite the fact that it works perfectly for golang | |
# out of pure frustration I created this little plugin: | |
from os.path import exists | |
from pathlib import Path | |
from subprocess import call | |
import sublime | |
import sublime_plugin | |
class ReformatSourceCodeCommand(sublime_plugin.EventListener): | |
def on_pre_save_async(self, view: sublime.View): | |
filename = view.file_name() | |
variables = view.window().extract_variables() | |
if variables["file_extension"] != "py": | |
return | |
project_path = Path(variables["project_path"]) | |
tools = [ | |
project_path / ".env" / "bin" / "isort", | |
project_path / ".env" / "bin" / "black", | |
] | |
for tool in tools: | |
if not exists(tool): | |
# print(f"{tool} not found; skipping") | |
continue | |
# print(f"calling {tool} {filename}") | |
call([tool, filename]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment