nicklockwood / SwiftFormat

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

Broken indentation with compiler flags #1439

Closed honghaoz closed 1 year ago

honghaoz commented 1 year ago

The following code indentation is broken:

  LoadingSpinner(
    style: style,
    color: color
  )
  #if DEBUG
    .id("LoadingSpinnerToast")
  #else
  .id("lstoast")
  #endif
    .shape(.rectangle(12))
    .background(Color(white: 0, alpha: 0.75))

Would expect to be:

  LoadingSpinner(
    style: style,
    color: color
  )
  #if DEBUG
  .id("LoadingSpinnerToast")
  #else
  .id("lstoast")
  #endif
  .shape(.rectangle(12))
  .background(Color(white: 0, alpha: 0.75))

I believe this is an old problem for a long time. Not related to Swift 5.8 or SwiftFormat 0.51.7

nicklockwood commented 1 year ago

@honghaoz fixed in 0.51.8

honghaoz commented 1 year ago

Thanks for fixing for the example above. However, it broke the following code:

// original code
private func makeNode() -> LayoutNode {
  guard someFlag else {
    return Empty()
  }

  #if os(macOS)
  return ClearNode()
    .onEvent(
      mouseEntered: { [weak self] touchingView, event in
        self.isHovering = true
        return true
      },
      mouseExited: { [weak self] touchingView, event in
        self.isHovering = false
        return true
      },
    )
  #endif
}
// badly formatted code
private func makeNode() -> LayoutNode {
  guard someFlag else {
    return Empty()
  }

  #if os(macOS)
  return ClearNode()
    .onEvent(
  mouseEntered: { [weak self] touchingView, event in // <-- this line is broken
        self.isHovering = true
        return true
      },
      mouseExited: { [weak self] touchingView, event in
        self.isHovering = false
        return true
      },
    )
  #endif
}
// removing the guard let on top works well
private func makeNode() -> LayoutNode {
  #if os(macOS)
  return ClearNode()
    .onEvent(
      mouseEntered: { [weak self] touchingView, event in
        self.isHovering = true
        return true
      },
      mouseExited: { [weak self] touchingView, event in
        self.isHovering = false
        return true
      },
    )
  #endif
}
honghaoz commented 1 year ago

Related to https://github.com/nicklockwood/SwiftFormat/issues/1440

nicklockwood commented 1 year ago

@honghaoz this is also fixed by the fix for https://github.com/nicklockwood/SwiftFormat/issues/1440

CyonAlexRDX commented 1 year ago

New release? :)