Unlike the TS implementation, this function *will not* compute default values for `precedingToken` and `tokenAtPosition`. It is the caller's responsibility to call `astnav.GetTokenAtPosition` to compute a default `tokenAtPosition`, or `astnav.FindPrecedingToken` to compute a default `precedingToken`
( file *ast.SourceFile, position int, precedingToken *ast.Node, tokenAtPosition *ast.Node, )
| 135 | // It is the caller's responsibility to call `astnav.GetTokenAtPosition` to compute a default `tokenAtPosition`, |
| 136 | // or `astnav.FindPrecedingToken` to compute a default `precedingToken`. |
| 137 | func getRangeOfEnclosingComment( |
| 138 | file *ast.SourceFile, |
| 139 | position int, |
| 140 | precedingToken *ast.Node, |
| 141 | tokenAtPosition *ast.Node, |
| 142 | ) *ast.CommentRange { |
| 143 | jsdoc := ast.FindAncestor(tokenAtPosition, (*ast.Node).IsJSDoc) |
| 144 | if jsdoc != nil { |
| 145 | tokenAtPosition = jsdoc.Parent |
| 146 | } |
| 147 | tokenStart := astnav.GetStartOfNode(tokenAtPosition, file, false /*includeJSDoc*/) |
| 148 | if tokenStart <= position && position < tokenAtPosition.End() { |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | // Between two consecutive tokens, all comments are either trailing on the former |
| 153 | // or leading on the latter (and none are in both lists). |
| 154 | var trailingRangesOfPreviousToken iter.Seq[ast.CommentRange] |
| 155 | if precedingToken != nil { |
| 156 | trailingRangesOfPreviousToken = scanner.GetTrailingCommentRanges(&ast.NodeFactory{}, file.Text(), precedingToken.End()) |
| 157 | } |
| 158 | leadingRangesOfNextToken := getLeadingCommentRangesOfNode(tokenAtPosition, file) |
| 159 | commentRanges := core.ConcatenateSeq(trailingRangesOfPreviousToken, leadingRangesOfNextToken) |
| 160 | for commentRange := range commentRanges { |
| 161 | // The end marker of a single-line comment does not include the newline character. |
| 162 | // In the following case where the cursor is at `^`, we are inside a comment: |
| 163 | // |
| 164 | // // asdf ^\n |
| 165 | // |
| 166 | // But for closed multi-line comments, we don't want to be inside the comment in the following case: |
| 167 | // |
| 168 | // /* asdf */^ |
| 169 | // |
| 170 | // Internally, we represent the end of the comment prior to the newline and at the '/', respectively. |
| 171 | // |
| 172 | // However, unterminated multi-line comments lack a `/`, end at the end of the file, and *do* contain their end. |
| 173 | // |
| 174 | if commentRange.ContainsExclusive(position) || |
| 175 | position == commentRange.End() && |
| 176 | (commentRange.Kind == ast.KindSingleLineCommentTrivia || position == len(file.Text())) { |
| 177 | return &commentRange |
| 178 | } |
| 179 | } |
| 180 | return nil |
| 181 | } |
no test coverage detected