swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

Make generated files // swift-format-ignore

Open weissi opened this issue 10 months ago • 9 comments

Description

I have generated files & swift-format. I don't want the formatter to reformat the generated code though. So it'd be awesome if it could generate a line with // swift-format-ignore which will disable formatting for that file.

Reproduction

N/a

Package version(s)

All

Expected behavior

No formatting

Environment

All

Additional information

No response

weissi avatar Feb 15 '25 07:02 weissi

Sounds good.

Are we happy for it to be behind a config option for those that do want to include the generated files in their format?

simonjbeaumont avatar Feb 15 '25 09:02 simonjbeaumont

This is probably the better solution although we could have something in the meanwhile. We use .swiftformatignore in different projects for now, which has turned out to be fine. The formatting script in .github-workflows repo has been configured to use .swiftformatignore. swift-format itself doesn't really care about it.

MahdiBM avatar Feb 15 '25 10:02 MahdiBM

I agree that swift-format would benefit from ignore support in its own config file. You’re right that the reusable GitHub workflows have support for this already, but I don’t think we can expect that all adopters use these. @weissi presumably you don’t use the reusable script since you filed the issue?

simonjbeaumont avatar Feb 15 '25 12:02 simonjbeaumont

I agree that swift-format would benefit from ignore support in its own config file. You’re right that the reusable GitHub workflows have support for this already, but I don’t think we can expect that all adopters use these. @weissi presumably you don’t use the reusable script since you filed the issue?

Correct, not using those. I'm fine with a configuration option to turn on/off // swift-format-ignore comments in gen'd files.

weissi avatar Feb 26 '25 09:02 weissi

That'd be fine with me, I'd just make sure it's not tied to swift-format.

Maybe a "top-level-comments" property, then users can specify "swift-format-ignore". + good docs.

MahdiBM avatar Feb 26 '25 11:02 MahdiBM

@MahdiBM wrote:

That'd be fine with me, I'd just make sure it's not tied to swift-format.

Maybe a "top-level-comments" property, then users can specify "swift-format-ignore". + good docs.

I'm less inclined to make this super generalisable. Seems like an odd thing for folks to be inserting arbitrary things at the top of the generated file and, while I would be sympathetic to not wanting to over-index on one tool if we had an ecosystem of tens of linters, swift-format is now in the Swift toolchain so IMHO we could just add this unilaterally.

Sounds like @FranzBusch agrees (https://github.com/apple/swift-openapi-generator/issues/738#issuecomment-2694512139) and if Swift Protobuf is doing this, then I'm inclined for us to.

What do others think. I know I originally suggested we make this configurable but maybe we should just add the comment(s) unilaterally? Any objections?

simonjbeaumont avatar Mar 04 '25 09:03 simonjbeaumont

@simonjbeaumont

swift-format is now in the Swift toolchain so IMHO we could just add this unilaterally.

I wish swift-format was good enough so one can just use it and not worry about much else, but in its current form it has some pretty bad formatting behaviors so I'm not excited to get the whole ecosystem locked on it. It would have been fine by me if swift-format could actually do its job properly.

See this about possibility of support for other formatters in sourcekit-lsp.

MahdiBM avatar Mar 11 '25 12:03 MahdiBM

If we're really against just hardcoding it, then I guess an additionalFileComments: could live alongside the additionalImports: config file option. Probably a list of strings, each of which becomes its own line comment.

additionalFileComments:
  - "swift-format-ignore-file"
  - "swiftlint:disable all"
  - "mumble mumble mumble"
// swift-format-ignore-file
// swiftlint:disable all
// mumble mumble mumble

We haven't had many requests for it, so as long as there's a way to it, for those that want it, I'm pretty flexible on how.

simonjbeaumont avatar Mar 11 '25 13:03 simonjbeaumont