Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Last week at work our project inherited a lot of Python code from another project. Unfortunately the code did not fit into our existing architecture - it was all done with global variables and functions, which would not work in a multi-threaded environment. </p> <p>We had ~80 files that needed to be reworked to be object oriented - all the functions moved into classes, parameters changed, import statements added, etc. We had a list of about 20 types of fix that needed to be done to each file. I would estimate that doing it by hand one person could do maybe 2-4 per day.</p> <p>So I did the first one by hand and then wrote a vim script to automate the changes. Most of it was a list of vim commands e.g.</p> <pre><code>" delete an un-needed function " g/someFunction(/ d " add wibble parameter to function foo " %s/foo(/foo( wibble,/ " convert all function calls bar(thing) into method calls thing.bar() " g/bar(/ normal nmaf(ldi(`aPa. </code></pre> <p>The last one deserves a bit of explanation:</p> <pre><code>g/bar(/ executes the following command on every line that contains "bar(" normal execute the following text as if it was typed in in normal mode n goes to the next match of "bar(" (since the :g command leaves the cursor position at the start of the line) ma saves the cursor position in mark a f( moves forward to the next opening bracket l moves right one character, so the cursor is now inside the brackets di( delete all the text inside the brackets `a go back to the position saved as mark a (i.e. the first character of "bar") P paste the deleted text before the current cursor position a. go into insert mode and add a "." </code></pre> <p>For a couple of more complex transformations such as generating all the import statements I embedded some python into the vim script.</p> <p>After a few hours of working on it I had a script that will do at least 95% of the conversion. I just open a file in vim then run <code>:source fixit.vim</code> and the file is transformed in a blink of the eye.</p> <p>We still have the work of changing the remaining 5% that was not worth automating and of testing the results, but by spending a day writing this script I estimate we have saved weeks of work.</p> <p>Of course it would have been possible to automate this with a scripting language like Python or Ruby, but it would have taken far longer to write and would be less flexible - the last example would have been difficult since regex alone would not be able to handle nested brackets, e.g. to convert <code>bar(foo(xxx))</code> to <code>foo(xxx).bar()</code>. Vim was perfect for the task.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload