<commit_before>//
//  StandardKeyboardActionHandler.swift
//  KeyboardKit
//
//  Created by <NAME> on 2019-04-24.
//  Copyright © 2019 <NAME>. All rights reserved.
//

/*
 
 This action handler is used by all `KeyboardViewController`
 instances by default, if you don't explicitly decide to use
 another one.
 
 This action handler handles all keyboard actions except the
 `.image` action, which you must handle manually since there
 is no way to send images to the text proxy.
 
 You can adjust how this class handles taps and long presses
 on keyboard actions, by overriding the `handleTap(on:)` and
 `handleLongPress(on:)` functions. You can also override the
 `tapAction(for:)` and `longPressAction(for:)` to completely
 change which action blocks to return for a keyboard action.
 
 You can enable haptic feedback by providing different types
 of haptic feedback for taps and long presses. To change the
 haptic feedback for a specific action, you can override the
 `giveHapticFeedbackForTap/LongPress(on:)` functions as well.
 
 */

import UIKit

open class StandardKeyboardActionHandler: KeyboardActionHandler {
    
    
    // MARK: - Initialization
    
    public init(
        inputViewController: UIInputViewController,
        tapHapticFeedback: HapticFeedback = .none,
        longPressHapticFeedback: HapticFeedback = .none) {
        self.inputViewController = inputViewController
        self.tapHapticFeedback = tapHapticFeedback
        self.longPressHapticFeedback = longPressHapticFeedback
    }
    
    
    // MARK: - Dependencies
    
    public weak var inputViewController: UIInputViewController?
    
    
    // MARK: - Properties
    
    private let tapHapticFeedback: HapticFeedback
    private let longPressHapticFeedback: HapticFeedback
    
    public var textDocumentProxy: UITextDocumentProxy? {
        return inputViewController?.textDocumentProxy
    }
    
    
    // MARK: - Handling Functions
    
    open func handleTap(on view: UIView, action: KeyboardAction) {
        guard let tapAction = tapAction(for: view, action: action) else { return }
        giveHapticFeedbackForTap(on: action)
        tapAction()
    }
    
    open func handleLongPress(on view: UIView, action: KeyboardAction) {
        guard let longPressAction = longPressAction(for: view, action: action) else { return }
        giveHapticFeedbackForLongPress(on: action)
        longPressAction()
    }
    
    
    // MARK: - Haptic Functions
    
    open func giveHapticFeedbackForTap(on action: KeyboardAction) {
        tapHapticFeedback.trigger()
    }
    
    open func giveHapticFeedbackForLongPress(on action: KeyboardAction) {
        longPressHapticFeedback.trigger()
    }
    
    
    // MARK: - Action Functions
    
    open func tapAction(for view: UIView, action: KeyboardAction) -> (() -> ())? {
        switch action {
        case .none: return nil
        case .backspace: return { [weak self] in self?.textDocumentProxy?.deleteBackward() }
        case .dismissKeyboard: return { [weak self] in self?.inputViewController?.dismissKeyboard() }
        case .character(let char): return { [weak self] in self?.textDocumentProxy?.insertText(char) }
        case .image: return nil
        case .moveCursorBack: return { [weak self] in self?.textDocumentProxy?.adjustTextPosition(byCharacterOffset: -1) }
        case .moveCursorForward: return { [weak self] in self?.textDocumentProxy?.adjustTextPosition(byCharacterOffset: -1) }
        case .newLine: return { [weak self] in self?.textDocumentProxy?.insertText("\n") }
        case .shift: return nil
        case .space: return { [weak self] in self?.textDocumentProxy?.insertText(" ") }
        case .switchKeyboard: return nil
        case .switchToNumericKeyboard: return nil
        }
    }
    
    open func longPressAction(for view: UIView, action: KeyboardAction) -> (() -> ())? {
        return nil
    }
}
<commit_msg>Use standard actions in standard action handler<commit_after>//
//  StandardKeyboardActionHandler.swift
//  KeyboardKit
//
//  Created by <NAME> on 2019-04-24.
//  Copyright © 2019 <NAME>. All rights reserved.
//

/*
 
 This action handler is used by all `KeyboardViewController`
 instances by default, if you don't explicitly decide to use
 another one. You do this by setting `keyboardActionHandler`
 to a custom action handler.
 
 This action handler uses the default action blocks for each
 keyboard action, if any, as the default tap action. You can
 adjust this behavior by subclassing this class and override
 `tapAction(for:)` and `longPressAction(for:)` to change the
 action blocks to use for each keyboard action. You can also
 override `handleTap(on:)` and `handleLongPress(on:)` if you
 only want to adjust the handling of the various actions.
 
 You can enable haptic feedback by providing haptic feedback
 types for taps and long presses when you create an instance
 of this class. You can also adjust the standard behavior by
 overriding the two `giveHapticFeedback` functions.
 
 */

import UIKit

open class StandardKeyboardActionHandler: KeyboardActionHandler {
    
    
    // MARK: - Initialization
    
    public init(
        inputViewController: UIInputViewController,
        tapHapticFeedback: HapticFeedback = .none,
        longPressHapticFeedback: HapticFeedback = .none) {
        self.inputViewController = inputViewController
        self.tapHapticFeedback = tapHapticFeedback
        self.longPressHapticFeedback = longPressHapticFeedback
    }
    
    
    // MARK: - Dependencies
    
    public weak var inputViewController: UIInputViewController?
    
    
    // MARK: - Properties
    
    private let tapHapticFeedback: HapticFeedback
    private let longPressHapticFeedback: HapticFeedback
    
    public var textDocumentProxy: UITextDocumentProxy? {
        return inputViewController?.textDocumentProxy
    }
    
    
    // MARK: - Action Functions
    
    open func tapAction(for view: UIView, action: KeyboardAction) -> (() -> ())? {
        if let inputAction = action.standardInputViewControllerAction {
            return { inputAction(self.inputViewController) }
        }
        if let proxyAction = action.standardTextDocumentProxyAction {
            return { proxyAction(self.textDocumentProxy) }
        }
        return nil
    }
    
    open func longPressAction(for view: UIView, action: KeyboardAction) -> (() -> ())? {
        return nil
    }
    
    
    // MARK: - Handling Functions
    
    open func handleTap(on view: UIView, action: KeyboardAction) {
        guard let tapAction = tapAction(for: view, action: action) else { return }
        giveHapticFeedbackForTap(on: action)
        tapAction()
    }
    
    open func handleLongPress(on view: UIView, action: KeyboardAction) {
        guard let longPressAction = longPressAction(for: view, action: action) else { return }
        giveHapticFeedbackForLongPress(on: action)
        longPressAction()
    }
    
    
    // MARK: - Haptic Functions
    
    open func giveHapticFeedbackForTap(on action: KeyboardAction) {
        tapHapticFeedback.trigger()
    }
    
    open func giveHapticFeedbackForLongPress(on action: KeyboardAction) {
        longPressHapticFeedback.trigger()
    }
}

{'fraction_non_alphanumeric': 0.07786828662156565, 'fraction_numerical': 0.003589672787518984, 'mean_word_length': 2.9563080283997816, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624930', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1931, 'n_words': 621}