From 938c4648b102975cd73f82457544ec7dc0318bfa Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 4 Oct 2021 08:10:24 +0530 Subject: new art: b8 --- docs/posts/rapid_refactoring_with_vim/index.html | 50 ++++++++++++------------ 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'docs/posts/rapid_refactoring_with_vim') diff --git a/docs/posts/rapid_refactoring_with_vim/index.html b/docs/posts/rapid_refactoring_with_vim/index.html index b4081c4..3d867a3 100644 --- a/docs/posts/rapid_refactoring_with_vim/index.html +++ b/docs/posts/rapid_refactoring_with_vim/index.html @@ -44,34 +44,34 @@

Last weekend, I was tasked with refactoring the 96 unit tests on ruma-events to use strictly typed json objects using serde_json::json! instead of raw strings. It was rather painless thanks to vim :)

Here’s a small sample of what had to be done (note the lines prefixed with the arrow):

-
use serde_json::{from_str};
-  
-  #[test]
-  fn deserialize() {
-    assert_eq!(
-from_str::<Action>(r#"{"set_tweak": "highlight"}"#),
-        Action::SetTweak(Tweak::Highlight { value: true })
-        );
-  }
+
use serde_json::{from_str};
+  
+  #[test]
+  fn deserialize() {
+    assert_eq!(
+from_str::<Action>(r#"{"set_tweak": "highlight"}"#),
+        Action::SetTweak(Tweak::Highlight { value: true })
+        );
+  }

had to be converted to:

-
use serde_json::{from_value};
-  
-  #[test]
-  fn deserialize() {
-    assert_eq!(
-from_value::<Action>(json!({"set_tweak": "highlight"})),
-        Action::SetTweak(Tweak::Highlight { value: true })
-        );
-  }
+
use serde_json::{from_value};
+  
+  #[test]
+  fn deserialize() {
+    assert_eq!(
+from_value::<Action>(json!({"set_tweak": "highlight"})),
+        Action::SetTweak(Tweak::Highlight { value: true })
+        );
+  }

The arglist

For the initial pass, I decided to handle imports, this was a simple find and replace operation, done to all the files containing tests. Luckily, modules (and therefore files) containing tests in Rust are annotated with the #[cfg(test)] attribute. I opened all such files:

-
# `grep -l pattern files` lists all the files
-#  matching the pattern
-
-vim $(grep -l 'cfg\(test\)' ./**/*.rs)
-
-# expands to something like:
-vim push_rules.rs room/member.rs key/verification/lib.rs
+
# `grep -l pattern files` lists all the files
+#  matching the pattern
+
+vim $(grep -l 'cfg\(test\)' ./**/*.rs)
+
+# expands to something like:
+vim push_rules.rs room/member.rs key/verification/lib.rs

Starting vim with more than one file at the shell prompt populates the arglist. Hit :args to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with :next and :prev. I use tpope’s vim-unimpaired 1, which adds ]a and [a, mapped to :next and :prev.

All that’s left to do is the find and replace, for which we will be using vim’s argdo, applying a substitution to every file in the arglist:

:argdo s/from_str/from_value/g
-- cgit v1.2.3