Skip to content

Instantly share code, notes, and snippets.

@toudi
Created November 22, 2022 14:26
Show Gist options
  • Save toudi/888a2c2112a31d6da689399a44b8e66d to your computer and use it in GitHub Desktop.
Save toudi/888a2c2112a31d6da689399a44b8e66d to your computer and use it in GitHub Desktop.
run black and isort inside sublime project
# 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