Skip to content

Commit

Permalink
Added blankLinesAroundMark rule
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 29, 2017
1 parent 7e1d94b commit 65cbf1f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -271,6 +271,28 @@ Here are all the rules that SwiftFormat currently applies, and the effects that
var baz: Bool
var quux: Int
```

***blankLinesAroundMark*** - adds a blank line before and after each `MARK:` comment. This rule can be configured using the `--insertlines` option:

```diff
func foo() {
// foo
}
// MARK: bar
func bar() {
// bar
}

func foo() {
// foo
}
+
// MARK: bar
+
func bar() {
// bar
}
```
***braces*** - implements K&R (default) or Allman-style indentation, depending on the `--allman` option:

Expand Down
18 changes: 18 additions & 0 deletions Sources/Rules.swift
Expand Up @@ -672,6 +672,24 @@ extension FormatRules {
}
}

/// Adds a blank line around MARK: comments
@objc public class func blankLinesAroundMark(_ formatter: Formatter) {
guard formatter.options.insertBlankLines else { return }
formatter.forEachToken { i, token in
guard case let .commentBody(comment) = token, comment.hasPrefix("MARK:"),
let startIndex = formatter.index(of: .nonSpace, before: i),
formatter.tokens[startIndex] == .startOfScope("//") else { return }
if let nextIndex = formatter.index(of: .linebreak, after: i),
formatter.next(.nonSpace, after: nextIndex)?.isLinebreak == false {
formatter.insertToken(.linebreak(formatter.options.linebreak), at: nextIndex)
}
if let lastIndex = formatter.index(of: .linebreak, before: startIndex),
formatter.last(.nonSpace, before: lastIndex)?.isLinebreak == false {
formatter.insertToken(.linebreak(formatter.options.linebreak), at: lastIndex)
}
}
}

/// Always end file with a linebreak, to avoid incompatibility with certain unix tools:
/// http://stackoverflow.com/questions/2287967/why-is-it-recommended-to-have-empty-line-in-the-end-of-file
@objc public class func linebreakAtEndOfFile(_ formatter: Formatter) {
Expand Down
59 changes: 59 additions & 0 deletions Tests/RulesTests.swift
Expand Up @@ -1364,6 +1364,65 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default, options: options), output + "\n")
}

// MARK: blankLinesAroundMark

func testInsertBlankLinesAroundMark() {
let input = """
let foo = "foo"
// MARK: bar
let bar = "bar"
"""
let output = """
let foo = "foo"
// MARK: bar
let bar = "bar"
"""
XCTAssertEqual(try format(input, rules: [FormatRules.blankLinesAroundMark]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default), output + "\n")
}

func testNoInsertExtraBlankLinesAroundMark() {
let input = """
let foo = "foo"
// MARK: bar
let bar = "bar"
"""
XCTAssertEqual(try format(input, rules: [FormatRules.blankLinesAroundMark]), input)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default), input + "\n")
}

func testInsertBlankLineAfterMarkAtStartOfFile() {
let input = """
// MARK: bar
let bar = "bar"
"""
let output = """
// MARK: bar
let bar = "bar"
"""
XCTAssertEqual(try format(input, rules: [FormatRules.blankLinesAroundMark]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default), output + "\n")
}

func testInsertBlankLineBeforeMarkAtEndOfFile() {
let input = """
let foo = "foo"
// MARK: bar
"""
let output = """
let foo = "foo"
// MARK: bar
"""
XCTAssertEqual(try format(input, rules: [FormatRules.blankLinesAroundMark]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default), output + "\n")
}

// MARK: linebreakAtEndOfFile

func testLinebreakAtEndOfFile() {
Expand Down

0 comments on commit 65cbf1f

Please sign in to comment.