Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add spm-module to complete command #275

Merged
merged 3 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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