<commit_before>//
//  NCKTextStorage.swift
//  Pods
//
//  Created by <NAME> on 8/15/16.
//
//

class NCKTextStorage: NSTextStorage {
    var textView: NCKTextView!

    var currentString: NSMutableAttributedString = NSMutableAttributedString()
    var attributes: Dictionary<String, AnyObject> = [:]
    
    var isChangeCharacters: Bool = false
    
    // MARK: - Must override
    override var string: String {
        return currentString.string
    }
    
    override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
        return currentString.attributesAtIndex(location, effectiveRange: range)
    }
    
    override func replaceCharactersInRange(range: NSRange, withString str: String) {
        var listItemFillText: String = ""
        var currentNumber: Int?
        
        // Unordered and Ordered list auto-complete support
        if NCKTextUtil.isReturn(str) {
            print("woo! return key touch!")
            
            var objectLine = self.string.substringToIndex(self.string.startIndex.advancedBy(range.location))
            let textSplits = objectLine.componentsSeparatedByString("\n")
            if textSplits.count > 0 {
                objectLine = textSplits[textSplits.count - 1]
            }
            
            let objectLineRange = NSRange(location: 0, length: objectLine.characters.count)
            
            // Check matches.
            let unorderedListMatches = NCKTextUtil.markdownUnorderedListRegularExpression.matchesInString(objectLine, options: [], range: objectLineRange)
            let orderedListMatches = NCKTextUtil.markdownOrderedListRegularExpression.matchesInString(objectLine, options: [], range: objectLineRange)
            
            if unorderedListMatches.count > 0 {
                print("it's a continue item of unordered list")
                
                var listPrefixItem = objectLine.componentsSeparatedByString(" ")[0]
                listItemFillText = "\(listPrefixItem) "
            }
            
            if orderedListMatches.count > 0 {
                print("it's a continue item of ordered list")
                
                var number = Int(objectLine.componentsSeparatedByString(".")[0])
                number! += 1
                listItemFillText = "\(number!). "
                
                currentNumber = number
            }
        }
        
        isChangeCharacters = true
        
        beginEditing()
        
        currentString.replaceCharactersInRange(range, withString: str + listItemFillText)
        edited(.EditedCharacters, range: range, changeInLength: (str.characters.count + listItemFillText.characters.count) - range.length)
        
        endEditing()
        
        // Selected range changed.
        if listItemFillText != "" {
            let selectedRangeLocation = textView.selectedRange.location + listItemFillText.characters.count
            
            textView.selectedRange = NSRange(location: selectedRangeLocation, length: textView.selectedRange.length)
            
//            if currentNumber != nil {
//                // Reorder numbers after current line.
//                var afterStrings = textView.text.substringFromIndex(textView.text.startIndex.advancedBy(textView.selectedRange.location))
//                
//                let orderedListAfterItemsMatches = NCKTextUtil.markdownOrderedListAfterItemsRegularExpression.matchesInString(afterStrings, options: [], range: NSRange(location: 0, length: afterStrings.characters.count))
//                
//                for orderedListAfterItem in orderedListAfterItemsMatches {
//                    currentNumber! += 1
//                    
//                    let location = orderedListAfterItem.range.location
//                    let length = orderedListAfterItem.range.length
//                    
//                    afterStrings.replaceRange(Range(start: afterStrings.startIndex.advancedBy(location), end: afterStrings.startIndex.advancedBy(location + length)), with: "\n\(currentNumber!). ")
//                }
//            }
        }
    }
    
    override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
        beginEditing()
        
        currentString.setAttributes(attrs, range: range)<commit_msg>Fix bug: _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index<commit_after>//
//  NCKTextStorage.swift
//  Pods
//
//  Created by <NAME> on 8/15/16.
//
//

class NCKTextStorage: NSTextStorage {
    var textView: NCKTextView!

    var currentString: NSMutableAttributedString = NSMutableAttributedString()
    var attributes: Dictionary<String, AnyObject> = [:]
    
    var isChangeCharacters: Bool = false
    
    // MARK: - Must override
    override var string: String {
        return currentString.string
    }
    
    override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
        return currentString.attributesAtIndex(location, effectiveRange: range)
    }
    
    override func replaceCharactersInRange(range: NSRange, withString str: String) {
        var listItemFillText: NSString = ""
        var currentNumber: Int?
        
        // Unordered and Ordered list auto-complete support
        if NCKTextUtil.isReturn(str) {
            print("woo! return key touch!")
            
            var objectLine = self.string.substringToIndex(self.string.startIndex.advancedBy(range.location))
            let textSplits = objectLine.componentsSeparatedByString("\n")
            if textSplits.count > 0 {
                objectLine = textSplits[textSplits.count - 1]
            }
            
            let objectLineRange = NSRange(location: 0, length: NSString(string: objectLine).length)
            
            // Check matches.
            let unorderedListMatches = NCKTextUtil.markdownUnorderedListRegularExpression.matchesInString(objectLine, options: [], range: objectLineRange)
            let orderedListMatches = NCKTextUtil.markdownOrderedListRegularExpression.matchesInString(objectLine, options: [], range: objectLineRange)
            
            if unorderedListMatches.count > 0 {
                print("it's a continue item of unordered list")
                
                var listPrefixItem = objectLine.componentsSeparatedByString(" ")[0]
                listItemFillText = "\(listPrefixItem) "
            }
            
            if orderedListMatches.count > 0 {
                print("it's a continue item of ordered list")
                
                var number = Int(objectLine.componentsSeparatedByString(".")[0])
                number! += 1
                listItemFillText = "\(number!). "
                
                currentNumber = number
            }
        }
        
        isChangeCharacters = true
        
        beginEditing()
        
        let finalStr: NSString = "\(str)\(listItemFillText)"
        
        currentString.replaceCharactersInRange(range, withString: String(finalStr))
        edited(.EditedCharacters, range: range, changeInLength: (finalStr.length - range.length))
        
        endEditing()
        
        // Selected range changed.
        if listItemFillText != "" {
            let selectedRangeLocation = textView.selectedRange.location + listItemFillText.length
            
            textView.selectedRange = NSRange(location: selectedRangeLocation, length: textView.selectedRange.length)
            
//            if currentNumber != nil {
//                // Reorder numbers after current line.
//                var afterStrings = textView.text.substringFromIndex(textView.text.startIndex.advancedBy(textView.selectedRange.location))
//                
//                let orderedListAfterItemsMatches = NCKTextUtil.markdownOrderedListAfterItemsRegularExpression.matchesInString(afterStrings, options: [], range: NSRange(location: 0, length: NSString(string: afterStrings).length))
//                
//                for orderedListAfterItem in orderedListAfterItemsMatches {
//                    currentNumber! += 1
//                    
//                    let location = orderedListAfterItem.range.location
//                    let length = orderedListAfterItem.range.length
//                    
//                    afterStrings.replaceRange(Range(start: afterStrings.startIndex.advancedBy(location), end: afterStrings.startIndex.advancedBy(location + length)), with: "\n\(currentNumber!). ")
//                }
//            }
        }
    }
    
    override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
        beginEditing()
        
        currentString.setAttributes(attrs, range: range)
{'fraction_non_alphanumeric': 0.08829240434037693, 'fraction_numerical': 0.0034266133637921186, 'mean_word_length': 2.03185595567867, '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': '237510', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2127, 'n_words': 446}