Extra Cheese

A Blog


Cyclomatic Complexity in Vim: First Steps

Oct 25, 2009

I've been kicking around the idea of integrating complexity analysis with vim. Tonight, I spent a couple hours getting a proof of concept working (for Python code, naturally). Each line gets highlighted based on the complexity of the function: green for low, yellow for medium, red for high.

A static image doesn't really do it justice – the highlighting updates whenever you write a file. There's a 15 second video of me demoing it that's much better than the picture above.

This is based on Dave Stanek's cyclomatic complexity code. It's hacked together and the complexity algorithm seems slightly wrong (unless I'm remembering it incorrectly). However, it does successfully compute the complexity and highlight the code, which is good enough for a blog post. ;)

If you're feeling adventurous, you can find the source code on BitBucket.



Showing 8 comments

Posted by brbd at Mon Oct 26 03:35:07 2009

Looks cool, but how do I enable it?  If I fire up vim with some python source it does not show anything.  I put complexity.vim to the plugin folder, and python support in vim itself is compiled in.


Posted by Gary Bernhardt at Mon Oct 26 12:08:49 2009

You may have to actually :source the file. I've never written an actual vim plugin before, so it's probably not be well-behaved. Hopefully I'll have time to clean this up and release it as a proper plugin in the near future.


Posted by Matt Wilson at Mon Oct 26 12:26:56 2009

All my code is red so it must be broke.

No, just kidding.  This is really neat!  I really like that you figured out how to add colors to the screen like that; going to study how to do that.


Posted by Gary Bernhardt at Mon Oct 26 12:52:30 2009

The basic mechanism is :sign, which places two-character-wide strings in that gutter region on the left. It was originally intended for indicating debugger break points. The script just adds one sign per line, with a different highlight group depending on the line's complexity.


Posted by Kevin Dahlhausen at Mon Oct 26 15:09:11 2009

This is great, I just sourced it and then loaded a python file.  Works great.


...Something like this for dating would have saved a lot of trouble way back when.


Posted by Uldis Bojars at Wed Oct 28 18:20:37 2009

A nice script! :)

But I have a little problem with it: one of Python files I tried it with does not show the complexity - the 2 char wide gutter area does not show up. Other than that it is just a normal Python code file. VIM syntax highlighting works fine on it, etc.

Any idea what could be the problem?


Posted by Uldis Bojars at Wed Oct 28 18:40:12 2009

An interesting modification to this script would be to display test code coverage information (e.g., generated by "trial --coverage" from Twisted framework) alongside the code.


Posted by Gary Bernhardt at Thu Oct 29 00:08:39 2009

Uldis,

If the colors don't show up, that probably means that it couldn't parse the file. One thing to keep in mind is that the script runs within Vim, so it uses whichever version of Python Vim was built with. So, if your code uses Python 2.5 features, but Vim was built with Python 2.4, the script will fail to analyze your code because the 2.4 parser won't understand the 2.5 syntax.


Name:


E-mail:


URL:


Comment: