<commit_before>//
// Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
// that can be found in the license/LICENSE.txt file.
//

import UIKit
import arithmeticParser

class ViewController: UIViewController, UITextViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        numpad.dataSource = self
        self.input.delegate = self
        inputDidChange()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet var partialResult: UILabel!
    @IBOutlet var result: UILabel!
    @IBOutlet var input: UITextView!
    @IBOutlet var numpad: UICollectionView!

    private let parser = PartialParser(composer: Calculator(), partialComposer: PartialRenderer())

    @IBAction func numpadButtonPressed(_ sender: UIButton) {
        let title = sender.currentTitle!
        if title == "" {
            return
        }

        if title == "⌫" {
            if !input.text.isEmpty {
                input.text.removeLast()
            }
        } else {
            input.text.append(title)
        }

        inputDidChange()
    }

    func textViewDidChange(_ textView: UITextView) {
        if textView === input {
            inputDidChange()
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.input.endEditing(true)
    }

    private func inputDidChange() {
        let parsed = parser.parseWithPartial(expression: input.text)
        if let resultValue = parsed.expression {
            result.text = "= \(resultValue)"
        } else {
            result.text = ""
        }

        let attributedText = parsed.partialExpression as! NSAttributedString

        if let remainder = parsed.remainder {
            partialResult.attributedText = attributedText +
                NSAttributedString(string: "    ") +
                NSAttributedString(string: remainder, attributes:
                    [.foregroundColor: UIColor.red,
                     .font: UIFont.boldSystemFont(ofSize: partialResult.font.pointSize)])
        } else {
            partialResult.attributedText = attributedText
        }
    }

    private let buttons = [
        "7", "8", "9", "/",
        "4", "5", "6", "*",
        "1", "2", "3", "-",
        ".", "0", "", "+",
        "(", ")", "", "⌫"
    ]

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {<commit_msg>Fix calculator sample after enabling -Xobjc-generics (#3787)<commit_after>//
// Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
// that can be found in the license/LICENSE.txt file.
//

import UIKit
import arithmeticParser

class ViewController: UIViewController, UITextViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        numpad.dataSource = self
        self.input.delegate = self
        inputDidChange()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet var partialResult: UILabel!
    @IBOutlet var result: UILabel!
    @IBOutlet var input: UITextView!
    @IBOutlet var numpad: UICollectionView!

    private let parser = PartialParser<KotlinDouble, NSAttributedString>(
        composer: Calculator(),
        partialComposer: PartialRenderer()
    )

    @IBAction func numpadButtonPressed(_ sender: UIButton) {
        let title = sender.currentTitle!
        if title == "" {
            return
        }

        if title == "⌫" {
            if !input.text.isEmpty {
                input.text.removeLast()
            }
        } else {
            input.text.append(title)
        }

        inputDidChange()
    }

    func textViewDidChange(_ textView: UITextView) {
        if textView === input {
            inputDidChange()
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.input.endEditing(true)
    }

    private func inputDidChange() {
        let parsed = parser.parseWithPartial(expression: input.text)
        if let resultValue = parsed.expression {
            result.text = "= \(resultValue)"
        } else {
            result.text = ""
        }

        let attributedText = parsed.partialExpression

        if let remainder = parsed.remainder {
            partialResult.attributedText = attributedText +
                NSAttributedString(string: "    ") +
                NSAttributedString(string: remainder, attributes:
                    [.foregroundColor: UIColor.red,
                     .font: UIFont.boldSystemFont(ofSize: partialResult.font.pointSize)])
        } else {
            partialResult.attributedText = attributedText
        }
    }

    private let buttons = [
        "7", "8", "9", "/",
        "4", "5", "6", "*",
        "1", "2", "3", "-",
        ".", "0", "", "+",
        "(", ")", "", "⌫"
    ]

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
{'fraction_non_alphanumeric': 0.10583671961581086, 'fraction_numerical': 0.008127077946065755, 'mean_word_length': 2.1300578034682083, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '6437085', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1477, 'n_words': 380}