<commit_before>//
//  AutoCorrectCommand.swift
//  SwiftLint
//
//  Created by <NAME> on 12/5/15.
//  Copyright © 2015 Realm. All rights reserved.
//

import Commandant
import Result
import SwiftLintFramework

struct AutoCorrectCommand: CommandProtocol {
    let verb = "autocorrect"
    let function = "Automatically correct warnings and errors"

    func run(_ options: AutoCorrectOptions) -> Result<(), CommandantError<()>> {
        let configuration = Configuration(options: options)
        let cache = options.ignoreCache ? nil : LinterCache(configuration: configuration)

        return configuration.visitLintableFiles(path: options.path, action: "Correcting",
                                                quiet: options.quiet, useScriptInputFiles: options.useScriptInputFiles,
                                                cache: cache) { linter in
            let corrections = linter.correct()
            if !corrections.isEmpty && !options.quiet {
                let correctionLogs = corrections.map({ $0.consoleDescription })
                queuedPrint(correctionLogs.joined(separator:"\n"))
            }
            if options.format {
                let formattedContents = linter.file.format(trimmingTrailingWhitespace: true,
                                                           useTabs: false, indentWidth: 4)
                _ = try? formattedContents
                    .write(toFile: linter.file.path!, atomically: true, encoding: .utf8)
            }
        }.flatMap { files in
            if !options.quiet {
                queuedPrintError("Done correcting \(files.count) files!")
            }
            return .success()
        }
    }
}

struct AutoCorrectOptions: OptionsProtocol {
    let path: String
    let configurationFile: String
    let useScriptInputFiles: Bool
    let quiet: Bool
    let format: Bool
    let cachePath: String
    let ignoreCache: Bool

    // swiftlint:disable line_length
    static func create(_ path: String) -> (_ configurationFile: String) -> (_ useScriptInputFiles: Bool) -> (_ quiet: Bool) -> (_ format: Bool) -> (_ cachePath: String) -> (_ ignoreCache: Bool) -> AutoCorrectOptions {
        return { configurationFile in { useScriptInputFiles in { quiet in { format in { cachePath in { ignoreCache in
            self.init(path: path, configurationFile: configurationFile, useScriptInputFiles: useScriptInputFiles, quiet: quiet, format: format, cachePath: cachePath, ignoreCache: ignoreCache)
        }}}}}}
    }

    static func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>> {
        // swiftlint:enable line_length
        return create
            <*> mode <| pathOption(action: "correct")
            <*> mode <| configOption
            <*> mode <| useScriptInputFilesOption
            <*> mode <| quietOption(action: "correcting")
            <*> mode <| Option(key: "format",
                               defaultValue: false,
                               usage: "should reformat the Swift files")
            <*> mode <| Option(key: "cache-path", defaultValue: "",
                               usage: "the directory of the cache used when correcting")
            <*> mode <| Option(key: "no-cache", defaultValue: false,
                               usage: "ignore cache when correcting")
    }
}<commit_msg>Added `use-tabs` option for `swiftlint autocorrect`<commit_after>//
//  AutoCorrectCommand.swift
//  SwiftLint
//
//  Created by <NAME> on 12/5/15.
//  Copyright © 2015 Realm. All rights reserved.
//

import Commandant
import Result
import SwiftLintFramework

struct AutoCorrectCommand: CommandProtocol {
    let verb = "autocorrect"
    let function = "Automatically correct warnings and errors"

    func run(_ options: AutoCorrectOptions) -> Result<(), CommandantError<()>> {
        let configuration = Configuration(options: options)
        let cache = options.ignoreCache ? nil : LinterCache(configuration: configuration)

        return configuration.visitLintableFiles(path: options.path, action: "Correcting",
                                                quiet: options.quiet, useScriptInputFiles: options.useScriptInputFiles,
                                                cache: cache) { linter in
            let corrections = linter.correct()
            if !corrections.isEmpty && !options.quiet {
                let correctionLogs = corrections.map({ $0.consoleDescription })
                queuedPrint(correctionLogs.joined(separator:"\n"))
            }
            if options.format {
                let formattedContents = linter.file.format(trimmingTrailingWhitespace: true,
                                                           useTabs: options.useTabs, indentWidth: 4)
                _ = try? formattedContents
                    .write(toFile: linter.file.path!, atomically: true, encoding: .utf8)
            }
        }.flatMap { files in
            if !options.quiet {
                queuedPrintError("Done correcting \(files.count) files!")
            }
            return .success()
        }
    }
}

struct AutoCorrectOptions: OptionsProtocol {
    let path: String
    let configurationFile: String
    let useScriptInputFiles: Bool
    let quiet: Bool
    let format: Bool
    let cachePath: String
    let ignoreCache: Bool
    let useTabs: Bool

    // swiftlint:disable line_length
    static func create(_ path: String) -> (_ configurationFile: String) -> (_ useScriptInputFiles: Bool) -> (_ quiet: Bool) -> (_ format: Bool) -> (_ cachePath: String) -> (_ ignoreCache: Bool) -> (_ useTabs: Bool) -> AutoCorrectOptions {
        return { configurationFile in { useScriptInputFiles in { quiet in { format in { cachePath in { ignoreCache in { useTabs in
            self.init(path: path, configurationFile: configurationFile, useScriptInputFiles: useScriptInputFiles, quiet: quiet, format: format, cachePath: cachePath, ignoreCache: ignoreCache, useTabs: useTabs)
        }}}}}}}
    }

    static func evaluate(_ mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<CommandantError<()>>> {
        // swiftlint:enable line_length
        return create
            <*> mode <| pathOption(action: "correct")
            <*> mode <| configOption
            <*> mode <| useScriptInputFilesOption
            <*> mode <| quietOption(action: "correcting")
            <*> mode <| Option(key: "format",
                               defaultValue: false,
                               usage: "should reformat the Swift files")
            <*> mode <| Option(key: "cache-path", defaultValue: "",
                               usage: "the directory of the cache used when correcting")
            <*> mode <| Option(key: "no-cache", defaultValue: false,
                               usage: "ignore cache when correcting")
            <*> mode <| Option(key: "use-tabs",
                               defaultValue: false,
                               usage: "should use tabs over spaces when reformatting")
    }
}
{'fraction_non_alphanumeric': 0.09982959386537915, 'fraction_numerical': 0.003408122692416927, 'mean_word_length': 2.0876808417360806, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556315', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1698, 'n_words': 485}