1111//===----------------------------------------------------------------------===//
1212
1313import Foundation
14+ #if os(macOS)
15+ import NaturalLanguage
16+ #endif
1417import SwiftSyntax
1518
1619/// All documentation comments must begin with a one-line summary of the declaration.
@@ -125,13 +128,31 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
125128 }
126129
127130 var sentences = [ String] ( )
131+ var tags = [ NLTag] ( )
128132 var tokenRanges = [ Range < String . Index > ] ( )
129- let tags = text. linguisticTags (
133+
134+ let tagger = NLTagger ( tagSchemes: [ . lexicalClass] )
135+ tagger. string = text
136+ tagger. enumerateTags (
130137 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
133149 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
135156 } . map {
136157 tokenRanges [ $0. offset] . lowerBound
137158 }
@@ -152,8 +173,8 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
152173 /// Returns the best approximation of sentences in the given text using string splitting around
153174 /// periods that are followed by spaces.
154175 ///
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
157178 /// sentences (such as those containing abbreviations that are followed by a period, like "Dr.")
158179 /// that the more advanced API can handle.
159180 private func nonLinguisticSentenceApproximations( in text: String ) -> (
0 commit comments