nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.96k stars 640 forks source link

Indent comments within blocks #219

Open hyperknot opened 6 years ago

hyperknot commented 6 years ago

By default both Xcode and AppCode aligns comments in the wrong place when the Cmd + / is pressed, that is at the start of the line.

class GameViewController: NSViewController {
  var one = 1

  override func viewDidLoad() {
//    super.viewDidLoad()
    print("test")
  }
}

AppCode's built-in reformatter actually supports fixing this, but for a year+ there AC couldn't solve why the default position of those comments are at column 0.

Now with today's SwiftFormat, I couldn't find an option to fix these. I believe fixing this means:

Actually, if I remove all spaces, so it looks like

  override func viewDidLoad() {
//super.viewDidLoad()

SwiftFormat actually beautifully fixes it!

  override func viewDidLoad() {
    // super.viewDidLoad()
    print("test")

So now it just needs to fix this even when the spaces are present.

I believe this should be the default behaviour, in line with every other language I used, but if people disagree it'd be extremely useful to add this as a switch. Right now, I'm actually inserting a single space before all comments by hand, so that SF aligns them correctly.

nicklockwood commented 6 years ago

Previously I was indenting the comment, but preserving the leading whitespace inside the comment, which looked very wrong for deeply indented code. I added an exception for code comments to leave them at the beginning of the line (i.e. to replicate Apple's comment behavior).

The reason for this is commenting-out blocks of code tends to be a temporary action, where you intend to either delete or uncomment the code later. If I mess with the indenting, then if you use the Command-' shortcut to uncomment it again, the indenting will be wrong and you'll have to run SwiftFormat again to fix it.

The example you've given is for a single-line comment, where this is less of an issue. But what about for a block of several lines, containing nested, indented code? Does AppCode do the right thing there? (the "right thing" being to preserve the relative indent between lines, but adjust the overall indent to match the surrounding code).

Ideally, I would like to implement something like this, but it's tricky. I don't want to have to parse code inside comments to determine how it should be indented, because there's no guarantee that code inside a comment is well-formed, or even that something that looks like code is really supposed to be treated as code at all.

The best option is probably to preserve the relative indent inside the commented lines and just apply a fixed offset to that indent for every line in a given block, but I've not attempted that yet.

hyperknot commented 6 years ago

You are right that's a more tricky issue for alignments within comments.

WebStorm (AppCode's JS cousin) is way more polished in this regard. It comments and uncomments like this:

module.exports = {
  setup: function() {
    // viewer.el = {
    //   map: element,
    // }
    viewer.measure = {}

Aligning perfectly and with exactly one space at the nearest line.

Can you not just do the same? That is:

This doesn't need parsing, just a simple checking of the nearest line's value, if I understand it right.

BTW, I disagree with the purpose of comments being "temporary action, where you intend to either delete or uncomment the code later". I use code comments for documentation more than anything, many times showing an alternative way.

For example, I here I'll leave these alternative lines there for debugging purposes in the future, documenting how to switch to a mode which is otherwise rarely used.

metalKitView.depthStencilPixelFormat = MTLPixelFormat.depth32Float_stencil8
metalKitView.colorPixelFormat = MTLPixelFormat.bgra8Unorm_srgb
// metalKitView.colorPixelFormat = MTLPixelFormat.bgra8Unorm
metalKitView.sampleCount = 1

and

let textureLoaderOptions = [
  MTKTextureLoader.Option.textureUsage: MTLTextureUsage.shaderRead.rawValue,
  MTKTextureLoader.Option.textureStorageMode: MTLStorageMode.private.rawValue,
  // MTKTextureLoader.Option.SRGB: true,
] as [MTKTextureLoader.Option: Any]
felixakiragreen commented 3 years ago

3 years later, wishing this was a feature! The way Xcode handles comments is driving me nuts. I tried using the "Comment Here" extension which helps, but still not perfect.