-
Notifications
You must be signed in to change notification settings - Fork 256
Add support for .swift-format-ignore files #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,19 +47,42 @@ public struct FileIterator: Sequence, IteratorProtocol { | |
| /// The file extension to check for when recursing through directories. | ||
| private let fileSuffix = ".swift" | ||
|
|
||
| /// Optional ignore manager for filtering files based on .swift-format-ignore files. | ||
| private let ignoreManager: IgnoreManager | ||
|
|
||
| /// Create a new file iterator over the given list of file URLs. | ||
| /// | ||
| /// The given URLs may be files or directories. If they are directories, the iterator will recurse | ||
| /// into them. Symlinks are never followed on Windows platforms as Foundation doesn't support it. | ||
| /// - Parameters: | ||
| /// - urls: `Array` of files or directories to iterate. | ||
| /// - followSymlinks: `Bool` to indicate if symbolic links should be followed when iterating. | ||
| /// - workingDirectory: `URL` that indicates the current working directory. Used for testing. | ||
| public init(urls: [URL], followSymlinks: Bool, workingDirectory: URL = URL(fileURLWithPath: ".")) { | ||
| /// - followSymlinks: `Bool` to indicate if symbolic links sthe current working directory. Used for testing. | ||
| /// - ignoreManager: Optional `IgnoreManager`hould be followed when iterating. | ||
| /// - workingDirectory: `URL` that indicates to filter files based on .swift-format-ignore files. | ||
| package init( | ||
| urls: [URL], | ||
| followSymlinks: Bool, | ||
| workingDirectory: URL = URL(fileURLWithPath: "."), | ||
| ignoreManager: IgnoreManager | ||
| ) { | ||
| self.workingDirectory = workingDirectory | ||
| self.urls = urls | ||
| self.urlIterator = self.urls.makeIterator() | ||
| self.followSymlinks = followSymlinks | ||
| self.ignoreManager = ignoreManager | ||
| } | ||
|
|
||
| public init( | ||
| urls: [URL], | ||
| followSymlinks: Bool, | ||
| workingDirectory: URL = URL(fileURLWithPath: ".") | ||
| ) { | ||
| self.init( | ||
| urls: urls, | ||
| followSymlinks: followSymlinks, | ||
| workingDirectory: workingDirectory, | ||
| ignoreManager: IgnoreManager() | ||
| ) | ||
| } | ||
|
|
||
| /// Iterate through the "paths" list, and emit the file paths in it. If we encounter a directory, | ||
|
|
@@ -85,6 +108,9 @@ public struct FileIterator: Sequence, IteratorProtocol { | |
| continue | ||
|
|
||
| case .typeDirectory: | ||
| if self.ignoreManager.shouldIgnore(file: next, isDirectory: true) { | ||
| continue | ||
| } | ||
| dirIterator = FileManager.default.enumerator( | ||
| at: next, | ||
| includingPropertiesForKeys: nil, | ||
|
|
@@ -145,7 +171,15 @@ public struct FileIterator: Sequence, IteratorProtocol { | |
| } else { | ||
| relativePath = path | ||
| } | ||
| output = URL(fileURLWithPath: relativePath, isDirectory: false, relativeTo: workingDirectory) | ||
| let fileURL = URL(fileURLWithPath: relativePath, isDirectory: false, relativeTo: workingDirectory) | ||
|
|
||
| // Apply ignore filtering | ||
| if self.ignoreManager.shouldIgnore(file: item, isDirectory: false) { | ||
| output = nil | ||
| continue // Skip this file and continue to next | ||
| } | ||
|
Comment on lines
+176
to
+180
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be checked in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You removed the another check for files in If so, what about the directories? E.g.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a file matches a patter found in A patter in |
||
|
|
||
| output = fileURL | ||
|
|
||
| default: | ||
| break | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then we don't need the additional
init(urls:followSymlinks:workingDirectory:)below. Though I still not sure if this needs to be injectable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept this here in the event we want to have dependency injection testing. I will change the access modifier to
package