<commit_before>//
//  TableManager.swift
//  Incidents
//
//  © 2015 <NAME> and its contributors. All rights reserved.
//  See the file COPYRIGHT.md for terms.
//

import Cocoa



class TableManager: CompletingControlDelegate {
    
    var incidentController: IncidentController
    
    var tableRowValues: [AnyObject] { return [] }  // sorted
    var stringValues: [String] { return [] }  // unsorted

    
    init(incidentController: IncidentController) {
        self.incidentController = incidentController
    }
    

    // FIXME
    func addStringValue(value: String) -> Bool {
        assert(false, "Unimplemented by subclass")
    }

    
    // FIXME
    func removeValue(value: AnyObject) {
        assert(false, "Unimplemented by subclass")
    }
    
    
    func objectAtIndex(index: Int) -> AnyObject? {
        guard index >= 0 else {
            logError("Negative table index: \(index)")
            return nil
        }
        
        guard index < tableRowValues.count else {
            logError("Table index out of bounds: \(index)")
            return nil
        }
        
        return tableRowValues[index]
    }
    
}



extension TableManager: TableViewDelegate {

    override var completionValues: [String] { return stringValues }
    
    func deleteFromTableView(tableView: TableView) {
        guard tableView.selectedRow != -1 else {
            logError("Deleting from table with no selected row?")
            return
        }
        
        guard tableRowValues.count > tableView.selectedRow else {
            logError("Selected row \(tableView.selectedRow) out of bounds?")
            return
        }
        
        removeValue(tableRowValues[tableView.selectedRow])
        
        incidentController.markEdited()
        incidentController.updateView()
    }
    
    
    // func openFromTableView(tableView: TableView) {}
    
}



extension TableManager: NSTableViewDataSource {
    
    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return stringValues.count
    }
    
    
    func tableView(
        tableView: NSTableView,
        objectValueForTableColumn tableColumn: NSTableColumn?,
        row: Int
    ) -> AnyObject? {
        if let value = objectAtIndex(row) {
            return value
        } else {
            return nil
        }
    }
    
}



extension TableManager {  // NSTextFieldDelegate
    
    override func control(
        control: NSControl,
        textView: NSTextView,
        doCommandBySelector commandSelector: Selector
    ) -> Bool {
        switch commandSelector {
            case Selector("deleteBackward:"):
                if control.stringValue.characters.count > 0 {
                    amBackspacing = true
                }
                return false
                
            default:
                return super.control(
                    control,
                    textView: textView,
                    doCommandBySelector: commandSelector
                )
        }
    }
    
    
    override func controlTextDidChange(notification: NSNotification) {
        if amBackspacing {
            amBackspacing = false
            return
        }
        
        if !amCompleting {
            guard let fieldEditor = notification.userInfo?["NSFieldEditor"] else {
                logError("No field editor?")
                return
            }
            
            // fieldEditor.complete() will trigger another call to
            // controlTextDidChange(), so we avoid infinite recursion with
            // the amCompleting variable.
            
            amCompleting = true
            fieldEditor.complete(self)
            amCompleting = false
        }
    }
    
}<commit_msg>Fix up refactor<commit_after>//
//  TableManager.swift
//  Incidents
//
//  © 2015 <NAME> and its contributors. All rights reserved.
//  See the file COPYRIGHT.md for terms.
//

import Cocoa



class TableManager: CompletingControlDelegate {
    
    var incidentController: IncidentController
    
    var tableRowValues: [AnyObject] { return [] }  // sorted
    var stringValues: [String] { return [] }  // unsorted

    override var completionValues: [String] { return stringValues }

    init(incidentController: IncidentController) {
        self.incidentController = incidentController
    }
    

    // FIXME
    func addStringValue(value: String) -> Bool {
        assert(false, "Unimplemented by subclass")
    }

    
    // FIXME
    func removeValue(value: AnyObject) {
        assert(false, "Unimplemented by subclass")
    }
    
    
    func objectAtIndex(index: Int) -> AnyObject? {
        guard index >= 0 else {
            logError("Negative table index: \(index)")
            return nil
        }
        
        guard index < tableRowValues.count else {
            logError("Table index out of bounds: \(index)")
            return nil
        }
        
        return tableRowValues[index]
    }
    
}



extension TableManager: TableViewDelegate {
    
    func deleteFromTableView(tableView: TableView) {
        guard tableView.selectedRow != -1 else {
            logError("Deleting from table with no selected row?")
            return
        }
        
        guard tableRowValues.count > tableView.selectedRow else {
            logError("Selected row \(tableView.selectedRow) out of bounds?")
            return
        }
        
        removeValue(tableRowValues[tableView.selectedRow])
        
        incidentController.markEdited()
        incidentController.updateView()
    }
    
    
    // func openFromTableView(tableView: TableView) {}
    
}



extension TableManager: NSTableViewDataSource {
    
    func numberOfRowsInTableView(tableView: NSTableView) -> Int {
        return stringValues.count
    }
    
    
    func tableView(
        tableView: NSTableView,
        objectValueForTableColumn tableColumn: NSTableColumn?,
        row: Int
    ) -> AnyObject? {
        if let value = objectAtIndex(row) {
            return value
        } else {
            return nil
        }
    }
    
}



extension TableManager {  // NSTextFieldDelegate
    
    override func control(
        control: NSControl,
        textView: NSTextView,
        doCommandBySelector commandSelector: Selector
    ) -> Bool {
        switch commandSelector {
            case Selector("insertNewline:"):
                let value = control.stringValue
                
                if value.characters.count > 0 {
                    if addStringValue(value) {
                        incidentController.markEdited()
                        incidentController.updateView()
                        
                        control.stringValue = ""
                    }
                }
                
                return true
                
            default:
                return super.control(
                    control,
                    textView: textView,
                    doCommandBySelector: commandSelector
                )
        }
    }
    
    }
{'fraction_non_alphanumeric': 0.07572540693559802, 'fraction_numerical': 0.001981599433828733, 'mean_word_length': 1.6064182958317963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '4795029', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1757, 'n_words': 423}