<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="/style.css"> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1"> <meta content="#ffffff" name="theme-color"> <meta name="HandheldFriendly" content="true"> <meta property="og:title" content="Rapid Refactoring With Vim"> <meta property="og:type" content="website"> <meta property="og:description" content="a static site {for, by, about} me "> <meta property="og:url" content="https://peppe.rs"> <link rel="icon" type="image/x-icon" href="/favicon.png"> <title>Rapid Refactoring With Vim · peppe.rs</title> <body> <div class="posts"> <div class="post"> <a href="/" class="post-end-link">⟵ Back</a> <a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/rapid_refactoring_with_vim.md ">View Raw</a> <div class="separator"></div> <div class="date"> 01/04 — 2020 <div class="stats"> <span class="stats-number"> 79.12 </span> <span class="stats-unit">cm</span>   <span class="stats-number"> 5.4 </span> <span class="stats-unit">min</span> </div> </div> <h1> Rapid Refactoring With Vim </h1> <div class="post-text"> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> <head> <meta charset="utf-8" /> <meta name="generator" content="pandoc" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> <title>rapid_refactoring_with_vim</title> <style> code{white-space: pre-wrap;} span.smallcaps{font-variant: small-caps;} span.underline{text-decoration: underline;} div.column{display: inline-block; vertical-align: top; width: 50%;} div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} ul.task-list{list-style: none;} pre > code.sourceCode { white-space: pre; position: relative; } pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } pre > code.sourceCode > span:empty { height: 1.2em; } code.sourceCode > span { color: inherit; text-decoration: inherit; } div.sourceCode { margin: 1em 0; } pre.sourceCode { margin: 0; } @media screen { div.sourceCode { overflow: auto; } } @media print { pre > code.sourceCode { white-space: pre-wrap; } pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } } pre.numberSource code { counter-reset: source-line 0; } pre.numberSource code > span { position: relative; left: -4em; counter-increment: source-line; } pre.numberSource code > span > a:first-child::before { content: counter(source-line); position: relative; left: -1em; text-align: right; vertical-align: baseline; border: none; display: inline-block; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; padding: 0 4px; width: 4em; } pre.numberSource { margin-left: 3em; padding-left: 4px; } div.sourceCode { } @media screen { pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } } code span.al { font-weight: bold; } /* Alert */ code span.an { font-style: italic; } /* Annotation */ code span.cf { font-weight: bold; } /* ControlFlow */ code span.co { font-style: italic; } /* Comment */ code span.cv { font-style: italic; } /* CommentVar */ code span.do { font-style: italic; } /* Documentation */ code span.dt { text-decoration: underline; } /* DataType */ code span.er { font-weight: bold; } /* Error */ code span.in { font-style: italic; } /* Information */ code span.kw { font-weight: bold; } /* Keyword */ code span.pp { font-weight: bold; } /* Preprocessor */ code span.wa { font-style: italic; } /* Warning */ </style> <!--[if lt IE 9]> <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script> <![endif]--> </head> <body> <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> <p>Here’s a small sample of what had to be done (note the lines prefixed with the arrow):</p> <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> <span id="cb1-2"><a href="#cb1-2"></a> </span> <span id="cb1-3"><a href="#cb1-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> <span id="cb1-4"><a href="#cb1-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> <span id="cb1-5"><a href="#cb1-5"></a> <span class="pp">assert_eq!</span>(</span> <span id="cb1-6"><a href="#cb1-6"></a>→ <span class="pp">from_str::</span><span class="op"><</span>Action<span class="op">></span>(<span class="st">r#"{"set_tweak": "highlight"}"#</span>)<span class="op">,</span></span> <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="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> <span id="cb1-8"><a href="#cb1-8"></a> )<span class="op">;</span></span> <span id="cb1-9"><a href="#cb1-9"></a> <span class="op">}</span></span></code></pre></div> <p>had to be converted to:</p> <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> <span id="cb2-2"><a href="#cb2-2"></a> </span> <span id="cb2-3"><a href="#cb2-3"></a> <span class="at">#[</span>test<span class="at">]</span></span> <span id="cb2-4"><a href="#cb2-4"></a> <span class="kw">fn</span> deserialize() <span class="op">{</span></span> <span id="cb2-5"><a href="#cb2-5"></a> <span class="pp">assert_eq!</span>(</span> <span id="cb2-6"><a href="#cb2-6"></a>→ <span class="pp">from_value::</span><span class="op"><</span>Action<span class="op">></span>(<span class="pp">json!</span>(<span class="op">{</span><span class="st">"set_tweak"</span><span class="op">:</span> <span class="st">"highlight"</span><span class="op">}</span>))<span class="op">,</span></span> <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="op">:</span> <span class="cn">true</span> <span class="op">}</span>)</span> <span id="cb2-8"><a href="#cb2-8"></a> )<span class="op">;</span></span> <span id="cb2-9"><a href="#cb2-9"></a> <span class="op">}</span></span></code></pre></div> <h3 id="the-arglist">The arglist</h3> <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> <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> <span id="cb3-2"><a href="#cb3-2"></a><span class="co"># matching the pattern</span></span> <span id="cb3-3"><a href="#cb3-3"></a></span> <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">'cfg\(test\)'</span> ./**/*.rs<span class="va">)</span></span> <span id="cb3-5"><a href="#cb3-5"></a></span> <span id="cb3-6"><a href="#cb3-6"></a><span class="co"># expands to something like:</span></span> <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> <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> <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> <pre><code>:argdo s/from_str/from_value/g</code></pre> <h3 id="the-quickfix-list">The quickfix list</h3> <p>Next up, replacing <code>r#" ... "#</code> with <code>json!( ... )</code>. I couldn’t search and replace that trivially, so I went with a macro call <a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> instead, starting with the cursor on ‘r’, represented by the caret, in my attempt to breakdown the process:</p> <pre><code>BUFFER: r#" ... "#; ^ ACTION: vllsjson!( BUFFER json!( ... "#; ^ ACTION: <esc>$F# BUFFER: json!( ... "#; ^ ACTION: vhs)<esc> BUFFER: json!( ... );</code></pre> <p>Here’s the recorded <a href="#fn3" class="footnote-ref" id="fnref3" role="doc-noteref"><sup>3</sup></a> macro in all its glory: <code>vllsjson!(<esc>$F#vhs)<esc></code>.</p> <p>Great! So now we just go ahead, find every occurrence of <code>r#</code> and apply the macro right? Unfortunately, there were more than a few occurrences of raw strings that had to stay raw strings. Enter, the quickfix list.</p> <p>The idea behind the quickfix list is to jump from one position in a file to another (maybe in a different file), much like how the arglist lets you jump from one file to another.</p> <p>One of the easiest ways to populate this list with a bunch of positions is to use <code>vimgrep</code>:</p> <pre><code># basic usage :vimgrep pattern files # search for raw strings :vimgrep 'r#' ./**/*.rs</code></pre> <p>Like <code>:next</code> and <code>:prev</code>, you can navigate the quickfix list with <code>:cnext</code> and <code>:cprev</code>. Every time you move up or down the list, vim indicates your index:</p> <pre><code>(1 of 131): r#"{"set_tweak": "highlight"}"#;</code></pre> <p>And just like <code>argdo</code>, you can <code>cdo</code> to apply commands to <em>every</em> match in the quickfix list:</p> <pre><code>:cdo norm! @q</code></pre> <p>But, I had to manually pick out matches, and it involved some button mashing.</p> <h3 id="external-filtering">External Filtering</h3> <p>Some code reviews later, I was asked to format all the json inside the <code>json!</code> macro. All you have to do is pass a visual selection through a pretty json printer. Select the range to be formatted in visual mode, and hit <code>:</code>, you will notice the command line displaying what seems to be gibberish:</p> <pre><code>:'<,'></code></pre> <p><code>'<</code> and <code>'></code> are <em>marks</em> <a href="#fn4" class="footnote-ref" id="fnref4" role="doc-noteref"><sup>4</sup></a>. More specifically, they are marks that vim sets automatically every time you make a visual selection, denoting the start and end of the selection.</p> <p>A range is one or more line specifiers separated by a <code>,</code>:</p> <pre><code>:1,7 lines 1 through 7 :32 just line 32 :. the current line :.,$ the current line to the last line :'a,'b mark 'a' to mark 'b'</code></pre> <p>Most <code>:</code> commands can be prefixed by ranges. <code>:help usr_10.txt</code> for more on that.</p> <p>Alright, lets pass json through <code>python -m json.tool</code>, a json formatter that accepts <code>stdin</code> (note the use of <code>!</code> to make use of an external program):</p> <pre><code>:'<,'>!python -m json.tool</code></pre> <p>Unfortunately that didn’t quite work for me because the range included some non-json text as well, a mix of regex and macros helped fix that. I think you get the drift.</p> <p>Another fun filter I use from time to time is <code>:!sort</code>, to sort css attributes, or <code>:!uniq</code> to remove repeated imports.</p> <section class="footnotes" role="doc-endnotes"> <hr /> <ol> <li id="fn1" role="doc-endnote"><p>https://github.com/tpope/vim-unimpaired It also handles various other mappings, <code>]q</code> and <code>[q</code> to navigate the quickfix list for example<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li> <li id="fn2" role="doc-endnote"><p><code>:help recording</code><a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li> <li id="fn3" role="doc-endnote"><p>When I’m recording a macro, I prefer starting out by storing it in register <code>q</code>, and then copying it over to another register if it works as intended. I think of <code>qq</code> as ‘quick record’.<a href="#fnref3" class="footnote-back" role="doc-backlink">↩︎</a></p></li> <li id="fn4" role="doc-endnote"><p><code>:help mark-motions</code><a href="#fnref4" class="footnote-back" role="doc-backlink">↩︎</a></p></li> </ol> </section> </body> </html> </div> <div class=intro> Hi. <a href=https://peppe.rs/index.xml class=feed-button>Subscribe</a> <p>I'm Akshay, I go by nerd or nerdypepper on the internet.</p> <p> 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. </p> <p>Send me a mail at nerdy@peppe.rs or a message at nerd@irc.rizon.net.</p> </div> <a href="/" class="post-end-link">⟵ Back</a> <a class="stats post-end-link" href="https://raw.githubusercontent.com/nerdypepper/site/master/posts/rapid_refactoring_with_vim.md ">View Raw</a> </div> </div> </body> </html>