Allow swiftformat directive in middle of comment

This commit is contained in:
Nick Lockwood
2021-10-08 22:07:51 +01:00
parent 0989164d98
commit d6e7d62e32
2 changed files with 11 additions and 4 deletions
+5 -4
View File
@@ -72,20 +72,21 @@ public class Formatter: NSObject {
// Process a comment token (which may contain directives)
func processCommentBody(_ comment: String, at index: Int) {
var prefix = "swiftformat:"
guard comment.hasPrefix(prefix) else {
guard let range = comment.range(of: prefix) else {
return
}
let comment = String(comment[range.upperBound...])
guard let directive = ["disable", "enable", "options"].first(where: {
comment.hasPrefix("\(prefix)\($0)")
comment.hasPrefix($0)
}) else {
let parts = comment.components(separatedBy: ":")
var directive = parts[1]
var directive = parts[0]
if let range = directive.rangeOfCharacter(from: .whitespacesAndNewlines) {
directive = String(directive[..<range.lowerBound])
}
return fatalError("Unknown directive swiftformat:\(directive)", at: index)
}
prefix += directive
prefix = directive
wasNextDirective = comment.hasPrefix("\(prefix):next")
let offset = (wasNextDirective ? "\(prefix):next" : prefix).endIndex
let argumentsString = String(comment[offset...])
+6
View File
@@ -120,6 +120,12 @@ class FormatterTests: XCTestCase {
XCTAssertEqual(try format(input, rules: FormatRules.default), output)
}
func testDirectiveInMiddleOfComment() {
let input = "//fixme: swiftformat:disable spaceAroundOperators - bug\nlet foo : Int=5;"
let output = "// FIXME: swiftformat:disable spaceAroundOperators - bug\nlet foo : Int=5\n"
XCTAssertEqual(try format(input, rules: FormatRules.default), output)
}
func testDisableAndReEnableRules() {
let input = """
// swiftformat:disable indent blankLinesBetweenScopes redundantSelf