-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpost-edit-format.py
More file actions
45 lines (33 loc) · 879 Bytes
/
post-edit-format.py
File metadata and controls
45 lines (33 loc) · 879 Bytes
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
# /// script
# requires-python = ">=3.12"
# dependencies = []
# ///
"""Post-edit hook to auto-format Rust files with cargo fmt."""
import json
import os
import subprocess
import sys
from pathlib import Path
def main() -> None:
input_data = json.load(sys.stdin)
tool_name = input_data.get("tool_name")
tool_input = input_data.get("tool_input", {})
file_path = tool_input.get("file_path")
if tool_name not in ("Write", "Edit", "MultiEdit"):
return
if not file_path:
return
path = Path(file_path)
if path.suffix != ".rs":
return
cwd = os.environ.get("CLAUDE_PROJECT_DIR", os.getcwd())
try:
subprocess.run(
["cargo", "fmt", "--", file_path],
cwd=cwd,
capture_output=True,
)
except FileNotFoundError:
pass
if __name__ == "__main__":
main()