Skip to content
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

[SR-13458] Allow more flexible and strict line break configurations #315

Open
klanchman opened this issue Aug 27, 2020 · 0 comments
Open

Comments

@klanchman
Copy link

Previous ID SR-13458
Radar None
Original Reporter @klanchman
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s swift-format
Labels Improvement
Assignee None
Priority Medium

md5: d43456d18f7712a96680bbb98f0e91ff

Issue Description:

I'd like to see swift-format include additional configuration for line break formatting that allows for some additional flexibility and strictness in where line breaks are inserted/removed.

Right now swift-format includes the respectsExistingLineBreaks option, but setting it to false doesn't do what I expect, nor what I'd like.

Suppose I had unformatted code that looked like this:

class MyClass {

  var foo: String?
  var bar: String? {
    didSet {
      print("foo set")
    }
  }

  func doStuff() {

    guard let foo = foo else {
      return
    }

    doOtherStuff(foo)

  }

}

Setting respectsExistingLineBreaks to false yields the following:

class MyClass {

  var foo: String?
  var bar: String? { didSet { print("foo set") } }

  func doStuff() {

    guard let foo = foo else { return }

    doOtherStuff(foo)

  }

}

Given the documentation's description, "only inserting line breaks where absolutely necessary and removing any others", what I expected would be more like this:

class MyClass {
  var foo: String?
  var bar: String? { didSet { print("foo set") } }
  func doStuff() {
    guard let foo = foo else { return }
    doOtherStuff(foo)
  }
}

However, my goal would be to have swift-format output the following:

class MyClass {
  var foo: String?
  var bar: String? {
    didSet {
      print("foo set")
    }
  }

  func doStuff() {
    guard let foo = foo else {
      return
    }

    doOtherStuff(foo)
  }
}

This is more in line with the output of other code formatters I use, such as Prettier.

Notably, in the desired output:

  • blank lines are removed following open and close braces (e.g. within the class declaration, and within doStuff() function)

  • line breaks within braces for short statements are preserved (e.g. the entire didSet block, and the return in the guard)

  • line breaks (or lack of) between consecutive declarations are preserved (e.g. no break between the two var declarations, but break between the last var and the func)

The second bullet point has some caveats, mainly that you'd probably want the line breaks to be consistent on each side of the block. For example, I would not want swift-format to keep this:

var bar: String? { didSet {
  print("foo set")
}}

Setting respectsExistingLineBreaks to true and formatting the code above already results in what I'd expect / want in my "ideal" output:

var bar: String? {
  didSet {
    print("foo set")
  }
}

I might go as far as to say that the rule I'd want for the second bullet point is basically "require a line break after opening curly braces and before closing curly braces".


As far as I can tell, with the options that swift-format has, currently it is not possible to enforce the desired output.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant