<commit_before>//
//  StaticTableViewController.swift
//  BFWControls
//
//  Created by <NAME> on 29/05/2016.
//  Copyright © 2016 BareFeetWare.
//  Free to use at your own risk, with acknowledgement to BareFeetWare.
//

import UIKit

open class StaticTableViewController: UITableViewController {
    
    // MARK: - Variables
    
    @IBInspectable open var filledUsingLastCell: Bool = false
    @IBInspectable open var intrinsicHeightCells: Bool = false
    
    fileprivate var isDynamicLastCell: Bool = false
    fileprivate var needRefreshDynamicLastCellHeight: Bool = true
    fileprivate var dynamicLastCellHeight: CGFloat?
    fileprivate var previousRowFrame = CGRect()
    fileprivate var shouldCallHeightForRow = true
    
    /// Override in subclass, usually by connecting to an IBOutlet collection.
    open var excludedCells: [UITableViewCell]? {
        return nil
    }
    
    // TODO: Move to UITableView?
    open var lastCellIndexPath: IndexPath {
        let lastSection = numberOfSections(in: tableView) - 1
        let lastRow = self.tableView(tableView, numberOfRowsInSection: lastSection) - 1
        let indexPath = IndexPath(row: lastRow, section: lastSection)
        return indexPath
    }
    
    open lazy var lastCellIntrinsicHeight: CGFloat = {
        var height = self.tableView.estimatedRowHeight
        if let cell = self.tableView.cellForRow(at: self.lastCellIndexPath) {
            // Already on screen or static table view controller.
            cell.layoutIfNeeded()
            height = cell.frame.height
        } else {
            // Hasn't loaded yet in cellForRowAt, calculate height after tableView is fully loaded.
            self.isDynamicLastCell = true
        }
        return height
    }()
    
    fileprivate var secondLastCellIndexPath: IndexPath {
        let indexPath = lastCellIndexPath
        let prevIndexPath: IndexPath
        let seconLastRowIndex = indexPath.row - 1
        if seconLastRowIndex < 0 {
            let section = (0 ..< indexPath.section).reversed().first(where: { tableView(tableView, numberOfRowsInSection: $0) > 0 } ) ?? 0
            let row = max(0, tableView(tableView, numberOfRowsInSection: section) - 1)
            prevIndexPath = IndexPath(row: row, section: section)
        } else {
            prevIndexPath = IndexPath(row: seconLastRowIndex, section: indexPath.section)
        }
        return prevIndexPath
    }
    
    fileprivate var secondLastCellRect: CGRect {
        return lastCellIndexPath == secondLastCellIndexPath
            ? .zero
            : tableView.rectForRow(at: secondLastCellIndexPath)
    }
    
    fileprivate var normalisedTableViewHeight: CGFloat {
        if #available(iOS 11.0, *) {
            return tableView.frame.size.height - tableView.safeAreaInsets.top - tableView.safeAreaInsets.bottom
        } else {
            return tableView.frame.size.height - tableView.contentInset.top
        }
    }
    
    // MARK: - Functions
    
    open func indexPaths(toInsert cells: [UITableViewCell]) -> [IndexPath] {
        var indexPaths = [IndexPath]()
        for section in 0 ..< super.numberOfSections(in: tableView) {
            var numberOfExcludedRows = 0
            for row in 0 ..< super.tableView(tableView, numberOfRowsInSection: superSection(forSection: section)) {
                let superIndexPath = IndexPath(row: row, section: section)
                let superCell = super.tableView(tableView, cellForRowAt: superIndexPath)
                if cells.contains(superCell) && tableView.indexPath(for: superCell) == nil {
                    let indexPath = IndexPath(row: row - numberOfExcludedRows, section: section)
                    indexPaths += [indexPath]
                } else if excludedCells?.contains(superCell) ?? false {
                    numberOfExcludedRows += 1
                }
            }
        }
        return indexPaths
    }
    
    // MARK: - Private functions
    
    private func numberOfExcludedSections(beforeSection section: Int) -> Int {
        var numberOfExcludedSections = 0
        for superSection in 0 ..< super.numberOfSections(in: tableView) {
            if !shouldInclude(superSection: superSection) {
                numberOfExcludedSections += 1
            } else if superSection - numberOfExcludedSections == section {
                break
            }
        }
        return numberOfExcludedSections
    }
    
    private func numberOfExcludedRowsInSuperSection(before superIndexPath: IndexPath) -> Int {
        var numberOfExcludedRows = 0
        if let excludedCells = excludedCells {
            for superRow in 0 ..< super.tableView(tableView, numberOfRowsInSection: superIndexPath.section) {
                let superIndexPath = IndexPath(row: superRow, section: superIndexPath.section)
                let cell = super.tableView(tableView, cellForRowAt: superIndexPath)
                if excludedCells.contains(cell) {
                    numberOfExcludedRows += 1
                } else if superRow - numberOfExcludedRows == superIndexPath.row {
                    break
                }
            }
        }<commit_msg>Renamed local variable which had same name as parameter. Was preventing some cells from being excluded.<commit_after>//
//  StaticTableViewController.swift
//  BFWControls
//
//  Created by <NAME> on 29/05/2016.
//  Copyright © 2016 BareFeetWare.
//  Free to use at your own risk, with acknowledgement to BareFeetWare.
//

import UIKit

open class StaticTableViewController: UITableViewController {
    
    // MARK: - Variables
    
    @IBInspectable open var filledUsingLastCell: Bool = false
    @IBInspectable open var intrinsicHeightCells: Bool = false
    
    fileprivate var isDynamicLastCell: Bool = false
    fileprivate var needRefreshDynamicLastCellHeight: Bool = true
    fileprivate var dynamicLastCellHeight: CGFloat?
    fileprivate var previousRowFrame = CGRect()
    fileprivate var shouldCallHeightForRow = true
    
    /// Override in subclass, usually by connecting to an IBOutlet collection.
    open var excludedCells: [UITableViewCell]? {
        return nil
    }
    
    // TODO: Move to UITableView?
    open var lastCellIndexPath: IndexPath {
        let lastSection = numberOfSections(in: tableView) - 1
        let lastRow = self.tableView(tableView, numberOfRowsInSection: lastSection) - 1
        let indexPath = IndexPath(row: lastRow, section: lastSection)
        return indexPath
    }
    
    open lazy var lastCellIntrinsicHeight: CGFloat = {
        var height = self.tableView.estimatedRowHeight
        if let cell = self.tableView.cellForRow(at: self.lastCellIndexPath) {
            // Already on screen or static table view controller.
            cell.layoutIfNeeded()
            height = cell.frame.height
        } else {
            // Hasn't loaded yet in cellForRowAt, calculate height after tableView is fully loaded.
            self.isDynamicLastCell = true
        }
        return height
    }()
    
    fileprivate var secondLastCellIndexPath: IndexPath {
        let indexPath = lastCellIndexPath
        let prevIndexPath: IndexPath
        let seconLastRowIndex = indexPath.row - 1
        if seconLastRowIndex < 0 {
            let section = (0 ..< indexPath.section).reversed().first(where: { tableView(tableView, numberOfRowsInSection: $0) > 0 } ) ?? 0
            let row = max(0, tableView(tableView, numberOfRowsInSection: section) - 1)
            prevIndexPath = IndexPath(row: row, section: section)
        } else {
            prevIndexPath = IndexPath(row: seconLastRowIndex, section: indexPath.section)
        }
        return prevIndexPath
    }
    
    fileprivate var secondLastCellRect: CGRect {
        return lastCellIndexPath == secondLastCellIndexPath
            ? .zero
            : tableView.rectForRow(at: secondLastCellIndexPath)
    }
    
    fileprivate var normalisedTableViewHeight: CGFloat {
        if #available(iOS 11.0, *) {
            return tableView.frame.size.height - tableView.safeAreaInsets.top - tableView.safeAreaInsets.bottom
        } else {
            return tableView.frame.size.height - tableView.contentInset.top
        }
    }
    
    // MARK: - Functions
    
    open func indexPaths(toInsert cells: [UITableViewCell]) -> [IndexPath] {
        var indexPaths = [IndexPath]()
        for section in 0 ..< super.numberOfSections(in: tableView) {
            var numberOfExcludedRows = 0
            for row in 0 ..< super.tableView(tableView, numberOfRowsInSection: superSection(forSection: section)) {
                let superIndexPath = IndexPath(row: row, section: section)
                let superCell = super.tableView(tableView, cellForRowAt: superIndexPath)
                if cells.contains(superCell) && tableView.indexPath(for: superCell) == nil {
                    let indexPath = IndexPath(row: row - numberOfExcludedRows, section: section)
                    indexPaths += [indexPath]
                } else if excludedCells?.contains(superCell) ?? false {
                    numberOfExcludedRows += 1
                }
            }
        }
        return indexPaths
    }
    
    // MARK: - Private functions
    
    private func numberOfExcludedSections(beforeSection section: Int) -> Int {
        var numberOfExcludedSections = 0
        for superSection in 0 ..< super.numberOfSections(in: tableView) {
            if !shouldInclude(superSection: superSection) {
                numberOfExcludedSections += 1
            } else if superSection - numberOfExcludedSections == section {
                break
            }
        }
        return numberOfExcludedSections
    }
    
    private func numberOfExcludedRowsInSuperSection(before superIndexPath: IndexPath) -> Int {
        var numberOfExcludedRows = 0
        if let excludedCells = excludedCells {
            for superRow in 0 ..< super.tableView(tableView, numberOfRowsInSection: superIndexPath.section) {
                let steppedSuperIndexPath = IndexPath(row: superRow, section: superIndexPath.section)
                let cell = super.tableView(tableView, cellForRowAt: steppedSuperIndexPath)
                if excludedCells.contains(cell) {
                    numberOfExcludedRows += 1
                } else if superRow - numberOfExcludedRows == superIndexPath.row {
                    break
                }
            }
        }
{'fraction_non_alphanumeric': 0.07546082949308756, 'fraction_numerical': 0.006720430107526882, 'mean_word_length': 2.2573483427141965, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, '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': '375703', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2732, 'n_words': 721}