From a248a8dcb07022a2ecf2fae679a9c26ba69cd8a6 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Sat, 22 May 2021 13:18:33 +0100 Subject: [PATCH] Fix skipping of files when using --stdinpath --- Sources/CommandLine.swift | 8 ++++++-- Sources/Options.swift | 18 ++++++++++++++++++ Sources/SwiftFormat.swift | 24 ++---------------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Sources/CommandLine.swift b/Sources/CommandLine.swift index 19edc735..7a816baf 100644 --- a/Sources/CommandLine.swift +++ b/Sources/CommandLine.swift @@ -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 { diff --git a/Sources/Options.swift b/Sources/Options.swift index a4fb92c8..477be0ab 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -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 + } } diff --git a/Sources/SwiftFormat.swift b/Sources/SwiftFormat.swift index bbc04f42..eb83b2af 100644 --- a/Sources/SwiftFormat.swift +++ b/Sources/SwiftFormat.swift @@ -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)