generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SyntaxExt.swift
66 lines (54 loc) · 1.95 KB
/
SyntaxExt.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#if canImport(SwiftCompilerPlugin)
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxMacros
extension TokenSyntax {
var isWillSetOrDidSet: Bool {
self == .keyword(.didSet) || self == .keyword(.willSet)
}
}
extension SyntaxProtocol {
var documentation: String? {
leadingTrivia.documentation
}
}
extension Trivia {
var documentation: String? {
let lines = compactMap { $0.documentation }
guard lines.count > 1 else { return lines.first?.trimmingCharacters(in: .whitespaces) }
let indentation = lines.compactMap { $0.firstIndex(where: { !$0.isWhitespace })?.utf16Offset(in: $0) }
.min() ?? 0
return lines.map {
guard $0.count > indentation else { return String($0) }
return String($0.suffix($0.count - indentation))
}.joined(separator: "\\n")
}
}
extension TriviaPiece {
var documentation: String? {
switch self {
case let .docLineComment(comment):
let startIndex = comment.index(comment.startIndex, offsetBy: 3)
return String(comment.suffix(from: startIndex))
case let .lineComment(comment):
let startIndex = comment.index(comment.startIndex, offsetBy: 2)
return String(comment.suffix(from: startIndex))
case let .docBlockComment(comment):
let startIndex = comment.index(comment.startIndex, offsetBy: 3)
let endIndex = comment.index(comment.endIndex, offsetBy: -2)
return String(comment[startIndex ..< endIndex])
case let .blockComment(comment):
let startIndex = comment.index(comment.startIndex, offsetBy: 2)
let endIndex = comment.index(comment.endIndex, offsetBy: -2)
return String(comment[startIndex ..< endIndex])
default:
return nil
}
}
}
extension String {
var wrapped: String {
"#\"\(self)\"#"
}
}
#endif