0001
0009 import Commandant
0010 import Foundation
0011 import Result
0012 import SourceKittenFramework
0013
0014 struct CompleteCommand| main.swift:17 | registry.register(CompleteCommand()) |
: CommandType {
0015 let verb = "complete"
0016 let function = "Generate code completion options."
0017
0018 func run(options: CompleteOptions) -> Result<(), SourceKittenError> {
0019 let path: String
0020 let contents: String
0021 if !options.file.isEmpty {
0022 path = options.file.absolutePathRepresentation()
0023 guard let file = File(path: path) else {
0024 return .Failure(.ReadFailed(path: options.file))
0025 }
0026 contents = file.contents
0027 } else {
0028 path = "\(NSUUID().UUIDString).swift"
0029 contents = options.text
0030 }
0031
0032 var args = ["-c", path]
0033 if !options.compilerargs.isEmpty {
0034 args.appendContentsOf(options.compilerargs.componentsSeparatedByString(" "))
0035 }
0036 if args.indexOf("-sdk") == nil {
0037 args.appendContentsOf(["-sdk", sdkPath()])
0038 }
0039
0040 let request = Request.CodeCompletionRequest(file: path, contents: contents,
0041 offset: Int64(options.offset),
0042 arguments: args)
0043 print(CodeCompletionItem.parseResponse(request.send()))
0044 return .Success()
0045 }
0046 }
0047
0048 struct CompleteOptions| CompleteCommand.swift:54 | static func create(file: String) -> (text: String) -> (offset: Int) -> (compilerargs: String) -> CompleteOptions { |
: OptionsType {
0049 let file: String
0050 let text: String
0051 let offset: Int
0052 let compilerargs: String
0053
0054 static func create(file: String) -> (text: String) -> (offset: Int) -> (compilerargs: String) -> CompleteOptions {
0055 return { text in { offset in { compilerargs in
0056 self.init(file: file, text: text, offset: offset, compilerargs: compilerargs)
0057 }}}
0058 }
0059
0060 static func evaluate(m: CommandMode) -> Result<CompleteOptions, CommandantError<SourceKittenError>> {
0061 return create
0062 <*> m <| Option(key: "file", defaultValue: "", usage: "relative or absolute path of Swift file to parse")
0063 <*> m <| Option(key: "text", defaultValue: "", usage: "Swift code text to parse")
0064 <*> m <| Option(key: "offset", defaultValue: 0, usage: "Offset for which to generate code completion options.")
0065 <*> m <| Option(key: "compilerargs", defaultValue: "", usage: "Compiler arguments to pass to SourceKit. This must be specified following the '--'")
0066 }
0067 }
0068