[gnome-builder] beautifier: Add a Rust formatter



commit 4867a346380bce42f5489968b7363fd913df71ed
Author: vanadiae <vanadiae35 gmail com>
Date:   Sat Feb 6 20:25:29 2021 +0100

    beautifier: Add a Rust formatter
    
    It uses rustfmt and will respect the project's rustfmt.toml.

 src/plugins/beautifier/beautifier.gresource.xml |  2 ++
 src/plugins/beautifier/config/rust/config.ini   | 10 +++++++
 src/plugins/beautifier/internal/run_rustfmt.py  | 37 +++++++++++++++++++++++++
 3 files changed, 49 insertions(+)
---
diff --git a/src/plugins/beautifier/beautifier.gresource.xml b/src/plugins/beautifier/beautifier.gresource.xml
index 6e40f6899..0591460ee 100644
--- a/src/plugins/beautifier/beautifier.gresource.xml
+++ b/src/plugins/beautifier/beautifier.gresource.xml
@@ -21,6 +21,7 @@
     <file>config/objc/config.ini</file>
     <file>config/objc/objc.cfg</file>
     <file>config/python/config.ini</file>
+    <file>config/rust/config.ini</file>
     <file>config/xml/config.ini</file>
 
     <file>self/global.ini</file>
@@ -29,5 +30,6 @@
     <file>self/c/gb-uncrustify.cfg</file>
 
     <file>internal/align_makefile.py</file>
+    <file>internal/run_rustfmt.py</file>
   </gresource>
 </gresources>
diff --git a/src/plugins/beautifier/config/rust/config.ini b/src/plugins/beautifier/config/rust/config.ini
new file mode 100644
index 000000000..cd289b8b6
--- /dev/null
+++ b/src/plugins/beautifier/config/rust/config.ini
@@ -0,0 +1,10 @@
+# Rust language config file
+
+[global]
+
+default = rustfmt
+
+[rustfmt]
+
+command-pattern = [internal]run_rustfmt.py @s@
+name = Rustfmt
diff --git a/src/plugins/beautifier/internal/run_rustfmt.py b/src/plugins/beautifier/internal/run_rustfmt.py
new file mode 100755
index 000000000..1647fe911
--- /dev/null
+++ b/src/plugins/beautifier/internal/run_rustfmt.py
@@ -0,0 +1,37 @@
+#!/bin/env python3
+
+import os
+import sys
+import pathlib
+import subprocess
+
+# That's a terrible way to fix https://github.com/rust-lang/rustfmt/issues/4660
+# This script wouldn't be needed at all if this bug wasn't a thing, and we could have called rustfmt 
directly from the config.ini
+
+cwd = pathlib.Path(os.getcwd()).resolve()
+
+cargo_home = pathlib.Path(os.environ['CARGO_HOME'] if 'CARGO_HOME' in os.environ else 
os.path.expanduser("~/.cargo"))
+
+command = [cargo_home.joinpath('bin').joinpath('rustfmt'), '--quiet', '--emit', 'stdout']
+while True:
+    if cwd.joinpath(".rustfmt.toml").exists() or cwd.joinpath("rustfmt.toml").exists():
+        command.append('--config-path=' + str(cwd))
+        break
+    cwd = cwd.parent
+    if str(cwd) == str(cwd.anchor):
+        break
+
+command.append(sys.argv[1])
+
+runned_process = subprocess.run(command, capture_output=True, text=True)
+
+print(runned_process.stderr, file=sys.stderr)
+# Because print() will always print at least a new line, don't
+# output anything on stdout if rustfmt errored, otherwise the
+# beautifier plugin will replace the whole selected text with a newline
+if runned_process.returncode == 0:
+    print(runned_process.stdout)
+
+# propagate return code so it can know if rustfmt couldn't format
+# the code, and hence ignore the outputted code and show the error message to the user
+exit(runned_process.returncode)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]