nicklockwood / SwiftFormat

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

"spaceAroundOperators" is not being applied #1755

Open Ahbee opened 1 month ago

Ahbee commented 1 month ago

Im using the Xcode source editor extension with the default options, However the space around the + and * is not being applied for this input:

import Foundation

func test() -> Double {
  let T = 5.0
  let e = 6.1
  let c0 = 1.0
  let c1 = -1.0
  let p = c0+T*(c1+T)
  return (e / pow(p, 8))*100.0
}

the formatting does nothing but I expect it to look like this:

import Foundation

func test() -> Double {
  let T = 5.0
  let e = 6.1
  let c0 = 1.0
  let c1 = -1.0
  let p = c0 + T * (c1 + T)
  return (e / pow(p, 8)) * 100.0
}
nicklockwood commented 1 month ago

The explanation here is the same as for your other report - in Swift a+ b is not legal code.

In this case since the intent is obvious I can probably add a workaround, but there are other cases like a+ -b that are legal in C but would be ambiguous in Swift.

Sorry, my reading comprehension is not good today 🤦‍♂️ I'm not sure why the formatting isn't being applied here - I will investigate.

nicklockwood commented 1 month ago

@Ahbee I think what's happening here is that by default the SwiftFormat Xcode extension has the "Infer Options Automatically" option enabled, which means that it looks at the existing contents of the file to decide which settings to use. If your file has more unspaced operators than spaced ones, it will assume that's the style you wanted.

Uncheck the "Infer Options Automatically" option in the SwiftFormat for Xcode app and see if that solves it.

Ahbee commented 1 month ago

yeah that seems to solve it thanks! But I feel that option should not be part of the default? I think most new users would just be confused why the code is not formatting?