swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

BasicFormat should automatically determine indentation level of a file

Open bnbarham opened this issue 2 years ago • 7 comments

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.

bnbarham avatar Mar 31 '23 04:03 bnbarham

rdar://107455224

ahoppen avatar Mar 31 '23 04:03 ahoppen

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

ahoppen avatar Mar 31 '23 04:03 ahoppen

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 🤷

bnbarham avatar Mar 31 '23 05:03 bnbarham

Is there something else to do in this issue after the #2514 PR?

Matejkob avatar Apr 05 '24 11:04 Matejkob

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

ahoppen avatar Apr 05 '24 17:04 ahoppen

Do we wanna use this API in BasicFormat?

Matejkob avatar Apr 05 '24 20:04 Matejkob

Yes, that would be nice, I think.

ahoppen avatar Apr 05 '24 20:04 ahoppen