<commit_before>//
//  MentionTextView.swift
//  Mentions
//
//  Created by magicmon on 2017. 3. 28..
//
//

import UIKit

public class MentionTextView: UITextView {
    
    public var highlightColor: UIColor = UIColor.blue
    public var pattern: ParserPattern = .mention
    
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        delegate = self
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        delegate = self
    }
    
    var highlightUsers: [(String, NSRange)] = []        // 맨션 유저의 리스트
    
    public var mentionText: String {
        get {
            return self.text
        } set {
            let (matchText, matchUsers) = self.parse(newValue, pattern: pattern, template: "$1")
            
            self.text = matchText
            
            if let matchUsers = matchUsers {
                highlightUsers.removeAll()
                for (user, range) in matchUsers {
                    highlightUsers.append(user, range)
                }
                
                refreshAttributedText()
            }
        }
    }
}

// MARK: - Highlight
extension MentionTextView {
    func insert(to user: String?, with nsrange: NSRange?) {
        guard let user = user, let nsrange = nsrange else {
            return
        }
        
        guard let range = self.text.rangeFromNSRange(nsrange) else {
            return
        }
        
        // 변환된 맨션을 텍스트뷰에 추가
        
        // 넣기전 기존의 맨션 위치 재 정렬
        self.textView(self, shouldChangeTextIn: nsrange, replacementText: "@\(user)")
        
        // 텍스트 수정(맨션 문자 + 닉네임)
        self.text = self.text.replacingCharacters(in: range, with: "@\(user)")
        <commit_msg>bug fix.<commit_after>//
//  MentionTextView.swift
//  Mentions
//
//  Created by magicmon on 2017. 3. 28..
//
//

import UIKit

public class MentionTextView: UITextView {
    
    public var highlightColor: UIColor = UIColor.blue
    public var pattern: ParserPattern = .mention
    
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        delegate = self
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        delegate = self
    }
    
    var highlightUsers: [(String, NSRange)] = []        // 맨션 유저의 리스트
    
    public var mentionText: String {
        get {
            return self.text
        } set {
            let (matchText, matchUsers) = self.parse(newValue, pattern: pattern, template: "$1")
            
            self.text = matchText
            
            if let matchUsers = matchUsers {
                highlightUsers.removeAll()
                for (user, range) in matchUsers {
                    highlightUsers.append(user, range)
                }
                
                refresh()
            }
        }
    }
}

// MARK: - Highlight
extension MentionTextView {
    func insert(to user: String?, with nsrange: NSRange?) {
        guard let user = user, let nsrange = nsrange else {
            return
        }
        
        guard let range = self.text.rangeFromNSRange(nsrange) else {
            return
        }
        
        // 변환된 맨션을 텍스트뷰에 추가
        
        // 넣기전 기존의 맨션 위치 재 정렬
        self.textView(self, shouldChangeTextIn: nsrange, replacementText: "@\(user)")
        
        // 텍스트 수정(맨션 문자 + 닉네임)
        self.text = self.text.replacingCharacters(in: range, with: "@\(user)")
        
{'fraction_non_alphanumeric': 0.09535724214623297, 'fraction_numerical': 0.004448151237142063, 'mean_word_length': 1.5829145728643217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5622446', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1172, 'n_words': 269}