| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2004, 2011 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | import org.eclipse.jdt.core.compiler.CharOperation; |
| 17 | import org.eclipse.jdt.core.compiler.InvalidInputException; |
| 18 | import org.eclipse.jdt.internal.compiler.parser.Scanner; |
| 19 | import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; |
| 20 | import org.eclipse.jdt.internal.compiler.util.Util; |
| 21 | |
| 22 | /** |
| 23 | * Internal class for associating comments with AST nodes. |
| 24 | * |
| 25 | * @since 3.0 |
| 26 | */ |
| 27 | class DefaultCommentMapper { |
| 28 | Comment[] comments; |
| 29 | Scanner scanner; |
| 30 | |
| 31 | // extended nodes storage |
| 32 | int leadingPtr; |
| 33 | ASTNode[] leadingNodes; |
| 34 | long[] leadingIndexes; |
| 35 | int trailingPtr, lastTrailingPtr; |
| 36 | ASTNode[] trailingNodes; |
| 37 | long[] trailingIndexes; |
| 38 | static final int STORAGE_INCREMENT = 16; |
| 39 | |
| 40 | /** |
| 41 | * @param table the given table of comments |
| 42 | */ |
| 43 | DefaultCommentMapper(Comment[] table) { |
| 44 | this.comments = table; |
| 45 | } |
| 46 | |
| 47 | boolean hasSameTable(Comment[] table) { |
| 48 | return this.comments == table; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get comment of the list which includes a given position |
| 53 | * |
| 54 | * @param position The position belonging to the looked up comment |
| 55 | * @return comment which includes the given position or null if none was found |
| 56 | */ |
| 57 | Comment getComment(int position) { |
| 58 | |
| 59 | if (this.comments == null) { |
| 60 | return null; |
| 61 | } |
| 62 | int size = this.comments.length; |
| 63 | if (size == 0) { |
| 64 | return null; |
| 65 | } |
| 66 | int index = getCommentIndex(0, position, 0); |
| 67 | if (index<0) { |
| 68 | return null; |
| 69 | } |
| 70 | return this.comments[index]; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Get the index of comment which contains given position. |
| 75 | * If there's no matching comment, then return depends on exact parameter: |
| 76 | * = 0: return -1 |
| 77 | * < 0: return index of the comment before the given position |
| 78 | * > 0: return index of the comment after the given position |
| 79 | */ |
| 80 | private int getCommentIndex(int start, int position, int exact) { |
| 81 | if (position == 0) { |
| 82 | if (this.comments.length > 0 && this.comments[0].getStartPosition() == 0) { |
| 83 | return 0; |
| 84 | } |
| 85 | return -1; |
| 86 | } |
| 87 | int bottom = start, top = this.comments.length - 1; |
| 88 | int i = 0, index = -1; |
| 89 | Comment comment = null; |
| 90 | while (bottom <= top) { |
| 91 | i = bottom + (top - bottom) /2; |
| 92 | comment = this.comments[i]; |
| 93 | int commentStart = comment.getStartPosition(); |
| 94 | if (position < commentStart) { |
| 95 | top = i-1; |
| 96 | } else if (position >=(commentStart+comment.getLength())) { |
| 97 | bottom = i+1; |
| 98 | } else { |
| 99 | index = i; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | if (index<0 && exact!=0) { |
| 104 | comment = this.comments[i]; |
| 105 | if (position < comment.getStartPosition()) { |
| 106 | return exact<0 ? i-1 : i; |
| 107 | } else { |
| 108 | return exact<0 ? i : i+1; |
| 109 | } |
| 110 | } |
| 111 | return index; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the extended start position of the given node. Unlike |
| 116 | * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()}, |
| 117 | * the extended source range may include comments and whitespace |
| 118 | * immediately before or after the normal source range for the node. |
| 119 | * |
| 120 | * @param node the node |
| 121 | * @return the 0-based character index, or <code>-1</code> |
| 122 | * if no source position information is recorded for this node |
| 123 | * @see #getExtendedLength(ASTNode) |
| 124 | * @since 3.0 |
| 125 | */ |
| 126 | public int getExtendedStartPosition(ASTNode node) { |
| 127 | if (this.leadingPtr >= 0) { |
| 128 | long range = -1; |
| 129 | for (int i=0; range<0 && i<=this.leadingPtr; i++) { |
| 130 | if (this.leadingNodes[i] == node) range = this.leadingIndexes[i]; |
| 131 | } |
| 132 | if (range >= 0) { |
| 133 | return this.comments[(int)(range>>32)].getStartPosition() ; |
| 134 | } |
| 135 | } |
| 136 | return node.getStartPosition(); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Search the line number corresponding to a specific position |
| 141 | * between the given line range (inclusive) |
| 142 | * @param position int |
| 143 | * @parem lineRange size-2 int[] |
| 144 | * @return int |
| 145 | */ |
| 146 | public final int getLineNumber(int position, int[] lineRange) { |
| 147 | int[] lineEnds = this.scanner.lineEnds; |
| 148 | int length = lineEnds.length; |
| 149 | return Util.getLineNumber(position, lineEnds, (lineRange[0] > length ? length : lineRange[0]) -1, (lineRange[1] > length ? length : lineRange[1]) - 1); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Returns the extended end position of the given node. |
| 154 | */ |
| 155 | public int getExtendedEnd(ASTNode node) { |
| 156 | int end = node.getStartPosition() + node.getLength(); |
| 157 | if (this.trailingPtr >= 0) { |
| 158 | long range = -1; |
| 159 | for (int i=0; range<0 && i<=this.trailingPtr; i++) { |
| 160 | if (this.trailingNodes[i] == node) range = this.trailingIndexes[i]; |
| 161 | } |
| 162 | if (range >= 0) { |
| 163 | Comment lastComment = this.comments[(int) range]; |
| 164 | end = lastComment.getStartPosition() + lastComment.getLength(); |
| 165 | } |
| 166 | } |
| 167 | return end-1; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the extended source length of the given node. Unlike |
| 172 | * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()}, |
| 173 | * the extended source range may include comments and whitespace |
| 174 | * immediately before or after the normal source range for the node. |
| 175 | * |
| 176 | * @param node the node |
| 177 | * @return a (possibly 0) length, or <code>0</code> |
| 178 | * if no source position information is recorded for this node |
| 179 | * @see #getExtendedStartPosition(ASTNode) |
| 180 | * @see #getExtendedEnd(ASTNode) |
| 181 | * @since 3.0 |
| 182 | */ |
| 183 | public int getExtendedLength(ASTNode node) { |
| 184 | return getExtendedEnd(node) - getExtendedStartPosition(node) + 1; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return index of first leading comment of a given node. |
| 189 | * |
| 190 | * @param node |
| 191 | * @return index of first leading comment or -1 if node has no leading comment |
| 192 | */ |
| 193 | int firstLeadingCommentIndex(ASTNode node) { |
| 194 | if (this.leadingPtr >= 0) { |
| 195 | for (int i=0; i<=this.leadingPtr; i++) { |
| 196 | if (this.leadingNodes[i] == node) { |
| 197 | return (int) (this.leadingIndexes[i]>>32); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Return index of last trailing comment of a given node. |
| 206 | * |
| 207 | * @param node |
| 208 | * @return index of last trailing comment or -1 if node has no trailing comment |
| 209 | */ |
| 210 | int lastTrailingCommentIndex(ASTNode node) { |
| 211 | if (this.trailingPtr >= 0) { |
| 212 | for (int i=0; i<=this.trailingPtr; i++) { |
| 213 | if (this.trailingNodes[i] == node) { |
| 214 | return (int) this.trailingIndexes[i]; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Initialize leading and trailing comments tables in whole nodes hierarchy of a compilation |
| 223 | * unit. |
| 224 | * Scanner is necessary to scan between nodes and comments and verify if there's |
| 225 | * nothing else than white spaces. |
| 226 | */ |
| 227 | void initialize(CompilationUnit unit, Scanner sc) { |
| 228 | |
| 229 | // Init array pointers |
| 230 | this.leadingPtr = -1; |
| 231 | this.trailingPtr = -1; |
| 232 | |
| 233 | // Init comments |
| 234 | this.comments = unit.optionalCommentTable; |
| 235 | if (this.comments == null) { |
| 236 | return; |
| 237 | } |
| 238 | int size = this.comments.length; |
| 239 | if (size == 0) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Init scanner and start ranges computing |
| 244 | this.scanner = sc; |
| 245 | this.scanner.tokenizeWhiteSpace = true; |
| 246 | |
| 247 | // Start unit visit |
| 248 | DefaultASTVisitor commentVisitor = new CommentMapperVisitor(); |
| 249 | unit.accept(commentVisitor); |
| 250 | |
| 251 | // Reduce leading arrays if necessary |
| 252 | int leadingCount = this.leadingPtr + 1; |
| 253 | if (leadingCount > 0 && leadingCount < this.leadingIndexes.length) { |
| 254 | System.arraycopy(this.leadingNodes, 0, this.leadingNodes = new ASTNode[leadingCount], 0, leadingCount); |
| 255 | System.arraycopy(this.leadingIndexes, 0, this.leadingIndexes= new long[leadingCount], 0, leadingCount); |
| 256 | } |
| 257 | |
| 258 | // Reduce trailing arrays if necessary |
| 259 | if (this.trailingPtr >= 0) { |
| 260 | // remove last remaining unresolved nodes |
| 261 | while (this.trailingIndexes[this.trailingPtr] == -1) { |
| 262 | this.trailingPtr--; |
| 263 | if (this.trailingPtr < 0) { |
| 264 | this.trailingIndexes = null; |
| 265 | this.trailingNodes = null; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // reduce array size |
| 271 | int trailingCount = this.trailingPtr + 1; |
| 272 | if (trailingCount > 0 && trailingCount < this.trailingIndexes.length) { |
| 273 | System.arraycopy(this.trailingNodes, 0, this.trailingNodes = new ASTNode[trailingCount], 0, trailingCount); |
| 274 | System.arraycopy(this.trailingIndexes, 0, this.trailingIndexes= new long[trailingCount], 0, trailingCount); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Release scanner as it's only used during unit visit |
| 279 | this.scanner = null; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Search and store node leading comments. Comments are searched in position range |
| 284 | * from previous extended position to node start position. If one or several comment are found, |
| 285 | * returns first comment start position, otherwise returns node start position. |
| 286 | * <p> |
| 287 | * Starts to search for first comment before node start position and return if none was found... |
| 288 | *</p><p> |
| 289 | * When first comment is found before node, goes up in comment list until one of |
| 290 | * following conditions becomes true: |
| 291 | * <ol> |
| 292 | * <li>comment end is before previous end</li> |
| 293 | * <li>comment start and previous end is on the same line but not on same line of node start</li> |
| 294 | * <li>there's other than white characters between current node and comment</li> |
| 295 | * <li>there's more than 1 line between current node and comment</li> |
| 296 | * </ol> |
| 297 | * If some comment have been found, then no token should be on |
| 298 | * on the same line before, so remove all comments which do not verify this assumption. |
| 299 | * </p><p> |
| 300 | * If finally there's leading still comments, then stores indexes of the first and last one |
| 301 | * in leading comments table. |
| 302 | */ |
| 303 | int storeLeadingComments(ASTNode node, int previousEnd, int[] parentLineRange) { |
| 304 | // Init extended position |
| 305 | int nodeStart = node.getStartPosition(); |
| 306 | int extended = nodeStart; |
| 307 | |
| 308 | // Get line of node start position |
| 309 | int previousEndLine = getLineNumber(previousEnd, parentLineRange); |
| 310 | int nodeStartLine = getLineNumber(nodeStart, parentLineRange); |
| 311 | |
| 312 | // Find first comment index |
| 313 | int idx = getCommentIndex(0, nodeStart, -1); |
| 314 | if (idx == -1) { |
| 315 | return nodeStart; |
| 316 | } |
| 317 | |
| 318 | // Look after potential comments |
| 319 | int startIdx = -1; |
| 320 | int endIdx = idx; |
| 321 | int previousStart = nodeStart; |
| 322 | while (idx >= 0 && previousStart >= previousEnd) { |
| 323 | // Verify for each comment that there's only white spaces between end and start of {following comment|node} |
| 324 | Comment comment = this.comments[idx]; |
| 325 | int commentStart = comment.getStartPosition(); |
| 326 | int end = commentStart+comment.getLength()-1; |
| 327 | int commentLine = getLineNumber(commentStart, parentLineRange); |
| 328 | if (end <= previousEnd || (commentLine == previousEndLine && commentLine != nodeStartLine)) { |
| 329 | // stop search on condition 1) and 2) |
| 330 | break; |
| 331 | } else if ((end+1) < previousStart) { // may be equals => then no scan is necessary |
| 332 | this.scanner.resetTo(end+1, previousStart); |
| 333 | try { |
| 334 | int token = this.scanner.getNextToken(); |
| 335 | if (token != TerminalTokens.TokenNameWHITESPACE || this.scanner.currentPosition != previousStart) { |
| 336 | // stop search on condition 3) |
| 337 | // if first comment fails, then there's no extended position in fact |
| 338 | if (idx == endIdx) { |
| 339 | return nodeStart; |
| 340 | } |
| 341 | break; |
| 342 | } |
| 343 | } catch (InvalidInputException e) { |
| 344 | // Should not happen, but return no extended position... |
| 345 | return nodeStart; |
| 346 | } |
| 347 | // verify that there's no more than one line between node/comments |
| 348 | char[] gap = this.scanner.getCurrentIdentifierSource(); |
| 349 | int nbrLine = 0; |
| 350 | int pos = -1; |
| 351 | while ((pos=CharOperation.indexOf('\n', gap,pos+1)) >= 0) { |
| 352 | nbrLine++; |
| 353 | } |
| 354 | if (nbrLine > 1) { |
| 355 | // stop search on condition 4) |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | // Store previous infos |
| 360 | previousStart = commentStart; |
| 361 | startIdx = idx--; |
| 362 | } |
| 363 | if (startIdx != -1) { |
| 364 | // Verify that there's no token on the same line before first leading comment |
| 365 | int commentStart = this.comments[startIdx].getStartPosition(); |
| 366 | if (previousEnd < commentStart && previousEndLine != nodeStartLine) { |
| 367 | int lastTokenEnd = previousEnd; |
| 368 | this.scanner.resetTo(previousEnd, commentStart); |
| 369 | try { |
| 370 | while (this.scanner.currentPosition < commentStart) { |
| 371 | if (this.scanner.getNextToken() != TerminalTokens.TokenNameWHITESPACE) { |
| 372 | lastTokenEnd = this.scanner.getCurrentTokenEndPosition(); |
| 373 | } |
| 374 | } |
| 375 | } catch (InvalidInputException e) { |
| 376 | // do nothing |
| 377 | } |
| 378 | int lastTokenLine = getLineNumber(lastTokenEnd, parentLineRange); |
| 379 | int length = this.comments.length; |
| 380 | while (startIdx<length && lastTokenLine == getLineNumber(this.comments[startIdx].getStartPosition(), parentLineRange) && nodeStartLine != lastTokenLine) { |
| 381 | startIdx++; |
| 382 | } |
| 383 | } |
| 384 | // Store leading comments indexes |
| 385 | if (startIdx <= endIdx) { |
| 386 | if (++this.leadingPtr == 0) { |
| 387 | this.leadingNodes = new ASTNode[STORAGE_INCREMENT]; |
| 388 | this.leadingIndexes = new long[STORAGE_INCREMENT]; |
| 389 | } else if (this.leadingPtr == this.leadingNodes.length) { |
| 390 | int newLength = (this.leadingPtr*3/2)+STORAGE_INCREMENT; |
| 391 | System.arraycopy(this.leadingNodes, 0, this.leadingNodes = new ASTNode[newLength], 0, this.leadingPtr); |
| 392 | System.arraycopy(this.leadingIndexes, 0, this.leadingIndexes = new long[newLength], 0, this.leadingPtr); |
| 393 | } |
| 394 | this.leadingNodes[this.leadingPtr] = node; |
| 395 | this.leadingIndexes[this.leadingPtr] = (((long)startIdx)<<32) + endIdx; |
| 396 | extended = this.comments[endIdx].getStartPosition(); |
| 397 | } |
| 398 | } |
| 399 | return extended; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Search and store node trailing comments. Comments are searched in position range |
| 404 | * from node end position to specified next start. If one or several comment are found, |
| 405 | * returns last comment end position, otherwise returns node end position. |
| 406 | * <p> |
| 407 | * Starts to search for first comment after node end position and return if none was found... |
| 408 | *</p><p> |
| 409 | * When first comment is found after node, goes down in comment list until one of |
| 410 | * following conditions becomes true: |
| 411 | * <ol> |
| 412 | * <li>comment start is after next start</li> |
| 413 | * <li>there's other than white characters between current node and comment</li> |
| 414 | * <li>there's more than 1 line between current node and comment</li> |
| 415 | *</ol> |
| 416 | * If at least potential comments have been found, then all of them has to be separated |
| 417 | * from following node. So, remove all comments which do not verify this assumption. |
| 418 | * Note that this verification is not applicable on last node. |
| 419 | * </p><p> |
| 420 | * If finally there's still trailing comments, then stores indexes of the first and last one |
| 421 | * in trailing comments table. |
| 422 | */ |
| 423 | int storeTrailingComments(ASTNode node, int nextStart, boolean lastChild, int[] parentLineRange) { |
| 424 | |
| 425 | // Init extended position |
| 426 | int nodeEnd = node.getStartPosition()+node.getLength()-1; |
| 427 | if (nodeEnd == nextStart) { |
| 428 | // special case for last child of its parent |
| 429 | if (++this.trailingPtr == 0) { |
| 430 | this.trailingNodes = new ASTNode[STORAGE_INCREMENT]; |
| 431 | this.trailingIndexes = new long[STORAGE_INCREMENT]; |
| 432 | this.lastTrailingPtr = -1; |
| 433 | } else if (this.trailingPtr == this.trailingNodes.length) { |
| 434 | int newLength = (this.trailingPtr*3/2)+STORAGE_INCREMENT; |
| 435 | System.arraycopy(this.trailingNodes, 0, this.trailingNodes = new ASTNode[newLength], 0, this.trailingPtr); |
| 436 | System.arraycopy(this.trailingIndexes, 0, this.trailingIndexes = new long[newLength], 0, this.trailingPtr); |
| 437 | } |
| 438 | this.trailingNodes[this.trailingPtr] = node; |
| 439 | this.trailingIndexes[this.trailingPtr] = -1; |
| 440 | return nodeEnd; |
| 441 | } |
| 442 | int extended = nodeEnd; |
| 443 | |
| 444 | // Get line number |
| 445 | int nodeEndLine = getLineNumber(nodeEnd, parentLineRange); |
| 446 | |
| 447 | // Find comments range index |
| 448 | int idx = getCommentIndex(0, nodeEnd, 1); |
| 449 | if (idx == -1) { |
| 450 | return nodeEnd; |
| 451 | } |
| 452 | |
| 453 | // Look after potential comments |
| 454 | int startIdx = idx; |
| 455 | int endIdx = -1; |
| 456 | int length = this.comments.length; |
| 457 | int commentStart = extended+1; |
| 458 | int previousEnd = nodeEnd+1; |
| 459 | int sameLineIdx = -1; |
| 460 | while (idx<length && commentStart < nextStart) { |
| 461 | // get comment and leave if next starting position has been reached |
| 462 | Comment comment = this.comments[idx]; |
| 463 | commentStart = comment.getStartPosition(); |
| 464 | // verify that there's nothing else than white spaces between node/comments |
| 465 | if (commentStart >= nextStart) { |
| 466 | // stop search on condition 1) |
| 467 | break; |
| 468 | } else if (previousEnd < commentStart) { |
| 469 | this.scanner.resetTo(previousEnd, commentStart); |
| 470 | try { |
| 471 | int token = this.scanner.getNextToken(); |
| 472 | if (token != TerminalTokens.TokenNameWHITESPACE || this.scanner.currentPosition != commentStart) { |
| 473 | // stop search on condition 2) |
| 474 | // if first index fails, then there's no extended position in fact... |
| 475 | if (idx == startIdx) { |
| 476 | return nodeEnd; |
| 477 | } |
| 478 | // otherwise we get the last index of trailing comment => break |
| 479 | break; |
| 480 | } |
| 481 | } catch (InvalidInputException e) { |
| 482 | // Should not happen, but return no extended position... |
| 483 | return nodeEnd; |
| 484 | } |
| 485 | // verify that there's no more than one line between node/comments |
| 486 | char[] gap = this.scanner.getCurrentIdentifierSource(); |
| 487 | int nbrLine = 0; |
| 488 | int pos = -1; |
| 489 | while ((pos=CharOperation.indexOf('\n', gap,pos+1)) >= 0) { |
| 490 | nbrLine++; |
| 491 | } |
| 492 | if (nbrLine > 1) { |
| 493 | // stop search on condition 3) |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | // Store index if we're on the same line than node end |
| 498 | int commentLine = getLineNumber(commentStart, parentLineRange); |
| 499 | if (commentLine == nodeEndLine) { |
| 500 | sameLineIdx = idx; |
| 501 | } |
| 502 | // Store previous infos |
| 503 | previousEnd = commentStart+comment.getLength(); |
| 504 | endIdx = idx++; |
| 505 | } |
| 506 | if (endIdx != -1) { |
| 507 | // Verify that following node start is separated |
| 508 | if (!lastChild) { |
| 509 | int nextLine = getLineNumber(nextStart, parentLineRange); |
| 510 | int previousLine = getLineNumber(previousEnd, parentLineRange); |
| 511 | if((nextLine - previousLine) <= 1) { |
| 512 | if (sameLineIdx == -1) return nodeEnd; |
| 513 | endIdx = sameLineIdx; |
| 514 | } |
| 515 | } |
| 516 | // Store trailing comments indexes |
| 517 | if (++this.trailingPtr == 0) { |
| 518 | this.trailingNodes = new ASTNode[STORAGE_INCREMENT]; |
| 519 | this.trailingIndexes = new long[STORAGE_INCREMENT]; |
| 520 | this.lastTrailingPtr = -1; |
| 521 | } else if (this.trailingPtr == this.trailingNodes.length) { |
| 522 | int newLength = (this.trailingPtr*3/2)+STORAGE_INCREMENT; |
| 523 | System.arraycopy(this.trailingNodes, 0, this.trailingNodes = new ASTNode[newLength], 0, this.trailingPtr); |
| 524 | System.arraycopy(this.trailingIndexes, 0, this.trailingIndexes = new long[newLength], 0, this.trailingPtr); |
| 525 | } |
| 526 | this.trailingNodes[this.trailingPtr] = node; |
| 527 | long nodeRange = (((long)startIdx)<<32) + endIdx; |
| 528 | this.trailingIndexes[this.trailingPtr] = nodeRange; |
| 529 | // Compute new extended end |
| 530 | extended = this.comments[endIdx].getStartPosition()+this.comments[endIdx].getLength()-1; |
| 531 | // Look for children unresolved extended end |
| 532 | ASTNode previousNode = node; |
| 533 | int ptr = this.trailingPtr - 1; // children extended end were stored before |
| 534 | while (ptr >= 0) { |
| 535 | long range = this.trailingIndexes[ptr]; |
| 536 | if (range != -1) break; // there's no more unresolved nodes |
| 537 | ASTNode unresolved = this.trailingNodes[ptr]; |
| 538 | if (previousNode != unresolved.getParent()) break; // we're no longer in node ancestor hierarchy |
| 539 | this.trailingIndexes[ptr] = nodeRange; |
| 540 | previousNode = unresolved; |
| 541 | ptr--; // get previous node |
| 542 | } |
| 543 | // Remove remaining unresolved nodes |
| 544 | if (ptr > this.lastTrailingPtr) { |
| 545 | int offset = ptr - this.lastTrailingPtr; |
| 546 | for (int i=ptr+1; i<=this.trailingPtr; i++) { |
| 547 | this.trailingNodes[i-offset] = this.trailingNodes[i]; |
| 548 | this.trailingIndexes[i-offset] = this.trailingIndexes[i]; |
| 549 | } |
| 550 | this.trailingPtr -= offset; |
| 551 | } |
| 552 | this.lastTrailingPtr = this.trailingPtr; |
| 553 | } |
| 554 | return extended; |
| 555 | } |
| 556 | |
| 557 | class CommentMapperVisitor extends DefaultASTVisitor { |
| 558 | |
| 559 | ASTNode topSiblingParent = null; |
| 560 | ASTNode[] siblings = new ASTNode[10]; |
| 561 | int[][] parentLineRange = new int[10][]; |
| 562 | int siblingPtr = -1; |
| 563 | |
| 564 | @Override |
| 565 | protected boolean visitNode(ASTNode node) { |
| 566 | |
| 567 | // Get default previous end |
| 568 | ASTNode parent = node.getParent(); |
| 569 | int previousEnd = parent.getStartPosition(); |
| 570 | |
| 571 | // Look for sibling node |
| 572 | ASTNode sibling = parent == this.topSiblingParent ? (ASTNode) this.siblings[this.siblingPtr] : null; |
| 573 | if (sibling != null) { |
| 574 | // Found one previous sibling, so compute its trailing comments using current node start position |
| 575 | try { |
| 576 | previousEnd = storeTrailingComments(sibling, node.getStartPosition(), false, this.parentLineRange[this.siblingPtr]); |
| 577 | } catch (Exception ex) { |
| 578 | // Give up extended ranges at this level if unexpected exception happens... |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Stop visit for malformed node (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=84049) |
| 583 | if ((node.typeAndFlags & ASTNode.MALFORMED) != 0) { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | // Compute leading comments for current node |
| 588 | int[] previousLineRange = this.siblingPtr > -1 ? this.parentLineRange[this.siblingPtr] : new int[] {1, DefaultCommentMapper.this.scanner.linePtr+1}; |
| 589 | try { |
| 590 | storeLeadingComments(node, previousEnd, previousLineRange); |
| 591 | } catch (Exception ex) { |
| 592 | // Give up extended ranges at this level if unexpected exception happens... |
| 593 | } |
| 594 | |
| 595 | // Store current node as waiting sibling for its parent |
| 596 | if (this.topSiblingParent != parent) { |
| 597 | if (this.siblings.length == ++this.siblingPtr) { |
| 598 | System.arraycopy(this.siblings, 0, this.siblings = new ASTNode[this.siblingPtr*2], 0, this.siblingPtr); |
| 599 | System.arraycopy(this.parentLineRange, 0, this.parentLineRange = new int[this.siblingPtr*2][], 0, this.siblingPtr); |
| 600 | } |
| 601 | if (this.topSiblingParent == null) { |
| 602 | // node is a CompilationUnit |
| 603 | this.parentLineRange[this.siblingPtr] = previousLineRange; |
| 604 | } else { |
| 605 | int parentStart = parent.getStartPosition(); |
| 606 | int firstLine = getLineNumber(parentStart, previousLineRange); |
| 607 | int lastLine = getLineNumber(parentStart + parent.getLength() - 1, previousLineRange); |
| 608 | if (this.parentLineRange[this.siblingPtr] == null) { |
| 609 | this.parentLineRange[this.siblingPtr] = new int[] {firstLine, lastLine}; |
| 610 | } else { |
| 611 | int[] lineRange = this.parentLineRange[this.siblingPtr]; |
| 612 | lineRange[0] = firstLine; |
| 613 | lineRange[1] = lastLine; |
| 614 | } |
| 615 | } |
| 616 | this.topSiblingParent = parent; |
| 617 | } |
| 618 | this.siblings[this.siblingPtr] = node; |
| 619 | |
| 620 | // We're always ok to visit sub-levels |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | protected void endVisitNode(ASTNode node) { |
| 626 | |
| 627 | // Look if a child node is waiting for trailing comments computing |
| 628 | ASTNode sibling = this.topSiblingParent == node ? (ASTNode) this.siblings[this.siblingPtr] : null; |
| 629 | if (sibling != null) { |
| 630 | try { |
| 631 | storeTrailingComments(sibling, node.getStartPosition()+node.getLength()-1, true, this.parentLineRange[this.siblingPtr]); |
| 632 | } catch (Exception ex) { |
| 633 | // Give up extended ranges at this level if unexpected exception happens... |
| 634 | } |
| 635 | } |
| 636 | // Remove sibling if needed |
| 637 | if (this.topSiblingParent != null /*not a CompilationUnit*/ |
| 638 | && this.topSiblingParent == node) { |
| 639 | this.siblingPtr--; |
| 640 | this.topSiblingParent = node.getParent(); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public boolean visit (Modifier modifier) { |
| 646 | // we don't want to map comment to the modifier |
| 647 | return false; |
| 648 | } |
| 649 | @Override |
| 650 | public boolean visit ( CompilationUnit node) { |
| 651 | // do nothing special, just go down in sub-levels |
| 652 | return true; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 |
Members