Fix skipping of files when using --stdinpath

This commit is contained in:
Nick Lockwood
2021-05-22 21:53:06 +01:00
parent 22794843e9
commit a248a8dcb0
3 changed files with 26 additions and 24 deletions
+6 -2
View File
@@ -622,9 +622,11 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
try serializeOptions(options, to: outputURL)
} else {
printRunningMessage()
var shouldSkip = false
if let stdinURL = options.formatOptions?.fileInfo.filePath.map(URL.init(fileURLWithPath:)) {
do {
try gatherOptions(&options, for: stdinURL, with: { print($0, as: .info) })
shouldSkip = options.shouldSkipFile(stdinURL)
} catch {
if printWarnings([error]) {
status = .finished(error)
@@ -632,8 +634,10 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
}
}
}
let output = try applyRules(input, options: options, lineRange: lineRange,
verbose: verbose, lint: lint, reporter: reporter)
let output = shouldSkip ? input : try applyRules(
input, options: options, lineRange: lineRange,
verbose: verbose, lint: lint, reporter: reporter
)
if let outputURL = outputURL, !useStdout {
if (try? String(contentsOf: outputURL)) != output, !dryrun {
do {
+18
View File
@@ -577,6 +577,20 @@ public struct FileOptions {
self.unexcludedGlobs = unexcludedGlobs
self.minVersion = minVersion
}
public func shouldSkipFile(_ inputURL: URL) -> Bool {
let path = inputURL.standardizedFileURL.path
for excluded in excludedGlobs {
guard excluded.matches(path) else {
continue
}
if unexcludedGlobs.contains(where: { $0.matches(path) }) {
return false
}
return true
}
return false
}
}
/// All options
@@ -603,4 +617,8 @@ public struct Options {
self.rules = rules
self.lint = lint
}
public func shouldSkipFile(_ inputURL: URL) -> Bool {
return fileOptions?.shouldSkipFile(inputURL) ?? false
}
}
+2 -22
View File
@@ -116,7 +116,7 @@ public func enumerateFiles(withInputURL inputURL: URL,
let queue = concurrent ? DispatchQueue.global(qos: .userInitiated) : completionQueue
func wasSkipped(_ inputURL: URL, with options: Options) -> Bool {
guard shouldSkipFile(inputURL, with: options) else {
guard options.shouldSkipFile(inputURL) else {
return false
}
if let handler = skipped {
@@ -275,33 +275,13 @@ func gatherOptions(_ options: inout Options, for inputURL: URL, with logger: Log
var directory = URL(fileURLWithPath: inputURL.pathComponents[0]).standardized
for part in inputURL.pathComponents.dropFirst().dropLast() {
directory.appendPathComponent(part)
if shouldSkipFile(directory, with: options) {
if options.shouldSkipFile(directory) {
return
}
try processDirectory(directory, with: &options, logger: logger)
}
}
// Determine if file should be skipped
private func shouldSkipFile(_ inputURL: URL, with options: Options) -> Bool {
guard let excludedGlobs = options.fileOptions?.excludedGlobs else {
return false
}
let path = inputURL.standardizedFileURL.path
for excluded in excludedGlobs {
guard excluded.matches(path) else {
continue
}
if let unexcluded = options.fileOptions?.unexcludedGlobs,
unexcluded.contains(where: { $0.matches(path) })
{
return false
}
return true
}
return false
}
// Process configuration files in specified directory.
private var configCache = [URL: [String: String]]()
private let configQueue = DispatchQueue(label: "swiftformat.config", qos: .userInteractive)