nicklockwood / SwiftFormat

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

a case report: CIVector.init #313

Closed metasmile closed 6 years ago

metasmile commented 6 years ago

This tool converts from

CIVector.init(float3x3: matrix)

to

CIVector(float3x3: matrix)

result:

Error:(178, 13) argument labels '(float3x3:)' do not match any available overloads
metasmile commented 6 years ago

and this is only for Swift4.2 as you know

nicklockwood commented 6 years ago

@metasmile I'm a bit confused about this one, as these should be equivalent.

Does CIVector have a static func called init or something? I'm not sure if this a bug in SwiftFormat or some sort of weird edge-case.

In any case, you can fix it using:

// swiftformat:disable:next redundantInit
CIVector.init(float3x3: matrix)
nicklockwood commented 6 years ago

@metasmile any update on this?

metasmile commented 6 years ago

😅Oh Sorry, I've forgotten it. CIVector.init(float3x3: matrix) was an extension in my chaos-big codebase. But still I think every user can always useinit extension. so we should find some smarter way for detecting the relationship with extensions.

btw, closed this issue at first.

metasmile commented 6 years ago

@metasmile any update on this?

So anyway, is "SwiftFormatter" is real "formatter"? extension relationship is essential.

nicklockwood commented 6 years ago

@metasmile SwiftFormat is confused by the use of init as a function name, which is not common practice. It doesn't compile your project or look in other files beyond the one it's currently formatting, so doesn't know about extension methods that might have been added in another file.

The default rules that are enabled are ones that work for most projects. If that rule doesn't work for your prject, you can disable it for specific files or lines using a comment, or disable it completely for the whole project by adding --disable redundantInit to the command line or inside a .swiftformat file in your project.

If I can think of a way to automatically disable it for init functions then I'll do that, but I don't think it's possible without radically changing how the formatter works.