import AppKit

class CopyableTextView: NSTextView, NSTextViewDelegate {
    func clearSelection() {
        setSelectedRange(NSRange(location: 0, length: 0))
    }

    func copyRange() {
        let range = selectedRange()
        let start = string.index(string.startIndex, offsetBy: range.location)
        let end = string.index(start, offsetBy: range.length)

        let pasteboard = NSPasteboard.general
        pasteboard.prepareForNewContents()
        pasteboard.setString(String(string[start..<end]), forType: .string)
    }
}
