mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Allow swiftformat directive in middle of comment
This commit is contained in:
@@ -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...])
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user