Steve On Stuff

Fixing Xcode's Comment Wrapping Problem

28 Jun 2018

I’m sure we’ve all come across the comment wrapping problem. You write a long comment that looks like this:

// This is a really long comment that describes something important, but it either goes off the edge of the screen, or Xcode wraps it a the edge of the editor

This comment isn’t that easy to read. There’s a good reason why newspapers write in columns, and websites show articles in a narrow strip in the middle of your screen - It’s hard to read long horizontal text!

It would be better if our comment looked like this:

// This is a really long comment that describes something
// important, but it either goes off the edge of the screen,
// or Xcode wraps it a the edge of the editor

This comment wraps at 60 characters. As long as your monitor can display 60 characters across then it will look the same whether you’re reading on a 12 inch display or a 27 inch one, and it’s much easier to read.

If you take a look at the documentation for the swift standard library, Apple also use this formatting technique

Formatting your own comments like this usually just requires writing the whole comment out first, then just going though and adding the line breaks and the // prefix to each line. A few extra key strokes but no biggie.

But then, inevitably, you have to edit it.

This requires having to undo all of the wrapping, insert the new text, move words to different lines, reinsert the line breaks in new positions. It’s a world of pain.

The Solution

It sounds like we need an automated solution for this! Xcode doesn’t provide one, so I decided to write my own. Xcode Source Editor Extensions (yeah, remember those?) are actually a perfect fit for this.

Simply write out your comment, select ‘wrap’ and:

Wrapping gif

And if you want to edit an already wrapped comment? ‘Re-wrap’ has got you covered:

Rewrapping gif

There’s a few other handy features too:

Check out Comment wrapper on GitHub or download on the app store

Next »


« Previous




I'm Steve Barnegren, an iOS developer based in London. If you want to get in touch: