Skip to content

Add swift-format documentation #4191

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

Merged
merged 1 commit into from
Aug 12, 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
49 changes: 49 additions & 0 deletions docs/SwiftFormat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# Swift-format

## Introduction

Note: This tool is still a work in progress.

swift-format is a tool for automatically format your Swift files according to a
set of rules. It is implemented as another driver kind, like swiftc, the batch
compiler, so swift-format is actually a symbolic link to swift. This tool uses
libIDE to format code, so it can be leveraged from multiple systems and editors.

## Usage

To print all the available options:

swift-format -help

By default, swift-format will output the formatted file to the standard output:

swift-format sample.swift

You can either output the result to a separate file:

swift-format sample.swift -o result.swift

Or you can format in-place (the original file will be overwritten):

swift-format -in-place sample.swift

If you want to indent using tabs instead of spaces, use the "-use-tabs" option:

swift-format -use-tabs sample.swift

You can set the number of tabs or spaces using the "-tab-width" and
"-indent-width" options, respectively.

swift-format supports formatting a range of lines from a file:

swift-format -line-range 2:45 sample.swift

This will format the file from lines 2 to 45, inclusive.

You can format several files, but the "-line-range" option is not supported in
that case.

You can also provide several line ranges by using multiple "-line-range" options:

swift-format -line-range 2:45 -line-range 100:120 sample.swift
6 changes: 3 additions & 3 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,19 @@ def use_tabs : Flag<["-"], "use-tabs">, Group<code_formatting_Group>,
Flags<[SwiftFormatOption]>,
HelpText<"Use tabs for indentation.">;

def inplace : Flag<["-"], "inplace">, Group<code_formatting_Group>,
def in_place : Flag<["-"], "in-place">, Group<code_formatting_Group>,
Flags<[SwiftFormatOption]>,
HelpText<"Overwrite input file with formatted file.">;

def tab_width : Separate<["-"], "tabwidth">, Group<code_formatting_Group>,
def tab_width : Separate<["-"], "tab-width">, Group<code_formatting_Group>,
Flags<[SwiftFormatOption]>,
HelpText<"Width of tab character.">, MetaVarName<"<n>">;

def indent_width : Separate<["-"], "indent-width">, Group<code_formatting_Group>,
Flags<[SwiftFormatOption]>,
HelpText<"Number of characters to indent.">, MetaVarName<"<n>">;

def line_ranges : Separate<["-"], "line-ranges">, Group<code_formatting_Group>,
def line_range : Separate<["-"], "line-range">, Group<code_formatting_Group>,
Flags<[SwiftFormatOption]>,
HelpText<"<start line>:<end line>. Formats a range of lines (1-based). "
"Can only be used with one input file.">, MetaVarName<"<n:n>">;
Expand Down
2 changes: 1 addition & 1 deletion test/swift-format/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: diff -u %s.indent2.response %t.response
// RUN: %swift-format -use-tabs %s >%t.response
// RUN: diff -u %s.tabs.response %t.response
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
// RUN: %swift-format -line-range 12:18 %s >%t.response
// RUN: diff -u %s.lines.response %t.response

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion test/swift-format/main.swift.indent2.response
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: diff -u %s.indent2.response %t.response
// RUN: %swift-format -use-tabs %s >%t.response
// RUN: diff -u %s.tabs.response %t.response
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
// RUN: %swift-format -line-range 12:18 %s >%t.response
// RUN: diff -u %s.lines.response %t.response

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion test/swift-format/main.swift.lines.response
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: diff -u %s.indent2.response %t.response
// RUN: %swift-format -use-tabs %s >%t.response
// RUN: diff -u %s.tabs.response %t.response
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
// RUN: %swift-format -line-range 12:18 %s >%t.response
// RUN: diff -u %s.lines.response %t.response

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion test/swift-format/main.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: diff -u %s.indent2.response %t.response
// RUN: %swift-format -use-tabs %s >%t.response
// RUN: diff -u %s.tabs.response %t.response
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
// RUN: %swift-format -line-range 12:18 %s >%t.response
// RUN: diff -u %s.lines.response %t.response

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion test/swift-format/main.swift.tabs.response
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// RUN: diff -u %s.indent2.response %t.response
// RUN: %swift-format -use-tabs %s >%t.response
// RUN: diff -u %s.tabs.response %t.response
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
// RUN: %swift-format -line-range 12:18 %s >%t.response
// RUN: diff -u %s.lines.response %t.response

import Foundation
Expand Down
4 changes: 2 additions & 2 deletions tools/driver/swift_format_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SwiftFormatInvocation {
if (ParsedArgs.getLastArg(OPT_use_tabs))
UseTabs = true;

if (ParsedArgs.getLastArg(OPT_inplace))
if (ParsedArgs.getLastArg(OPT_in_place))
InPlace = true;

if (const Arg *A = ParsedArgs.getLastArg(OPT_tab_width))
Expand All @@ -133,7 +133,7 @@ class SwiftFormatInvocation {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(ParsedArgs), A->getValue());

for (const Arg *A : make_range(ParsedArgs.filtered_begin(OPT_line_ranges),
for (const Arg *A : make_range(ParsedArgs.filtered_begin(OPT_line_range),
ParsedArgs.filtered_end()))
LineRanges.push_back(A->getValue());

Expand Down