         }
         
         if self.isAutomaticTabExpansionEnabled {
-            let tabWidth = self.tabWidth
-            let column = self.string.column(of: self.rangeForUserTextChange.location, tabWidth: tabWidth)
-            let length = tabWidth - (column % tabWidth)
+            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)

            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 {

            replacementRange.length == 0, plainString == "}",
            let insertionIndex = Range(self.selectedRange, in: self.string)?.upperBound
        {
            let lineRange = self.string.lineRange(at: insertionIndex)
            
            // decrease indent level if the line is consists of only whitespaces
            if self.string.range(of: "^[ \\t]+\\n?$", options: .regularExpression, range: lineRange) != nil,
                let precedingIndex = self.string.indexOfBracePair(endIndex: insertionIndex, pair: BracePair("{", "}")) {
                let desiredLevel = self.string.indentLevel(at: precedingIndex, tabWidth: self.tabWidth)
                let currentLevel = self.string.indentLevel(at: insertionIndex, tabWidth: self.tabWidth)
                let levelToReduce = currentLevel - desiredLevel
                
                if levelToReduce > 0 {
