org.eclipse.jdt.core.dom.CompilationUnit#getExtendedLength ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.CompilationUnit#getExtendedLength ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Returns the source of the given node from the location where it was parsed.
 * @param node the node to get the source from
 * @param extendedRange if set, the extended ranges of the nodes should ne used
 * @param removeIndent if set, the indentation is removed.
 * @return return the source for the given node or null if accessing the source failed.
 */
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
	ASTNode root= node.getRoot();
	if (root instanceof CompilationUnit) {
		CompilationUnit astRoot= (CompilationUnit) root;
		ITypeRoot typeRoot= astRoot.getTypeRoot();
		try {
			if (typeRoot != null && typeRoot.getBuffer() != null) {
				IBuffer buffer= typeRoot.getBuffer();
				int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
				int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
				String str= buffer.getText(offset, length);
				if (removeIndent) {
					IJavaProject project= typeRoot.getJavaProject();
					int indent= StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
					str= Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
				}
				return str;
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	return null;
}
 
private IRegion evaluateReplaceRange(CompilationUnit root) {
	List imports= root.imports();
	if (!imports.isEmpty()) {
		ImportDeclaration first= (ImportDeclaration) imports.get(0);
		ImportDeclaration last= (ImportDeclaration) imports.get(imports.size() - 1);

		int startPos= first.getStartPosition(); // no extended range for first: bug 121428
		int endPos= root.getExtendedStartPosition(last) + root.getExtendedLength(last);
		int endLine= root.getLineNumber(endPos);
		if (endLine > 0) {
			int nextLinePos= root.getPosition(endLine + 1, 0);
			if (nextLinePos >= 0) {
				int firstTypePos= getFirstTypeBeginPos(root);
				if (firstTypePos != -1 && firstTypePos < nextLinePos) {
					endPos= firstTypePos;
				} else {
					endPos= nextLinePos;
				}
			}
		}
		return new Region(startPos, endPos - startPos);
	} else {
		int start= getPackageStatementEndPos(root);
		return new Region(start, 0);
	}
}
 
/**
 * Returns the target source range of the given node. Unlike
 * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()},
 * the extended source range may include comments and whitespace
 * immediately before or after the normal source range for the node.
 * <p>
 * The returned source ranges must satisfy the following conditions:
 * <dl>
 * <li>no two source ranges in an AST may be overlapping</li>
 * <li>a source range of a parent node must fully cover the source ranges of its children</li>
 * 	</dl>
 * 	</p>
 * <p>
 * The default implementation uses
 * {@link CompilationUnit#getExtendedStartPosition(ASTNode)}
 * and {@link CompilationUnit#getExtendedLength(ASTNode)}
 * to compute the target source range. Clients may override or
 * extend this method to expand or contract the source range of the
 * given node. The resulting source range must cover at least the
 * original source range of the node.
 * </p>
 *
 * @param node the node with a known source range in the compilation unit
 * being rewritten
 * @return the exact source range in the compilation unit being rewritten
 * that should be replaced (or deleted)
 */
public SourceRange computeSourceRange(ASTNode node) {
	ASTNode root= node.getRoot();
	if (root instanceof CompilationUnit) {
		CompilationUnit cu= (CompilationUnit) root;
		return new SourceRange(cu.getExtendedStartPosition(node), cu.getExtendedLength(node));
	}
	return new SourceRange(node.getStartPosition(), node.getLength());
}