From d6e7d62e32da10e2ddc191dad04eb967db1562dd Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Fri, 8 Oct 2021 19:32:14 +0100 Subject: [PATCH] Allow swiftformat directive in middle of comment --- Sources/Formatter.swift | 9 +++++---- Tests/FormatterTests.swift | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Formatter.swift b/Sources/Formatter.swift index 405e1f5d..54378b82 100644 --- a/Sources/Formatter.swift +++ b/Sources/Formatter.swift @@ -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[..