View | Details | Raw Unified | Return to bug 179684 | Differences between
and this patch

Collapse All | Expand All

(-)dom/org/eclipse/jdt/core/dom/DefaultCommentMapper.java (-21 / +51 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.dom;
11
package org.eclipse.jdt.core.dom;
12
12
13
import java.util.HashMap;
14
15
import org.eclipse.jdt.core.compiler.CharOperation;
13
import org.eclipse.jdt.core.compiler.CharOperation;
16
import org.eclipse.jdt.core.compiler.InvalidInputException;
14
import org.eclipse.jdt.core.compiler.InvalidInputException;
17
import org.eclipse.jdt.internal.compiler.parser.Scanner;
15
import org.eclipse.jdt.internal.compiler.parser.Scanner;
Lines 285-298 Link Here
285
	 * If finally there's leading still comments, then stores indexes of the first and last one
283
	 * If finally there's leading still comments, then stores indexes of the first and last one
286
	 * in leading comments table.
284
	 * in leading comments table.
287
	 */
285
	 */
288
	int storeLeadingComments(ASTNode node, int previousEnd) {
286
	int storeLeadingComments(ASTNode node, int previousEnd, int[] parentLineRange) {
289
		// Init extended position
287
		// Init extended position
290
		int nodeStart = node.getStartPosition();
288
		int nodeStart = node.getStartPosition();
291
		int extended = nodeStart;
289
		int extended = nodeStart;
292
		
290
		
293
		// Get line of node start position
291
		// Get line of node start position
294
		int previousEndLine = this.scanner.getLineNumber(previousEnd);
292
		int previousEndLine = this.scanner.getLineNumber(previousEnd, parentLineRange);
295
		int nodeStartLine = this.scanner.getLineNumber(nodeStart);
293
		int nodeStartLine = this.scanner.getLineNumber(nodeStart, parentLineRange);
296
		
294
		
297
		// Find first comment index
295
		// Find first comment index
298
		int idx = getCommentIndex(0, nodeStart, -1);
296
		int idx = getCommentIndex(0, nodeStart, -1);
Lines 309-315 Link Here
309
			Comment comment = this.comments[idx];
307
			Comment comment = this.comments[idx];
310
			int commentStart = comment.getStartPosition();
308
			int commentStart = comment.getStartPosition();
311
			int end = commentStart+comment.getLength()-1;
309
			int end = commentStart+comment.getLength()-1;
312
			int commentLine = this.scanner.getLineNumber(commentStart);
310
			int commentLine = this.scanner.getLineNumber(commentStart, parentLineRange);
313
			if (end <= previousEnd || (commentLine == previousEndLine && commentLine != nodeStartLine)) {
311
			if (end <= previousEnd || (commentLine == previousEndLine && commentLine != nodeStartLine)) {
314
				// stop search on condition 1) and 2)
312
				// stop search on condition 1) and 2)
315
				break;
313
				break;
Lines 360-368 Link Here
360
				} catch (InvalidInputException e) {
358
				} catch (InvalidInputException e) {
361
					// do nothing
359
					// do nothing
362
				}
360
				}
363
				int lastTokenLine = this.scanner.getLineNumber(lastTokenEnd);
361
				int lastTokenLine = this.scanner.getLineNumber(lastTokenEnd, parentLineRange);
364
				int length = this.comments.length;
362
				int length = this.comments.length;
365
				while (startIdx<length && lastTokenLine == this.scanner.getLineNumber(this.comments[startIdx].getStartPosition()) && nodeStartLine != lastTokenLine) {
363
				while (startIdx<length && lastTokenLine == this.scanner.getLineNumber(this.comments[startIdx].getStartPosition(), parentLineRange) && nodeStartLine != lastTokenLine) {
366
					startIdx++;
364
					startIdx++;
367
				}
365
				}
368
			}
366
			}
Lines 405-411 Link Here
405
	 * If finally there's still trailing comments, then stores indexes of the first and last one
403
	 * If finally there's still trailing comments, then stores indexes of the first and last one
406
	 * in trailing comments table.
404
	 * in trailing comments table.
407
	 */
405
	 */
408
	int storeTrailingComments(ASTNode node, int nextStart,  boolean lastChild) {
406
	int storeTrailingComments(ASTNode node, int nextStart,  boolean lastChild, int[] parentLineRange) {
409
407
410
		// Init extended position
408
		// Init extended position
411
		int nodeEnd = node.getStartPosition()+node.getLength()-1;
409
		int nodeEnd = node.getStartPosition()+node.getLength()-1;
Lines 427-433 Link Here
427
		int extended = nodeEnd;
425
		int extended = nodeEnd;
428
		
426
		
429
		// Get line number
427
		// Get line number
430
		int nodeEndLine = this.scanner.getLineNumber(nodeEnd);
428
		int nodeEndLine = this.scanner.getLineNumber(nodeEnd, parentLineRange);
431
		
429
		
432
		// Find comments range index
430
		// Find comments range index
433
		int idx = getCommentIndex(0, nodeEnd, 1);
431
		int idx = getCommentIndex(0, nodeEnd, 1);
Lines 480-486 Link Here
480
				}
478
				}
481
			}
479
			}
482
			// Store index if we're on the same line than node end
480
			// Store index if we're on the same line than node end
483
			int commentLine = this.scanner.getLineNumber(commentStart);
481
			int commentLine = this.scanner.getLineNumber(commentStart, parentLineRange);
484
			if (commentLine == nodeEndLine) {
482
			if (commentLine == nodeEndLine) {
485
				sameLineIdx = idx;
483
				sameLineIdx = idx;
486
			}
484
			}
Lines 491-498 Link Here
491
		if (endIdx != -1) {
489
		if (endIdx != -1) {
492
			// Verify that following node start is separated
490
			// Verify that following node start is separated
493
			if (!lastChild) {
491
			if (!lastChild) {
494
				int nextLine = this.scanner.getLineNumber(nextStart);
492
				int nextLine = this.scanner.getLineNumber(nextStart, parentLineRange);
495
				int previousLine = this.scanner.getLineNumber(previousEnd);
493
				int previousLine = this.scanner.getLineNumber(previousEnd, parentLineRange);
496
				if((nextLine - previousLine) <= 1) {
494
				if((nextLine - previousLine) <= 1) {
497
					if (sameLineIdx == -1) return nodeEnd;
495
					if (sameLineIdx == -1) return nodeEnd;
498
					endIdx = sameLineIdx;
496
					endIdx = sameLineIdx;
Lines 541-560 Link Here
541
539
542
	class CommentMapperVisitor extends DefaultASTVisitor {
540
	class CommentMapperVisitor extends DefaultASTVisitor {
543
541
544
		HashMap waitingSiblings = new HashMap(10);
542
		ASTNode topSiblingParent = null;
543
		ASTNode[] siblings = new ASTNode[10];
544
		int[][] parentLineRange = new int[10][];
545
		int siblingPtr = -1;
545
546
546
		protected boolean visitNode(ASTNode node) {
547
		protected boolean visitNode(ASTNode node) {
547
548
548
			// Get default previous end
549
			// Get default previous end
549
			ASTNode parent = node.getParent();
550
			ASTNode parent = node.getParent();
550
			int previousEnd = parent.getStartPosition();
551
			int previousEnd = parent.getStartPosition();
551
552
			
552
			// Look for sibling node
553
			// Look for sibling node
553
			ASTNode sibling = (ASTNode) this.waitingSiblings.get(parent);
554
 			ASTNode sibling = parent == this.topSiblingParent ? (ASTNode) this.siblings[this.siblingPtr] : null;
554
			if (sibling != null) {
555
			if (sibling != null) {
555
				// Found one previous sibling, so compute its trailing comments using current node start position
556
				// Found one previous sibling, so compute its trailing comments using current node start position
556
				try {
557
				try {
557
					previousEnd = storeTrailingComments(sibling, node.getStartPosition(), false);
558
					previousEnd = storeTrailingComments(sibling, node.getStartPosition(), false, this.parentLineRange[this.siblingPtr]);
558
				} catch (Exception ex) {
559
				} catch (Exception ex) {
559
					// Give up extended ranges at this level if unexpected exception happens...
560
					// Give up extended ranges at this level if unexpected exception happens...
560
				}
561
				}
Lines 566-579 Link Here
566
			}
567
			}
567
568
568
			// Compute leading comments for current node
569
			// Compute leading comments for current node
570
			int[] previousLineRange = this.siblingPtr > -1 ? this.parentLineRange[this.siblingPtr] : new int[] {1, DefaultCommentMapper.this.scanner.linePtr+1};
569
			try {
571
			try {
570
				storeLeadingComments(node, previousEnd);
572
				storeLeadingComments(node, previousEnd, previousLineRange);
571
			} catch (Exception ex) {
573
			} catch (Exception ex) {
572
				// Give up extended ranges at this level if unexpected exception happens...
574
				// Give up extended ranges at this level if unexpected exception happens...
573
			}
575
			}
574
			
576
			
575
			// Store current node as waiting sibling for its parent
577
			// Store current node as waiting sibling for its parent
576
			this.waitingSiblings.put(parent, node);
578
			if (this.topSiblingParent != parent) {
579
				if (this.siblings.length == ++this.siblingPtr) {
580
					System.arraycopy(this.siblings, 0, this.siblings = new ASTNode[this.siblingPtr*2], 0, this.siblingPtr);
581
					System.arraycopy(this.parentLineRange, 0, this.parentLineRange = new int[this.siblingPtr*2][], 0, this.siblingPtr);
582
				}
583
				if (this.topSiblingParent == null) {
584
					// node is a CompilationUnit
585
					this.parentLineRange[this.siblingPtr] = previousLineRange;
586
				} else {
587
					int parentStart = parent.getStartPosition();
588
					int firstLine = DefaultCommentMapper.this.scanner.getLineNumber(parentStart, previousLineRange);
589
					int lastLine = DefaultCommentMapper.this.scanner.getLineNumber(parentStart + parent.getLength() - 1, previousLineRange);
590
					if (this.parentLineRange[this.siblingPtr] == null) {
591
						this.parentLineRange[this.siblingPtr] = new int[] {firstLine, lastLine};
592
					} else {
593
						int[] lineRange = this.parentLineRange[this.siblingPtr];
594
						lineRange[0] = firstLine;
595
						lineRange[1] = lastLine;
596
					}
597
				}
598
				this.topSiblingParent = parent;
599
			}
600
			this.siblings[this.siblingPtr] = node;
577
601
578
			// We're always ok to visit sub-levels
602
			// We're always ok to visit sub-levels
579
			return true;
603
			return true;
Lines 582-595 Link Here
582
		protected void endVisitNode(ASTNode node) {
606
		protected void endVisitNode(ASTNode node) {
583
607
584
			// Look if a child node is waiting for trailing comments computing
608
			// Look if a child node is waiting for trailing comments computing
585
			ASTNode sibling = (ASTNode) this.waitingSiblings.get(node);
609
			ASTNode sibling = this.topSiblingParent == node ? (ASTNode) this.siblings[this.siblingPtr] : null;
586
			if (sibling != null) {
610
			if (sibling != null) {
587
				try {
611
				try {
588
					storeTrailingComments(sibling, node.getStartPosition()+node.getLength()-1, true);
612
					storeTrailingComments(sibling, node.getStartPosition()+node.getLength()-1, true, this.parentLineRange[this.siblingPtr]);
589
				} catch (Exception ex) {
613
				} catch (Exception ex) {
590
					// Give up extended ranges at this level if unexpected exception happens...
614
					// Give up extended ranges at this level if unexpected exception happens...
591
				}
615
				}
592
			}
616
			}
617
			// Remove sibling if needed
618
			if (this.topSiblingParent != null /*not a CompilationUnit*/
619
					&& this.topSiblingParent == node) {
620
				this.siblingPtr--;
621
				this.topSiblingParent = node.getParent();
622
			}
593
		}
623
		}
594
624
595
		public boolean visit ( CompilationUnit node) {
625
		public boolean visit ( CompilationUnit node) {
(-)compiler/org/eclipse/jdt/internal/compiler/parser/Scanner.java (-5 / +18 lines)
Lines 3543-3562 Link Here
3543
3543
3544
	return floating ? TokenNameDoubleLiteral : TokenNameIntegerLiteral;
3544
	return floating ? TokenNameDoubleLiteral : TokenNameIntegerLiteral;
3545
}
3545
}
3546
3546
/**
3547
/**
3547
 * Search the line number corresponding to a specific position
3548
 * Search the line number corresponding to a specific position
3548
 * @param position int
3549
 * @param position int
3549
 * @return int
3550
 * @return int
3550
 */
3551
 */
3551
public final int getLineNumber(int position) {
3552
public final int getLineNumber(int position) {
3552
3553
	return getLineNumber(position, 1, this.linePtr+1);
3554
}
3555
/**
3556
 * Search the line number corresponding to a specific position
3557
 * between the given line range (inclusive)
3558
 * @param position int
3559
 * @parem lineRange size-2 int[]
3560
 * @return int
3561
 */
3562
public final int getLineNumber(int position, int[] lineRange) {
3563
	return getLineNumber(position, lineRange[0], lineRange[1]);
3564
}
3565
private int getLineNumber(int position, int firstLine, int lastLine) {
3553
	if (this.lineEnds == null)
3566
	if (this.lineEnds == null)
3554
		return 1;
3567
		return 1;
3555
	int length = this.linePtr+1;
3568
	if (lastLine == 0)
3556
	if (length == 0)
3557
		return 1;
3569
		return 1;
3558
	int g = 0, d = length - 1;
3570
	int length = this.lineEnds.length;
3559
	int m = 0;
3571
	int g = (firstLine > length ? length : firstLine) -1, d = (lastLine > length ? length : lastLine) -1;
3572
	int m = g;
3560
	while (g <= d) {
3573
	while (g <= d) {
3561
		m = g + (d - g) /2;
3574
		m = g + (d - g) /2;
3562
		if (position < this.lineEnds[m]) {
3575
		if (position < this.lineEnds[m]) {

Return to bug 179684