Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Common Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/cpl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.dom.rewrite; |
12 |
|
13 |
import org.eclipse.jdt.core.dom.ASTNode; |
14 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
15 |
|
16 |
/** |
17 |
* An object for computing adjusted source ranges for AST nodes |
18 |
* that are being replaced or deleted. |
19 |
* <p> |
20 |
* For example, a refactoring like inline method may choose to replace |
21 |
* calls to the method but leave intact any comments immediately preceding |
22 |
* the calls. On the other hand, a refactoring like extract method may choose |
23 |
* to extract not only the nodes for the selected code but also any |
24 |
* comments preceding or following them. |
25 |
* </p> |
26 |
* <p> |
27 |
* Clients should subclass if they need to influence the |
28 |
* the source range to be affected when replacing or deleting a particular node. |
29 |
* An instance of the subclass should be registered with |
30 |
* {@link ASTRewrite#setTargetSourceRangeComputer(TargetSourceRangeComputer)}. |
31 |
* During a call to {@link ASTRewrite#rewriteAST(org.eclipse.jface.text.IDocument, java.util.Map)}, |
32 |
* the {@link #computeSourceRange(ASTNode)} method on this object will be |
33 |
* used to compute the source range for a node being deleted or replaced. |
34 |
* </p> |
35 |
* |
36 |
* @since 3.1 |
37 |
*/ |
38 |
public class TargetSourceRangeComputer { |
39 |
|
40 |
/** |
41 |
* Reified source range. Instances are "value" object |
42 |
* (cannot be modified). |
43 |
* |
44 |
* @since 3.1 |
45 |
*/ |
46 |
public static final class SourceRange { |
47 |
/** |
48 |
* 0-based character index, or <code>-1</code> |
49 |
* if no source position information is known. |
50 |
*/ |
51 |
private int startPosition; |
52 |
|
53 |
/** |
54 |
* (possibly 0) length, or <code>0</code> |
55 |
* if no source position information is known. |
56 |
*/ |
57 |
private int length; |
58 |
|
59 |
/** |
60 |
* Creates a new source range. |
61 |
* |
62 |
* @param startPosition the 0-based character index, or <code>-1</code> |
63 |
* if no source position information is known |
64 |
* @param length the (possibly 0) length, or <code>0</code> |
65 |
* if no source position information is known |
66 |
*/ |
67 |
public SourceRange(int startPosition, int length) { |
68 |
this.startPosition = startPosition; |
69 |
this.length = length; |
70 |
} |
71 |
|
72 |
/** |
73 |
* Returns the start position. |
74 |
* |
75 |
* @return the 0-based character index, or <code>-1</code> |
76 |
* if no source position information is known |
77 |
*/ |
78 |
public int getStartPosition() { |
79 |
return this.startPosition; |
80 |
} |
81 |
|
82 |
/** |
83 |
* Returns the source length. |
84 |
* |
85 |
* @return a (possibly 0) length, or <code>0</code> |
86 |
* if no source position information is known |
87 |
*/ |
88 |
public int getLength() { |
89 |
return this.length; |
90 |
} |
91 |
} |
92 |
|
93 |
/** |
94 |
* Creates a new target source range computer. |
95 |
*/ |
96 |
public TargetSourceRangeComputer() { |
97 |
// do nothing |
98 |
} |
99 |
|
100 |
/** |
101 |
* Returns the target source range of the given node. Unlike |
102 |
* {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()}, |
103 |
* the extended source range may include comments and whitespace |
104 |
* immediately before or after the normal source range for the node. |
105 |
* <p> |
106 |
* The default implementation uses |
107 |
* {@link CompilationUnit#getExtendedStartPosition(ASTNode)} |
108 |
* and {@link CompilationUnit#getExtendedLength(ASTNode)} |
109 |
* to compute the target source range. Clients may override or |
110 |
* extend this method to expand or contract the source range of the |
111 |
* given node. The resulting source range must cover at least the |
112 |
* original source range of the node. |
113 |
* </p> |
114 |
* |
115 |
* @param node the node with a known source range in the compilation unit |
116 |
* being rewritten |
117 |
* @return the exact source range in the compilation unit being rewritten |
118 |
* that should be replaced (or deleted) |
119 |
*/ |
120 |
public SourceRange computeSourceRange(ASTNode node) { |
121 |
// TODO (Martin) Is it true that this method will only be called on AST nodes that are part of the original tree and have at least a basic source position? |
122 |
CompilationUnit cu = (CompilationUnit) node.getRoot(); |
123 |
return new SourceRange( |
124 |
cu.getExtendedStartPosition(node), |
125 |
cu.getExtendedLength(node)); |
126 |
} |
127 |
} |