Thursday, January 26, 2017

editorconfig

editorconfig

editorconfig

I recently came across editorconfig while working on patches for Mesa. One thing I had noticed was that the file I was modifying was using inconsistent indentation (tabs vs. spaces) and it just happened to be in the lines that I modified, so I wanted to first make it consistent and then make the changes.

When I posted the patches I also included a vim modeline that will instruct vim to use specific settings for a that particular file. That has of course several disadvantages:

  1. It only works for vim, or editors that understand that modeline format
  2. Each file requires a duplicate of the modeline

There are various attempts to work around 2. using insecure vim options. And apparently there is a way to achieve this in Emacs. Given that I don't use Emacs, and evidently other Mesa developers don't either, that left me with no options.

The flaw in using these modelines was pointed out during review. Now there are a couple of vim modelines in Mesa code, so it had seemed like the best option at first, but it's obviously not a viable option to add modelines to each and every file of the huge code base that Mesa is. Also, a solution like that would have to be duplicated for other types of modelines.

Enter editorconfig

An interesting hint was dropped during review, though. The Mesa git repository contains a file named .editorconfig in the top-level directory. This is in a format specified by the editorconfig project.

The format is very simple and well documented on the above website. In a nutshell it allows you to specify coding style options that are applied to a set of files selected by a glob pattern.

Coding style is about more than just indentation, but indentation is certainly the easiest to leave up to the editor. Other aspects of the coding style are easy to adhere to when writing code (curly bracket placement, spaces around operators, ...). editorconfig focusses mainly on getting the indentation and whitespace usage right.

The best part is that editorconfig is supported by a number of editors. Some have built-in support and for all others it's likely that you can add support using one of the plugins that you can find on the editorconfig website.

For vim, the plugin to use is editorconfig-vim and which is available in many distributions. Debian has a package called vim-editorconfig and it is also available to Arch Linux users via the editorconfig-vim package in the AUR.

Again, the editorconfig website has links to many, many more plugins, so go look there if you want to try it out on your favorite editor.

Spread the word

Inconsistent indentation is one of the more annoying parts of open source software development [1]. I was quite surprised that I hadn't heard about the editorconfig project before, after all it's been around for half a decade.

If you've never heard of it before either, please help spread the word [2].

The world needs more editorconfig.


[1]... and software development in general.
[2]Please also help spread the word if you've heard of it before.