11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Foundation
14
+ #if os(macOS)
15
+ import NaturalLanguage
16
+ #endif
14
17
import SwiftSyntax
15
18
16
19
/// All documentation comments must begin with a one-line summary of the declaration.
@@ -125,13 +128,31 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
125
128
}
126
129
127
130
var sentences = [ String] ( )
131
+ var tags = [ NLTag] ( )
128
132
var tokenRanges = [ Range < String . Index > ] ( )
129
- let tags = text. linguisticTags (
133
+
134
+ let tagger = NLTagger ( tagSchemes: [ . lexicalClass] )
135
+ tagger. string = text
136
+ tagger. enumerateTags (
130
137
in: text. startIndex..< text. endIndex,
131
- scheme: NSLinguisticTagScheme . lexicalClass. rawValue,
132
- tokenRanges: & tokenRanges)
138
+ unit: . word,
139
+ scheme: . lexicalClass
140
+ ) { tag, range in
141
+ if let tag {
142
+ tags. append ( tag)
143
+ tokenRanges. append ( range)
144
+ }
145
+ return true
146
+ }
147
+
148
+ var isInsideQuotes = false
133
149
let sentenceTerminatorIndices = tags. enumerated ( ) . filter {
134
- $0. element == " SentenceTerminator "
150
+ if $0. element == NLTag . openQuote {
151
+ isInsideQuotes = true
152
+ } else if $0. element == NLTag . closeQuote {
153
+ isInsideQuotes = false
154
+ }
155
+ return !isInsideQuotes && $0. element == NLTag . sentenceTerminator
135
156
} . map {
136
157
tokenRanges [ $0. offset] . lowerBound
137
158
}
@@ -152,8 +173,8 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
152
173
/// Returns the best approximation of sentences in the given text using string splitting around
153
174
/// periods that are followed by spaces.
154
175
///
155
- /// This method is a fallback for platforms (like Linux, currently) where `String` does not
156
- /// support `NSLinguisticTagger ` and its related APIs. It will fail to catch certain kinds of
176
+ /// This method is a fallback for platforms (like Linux, currently) that does not
177
+ /// support `NaturalLanguage ` and its related APIs. It will fail to catch certain kinds of
157
178
/// sentences (such as those containing abbreviations that are followed by a period, like "Dr.")
158
179
/// that the more advanced API can handle.
159
180
private func nonLinguisticSentenceApproximations( in text: String ) -> (
0 commit comments