@@ -158,16 +158,14 @@ function findCommentSyntaxMatches(options: {
158158 if ( syntax [ type ] !== sequence . delimiter ) return
159159 // Otherwise, set the ranges and mark the array as updated
160160 match [ delimiterProp ] = createRange ( {
161- line,
162- lineIndex,
163- startColumn : sequence . index ,
164- endColumn : sequence . index + sequence . delimiter . length ,
161+ codeLines,
162+ start : { line : lineIndex , column : sequence . index } ,
163+ end : { line : lineIndex , column : sequence . index + sequence . delimiter . length } ,
165164 } )
166165 match [ whitespaceProp ] = createRange ( {
167- line,
168- lineIndex,
169- startColumn : sequence . index - sequence . leadingWhitespace . length ,
170- endColumn : sequence . index + sequence . delimiter . length + sequence . trailingWhitespace . length ,
166+ codeLines,
167+ start : { line : lineIndex , column : sequence . index - sequence . leadingWhitespace . length } ,
168+ end : { line : lineIndex , column : sequence . index + sequence . delimiter . length + sequence . trailingWhitespace . length } ,
171169 } )
172170 foundNewMatches = true
173171 } )
@@ -254,28 +252,40 @@ function getCommentFromMatchingSyntaxPair(options: {
254252 const syntax = multiLineCommentSyntaxes [ bestMatchIndex ]
255253 const isOnSingleLine = match . openingRange . start . line === match . closingRange . end . line
256254 const isOnSingleLineBeforeCode = isOnSingleLine && match . closingRangeWithWhitespace . end . column
257- const commentRange : SourceRange = {
255+ const commentRange = createRange ( {
256+ codeLines,
258257 start : isOnSingleLineBeforeCode ? match . openingRange . start : match . openingRangeWithWhitespace . start ,
259258 end : match . closingRangeWithWhitespace . end ,
260- }
261- const innerRange : SourceRange = {
259+ } )
260+ // By default, assume that the annotation is the only content of the current comment
261+ // (this will be adjusted later if another annotation or non-annotation content is found)
262+ const annotationRange = createRange ( {
263+ codeLines,
264+ start : commentRange . start ,
265+ end : commentRange . end ,
266+ } )
267+
268+ // Now determine and go through the comment's inner range to collect all contents
269+ const commentInnerRange = createRange ( {
270+ codeLines,
262271 start : match . openingRangeWithWhitespace . end ,
263272 end : match . closingRangeWithWhitespace . start ,
264- }
273+ } )
265274 // If the opening sequence ends at a line boundary, adjust the inner range to exclude it
266- if ( ! innerRange . start . column && ! isOnSingleLine ) {
267- innerRange . start = { line : innerRange . start . line + 1 }
275+ if ( ! commentInnerRange . start . column && ! isOnSingleLine ) {
276+ commentInnerRange . start = { line : commentInnerRange . start . line + 1 }
268277 }
269278 // If the closing sequence starts on a line boundary, adjust the inner range to exclude it
270- if ( ! innerRange . end . column && ! isOnSingleLine ) {
271- innerRange . end = { line : innerRange . end . line - 1 }
279+ if ( ! commentInnerRange . end . column && ! isOnSingleLine ) {
280+ commentInnerRange . end = { line : commentInnerRange . end . line - 1 }
272281 }
282+
273283 const contents : string [ ] = [ ]
274284 const contentRanges : SourceRange [ ] = [ ]
275285
276- for ( let lineIndex = innerRange . start . line ; lineIndex <= innerRange . end . line ; lineIndex ++ ) {
277- const startColumn = lineIndex === tag . range . end . line ? tag . range . end . column : lineIndex === innerRange . start . line ? innerRange . start . column : undefined
278- const endColumn = lineIndex === innerRange . end . line ? innerRange . end . column : undefined
286+ for ( let lineIndex = commentInnerRange . start . line ; lineIndex <= commentInnerRange . end . line ; lineIndex ++ ) {
287+ const startColumn = lineIndex === tag . range . end . line ? tag . range . end . column : lineIndex === commentInnerRange . start . line ? commentInnerRange . start . column : undefined
288+ const endColumn = lineIndex === commentInnerRange . end . line ? commentInnerRange . end . column : undefined
279289
280290 const lineContent = getTextContentInLine ( {
281291 codeLines,
@@ -286,26 +296,26 @@ function getCommentFromMatchingSyntaxPair(options: {
286296 } )
287297 if ( lineIndex < tag . range . end . line ) {
288298 // If the current comment line has content and is located before the annotation tag,
289- // we need to reduce the comment range to exclude any non-annotation content
299+ // we need to reduce the annotation range to exclude any non-annotation content
290300 // including the opening and closing comment syntaxes, so removing the annotation
291301 // later doesn't break the commment
292302 if ( lineContent . content . length ) {
293- commentRange . start = { line : tag . range . start . line }
294- commentRange . end = { ...innerRange . end }
303+ annotationRange . start = { line : tag . range . start . line }
304+ annotationRange . end = { ...commentInnerRange . end }
295305 }
296306 } else if ( lineIndex >= tag . range . end . line && lineContent . content === '---' ) {
297307 // We encountered a separator line after the annotation tag, so this is a mixed
298- // comment with multiple pieces of content and we must limit the comment range
308+ // comment with multiple pieces of content and we must limit the annotation range
299309 // to the current annotation (however, we still include the separator line)
300- commentRange . start = { line : tag . range . start . line }
301- commentRange . end = { line : lineIndex }
310+ annotationRange . start = { line : tag . range . start . line }
311+ annotationRange . end = { line : lineIndex }
302312 break
303313 } else if ( lineIndex >= tag . range . end . line && lineContent . content . startsWith ( '[!' ) ) {
304314 // We encountered the beginning of another annotation tag, so this is a mixed
305- // comment with multiple pieces of content and we must limit the comment range
315+ // comment with multiple pieces of content and we must limit the annotation range
306316 // to the current annotation
307- commentRange . start = { line : tag . range . start . line }
308- commentRange . end = { line : lineIndex - 1 }
317+ annotationRange . start = { line : tag . range . start . line }
318+ annotationRange . end = { line : lineIndex - 1 }
309319 break
310320 } else {
311321 contents . push ( lineContent . content )
@@ -327,6 +337,7 @@ function getCommentFromMatchingSyntaxPair(options: {
327337 tag,
328338 contents,
329339 commentRange,
340+ annotationRange,
330341 contentRanges,
331342 targetRanges : [ ] ,
332343 }
0 commit comments