mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
47 lines
1.8 KiB
Swift
47 lines
1.8 KiB
Swift
//
|
|
// SpaceAroundComments.swift
|
|
// SwiftFormat
|
|
//
|
|
// Created by Nick Lockwood on 8/31/16.
|
|
// Copyright © 2024 Nick Lockwood. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension FormatRule {
|
|
/// Add space around comments, except at the start or end of a line
|
|
static let spaceAroundComments = FormatRule(
|
|
help: "Add space before and/or after comments."
|
|
) { formatter in
|
|
formatter.forEach(.startOfScope("//")) { i, _ in
|
|
if let prevToken = formatter.token(at: i - 1), !prevToken.isSpaceOrLinebreak {
|
|
formatter.insert(.space(" "), at: i)
|
|
}
|
|
}
|
|
formatter.forEach(.endOfScope("*/")) { i, _ in
|
|
guard let startIndex = formatter.index(of: .startOfScope("/*"), before: i),
|
|
case let .commentBody(commentStart)? = formatter.next(.nonSpaceOrLinebreak, after: startIndex),
|
|
case let .commentBody(commentEnd)? = formatter.last(.nonSpaceOrLinebreak, before: i),
|
|
!commentStart.hasPrefix("@"), !commentEnd.hasSuffix("@")
|
|
else {
|
|
return
|
|
}
|
|
if let nextToken = formatter.token(at: i + 1) {
|
|
if !nextToken.isSpaceOrLinebreak {
|
|
if nextToken != .delimiter(",") {
|
|
formatter.insert(.space(" "), at: i + 1)
|
|
}
|
|
} else if formatter.next(.nonSpace, after: i + 1) == .delimiter(",") {
|
|
formatter.removeToken(at: i + 1)
|
|
}
|
|
}
|
|
if let prevToken = formatter.token(at: startIndex - 1), !prevToken.isSpaceOrLinebreak {
|
|
if case let .commentBody(text) = prevToken, text.last?.unicodeScalars.last?.isSpace == true {
|
|
return
|
|
}
|
|
formatter.insert(.space(" "), at: startIndex)
|
|
}
|
|
}
|
|
}
|
|
}
|