         
         guard range.isEmpty else { return nil }
         
-        let lineRange = (self as NSString).lineRange(at: range.location)
-        let forwardRange = NSRange(lineRange.location..<range.location)
+        let lineStartIndex = (self as NSString).lineStartIndex(at: range.location)
+        let forwardRange = NSRange(lineStartIndex..<range.location)
+        
         guard (self as NSString).range(of: "^ +$", options: .regularExpression, range: forwardRange).length > 1 else { return nil }
         
         let column = self.column(of: range.location, tabWidth: tabWidth)
         let targetLength = tabWidth - (column % tabWidth)
-        let targetRange = NSRange((range.location - targetLength)..<range.location)
+        let targetRange = NSRange(location: range.location - targetLength, length: targetLength)
         
         guard
             range.location >= targetLength,

        
        let lineRange = self.lineRange(at: index)
        
        guard let indentRange = self.range(of: "^[ \\t]+", options: .regularExpression, range: lineRange) else {
            return index..<index
        }
        
        return indentRange
    }
    
    

        guard !indentRange.isEmpty else { return 0 }
        
        let indent = self[workaround: indentRange]
        let numberOfTabs = indent.count { $0 == "\t" }
        
        return numberOfTabs + ((indent.count - numberOfTabs) / tabWidth)
    }
