Skip to content

Commit

Permalink
Linebreak is no longer inserted at end of fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 8, 2016
1 parent 8519b96 commit 0831657
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions SwiftFormat/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ public func blankLinesBetweenScopes(_ formatter: Formatter) {
/// 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
public func linebreakAtEndOfFile(_ formatter: Formatter) {
guard !formatter.options.fragment else { return }
if let lastToken = formatter.previousToken(fromIndex: formatter.tokens.count, matching: {
!$0.isWhitespace && !$0.isError }), !lastToken.isLinebreak {
formatter.insertToken(.linebreak(formatter.options.linebreak), atIndex: formatter.tokens.count)
Expand Down
8 changes: 8 additions & 0 deletions SwiftFormatTests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,14 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try! format(input, rules: defaultRules), output)
}

func testNoLinebreakAtEndOfFragment() {
let input = "foo\nbar"
let output = "foo\nbar"
let options = FormatOptions(fragment: true)
XCTAssertEqual(try! format(input, rules: [linebreakAtEndOfFile], options: options), output)
XCTAssertEqual(try! format(input + "\n", rules: defaultRules, options: options), output + "\n")
}

// MARK: indent parens

func testSimpleScope() {
Expand Down

0 comments on commit 0831657

Please sign in to comment.