Skip to content

Commit 4ccf6c9

Browse files
authored
Merge pull request #4191 from danielmartin/swift-format-documentation
Add swift-format documentation
2 parents 7b67396 + 14741c4 commit 4ccf6c9

File tree

8 files changed

+59
-10
lines changed

8 files changed

+59
-10
lines changed

docs/SwiftFormat.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# Swift-format
3+
4+
## Introduction
5+
6+
Note: This tool is still a work in progress.
7+
8+
swift-format is a tool for automatically format your Swift files according to a
9+
set of rules. It is implemented as another driver kind, like swiftc, the batch
10+
compiler, so swift-format is actually a symbolic link to swift. This tool uses
11+
libIDE to format code, so it can be leveraged from multiple systems and editors.
12+
13+
## Usage
14+
15+
To print all the available options:
16+
17+
swift-format -help
18+
19+
By default, swift-format will output the formatted file to the standard output:
20+
21+
swift-format sample.swift
22+
23+
You can either output the result to a separate file:
24+
25+
swift-format sample.swift -o result.swift
26+
27+
Or you can format in-place (the original file will be overwritten):
28+
29+
swift-format -in-place sample.swift
30+
31+
If you want to indent using tabs instead of spaces, use the "-use-tabs" option:
32+
33+
swift-format -use-tabs sample.swift
34+
35+
You can set the number of tabs or spaces using the "-tab-width" and
36+
"-indent-width" options, respectively.
37+
38+
swift-format supports formatting a range of lines from a file:
39+
40+
swift-format -line-range 2:45 sample.swift
41+
42+
This will format the file from lines 2 to 45, inclusive.
43+
44+
You can format several files, but the "-line-range" option is not supported in
45+
that case.
46+
47+
You can also provide several line ranges by using multiple "-line-range" options:
48+
49+
swift-format -line-range 2:45 -line-range 100:120 sample.swift

include/swift/Option/Options.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,19 @@ def use_tabs : Flag<["-"], "use-tabs">, Group<code_formatting_Group>,
321321
Flags<[SwiftFormatOption]>,
322322
HelpText<"Use tabs for indentation.">;
323323

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

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

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

336-
def line_ranges : Separate<["-"], "line-ranges">, Group<code_formatting_Group>,
336+
def line_range : Separate<["-"], "line-range">, Group<code_formatting_Group>,
337337
Flags<[SwiftFormatOption]>,
338338
HelpText<"<start line>:<end line>. Formats a range of lines (1-based). "
339339
"Can only be used with one input file.">, MetaVarName<"<n:n>">;

test/swift-format/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: diff -u %s.indent2.response %t.response
55
// RUN: %swift-format -use-tabs %s >%t.response
66
// RUN: diff -u %s.tabs.response %t.response
7-
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
7+
// RUN: %swift-format -line-range 12:18 %s >%t.response
88
// RUN: diff -u %s.lines.response %t.response
99

1010
import Foundation

test/swift-format/main.swift.indent2.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: diff -u %s.indent2.response %t.response
55
// RUN: %swift-format -use-tabs %s >%t.response
66
// RUN: diff -u %s.tabs.response %t.response
7-
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
7+
// RUN: %swift-format -line-range 12:18 %s >%t.response
88
// RUN: diff -u %s.lines.response %t.response
99

1010
import Foundation

test/swift-format/main.swift.lines.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: diff -u %s.indent2.response %t.response
55
// RUN: %swift-format -use-tabs %s >%t.response
66
// RUN: diff -u %s.tabs.response %t.response
7-
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
7+
// RUN: %swift-format -line-range 12:18 %s >%t.response
88
// RUN: diff -u %s.lines.response %t.response
99

1010
import Foundation

test/swift-format/main.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: diff -u %s.indent2.response %t.response
55
// RUN: %swift-format -use-tabs %s >%t.response
66
// RUN: diff -u %s.tabs.response %t.response
7-
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
7+
// RUN: %swift-format -line-range 12:18 %s >%t.response
88
// RUN: diff -u %s.lines.response %t.response
99

1010
import Foundation

test/swift-format/main.swift.tabs.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: diff -u %s.indent2.response %t.response
55
// RUN: %swift-format -use-tabs %s >%t.response
66
// RUN: diff -u %s.tabs.response %t.response
7-
// RUN: %swift-format -line-ranges 12:18 %s >%t.response
7+
// RUN: %swift-format -line-range 12:18 %s >%t.response
88
// RUN: diff -u %s.lines.response %t.response
99

1010
import Foundation

tools/driver/swift_format_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class SwiftFormatInvocation {
120120
if (ParsedArgs.getLastArg(OPT_use_tabs))
121121
UseTabs = true;
122122

123-
if (ParsedArgs.getLastArg(OPT_inplace))
123+
if (ParsedArgs.getLastArg(OPT_in_place))
124124
InPlace = true;
125125

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

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

0 commit comments

Comments
 (0)