 import Foundation
 import XcodeKit
 
+fileprivate extension XCSourceTextBuffer {
+    var emptySelection: Bool {
+        let textRange = self.selections.firstObject as! XCSourceTextRange
+        return (textRange.start.line == textRange.end.line && textRange.start.column == textRange.end.column)
+    }
+    
+    var isSwiftSource: Bool {
+        return self.contentUTI == "public.swift-source"
+    }
+}
+
 struct CommandIdentifier {
     static let markAll = "MarkAllCommand"
     static let markSelected = "MarkSelectedCommand"
+}
+
+enum MarkError: Error {
+    case unsupportedSource(String)
+}
+
+struct MarkErrorDesciptions {
+    static let unsupportedSource = "Source code language is currently unsupported."
 }
 
 class SourceEditorCommand: NSObject, XCSourceEditorCommand {

    func insert(input: [Any], into buffer: XCSourceTextBuffer) {
        var insertedLinesCount = 0
        for mark in (input as! [MarkTuple]) {
            let lineIndex = mark.lineIndex + 2
            
            for markLine in mark.lines {
                buffer.lines.insert(markLine, at: lineIndex + insertedLinesCount)
                insertedLinesCount += 2
            }
        }
    }

    
}

    func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
        
        let buffer = invocation.buffer
        if buffer.isSwiftSource == false {
            completionHandler(MarkError.unsupportedSource(MarkErrorDesciptions.unsupportedSource))
        }
        
        var marksToInsert = [Any]()
        if invocation.commandIdentifier.contains(CommandIdentifier.markAll) {
            marksToInsert = MarkParser.parse(buffer: buffer)
        }
