aboutsummaryrefslogtreecommitdiff
path: root/docs/posts/rapid_refactoring_with_vim
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-17 08:04:30 +0100
committerAkshay <[email protected]>2020-10-17 08:04:30 +0100
commit3414053d7cd17b43dd929e4e5fc468d98145e0d5 (patch)
tree7b6ec8cfe18e217343cdd54986d5c179fc9e8098 /docs/posts/rapid_refactoring_with_vim
parent897df34e5e55448ae89fbf1a77533e792e7a5c4e (diff)
update intro, new post: Self-hosting Git!
Diffstat (limited to 'docs/posts/rapid_refactoring_with_vim')
-rw-r--r--docs/posts/rapid_refactoring_with_vim/index.html55
1 files changed, 29 insertions, 26 deletions
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 @@
40 <div class="post-text"> 40 <div class="post-text">
41 <p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p> 41 <p>Last weekend, I was tasked with refactoring the 96 unit tests on <a href="https://github.com/ruma/ruma-events/pull/70">ruma-events</a> to use strictly typed json objects using <code>serde_json::json!</code> instead of raw strings. It was rather painless thanks to vim :)</p>
42<p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> 42<p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p>
43<div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">}</span>;</span> 43<div class="sourceCode" id="cb1"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_str<span class="op">};</span></span>
44<span id="cb1-2"><a href="#cb1-2"></a> </span> 44<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> </span>
45<span id="cb1-3"><a href="#cb1-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> 45<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span>
46<span id="cb1-4"><a href="#cb1-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 46<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span>
47<span id="cb1-5"><a href="#cb1-5"></a> <span class="pp">assert_eq!</span>(</span> 47<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span>
48<span id="cb1-6"><a href="#cb1-6"></a>→ <span class="pp">from_str::</span>&lt;Action&gt;(<span class="st">r#&quot;{&quot;set_tweak&quot;: &quot;highlight&quot;}&quot;#</span>),</span> 48<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a>→ <span class="pp">from_str::</span><span class="op">&lt;</span>Action<span class="op">&gt;</span>(<span class="st">r#&quot;{&quot;set_tweak&quot;: &quot;highlight&quot;}&quot;#</span>)<span class="op">,</span></span>
49<span id="cb1-7"><a href="#cb1-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value: <span class="cn">true</span> <span class="op">}</span>)</span> 49<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span>
50<span id="cb1-8"><a href="#cb1-8"></a> );</span> 50<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a> )<span class="op">;</span></span>
51<span id="cb1-9"><a href="#cb1-9"></a> <span class="op">}</span></span></code></pre></div> 51<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div>
52<p>had to be converted to:</p> 52<p>had to be converted to:</p>
53<div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">}</span>;</span> 53<div class="sourceCode" id="cb2"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a>→ <span class="kw">use</span> <span class="pp">serde_json::</span><span class="op">{</span>from_value<span class="op">};</span></span>
54<span id="cb2-2"><a href="#cb2-2"></a> </span> 54<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> </span>
55<span id="cb2-3"><a href="#cb2-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> 55<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="at">#[</span>test<span class="at">]</span></span>
56<span id="cb2-4"><a href="#cb2-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> 56<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span>
57<span id="cb2-5"><a href="#cb2-5"></a> <span class="pp">assert_eq!</span>(</span> 57<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> <span class="pp">assert_eq!</span>(</span>
58<span id="cb2-6"><a href="#cb2-6"></a>→ <span class="pp">from_value::</span>&lt;Action&gt;(<span class="pp">json!</span>(<span class="op">{</span><span class="st">&quot;set_tweak&quot;</span>: <span class="st">&quot;highlight&quot;</span><span class="op">}</span>)),</span> 58<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a>→ <span class="pp">from_value::</span><span class="op">&lt;</span>Action<span class="op">&gt;</span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">&quot;set_tweak&quot;</span><span class="op">:</span> <span class="st">&quot;highlight&quot;</span><span class="op">}</span>))<span class="op">,</span></span>
59<span id="cb2-7"><a href="#cb2-7"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value: <span class="cn">true</span> <span class="op">}</span>)</span> 59<span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a> <span class="pp">Action::</span>SetTweak(<span class="pp">Tweak::</span>Highlight <span class="op">{</span> value<span class="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span>
60<span id="cb2-8"><a href="#cb2-8"></a> );</span> 60<span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> )<span class="op">;</span></span>
61<span id="cb2-9"><a href="#cb2-9"></a> <span class="op">}</span></span></code></pre></div> 61<span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="op">}</span></span></code></pre></div>
62<h3 id="the-arglist">The arglist</h3> 62<h3 id="the-arglist">The arglist</h3>
63<p>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 <code>#[cfg(test)]</code> attribute. I opened all such files:</p> 63<p>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 <code>#[cfg(test)]</code> attribute. I opened all such files:</p>
64<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># `grep -l pattern files` lists all the files</span></span> 64<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="co"># `grep -l pattern files` lists all the files</span></span>
65<span id="cb3-2"><a href="#cb3-2"></a><span class="co"># matching the pattern</span></span> 65<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a><span class="co"># matching the pattern</span></span>
66<span id="cb3-3"><a href="#cb3-3"></a></span> 66<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span>
67<span id="cb3-4"><a href="#cb3-4"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">&#39;cfg\(test\)&#39;</span> ./**/*.rs<span class="va">)</span></span> 67<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a><span class="ex">vim</span> <span class="va">$(</span><span class="fu">grep</span> -l <span class="st">&#39;cfg\(test\)&#39;</span> ./**/*.rs<span class="va">)</span></span>
68<span id="cb3-5"><a href="#cb3-5"></a></span> 68<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a></span>
69<span id="cb3-6"><a href="#cb3-6"></a><span class="co"># expands to something like:</span></span> 69<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a><span class="co"># expands to something like:</span></span>
70<span id="cb3-7"><a href="#cb3-7"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div> 70<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a><span class="ex">vim</span> push_rules.rs room/member.rs key/verification/lib.rs</span></code></pre></div>
71<p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p> 71<p>Starting vim with more than one file at the shell prompt populates the arglist. Hit <code>:args</code> to see the list of files currently ready to edit. The square [brackets] indicate the current file. Navigate through the arglist with <code>:next</code> and <code>:prev</code>. I use tpope’s vim-unimpaired <a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>, which adds <code>]a</code> and <code>[a</code>, mapped to <code>:next</code> and <code>:prev</code>.</p>
72<p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p> 72<p>All that’s left to do is the find and replace, for which we will be using vim’s <code>argdo</code>, applying a substitution to every file in the arglist:</p>
73<pre><code>:argdo s/from_str/from_value/g</code></pre> 73<pre><code>:argdo s/from_str/from_value/g</code></pre>
@@ -139,7 +139,10 @@ BUFFER: json!( ... );</code></pre>
139 <p>I'm Akshay, I go by nerd or nerdypepper on the internet.</p> 139 <p>I'm Akshay, I go by nerd or nerdypepper on the internet.</p>
140 <p> 140 <p>
141 I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer. 141 I am a compsci undergrad, Rust programmer and an enthusiastic Vimmer.
142 I write open-source stuff to pass time. I also design fonts: scientifica, curie. 142 I write <a href=https://git.peppe.rs>open-source stuff</a> to pass time.
143 I also design fonts:
144 <a href=https://git.peppe.rs/fonts/scientifica>scientifica</a>,
145 <a href=https://git.peppe.rs/fonts/curie>curie</a>.
143 </p> 146 </p>
144 <p>Send me a mail at [email protected] or a message at [email protected].</p> 147 <p>Send me a mail at [email protected] or a message at [email protected].</p>
145 </div> 148 </div>