         }
         
         let indent: String = {
-            guard let baseIndentRange = indentRange.intersection(NSRange(location: 0, length: self.selectedRange.location)) else {
+            guard let baseIndentRange = indentRange.intersection(NSRange(0..<self.selectedRange.location)) else {
                 return ""
             }
-            return (string as NSString).substring(with: baseIndentRange)
+            return (self.string as NSString).substring(with: baseIndentRange)
         }()
         
         // calculation for smart indent

            return super.insertNewline(sender)
        }
        
        let string = self.string
        let indentRange = string.rangeOfIndent(at: self.selectedRange.location)
        
        // don't auto-indent if indent is selected (2008-12-13)
        guard indentRange.length == 0 || indentRange != self.selectedRange else {

        }
        
        if self.isAutomaticTabExpansionEnabled {
            let column = self.string.column(of: self.rangeForUserTextChange.location, tabWidth: self.tabWidth)
            let length = self.tabWidth - (column % self.tabWidth)
            let spaces = String(repeating: " ", count: length)
            
            return super.insertText(spaces, replacementRange: self.rangeForUserTextChange)
