 import Foundation
 import XPC
 
-// MARK: Structure
-
-/**
-Print file structure information as JSON to STDOUT
-
-:param: file Path to the file to parse for structure information
-*/
-func printStructure(#file: String) {
-    // Construct a SourceKit request for getting general info about a Swift file
-    let request = xpc_dictionary_create(nil, nil, 0)
-    xpc_dictionary_set_uint64(request, "key.request", sourcekitd_uid_get_from_cstr("source.request.editor.open"))
-    xpc_dictionary_set_string(request, "key.name", "")
-    xpc_dictionary_set_string(request, "key.sourcefile", file)
-
-    // Initialize SourceKit XPC service
-    sourcekitd_initialize()
-
-    // Send SourceKit request
-    var response: XPCDictionary = fromXPC(sourcekitd_send_request_sync(request))
-    response.removeValueForKey("key.syntaxmap")
-    var declarationOffsets = [Int64, String]()
-    replaceUIDsWithStringsInDictionary(response, declarationOffsets: &declarationOffsets)
-    println(toJSON(response))
-}
-
-// MARK: Syntax
-
-/**
-Print syntax highlighting information as JSON to STDOUT
-
-:param: file Path to the file to parse for syntax highlighting information
-*/
-func printSyntaxHighlighting(#file: String) {
-    // Construct a SourceKit request for getting general info about the Swift file passed as argument
-    let request = xpc_dictionary_create(nil, nil, 0)
-    xpc_dictionary_set_uint64(request, "key.request", sourcekitd_uid_get_from_cstr("source.request.editor.open"))
-    xpc_dictionary_set_string(request, "key.name", "")
-    xpc_dictionary_set_string(request, "key.sourcefile", file)
-
-    // Initialize SourceKit XPC service
-    sourcekitd_initialize()
-
-    // Send SourceKit request
-    let response = sourcekitd_send_request_sync(request)
-    printSyntaxHighlighting(response)
-}
-
-/**
-Print syntax highlighting information as JSON to STDOUT
-
-:param: text Swift source code to parse for syntax highlighting information
-*/
-func printSyntaxHighlighting(#text: String) {
-    // Construct a SourceKit request for getting general info about the Swift source text passed as argument
-    let request = xpc_dictionary_create(nil, nil, 0)
-    xpc_dictionary_set_uint64(request, "key.request", sourcekitd_uid_get_from_cstr("source.request.editor.open"))
-    xpc_dictionary_set_string(request, "key.name", "")
-    xpc_dictionary_set_string(request, "key.sourcetext", text)
-
-    // Initialize SourceKit XPC service
-    sourcekitd_initialize()
-
-    // Send SourceKit request
-    let response = sourcekitd_send_request_sync(request)
-    printSyntaxHighlighting(response)
-}
-
-/**
-Print syntax highlighting information as JSON to STDOUT
-
-:param: sourceKitResponse XPC object returned from SourceKit "editor.open" call
-*/
-func printSyntaxHighlighting(sourceKitResponse: xpc_object_t) {
-    // Get syntaxmap XPC data and convert to NSData
-    let data: NSData = fromXPC(xpc_dictionary_get_value(sourceKitResponse, "key.syntaxmap"))!
-
-    // Get number of syntax tokens
-    var tokens = 0
-    data.getBytes(&tokens, range: NSRange(location: 8, length: 8))
-    tokens = tokens >> 4
-
-    println("[")
-
-    for i in 0..<tokens {
-        let parserOffset = 16 * i
-
-        var uid = UInt64(0)
-        data.getBytes(&uid, range: NSRange(location: 16 + parserOffset, length: 8))
-        let type = String(UTF8String: sourcekitd_uid_get_string_ptr(uid))!
-
-        var offset = 0
-        data.getBytes(&offset, range: NSRange(location: 24 + parserOffset, length: 4))
-
-        var length = 0
-        data.getBytes(&length, range: NSRange(location: 28 + parserOffset, length: 4))
-        length = length >> 1
-
-        print("  {\n    \"type\": \"\(type)\",\n    \"offset\": \(offset),\n    \"length\": \(length)\n  }")
-
-        if i != tokens-1 {
-            println(",")
-        } else {
-            println()
-        }
-    }
-
-    println("]")
-}
-
 // MARK: Helper Functions
+
+/// SourceKit UID to String map
+var uidStringMap = [UInt64: String]()
+
+/**
+Cache SourceKit requests for strings from UIDs
+
+:param: uid UID received from sourcekitd* responses
+:returns: Cached UID string if available, other
+*/
+func stringForSourceKitUID(uid: UInt64) -> String? {
+    if uid < 4_300_000_000 {
+        // UID's are all higher than 4.3M
+        return nil
+    } else if let string = uidStringMap[uid] {
+        return string
+    } else {
+        if let uidCString = sourcekitd_uid_get_string_ptr(uid) as UnsafePointer<Int8>? {
+            let uidString = String(UTF8String: uidCString)!
+            uidStringMap[uid] = uidString
+            return uidString
+        }
+    }
+    return nil
+}
 
 /**
 Print error message to STDERR

        case let object as NSData:
            json += "\"\(key)\": \"\(object)\","
        case let object as UInt64:
            if object > 4_300_000_000 { // UID's are all higher than 4.3M
                if let utf8String = sourcekitd_uid_get_string_ptr(object) as UnsafePointer<Int8>? {
                    let uidString = String(UTF8String: utf8String)!
                    json += "\"\(key)\": \"\(uidString)\","
                }
            } else {
                json += "\"\(key)\": \(object),"
            }
        case let object as Int64:
            json += "\"\(key)\": \(object),"
        case let object as Double:


:param: arguments compiler arguments to pass to SourceKit
:param: swiftFiles array of Swift file names to document
*/
func docs_for_swift_compiler_args(arguments: [String], swiftFiles: [String]) {
    sourcekitd_initialize()

    // Create the XPC array of compiler arguments once, to be reused for each request
