BasicFormat should automatically determine indentation level of a file
Description
We could take a pretty good guess on the indentation of the file.
There's a few ways we could do this. One would be to grab the indentation up to content in each line, then divide that by common indents and pick the closest match. This is probably good enough, but we can always get more accurate by eg. skipping multiline strings, or rather than an average, instead look at how many lines fit that indentation.
rdar://107455224
FWIW I filed a similar issue for the compiler a while back and that includes some hacky script to infer indentation. From a file. I’m not claiming that that’s the way we should do it but the pointer might be useful at a starting point https://github.com/apple/swift/issues/44694
From a quick glance I believe that's basically the last part, ie.
instead look at how many lines fit that indentation
Definitely one way to do it! I just figured taking the average is probably good enough and a little faster 🤷
Is there something else to do in this issue after the #2514 PR?
https://github.com/apple/swift-syntax/pull/2514 just adds the API to infer indentation. When creating a BasicFormat object, it still defaults to four spaces of indentation
https://github.com/apple/swift-syntax/blob/1672a3e1b01a7288fdb076bbbf3b34e5f0d70fdf/Sources/SwiftBasicFormat/BasicFormat.swift#L71
Do we wanna use this API in BasicFormat?
Yes, that would be nice, I think.