<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Posts about Vim</title><link>https://chriswarrick.com/</link><atom:link href="https://chriswarrick.com/blog/tags/vim.xml" rel="self" type="application/rss+xml" /><description>A rarely updated blog, mostly about programming.</description><lastBuildDate>Fri, 27 May 2016 08:46:35 GMT</lastBuildDate><generator>https://github.com/Kwpolska/YetAnotherBlogGenerator</generator><item><title>Code writing code: Python and Vim as development aids</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/</link><pubDate>Fri, 27 May 2016 08:46:35 GMT</pubDate><guid>https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/</guid><description>
Recently I was working on some C# and Java code. And along the way, I used
Python and Vim to (re)write my code. A small Python script and a 6-keystroke
Vim macro did it faster and better than a human would.
Every programmer should learn a good scripting language and use a programmable
editor like Vim. Why? Here are two examples, after the break.
</description><content:encoded><![CDATA[
<p>Recently I was working on some C# and Java code. And along the way, I used
Python and Vim to (re)write my code. A small Python script and a 6-keystroke
Vim macro did it faster and better than a human would.</p>
<p>Every programmer should learn a good scripting language and use a programmable
editor like Vim. Why? Here are two examples, after the break.</p>



<section id="episode-i-inotifypropertychanged-or-python-writing-c">
<h1>Episode I: <code class="docutils literal">INotifyPropertyChanged</code>, or Python writing C#</h1>
<p>I was building a private C# weekend project (that turned into a weeklong
project) — and by the way, WPF and C# are quite pleasant (Windows Forms is a
trainwreck, though). One of the things I used in that project was a DataGrid
bound to a list of custom objects (a DataGrid is a table, basically). And in
order to use it, you need to use the <code class="docutils literal">INotifyPropertyChanged</code> interface <a class="reference external" href="https://msdn.microsoft.com/en-us/library/ms229614(v=vs.100).aspx">(MSDN)</a>.
It involves doing something like this:</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-1" name="rest_code_4c40930703c34f739bfa440b60bb2d79-1"></a><span class="w"> </span><span class="k">private</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="n">name_</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">get</span><span class="p">;</span><span class="w"> </span><span class="k">set</span><span class="p">;</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// can also be a field</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-2" name="rest_code_4c40930703c34f739bfa440b60bb2d79-2"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-3" name="rest_code_4c40930703c34f739bfa440b60bb2d79-3"></a><span class="w"> </span><span class="na">[JsonProperty]</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-4" name="rest_code_4c40930703c34f739bfa440b60bb2d79-4"></a><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-5" name="rest_code_4c40930703c34f739bfa440b60bb2d79-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">get</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-6" name="rest_code_4c40930703c34f739bfa440b60bb2d79-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">return</span><span class="w"> </span><span class="n">name_</span><span class="p">;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-7" name="rest_code_4c40930703c34f739bfa440b60bb2d79-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-8" name="rest_code_4c40930703c34f739bfa440b60bb2d79-8"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">set</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-9" name="rest_code_4c40930703c34f739bfa440b60bb2d79-9"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">value</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">name_</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-10" name="rest_code_4c40930703c34f739bfa440b60bb2d79-10"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="n">name_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">value</span><span class="p">;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-11" name="rest_code_4c40930703c34f739bfa440b60bb2d79-11"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="n">NotifyPropertyChanged</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">);</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-12" name="rest_code_4c40930703c34f739bfa440b60bb2d79-12"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-13" name="rest_code_4c40930703c34f739bfa440b60bb2d79-13"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4c40930703c34f739bfa440b60bb2d79-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="rest_code_4c40930703c34f739bfa440b60bb2d79-14" name="rest_code_4c40930703c34f739bfa440b60bb2d79-14"></a><span class="w"> </span><span class="p">}</span>
</code></td></tr></table></div><p>That’s 12 lines of code (excluding <code class="docutils literal">[JsonProperty]</code> which comes from the
Json.NET library) for that pattern. Oh: and I need to do that for <strong>every</strong>
field/property of my class, because otherwise any changes to them would not be
reflected in the tables (and maybe one or two fields were <em>not</em> in the table).</p>
<p>Doing that by hand is really not feasible: you need to copy-paste this
large block 14 times and take care of 5 instances of the name (3 with
underscores, 2 without), 2 instances of the type, and the <code class="docutils literal">[JsonProperty]</code>
attribute (which does not appear on all properties).</p>
<p>So, I used one of those intelligent computer things to do it for me. I wrote a
really simple <a class="reference external" href="https://www.python.org/">Python</a> script and ran it. And I ended up with all 14 fields built
for me.</p>
<p><a class="reference external" href="link://listing/listings/code-writing-code/write_properties.py">code-writing-code/write_properties.py</a>  <a class="reference external" href="link://listing_source/listings/code-writing-code/write_properties.py">(Source)</a></p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-1" name="rest_code_5defa6b60bf345e8ac072f136362524b-1"></a><span class="ch">#!/usr/bin/env python3</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-2" name="rest_code_5defa6b60bf345e8ac072f136362524b-2"></a><span class="n">TEMPLATE</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-3" name="rest_code_5defa6b60bf345e8ac072f136362524b-3"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="si">%s</span><span class="s2">public </span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2"> {</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-4" name="rest_code_5defa6b60bf345e8ac072f136362524b-4"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get {</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-5" name="rest_code_5defa6b60bf345e8ac072f136362524b-5"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </span><span class="si">%s</span><span class="s2">_;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-6" name="rest_code_5defa6b60bf345e8ac072f136362524b-6"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-7" name="rest_code_5defa6b60bf345e8ac072f136362524b-7"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set {</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-8" name="rest_code_5defa6b60bf345e8ac072f136362524b-8"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value != </span><span class="si">%s</span><span class="s2">_) {</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-9" name="rest_code_5defa6b60bf345e8ac072f136362524b-9"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="si">%s</span><span class="s2">_ = value;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-10" name="rest_code_5defa6b60bf345e8ac072f136362524b-10"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotifyPropertyChanged(&quot;</span><span class="si">%s</span><span class="s2">&quot;);</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-11" name="rest_code_5defa6b60bf345e8ac072f136362524b-11"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-12" name="rest_code_5defa6b60bf345e8ac072f136362524b-12"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-13" name="rest_code_5defa6b60bf345e8ac072f136362524b-13"></a><span class="s2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-14" name="rest_code_5defa6b60bf345e8ac072f136362524b-14"></a><span class="s2">&quot;&quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-15"><code data-line-number="15"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-15" name="rest_code_5defa6b60bf345e8ac072f136362524b-15"></a><span class="n">JSONPROPERTY_TEMPLATE</span> <span class="o">=</span> <span class="s1">&#39;[JsonProperty]</span><span class="se">\n</span><span class="s1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-16"><code data-line-number="16"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-16" name="rest_code_5defa6b60bf345e8ac072f136362524b-16"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-17"><code data-line-number="17"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-17" name="rest_code_5defa6b60bf345e8ac072f136362524b-17"></a><span class="k">def</span><span class="w"> </span><span class="nf">write</span><span class="p">(</span><span class="n">has_jsonproperty</span><span class="p">,</span> <span class="n">vtype</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-18"><code data-line-number="18"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-18" name="rest_code_5defa6b60bf345e8ac072f136362524b-18"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">has_jsonproperty</span><span class="p">:</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-19"><code data-line-number="19"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-19" name="rest_code_5defa6b60bf345e8ac072f136362524b-19"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">jsonproperty</span> <span class="o">=</span> <span class="n">JSONPROPERTY_TEMPLATE</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-20"><code data-line-number="20"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-20" name="rest_code_5defa6b60bf345e8ac072f136362524b-20"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span><span class="p">:</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-21"><code data-line-number="21"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-21" name="rest_code_5defa6b60bf345e8ac072f136362524b-21"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">jsonproperty</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-22"><code data-line-number="22"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-22" name="rest_code_5defa6b60bf345e8ac072f136362524b-22"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">TEMPLATE</span> <span class="o">%</span> <span class="p">(</span><span class="n">jsonproperty</span><span class="p">,</span> <span class="n">vtype</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-23"><code data-line-number="23"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-23" name="rest_code_5defa6b60bf345e8ac072f136362524b-23"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-24"><code data-line-number="24"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-24" name="rest_code_5defa6b60bf345e8ac072f136362524b-24"></a><span class="n">properties</span> <span class="o">=</span> <span class="p">[</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-25"><code data-line-number="25"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-25" name="rest_code_5defa6b60bf345e8ac072f136362524b-25"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s1">&#39;1 string name&#39;</span><span class="p">,</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-26"><code data-line-number="26"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-26" name="rest_code_5defa6b60bf345e8ac072f136362524b-26"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s1">&#39;0 int another&#39;</span><span class="p">,</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-27"><code data-line-number="27"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-27" name="rest_code_5defa6b60bf345e8ac072f136362524b-27"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># 12 fields omitted for brevity</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-28"><code data-line-number="28"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-28" name="rest_code_5defa6b60bf345e8ac072f136362524b-28"></a><span class="p">]</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-29"><code data-line-number="29"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-29" name="rest_code_5defa6b60bf345e8ac072f136362524b-29"></a><span class="n">properties_split</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">]</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-30"><code data-line-number="30"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-30" name="rest_code_5defa6b60bf345e8ac072f136362524b-30"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-31"><code data-line-number="31"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-31" name="rest_code_5defa6b60bf345e8ac072f136362524b-31"></a><span class="c1"># Private definitions (internal)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-32"><code data-line-number="32"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-32" name="rest_code_5defa6b60bf345e8ac072f136362524b-32"></a><span class="k">for</span> <span class="n">has_jsonproperty</span><span class="p">,</span> <span class="n">vtype</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">properties_split</span><span class="p">:</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-33"><code data-line-number="33"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-33" name="rest_code_5defa6b60bf345e8ac072f136362524b-33"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private </span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">_ { get; set; }&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">vtype</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-34"><code data-line-number="34"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-34" name="rest_code_5defa6b60bf345e8ac072f136362524b-34"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-35"><code data-line-number="35"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-35" name="rest_code_5defa6b60bf345e8ac072f136362524b-35"></a><span class="nb">print</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-36"><code data-line-number="36"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-36" name="rest_code_5defa6b60bf345e8ac072f136362524b-36"></a><span class="c1"># Public definitions (with notifications)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-37"><code data-line-number="37"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-37" name="rest_code_5defa6b60bf345e8ac072f136362524b-37"></a><span class="k">for</span> <span class="n">has_jsonproperty</span><span class="p">,</span> <span class="n">vtype</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">properties_split</span><span class="p">:</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_5defa6b60bf345e8ac072f136362524b-38"><code data-line-number="38"></code></a></td><td class="code"><code><a id="rest_code_5defa6b60bf345e8ac072f136362524b-38" name="rest_code_5defa6b60bf345e8ac072f136362524b-38"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">print</span><span class="p">(</span><span class="n">write</span><span class="p">(</span><span class="n">has_jsonproperty</span> <span class="o">==</span> <span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="n">vtype</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
</code></td></tr></table></div><p>That script takes a list of properties and spits out a block of code, ready to
be pasted into the code. Visual Studio has a nice <em>Insert File as Text</em>
feature, so redirecting the output to a file and using that option is enough.</p>
</section>
<section id="episode-ii-fixing-argument-order-or-vim-re-writing-java">
<h1>Episode II: Fixing argument order, or Vim (re)writing Java</h1>
<p>Another project, <a class="reference external" href="https://github.com/Kwpolska/numbernamer">Number Namer</a>, written in Java, and it does what it says on
the tin: takes a number and writes it out as words, while being multilingual and
extensible. I used Eclipse for this project, because it looks good, is really
helpful with its code linting, and does not run slowly on my aging system (I’m
looking at you, IntelliJ IDEA aka PyCharm aka Android Studio…)</p>
<p>And so, I was building a test suite, using <a class="reference external" href="http://junit.org/">JUnit</a>. It’s pretty
straightforward, and I remember the syntax from Python’s unittest (even though
I write tests with <a class="reference external" href="http://pytest.org/">pytest</a> nowadays). Or so I thought.</p>
<div class="code"><pre class="code java"><a id="rest_code_4fd345f92b594e8bb7094164392eebcd-1" name="rest_code_4fd345f92b594e8bb7094164392eebcd-1" href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4fd345f92b594e8bb7094164392eebcd-1"></a><span class="c1">// (incorrect)</span>
<a id="rest_code_4fd345f92b594e8bb7094164392eebcd-2" name="rest_code_4fd345f92b594e8bb7094164392eebcd-2" href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4fd345f92b594e8bb7094164392eebcd-2"></a><span class="n">assertEquals</span><span class="p">(</span><span class="s">&quot;Basic integers (7) failed&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">namer</span><span class="p">.</span><span class="na">name</span><span class="p">(</span><span class="mi">7L</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;seven&quot;</span><span class="p">);</span>
<a id="rest_code_4fd345f92b594e8bb7094164392eebcd-3" name="rest_code_4fd345f92b594e8bb7094164392eebcd-3" href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4fd345f92b594e8bb7094164392eebcd-3"></a><span class="c1">// (fixed)                              ^ cursor</span>
<a id="rest_code_4fd345f92b594e8bb7094164392eebcd-4" name="rest_code_4fd345f92b594e8bb7094164392eebcd-4" href="https://chriswarrick.com/blog/2016/05/27/code-writing-code-python-and-vim/#rest_code_4fd345f92b594e8bb7094164392eebcd-4"></a><span class="n">assertEquals</span><span class="p">(</span><span class="s">&quot;Basic integers (7) failed&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;seven&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">namer</span><span class="p">.</span><span class="na">name</span><span class="p">(</span><span class="mi">7L</span><span class="p">));</span>
</pre></div>
<p>You see, the typical Python spelling is <code class="docutils literal">self.assertEquals(actual,
expected)</code>. Java adds a <code class="docutils literal">String</code> message parameter and it also swaps
<code class="docutils literal">actual</code> and <code class="docutils literal">expected</code>. Which I didn’t notice at first, and I wrote my
assertions incorrectly. While it doesn’t <em>really</em> matter (it will still work),
the output looked a bit weird.</p>
<p>And I noticed only when I finished writing my tests (and I had a typo in my
expected output). I wanted to fix them all — not manually, of course. So, I
closed this file, brought up Vim, searched for the motion I need (it’s
<code class="docutils literal">t{char}</code> — see <code class="docutils literal">:help t</code>). And I ended up with this
(cursor placed on the comma after the first argument):</p>
<div style="text-align: center;">
<kbd style="font-size: 2em;">dt,</kbd><kbd style="font-size: 2em;">t)</kbd><kbd style="font-size: 2em;">p</kbd>
</div><p>What does this do, you may ask? It’s actually pretty self-explanatory:</p>
<blockquote><p>
<b>d</b>elete <b>t</b>ill comma, (go) <b>t</b>ill closing parenthesis, <b>p</b>aste.
</p></blockquote><p>This fixes one line. Automatically. Make it a macro (wrap in <code class="docutils literal">qq</code> … <code class="docutils literal">q</code>,
use with <code class="docutils literal">&#64;q</code>) and now you can run it on all lines, either by moving manually or by
searching for <code class="docutils literal">,</code> and pressing <code class="docutils literal">n&#64;q</code> until you run out of lines.</p>
</section>
<section id="epilogue">
<h1>Epilogue</h1>
<p>Some of you might say “but VS/Eclipse/IDEA has an option for that somewhere” or
“[expensive tool] can do that” — and a Google search shows that there is an
Eclipse plugin to swap arguments and that I could also write a regex to solve
my second issue. Nevertheless, Python is a great tool in a programmer’s toolbox
— especially the interactive interpreter. And Vim is an awesome editor that can
accomplish magic in a few keystrokes — and there are many more things you can
do with it.</p>
<p class="lead">Go learn <a class="reference external" href="https://www.python.org/">Python</a> and <a class="reference external" href="http://www.vim.org/">Vim</a> now.</p>
<p>Also: don’t even bother with VsVim or IdeaVim or any other Vim emulation
plugins, they work in unusual ways and often don’t give you everything — for
example, VsVim has a Vim visual mode (<code class="docutils literal">v</code> key) and Visual Studio selection
mode (mouse), and only one allows Vim keystrokes (the other will replace
selected text).</p>
</section>
]]></content:encoded><category>Programming</category><category>C#</category><category>Java</category><category>programming</category><category>Python</category><category>Vim</category></item></channel></rss>