@@ -102,6 +102,21 @@ public class NameMatcher: SyntaxAnyVisitor {
102
102
return nil
103
103
}
104
104
105
+ /// Finds the first position to resolve that is in the leading or trailing trivia of this token.
106
+ ///
107
+ /// If one is found, also returns the range of the trivia in which the position was found.
108
+ private func firstPositionToResolve(
109
+ inTriviaOf token: TokenSyntax
110
+ ) -> ( position: AbsolutePosition , triviaRange: Range < AbsolutePosition > ) ? {
111
+ if let position = firstPositionToResolve ( in: token. leadingTriviaRange) {
112
+ return ( position, token. leadingTriviaRange)
113
+ }
114
+ if let position = firstPositionToResolve ( in: token. trailingTriviaRange) {
115
+ return ( position, token. trailingTriviaRange)
116
+ }
117
+ return nil
118
+ }
119
+
105
120
// MARK: - addResolvedLocIfRequested overloads
106
121
107
122
/// If a position should be resolved at at the start of `baseNameRange`, create a new `DeclNameLocation` to
@@ -205,13 +220,12 @@ public class NameMatcher: SyntaxAnyVisitor {
205
220
}
206
221
207
222
public override func visit( _ token: TokenSyntax ) -> SyntaxVisitorContinueKind {
208
- while let baseNamePosition = firstPositionToResolve ( in: token. leadingTriviaRange)
209
- ?? firstPositionToResolve ( in: token. trailingTriviaRange)
210
- {
223
+ while let ( baseNamePosition, triviaRange) = firstPositionToResolve ( inTriviaOf: token) {
211
224
// Parse the comment from the position that we want to resolve. This should parse any function calls or compound decl names, the rest of
212
225
// the comment will probably be parsed as garbage but that's OK because we don't actually care about it.
213
226
let positionOffsetInToken = baseNamePosition. utf8Offset - token. position. utf8Offset
214
- let commentTree = token. syntaxTextBytes [ positionOffsetInToken... ]
227
+ let triviaRangeEndOffsetInToken = triviaRange. upperBound. utf8Offset - token. position. utf8Offset
228
+ let commentTree = token. syntaxTextBytes [ positionOffsetInToken..< triviaRangeEndOffsetInToken]
215
229
. withUnsafeBufferPointer { ( buffer) -> ExprSyntax in
216
230
var parser = Parser ( buffer)
217
231
return ExprSyntax . parse ( from: & parser)
0 commit comments