@@ -36,6 +36,29 @@ extension SyntaxProtocol {
3636 return self . position + offset
3737 }
3838
39+ /// Returns the absolute position of the trivia piece at the given index in the receiver's
40+ /// trailing trivia collection.
41+ ///
42+ /// If the trivia piece spans multiple characters, the value returned is the position of the first
43+ /// character.
44+ ///
45+ /// - Precondition: `index` is a valid index in the receiver's trailing trivia collection.
46+ ///
47+ /// - Parameter index: The index of the trivia piece in the trailing trivia whose position should
48+ /// be returned.
49+ /// - Returns: The absolute position of the trivia piece.
50+ func position( ofTrailingTriviaAt index: Trivia . Index ) -> AbsolutePosition {
51+ guard trailingTrivia. indices. contains ( index) else {
52+ preconditionFailure ( " Index was out of bounds in the node's trailing trivia. " )
53+ }
54+
55+ var offset = SourceLength . zero
56+ for currentIndex in trailingTrivia. startIndex..< index {
57+ offset += trailingTrivia [ currentIndex] . sourceLength
58+ }
59+ return self . endPositionBeforeTrailingTrivia + offset
60+ }
61+
3962 /// Returns the source location of the trivia piece at the given index in the receiver's leading
4063 /// trivia collection.
4164 ///
@@ -56,6 +79,76 @@ extension SyntaxProtocol {
5679 ) -> SourceLocation {
5780 return converter. location ( for: position ( ofLeadingTriviaAt: index) )
5881 }
82+
83+ /// Returns the source location of the trivia piece at the given index in the receiver's trailing
84+ /// trivia collection.
85+ ///
86+ /// If the trivia piece spans multiple characters, the value returned is the location of the first
87+ /// character.
88+ ///
89+ /// - Precondition: `index` is a valid index in the receiver's trailing trivia collection.
90+ ///
91+ /// - Parameters:
92+ /// - index: The index of the trivia piece in the trailing trivia whose location should be
93+ /// returned.
94+ /// - converter: The `SourceLocationConverter` that was previously initialized using the root
95+ /// tree of this node.
96+ /// - Returns: The source location of the trivia piece.
97+ func startLocation(
98+ ofTrailingTriviaAt index: Trivia . Index ,
99+ converter: SourceLocationConverter
100+ ) -> SourceLocation {
101+ return converter. location ( for: position ( ofTrailingTriviaAt: index) )
102+ }
103+
104+ /// The collection of all contiguous trivia preceding this node; that is, the trailing trivia of
105+ /// the node before it and the leading trivia of the node itself.
106+ var allPrecedingTrivia : Trivia {
107+ var result : Trivia
108+ if let previousTrailingTrivia = previousToken ( viewMode: . sourceAccurate) ? . trailingTrivia {
109+ result = previousTrailingTrivia
110+ } else {
111+ result = Trivia ( )
112+ }
113+ result += leadingTrivia
114+ return result
115+ }
116+
117+ /// The collection of all contiguous trivia following this node; that is, the trailing trivia of
118+ /// the node and the leading trivia of the node after it.
119+ var allFollowingTrivia : Trivia {
120+ var result = trailingTrivia
121+ if let nextLeadingTrivia = nextToken ( viewMode: . sourceAccurate) ? . leadingTrivia {
122+ result += nextLeadingTrivia
123+ }
124+ return result
125+ }
126+
127+ /// Indicates whether the node has any preceding line comments.
128+ ///
129+ /// Due to the way trivia is parsed, a preceding comment might be in either the leading trivia of
130+ /// the node or the trailing trivia of the previous token.
131+ var hasPrecedingLineComment : Bool {
132+ if let previousTrailingTrivia = previousToken ( viewMode: . sourceAccurate) ? . trailingTrivia,
133+ previousTrailingTrivia. hasLineComment
134+ {
135+ return true
136+ }
137+ return leadingTrivia. hasLineComment
138+ }
139+
140+ /// Indicates whether the node has any preceding comments of any kind.
141+ ///
142+ /// Due to the way trivia is parsed, a preceding comment might be in either the leading trivia of
143+ /// the node or the trailing trivia of the previous token.
144+ var hasAnyPrecedingComment : Bool {
145+ if let previousTrailingTrivia = previousToken ( viewMode: . sourceAccurate) ? . trailingTrivia,
146+ previousTrailingTrivia. hasAnyComments
147+ {
148+ return true
149+ }
150+ return leadingTrivia. hasAnyComments
151+ }
59152}
60153
61154extension SyntaxCollection {
0 commit comments