-
Notifications
You must be signed in to change notification settings - Fork 1
/
autopep8.py
68 lines (54 loc) · 1.85 KB
/
autopep8.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
from __future__ import annotations
__version__ = "0.2.0"
__version_info__ = tuple([int(num) for num in __version__.split(".")])
import subprocess as sp
import typing
try:
import autopep8
except ImportError:
try:
sp.run(["pip", "install", "autopep8"], check=True)
import autopep8
except Exception as e:
autopep8 = None
if typing.TYPE_CHECKING:
from biscuit.api import ExtensionsAPI
class Autopep8:
def __init__(self, api: ExtensionsAPI) -> None:
self.api = api
self.base = api.base
def install(self) -> None:
if not autopep8:
return self.api.notifications.info(
"autopep8 pip package is not installed. Install it to use this extension.",
actions=[
(
"Install",
lambda: self.api.terminalmanager.run_command(
"pip install autopep8"
),
),
],
)
self.api.commands.register_command(
"Autopep8: Format active editor", self.format
)
def format(self, *_) -> str:
import autopep8
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
before = text.get_all_text()
try:
formatted = autopep8.fix_code(before)
if before != formatted:
editor.content.text.load_text(formatted)
except sp.CalledProcessError as e:
self.api.notifications.error(f"Autopep8: Failed to format file, see logs.")
self.api.logger.error(e)
return
def setup(api: ExtensionsAPI) -> Autopep8:
api.register("autopep8", Autopep8(api))