-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from jpsim/jp-format-command
add format command
- Loading branch information
Showing
16 changed files
with
319 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// FormatCommand.swift | ||
// SourceKitten | ||
// | ||
// Created by JP Simard on 2016-05-28. | ||
// Copyright (c) 2016 SourceKitten. All rights reserved. | ||
// | ||
|
||
import Commandant | ||
import Foundation | ||
import Result | ||
import SourceKittenFramework | ||
|
||
struct FormatCommand: CommandType { | ||
let verb = "format" | ||
let function = "Format Swift file" | ||
|
||
struct Options: OptionsType { | ||
let file: String | ||
let trimWhitespace: Bool | ||
let useTabs: Bool | ||
let indentWidth: Int | ||
|
||
static func create(file: String) -> (trimWhitespace: Bool) -> (useTabs: Bool) -> (indentWidth: Int) -> Options { | ||
return { trimWhitespace in { useTabs in { indentWidth in | ||
self.init(file: file, trimWhitespace: trimWhitespace, useTabs: useTabs, indentWidth: indentWidth) | ||
}}} | ||
} | ||
|
||
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 format") | ||
<*> m <| Option(key: "trim-whitespace", defaultValue: true, usage: "trim trailing whitespace") | ||
<*> m <| Option(key: "use-tabs", defaultValue: false, usage: "use tabs to indent") | ||
<*> m <| Option(key: "indent-width", defaultValue: 4, usage: "number of spaces to indent") | ||
} | ||
} | ||
|
||
func run(options: Options) -> Result<(), SourceKittenError> { | ||
guard !options.file.isEmpty else { | ||
return .Failure(.InvalidArgument(description: "file must be set when calling format")) | ||
} | ||
let absolutePath = (options.file as NSString).absolutePathRepresentation() | ||
try! File(path: absolutePath)? | ||
.format(trimmingTrailingWhitespace: options.trimWhitespace, | ||
useTabs: options.useTabs, | ||
indentWidth: options.indentWidth) | ||
.dataUsingEncoding(NSUTF8StringEncoding)? | ||
.writeToFile(absolutePath, options: []) | ||
return .Success() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Structure.swift | ||
// IndexCommand.swift | ||
// SourceKitten | ||
// | ||
// Created by JP Simard on 2015-04-24. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Structure.swift | ||
// StructureCommand.swift | ||
// SourceKitten | ||
// | ||
// Created by JP Simard on 2015-01-07. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Syntax.swift | ||
// SyntaxCommand.swift | ||
// SourceKitten | ||
// | ||
// Created by JP Simard on 2015-01-07. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Version.swift | ||
// VersionCommand.swift | ||
// SourceKitten | ||
// | ||
// Created by JP Simard on 2015-01-07. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.