Skip to content

Commit

Permalink
Merge pull request #275 from vdka/master
Browse files Browse the repository at this point in the history
add spm-module to complete command
  • Loading branch information
jpsim authored Oct 18, 2016
2 parents 85f149b + 458b920 commit 66ad4c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

##### Enhancements

* None.
* Add `--spm-module [ModuleName]` flag to `complete` to automatically detect
compiler flags for Swift Package Manager modules. `swift build` must be run
prior to support detection.
[vdka](https://github.com/vdka)
[#270](https://github.com/jpsim/SourceKitten/issues/270)

##### Bug Fixes

Expand Down
28 changes: 19 additions & 9 deletions Source/sourcekitten/CompleteCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ struct CompleteCommand: CommandProtocol {
let file: String
let text: String
let offset: Int
let spmModule: String
let compilerargs: [String]

static func create(file: String) -> (_ text: String) -> (_ offset: Int) -> (_ compilerargs: [String]) -> Options {
return { text in { offset in { compilerargs in
self.init(file: file, text: text, offset: offset, compilerargs: compilerargs)
}}}
static func create(file: String) -> (_ text: String) -> (_ offset: Int) -> (_ spmModule: String) -> (_ compilerargs: [String]) -> Options {
return { text in { offset in { spmModule in { compilerargs in
self.init(file: file, text: text, offset: offset, spmModule: spmModule, compilerargs: compilerargs)
}}}}
}

static func evaluate(_ m: CommandMode) -> Result<Options, CommandantError<SourceKittenError>> {
return create
<*> m <| Option(key: "file", defaultValue: "", usage: "relative or absolute path of Swift file to parse")
<*> m <| Option(key: "text", defaultValue: "", usage: "Swift code text to parse")
<*> m <| Option(key: "offset", defaultValue: 0, usage: "Offset for which to generate code completion options.")
<*> m <| Option(key: "spm-module", defaultValue: "", usage: "Read compiler flags from a Swift Package Manager module")
<*> m <| Argument(defaultValue: [String](), usage: "Compiler arguments to pass to SourceKit. This must be specified following the '--'")
}
}
Expand All @@ -50,13 +52,21 @@ struct CompleteCommand: CommandProtocol {
contents = options.text
}

var args = ["-c", path]
args.append(contentsOf: options.compilerargs)

if args.index(of: "-sdk") == nil {
args.append(contentsOf: ["-sdk", sdkPath()])
var args: [String]
if options.spmModule.isEmpty {
args = ["-c", path] + options.compilerargs
if args.index(of: "-sdk") == nil {
args.append(contentsOf: ["-sdk", sdkPath()])
}
} else {
guard let module = Module(spmName: options.spmModule) else {
return .failure(.invalidArgument(description: "Bad module name"))
}
args = module.compilerArguments
}



let request = Request.codeCompletionRequest(file: path, contents: contents,
offset: Int64(options.offset),
arguments: args)
Expand Down

0 comments on commit 66ad4c4

Please sign in to comment.