| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2005, 2019 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.rewrite; |
| 15 | |
| 16 | import org.eclipse.jdt.core.dom.ASTNode; |
| 17 | import org.eclipse.jdt.core.dom.CompilationUnit; |
| 18 | |
| 19 | /** |
| 20 | * An object for computing adjusted source ranges for AST nodes |
| 21 | * that are being replaced or deleted. |
| 22 | * <p> |
| 23 | * For example, a refactoring like inline method may choose to replace |
| 24 | * calls to the method but leave intact any comments immediately preceding |
| 25 | * the calls. On the other hand, a refactoring like extract method may choose |
| 26 | * to extract not only the nodes for the selected code but also any |
| 27 | * comments preceding or following them. |
| 28 | * </p> |
| 29 | * <p> |
| 30 | * Clients should subclass if they need to influence the |
| 31 | * the source range to be affected when replacing or deleting a particular node. |
| 32 | * An instance of the subclass should be registered with |
| 33 | * {@link ASTRewrite#setTargetSourceRangeComputer(TargetSourceRangeComputer)}. |
| 34 | * During a call to {@link ASTRewrite#rewriteAST(org.eclipse.jface.text.IDocument, java.util.Map)}, |
| 35 | * the {@link #computeSourceRange(ASTNode)} method on this object will be |
| 36 | * used to compute the source range for a node being deleted or replaced. |
| 37 | * </p> |
| 38 | * |
| 39 | * @since 3.1 |
| 40 | */ |
| 41 | public class TargetSourceRangeComputer { |
| 42 | |
| 43 | /** |
| 44 | * Reified source range. Instances are "value" object |
| 45 | * (cannot be modified). |
| 46 | * |
| 47 | * @since 3.1 |
| 48 | */ |
| 49 | public static final class SourceRange { |
| 50 | /** |
| 51 | * 0-based character index, or <code>-1</code> |
| 52 | * if no source position information is known. |
| 53 | */ |
| 54 | private int startPosition; |
| 55 | |
| 56 | /** |
| 57 | * (possibly 0) length, or <code>0</code> |
| 58 | * if no source position information is known. |
| 59 | */ |
| 60 | private int length; |
| 61 | |
| 62 | /** |
| 63 | * Creates a new source range. |
| 64 | * |
| 65 | * @param startPosition the 0-based character index, or <code>-1</code> |
| 66 | * if no source position information is known |
| 67 | * @param length the (possibly 0) length, or <code>0</code> |
| 68 | * if no source position information is known |
| 69 | */ |
| 70 | public SourceRange(int startPosition, int length) { |
| 71 | this.startPosition = startPosition; |
| 72 | this.length = length; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the start position. |
| 77 | * |
| 78 | * @return the 0-based character index, or <code>-1</code> |
| 79 | * if no source position information is known |
| 80 | */ |
| 81 | public int getStartPosition() { |
| 82 | return this.startPosition; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the source length. |
| 87 | * |
| 88 | * @return a (possibly 0) length, or <code>0</code> |
| 89 | * if no source position information is known |
| 90 | */ |
| 91 | public int getLength() { |
| 92 | return this.length; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Creates a new target source range computer. |
| 98 | */ |
| 99 | public TargetSourceRangeComputer() { |
| 100 | // do nothing |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the target source range of the given node. Unlike |
| 105 | * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()}, |
| 106 | * the extended source range may include comments and whitespace |
| 107 | * immediately before or after the normal source range for the node. |
| 108 | * <p> |
| 109 | * The returned source ranges must satisfy the following conditions: |
| 110 | * <ul> |
| 111 | * <li>no two source ranges in an AST may be overlapping</li> |
| 112 | * <li>a source range of a parent node must fully cover the source ranges of its children</li> |
| 113 | * </ul> |
| 114 | * <p> |
| 115 | * The default implementation uses |
| 116 | * {@link CompilationUnit#getExtendedStartPosition(ASTNode)} |
| 117 | * and {@link CompilationUnit#getExtendedLength(ASTNode)} |
| 118 | * to compute the target source range. Clients may override or |
| 119 | * extend this method to expand or contract the source range of the |
| 120 | * given node. The resulting source range must cover at least the |
| 121 | * original source range of the node. |
| 122 | * </p> |
| 123 | * |
| 124 | * @param node the node with a known source range in the compilation unit |
| 125 | * being rewritten |
| 126 | * @return the exact source range in the compilation unit being rewritten |
| 127 | * that should be replaced (or deleted) |
| 128 | */ |
| 129 | public SourceRange computeSourceRange(ASTNode node) { |
| 130 | ASTNode root= node.getRoot(); |
| 131 | if (root instanceof CompilationUnit) { |
| 132 | CompilationUnit cu= (CompilationUnit) root; |
| 133 | return new SourceRange(cu.getExtendedStartPosition(node), cu.getExtendedLength(node)); |
| 134 | } |
| 135 | return new SourceRange(node.getStartPosition(), node.getLength()); |
| 136 | } |
| 137 | } |
| 138 |
Members