             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

                selectionString.append(lineString.substring(with: NSMakeRange(rangeStart, rangeEnd - rangeStart)))
            }
            
            let result = parse(string: selectionString)
            marks.append(MarkTuple(range.end.line, result))
        }
        
        return marks
    }
    
    fileprivate static func parse(string: String) -> [String] {
        var linesToInsert = [String]()
        let protocols = string.components(separatedBy: ":").last
        if let protocolNames = protocols?.components(separatedBy: ",") {
            for name in protocolNames {
                let protocolName = name.alphabeticalString().fromCamelCase()
                linesToInsert.append("\n    //MARK: - \(protocolName)\n")
            }
        }
        return linesToInsert


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

extension NSRegularExpression {
