-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruff.py
100 lines (83 loc) · 2.95 KB
/
ruff.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from __future__ import annotations
import threading
__version__ = "0.2.0"
__version_info__ = tuple([int(num) for num in __version__.split(".")])
import os
import subprocess as sp
import typing
try:
import ruff
except ImportError:
try:
sp.run(["pip", "install", "ruff"], check=True)
sp.run(["pip", "install", "python-lsp-ruff"], check=True)
import ruff
except Exception as e:
ruff = None
if typing.TYPE_CHECKING:
from biscuit.api import ExtensionsAPI
class Ruff:
def __init__(self, api: ExtensionsAPI) -> None:
self.api = api
self.base = api.base
def install(self) -> None:
if not ruff:
return self.api.notifications.info(
"ruff pip package is not installed. Install it to use this extension.",
actions=[
(
"Install",
lambda: self.api.terminalmanager.run_command(
"pip install ruff python-lsp-ruff"
),
),
],
)
self.api.commands.register_command("Ruff: Format active editor", self.format)
self.api.commands.register_command("Ruff: Check active file", self.check_file)
self.api.commands.register_command(
"Ruff: Check all files", self.check_all_files
)
def format(self, *_) -> str:
editor = self.api.editorsmanager.active_editor
if not (editor and editor.content and editor.content.editable):
return
text = editor.content.text
if not text:
return
try:
sp.check_output(["ruff", "format", editor.content.path])
text.load_file()
except sp.CalledProcessError as e:
self.api.notifications.error(f"Ruff: Failed to format file")
self.api.logger.error(e)
return
def check_file(self, *_):
if not self.base.editorsmanager.active_editor:
return
path = os.path.abspath(self.base.editorsmanager.active_editor.path)
try:
output = sp.run(
f"ruff check {path}",
shell=True,
capture_output=True,
text=True,
)
self.base.problems.write(output.stdout)
except sp.CalledProcessError as e:
self.base.problems.clear()
def check_all_files(self, *_):
threading.Thread(target=self.threaded_check_all_files, daemon=True).start()
def threaded_check_all_files(self):
try:
output = sp.run(
f"ruff check {self.base.active_directory}",
shell=True,
capture_output=True,
text=True,
)
self.base.problems.write(output.stdout)
except sp.CalledProcessError as e:
self.base.problems.clear()
def setup(api: ExtensionsAPI) -> Ruff:
api.register("ruff", Ruff(api))