             var linesToInsert = [String]()
 
             let line = buffer.lines[index] as! String
+            let indexToInsertAt = (isExtension(line: line)) ? index - 1 : index
             if let match = protocolStringRegEx.matches(in: line)?.first {
                 let range = match.range
                 let substring = (line as NSString).substring(with: range)
-                linesToInsert = parse(string: substring)
+                let indentationString = (isExtension(line: line)) ? "" : "    "
+                linesToInsert = parse(string: substring, indentation: indentationString)
             }
-            result.append(MarkTuple(index, linesToInsert))
+            result.append(MarkTuple(indexToInsertAt, linesToInsert))
         }
  
         return result


fileprivate struct MarkRegExPattern {
    
    static let protocolStatementLine = "(class|struct|extension|protocol)(.*:.*,.*)"
    static let protocolStatementString = ":.*"
}

extension NSRegularExpression {

        var matches = [Int]()
        for lineIndex in 0 ..< buffer.lines.count {
            let line = buffer.lines[lineIndex] as! String
            let resultProtocolRegEx = protocolLineRegEx.matches(in: line)
            if (resultProtocolRegEx?.count)! > 0 ||  isExtension(line: line) {
                matches.append(lineIndex)
            }
        }
        return matches
    }
    
    fileprivate static func isExtension(line: String) -> Bool {
        return line.contains(MarkRegExPattern.extensionString) &&
        (line.contains(MarkRegExPattern.commaString) == false)
    }
}
