From 3414053d7cd17b43dd929e4e5fc468d98145e0d5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 17 Oct 2020 12:34:30 +0530 Subject: update intro, new post: Self-hosting Git! --- docs/posts/rapid_refactoring_with_vim/index.html | 55 +++++++++++++----------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'docs/posts/rapid_refactoring_with_vim/index.html') diff --git a/docs/posts/rapid_refactoring_with_vim/index.html b/docs/posts/rapid_refactoring_with_vim/index.html index d8a1c25..ff6cb4c 100644 --- a/docs/posts/rapid_refactoring_with_vim/index.html +++ b/docs/posts/rapid_refactoring_with_vim/index.html @@ -40,34 +40,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
@@ -139,7 +139,10 @@ BUFFER: json!( ... );

I'm Akshay, I go by nerd or nerdypepper on the internet.

I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. - I write open-source stuff to pass time. I also design fonts: scientifica, curie. + I write open-source stuff to pass time. + I also design fonts: + scientifica, + curie.

Send me a mail at nerdy@peppe.rs or a message at nerdypepper@irc.rizon.net.

-- cgit v1.2.3