1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
<!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">
<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>
<pre><code>→ use serde_json::{from_str};
#[test]
fn deserialize() {
assert_eq!(
→ from_str::<Action>(r#"{"set_tweak": "highlight"}"#),
Action::SetTweak(Tweak::Highlight { value: true })
);
}
</code></pre>
<p>had to be converted to:</p>
<pre><code>→ use serde_json::{from_value};
#[test]
fn deserialize() {
assert_eq!(
→ from_value::<Action>(json!({"set_tweak": "highlight"})),
Action::SetTweak(Tweak::Highlight { value: true })
);
}
</code></pre>
<h3 id="The%20arglist">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>
<pre><code># `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
</code></pre>
<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
<sup id="fnref1"><a href="#fn1" rel="footnote">1</a></sup>, 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%20quickfix%20list">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 <sup id="fnref2"><a href="#fn2" rel="footnote">2</a></sup> 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 <sup id="fnref3"><a href="#fn3" rel="footnote">3</a></sup> 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%20Filtering">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> <sup id="fnref4"><a href="#fn4" rel="footnote">4</a></sup>. 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>
<div class="footnotes">
<hr/>
<ol>
<li id="fn1">
<p><a href="https://github.com/tpope/vim-unimpaired">https://github.com/tpope/vim-unimpaired</a>
It also handles various other mappings, <code>]q</code> and <code>[q</code> to
navigate the quickfix list for example <a href="#fnref1" rev="footnote">↩</a></p>
</li>
<li id="fn2">
<p><code>:help recording</code> <a href="#fnref2" rev="footnote">↩</a></p>
</li>
<li id="fn3">
<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" rev="footnote">↩</a></p>
</li>
<li id="fn4">
<p><code>:help mark-motions</code> <a href="#fnref4" rev="footnote">↩</a></p>
</li>
</ol>
</div>
</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 [email protected] or a message at [email protected].</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>
|